admin管理员组文章数量:1125093
I am currently working on the implementation of my own "ls" command. I've been relying on the structure but I found an old post stating that some members of this structure such as d_type
shouldn't be used.
I have already been able to list the files in the repository but I want to sort the files depending of their type hence the reason of my post.
So I am just wondering if there is a way other than using the d_type
or if it is not actually wrong to use it.
I am currently working on the implementation of my own "ls" command. I've been relying on the structure but I found an old post stating that some members of this structure such as d_type
shouldn't be used.
I have already been able to list the files in the repository but I want to sort the files depending of their type hence the reason of my post.
So I am just wondering if there is a way other than using the d_type
or if it is not actually wrong to use it.
1 Answer
Reset to default 4If your question is about Ubuntu, that is Linux/glibc, described in detail by the readdir man page. Its dirent
does have the d_type
field. But keep in mind that it may give you DT_UNKNOWN
at any time and you should be able to deal with it.
The most portable (POSIX) way to code this would be not to rely on d_type
at all as POSIX only mandates the d_name
and d_ino
fields.
So what to do if you either don't have the d_type field or it has given you an unknown value? Use one of the stat
functions (for listing files probably lstat
to not follow links) and then for glibc you can follow Testing the Type of a File on the st_mode
field in the result of stat
.
本文标签: cAre all elements of the dirent structure open to useStack Overflow
版权声明:本文标题:c - Are all elements of the dirent structure open to use? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736655360a1946240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
struct dirent
. – Jabberwocky Commented 2 days agod_type
is not supported. POSIX specifies members that must be present in the structure. It does not limit the structure to only those members. – John Bollinger Commented 2 days ago