1GETDENTS(2) Linux Programmer's Manual GETDENTS(2)
2
3
4
6 getdents, getdents64 - get directory entries
7
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 are no glibc wrappers for these system calls; see NOTES.
15
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
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
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
111 SVr4.
112
114 Glibc does not provide a wrapper for these system calls; call them
115 using syscall(2). You will need to define the linux_dirent or
116 linux_dirent64 structure yourself. However, you probably want to use
117 readdir(3) instead.
118
119 These calls supersede readdir(2).
120
122 The program below demonstrates the use of getdents(). The following
123 output shows an example of what we see when running this program on an
124 ext2 directory:
125
126 $ ./a.out /testfs/
127 --------------- nread=120 ---------------
128 inode# file type d_reclen d_off d_name
129 2 directory 16 12 .
130 2 directory 16 24 ..
131 11 directory 24 44 lost+found
132 12 regular 16 56 a
133 228929 directory 16 68 sub
134 16353 directory 16 80 sub2
135 130817 directory 16 4096 sub3
136
137 Program source
138
139 #define _GNU_SOURCE
140 #include <dirent.h> /* Defines DT_* constants */
141 #include <fcntl.h>
142 #include <stdio.h>
143 #include <unistd.h>
144 #include <stdlib.h>
145 #include <sys/stat.h>
146 #include <sys/syscall.h>
147
148 #define handle_error(msg) \
149 do { perror(msg); exit(EXIT_FAILURE); } while (0)
150
151 struct linux_dirent {
152 long d_ino;
153 off_t d_off;
154 unsigned short d_reclen;
155 char d_name[];
156 };
157
158 #define BUF_SIZE 1024
159
160 int
161 main(int argc, char *argv[])
162 {
163 int fd, nread;
164 char buf[BUF_SIZE];
165 struct linux_dirent *d;
166 int bpos;
167 char d_type;
168
169 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
170 if (fd == -1)
171 handle_error("open");
172
173 for ( ; ; ) {
174 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
175 if (nread == -1)
176 handle_error("getdents");
177
178 if (nread == 0)
179 break;
180
181 printf("--------------- nread=%d ---------------\n", nread);
182 printf("inode# file type d_reclen d_off d_name\n");
183 for (bpos = 0; bpos < nread;) {
184 d = (struct linux_dirent *) (buf + bpos);
185 printf("%8ld ", d->d_ino);
186 d_type = *(buf + bpos + d->d_reclen - 1);
187 printf("%-10s ", (d_type == DT_REG) ? "regular" :
188 (d_type == DT_DIR) ? "directory" :
189 (d_type == DT_FIFO) ? "FIFO" :
190 (d_type == DT_SOCK) ? "socket" :
191 (d_type == DT_LNK) ? "symlink" :
192 (d_type == DT_BLK) ? "block dev" :
193 (d_type == DT_CHR) ? "char dev" : "???");
194 printf("%4d %10lld %s\n", d->d_reclen,
195 (long long) d->d_off, d->d_name);
196 bpos += d->d_reclen;
197 }
198 }
199
200 exit(EXIT_SUCCESS);
201 }
202
204 readdir(2), readdir(3), inode(7)
205
207 This page is part of release 5.02 of the Linux man-pages project. A
208 description of the project, information about reporting bugs, and the
209 latest version of this page, can be found at
210 https://www.kernel.org/doc/man-pages/.
211
212
213
214Linux 2019-03-06 GETDENTS(2)