简体   繁体   English

如何列出目录而不包括包含dirent.h的文件?

[英]How can I list directories without including files including dirent.h?

I have searched this a lot about this thing: How to make C print all directories without dirent.h.我已经搜索了很多关于这件事的内容:How to make C print all directory without dirent.h。 I found something called tiny dirent or something but it wasn't so useful for me becasue it had a licence on it.我发现了一个叫做微小的 dirent 的东西,但它对我来说不是那么有用,因为它上面有许可证。 I have found seveal things more that wasn't uselful.我发现了更多没用的东西。 Now, I want to do it myself that I found out the only way is to write one by myself.现在,我想自己做,我发现唯一的方法就是自己写一个。 I am using GCC to compile and I use linux to make one.我正在使用 GCC 进行编译,我使用 linux 进行编译。 So, how can I make my own dirent.h without including any header files?那么,如何在不包含任何 header 文件的情况下制作自己的 dirent.h?

This is operating system specific, since not all C implementations (think of some C program for Arduino ) have directories.这是特定于操作系统的,因为并非所有 C 实现(想想 Arduino 的一些C程序)都有目录。

How can I list directories without including files including dirent.h?如何列出目录而不包括包含dirent.h的文件?

By using some other libraries, or directly some syscalls(2) .通过使用其他一些库,或直接使用一些syscalls(2)

Did you consider using ntfw(3) , or readdir(3) ?您是否考虑使用ntfw(3)readdir(3)

So, how can I make my own dirent.h without including any header files?那么,如何在不包含任何 header 文件的情况下制作自己的 dirent.h?

You create C source files with a good editor such as GNU emacs .您可以使用良好的编辑器(例如GNU emacs )创建 C 源文件。 Or you use (or write) a program generating some C code (eg GNU bison ).或者您使用(或编写)生成一些 C 代码的程序(例如GNU bison )。

You should read a good operating system textbook if you want to create your own OS.如果你想创建自己的操作系统,你应该阅读一本好的操作系统教科书 Then see also OSDEV , and budget several years of full time work.然后参见OSDEV ,并预算几年的全职工作。 See also LinuxFromScratch , and study for inspiration the source code of the Linux kernel or of FreeBSD .另请参阅LinuxFromScratch ,并研究 Linux kernelFreeBSD的源代码以获得灵感。

I am using GCC to compile我正在使用 GCC 进行编译

Be sure to read the documentation of GCC ( user and internals ).请务必阅读 GCC 的文档(用户内部)。 Use it as gcc -Wall -Wextra -g .将其用作gcc -Wall -Wextra -g You might code your GCC plugin , or generate some C code with eg GPP您可以编写GCC 插件,或使用GPP生成一些 C 代码

On eg Linux the struct dirent structure returned by readdir have a d_type member which you can check for the type DT_DIR .在例如 Linux 上, readdir返回的struct dirent结构有一个d_type成员,您可以检查其类型DT_DIR


If you're writing your own POSIX-like operating system you can decide yourself what members the dirent structure contains, like the above mentioned d_type member.如果您正在编写自己的类似 POSIX 的操作系统,您可以自己决定dirent结构包含哪些成员,例如上面提到的d_type成员。

You won't be having opendir / readdir / closedir in your kernel, but have to implement such functionality yourself.您的 kernel 中不会有opendir / readdir / closedir ,但必须自己实现此类功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM