Members of Dirent structure
Asked Answered
D

3

31

I have started working with dirent.h library and I came across a very useful member of "struct dirent" structer which struct dirent *p->d_name in my book. But unfortunatly it doesn't states any other members of this structure;

I was wondering what else are the members of this structure and what are they used for?

Regards

Duncandunce answered 20/10, 2012 at 18:17 Comment(4)
I assume you're on Linux. In that case, simply read the dirent.h manual page (man dirent.h).Wingo
@NikosC. No manual entry for dirent.hFernando
@Fernando You're missing the POSIX man-pages package. (Whatever it's called in your Linux distro. On mine (Gentoo), it's sys-apps/man-pages-posix).Wingo
@NikosC. wow, I never knew that here's more than the default manual pages. Indeed, on my Kubuntu it was the package manpages-posix-dev.Fernando
B
39

The structure, struct dirent refers to directory entry.

http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html

In linux it is defined as:

struct dirent {
    ino_t          d_ino;       /* inode number */
    off_t          d_off;       /* offset to the next dirent */
    unsigned short d_reclen;    /* length of this record */
    unsigned char  d_type;      /* type of file; not supported
                                   by all file system types */
    char           d_name[256]; /* filename */
};

refer: man readdir

Or just look for "dirent.h" in the include directory.

Bringhurst answered 20/10, 2012 at 18:31 Comment(2)
As written in another answer, only d_ino and d_name are POSIX. The rest should be avoided, or at worse used very cautiously, and only if you understand the implications.Tellez
The rest of the fields are needed just for extremely simple things (like determining if an entry refers to a file or a sub-directory), and telling people not to use any of the other fields is pointless (because they almost always must use them).Disquieting
M
5

There are only two members (from wikipedia):

  • ino_t d_ino - file serial number
  • char d_name[] - name of entry (will not exceed a size of NAME_MAX)

Take a look at the unix spec as well.

Marchioness answered 20/10, 2012 at 18:20 Comment(1)
There might be some other (implementation or system specific) members, but you should not use them for POSIX portability reasons.Immersion
I
1

in addition to above answer of @Binyamin Sharet:

 off_t d_off - file offset
    unsigned short int d_reclen - length of the dirent record
    unsigned short int d_namlen - length of name
    unsigned int d_type - type of file
Illassorted answered 20/10, 2012 at 18:22 Comment(2)
These should not be used. They are implementation-specific and not defined by POSIX. You should probably update your answer to reflect that.Wingo
some filesystems (e.g. ext4, xfs) in some configurations (usually at mkfs time) can deliver the type of (some or all) the directory entries (the DT_* symbols). If not known, they are DT_UNKNOWN. The d_type member is not portable, but nevertheless widely available.Bessbessarabia

© 2022 - 2024 — McMap. All rights reserved.