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

NAME

6       getdents, getdents64 - get directory entries
7

SYNOPSIS

9       long getdents(unsigned int fd, struct linux_dirent *dirp,
10                    unsigned int count);
11
12       #define _GNU_SOURCE        /* See feature_test_macros(7) */
13       #include <dirent.h>
14
15       ssize_t getdents64(int fd, void *dirp, size_t count);
16
17       Note: There is no glibc wrapper for getdents(); see NOTES.
18

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

CONFORMING TO

114       SVr4.
115

NOTES

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

EXAMPLES

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

SEE ALSO

211       readdir(2), readdir(3), inode(7)
212

COLOPHON

214       This  page  is  part of release 5.10 of the Linux man-pages project.  A
215       description of the project, information about reporting bugs,  and  the
216       latest     version     of     this    page,    can    be    found    at
217       https://www.kernel.org/doc/man-pages/.
218
219
220
221Linux                             2020-11-01                       GETDENTS(2)
Impressum