1GETDENTS(2)                Linux Programmer's Manual               GETDENTS(2)
2
3
4

NAME

6       getdents, getdents64 - get directory entries
7

SYNOPSIS

9       int getdents(unsigned int fd, struct linux_dirent *dirp,
10                    unsigned int count);
11       int getdents64(unsigned int fd, struct linux_dirent64 *dirp,
12                    unsigned int count);
13
14       Note: There is no glibc wrapper for getdents(); see NOTES.
15

DESCRIPTION

17       These are not the interfaces you are interested in.  Look at readdir(3)
18       for the POSIX-conforming C library interface.  This page documents  the
19       bare kernel system call interfaces.
20
21   getdents()
22       The  system  call getdents() reads several linux_dirent structures from
23       the directory referred to by the open file descriptor fd into the  buf‐
24       fer  pointed to by dirp.  The argument count specifies the size of that
25       buffer.
26
27       The linux_dirent structure is declared as follows:
28
29           struct linux_dirent {
30               unsigned long  d_ino;     /* Inode number */
31               unsigned long  d_off;     /* Offset to next linux_dirent */
32               unsigned short d_reclen;  /* Length of this linux_dirent */
33               char           d_name[];  /* Filename (null-terminated) */
34                                 /* length is actually (d_reclen - 2 -
35                                    offsetof(struct linux_dirent, d_name)) */
36               /*
37               char           pad;       // Zero padding byte
38               char           d_type;    // File type (only since Linux
39                                         // 2.6.4); offset is (d_reclen - 1)
40               */
41           }
42
43       d_ino is an inode number.  d_off is the distance from the start of  the
44       directory  to the start of the next linux_dirent.  d_reclen is the size
45       of this entire linux_dirent.  d_name is a null-terminated filename.
46
47       d_type is a byte at the end of the structure that  indicates  the  file
48       type.  It contains one of the following values (defined in <dirent.h>):
49
50       DT_BLK      This is a block device.
51
52       DT_CHR      This is a character device.
53
54       DT_DIR      This is a directory.
55
56       DT_FIFO     This is a named pipe (FIFO).
57
58       DT_LNK      This is a symbolic link.
59
60       DT_REG      This is a regular file.
61
62       DT_SOCK     This is a UNIX domain socket.
63
64       DT_UNKNOWN  The file type is unknown.
65
66       The d_type field is implemented since Linux 2.6.4.  It occupies a space
67       that was previously a zero-filled  padding  byte  in  the  linux_dirent
68       structure.   Thus,  on kernels up to and including 2.6.3, attempting to
69       access this field always provides the value 0 (DT_UNKNOWN).
70
71       Currently, only some filesystems (among them: Btrfs,  ext2,  ext3,  and
72       ext4)  have  full  support  for returning the file type in d_type.  All
73       applications must properly handle a return of DT_UNKNOWN.
74
75   getdents64()
76       The original Linux getdents() system call did not handle large filesys‐
77       tems  and  large  file  offsets.   Consequently,  Linux  2.4 added get‐
78       dents64(), with wider types for the d_ino and d_off fields.   In  addi‐
79       tion, getdents64() supports an explicit d_type field.
80
81       The getdents64() system call is like getdents(), except that its second
82       argument is a pointer to a buffer containing structures of the  follow‐
83       ing type:
84
85           struct linux_dirent64 {
86               ino64_t        d_ino;    /* 64-bit inode number */
87               off64_t        d_off;    /* 64-bit offset to next structure */
88               unsigned short d_reclen; /* Size of this dirent */
89               unsigned char  d_type;   /* File type */
90               char           d_name[]; /* Filename (null-terminated) */
91           };
92

RETURN VALUE

94       On success, the number of bytes read is returned.  On end of directory,
95       0 is returned.  On error, -1 is returned, and errno  is  set  appropri‐
96       ately.
97

ERRORS

99       EBADF  Invalid file descriptor fd.
100
101       EFAULT Argument points outside the calling process's address space.
102
103       EINVAL Result buffer is too small.
104
105       ENOENT No such directory.
106
107       ENOTDIR
108              File descriptor does not refer to a directory.
109

CONFORMING TO

111       SVr4.
112

NOTES

114       Library  support  for getdents64() was added in glibc 2.30; there is no
115       glibc wrapper for getdents().  Calling getdents() (or  getdents64()  on
116       earlier  glibc  versions) requires the use of syscall(2).  In that case
117       you will need to define the linux_dirent  or  linux_dirent64  structure
118       yourself.
119
120       Probably,  you  probably want to use readdir(3) instead of these system
121       calls.
122
123       These calls supersede readdir(2).
124

EXAMPLES

126       The program below demonstrates the use of  getdents().   The  following
127       output  shows an example of what we see when running this program on an
128       ext2 directory:
129
130           $ ./a.out /testfs/
131           --------------- nread=120 ---------------
132           inode#    file type  d_reclen  d_off   d_name
133                  2  directory    16         12  .
134                  2  directory    16         24  ..
135                 11  directory    24         44  lost+found
136                 12  regular      16         56  a
137             228929  directory    16         68  sub
138              16353  directory    16         80  sub2
139             130817  directory    16       4096  sub3
140
141   Program source
142
143       #define _GNU_SOURCE
144       #include <dirent.h>     /* Defines DT_* constants */
145       #include <fcntl.h>
146       #include <stdio.h>
147       #include <unistd.h>
148       #include <stdlib.h>
149       #include <sys/stat.h>
150       #include <sys/syscall.h>
151
152       #define handle_error(msg) \
153               do { perror(msg); exit(EXIT_FAILURE); } while (0)
154
155       struct linux_dirent {
156           unsigned long  d_ino;
157           off_t          d_off;
158           unsigned short d_reclen;
159           char           d_name[];
160       };
161
162       #define BUF_SIZE 1024
163
164       int
165       main(int argc, char *argv[])
166       {
167           int fd, nread;
168           char buf[BUF_SIZE];
169           struct linux_dirent *d;
170           int bpos;
171           char d_type;
172
173           fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
174           if (fd == -1)
175               handle_error("open");
176
177           for ( ; ; ) {
178               nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
179               if (nread == -1)
180                   handle_error("getdents");
181
182               if (nread == 0)
183                   break;
184
185               printf("--------------- nread=%d ---------------\n", nread);
186               printf("inode#    file type  d_reclen  d_off   d_name\n");
187               for (bpos = 0; bpos < nread;) {
188                   d = (struct linux_dirent *) (buf + bpos);
189                   printf("%8ld  ", d->d_ino);
190                   d_type = *(buf + bpos + d->d_reclen - 1);
191                   printf("%-10s ", (d_type == DT_REG) ?  "regular" :
192                                    (d_type == DT_DIR) ?  "directory" :
193                                    (d_type == DT_FIFO) ? "FIFO" :
194                                    (d_type == DT_SOCK) ? "socket" :
195                                    (d_type == DT_LNK) ?  "symlink" :
196                                    (d_type == DT_BLK) ?  "block dev" :
197                                    (d_type == DT_CHR) ?  "char dev" : "???");
198                   printf("%4d %10lld  %s\n", d->d_reclen,
199                           (long long) d->d_off, d->d_name);
200                   bpos += d->d_reclen;
201               }
202           }
203
204           exit(EXIT_SUCCESS);
205       }
206

SEE ALSO

208       readdir(2), readdir(3), inode(7)
209

COLOPHON

211       This page is part of release 5.07 of the Linux  man-pages  project.   A
212       description  of  the project, information about reporting bugs, and the
213       latest    version    of    this    page,    can     be     found     at
214       https://www.kernel.org/doc/man-pages/.
215
216
217
218Linux                             2020-06-09                       GETDENTS(2)
Impressum