1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright 2008, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
7 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
8 .\" Derived from 'readdir.2'.
9 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
11 .TH GETDENTS 2 2021-03-22 "Linux" "Linux Programmer's Manual"
13 getdents, getdents64 \- get directory entries
16 .RI ( libc ", " \-lc )
19 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
20 .B #include <unistd.h>
22 .BI "long syscall(SYS_getdents, unsigned int " fd \
23 ", struct linux_dirent *" dirp ,
24 .BI " unsigned int " count );
26 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
27 .B #include <dirent.h>
29 .BI "ssize_t getdents64(int " fd ", void *" dirp ", size_t " count );
33 glibc provides no wrapper for
35 necessitating the use of
39 There is no definition of
40 .I struct linux_dirent
43 These are not the interfaces you are interested in.
46 for the POSIX-conforming C library interface.
47 This page documents the bare kernel system call interfaces.
53 structures from the directory
54 referred to by the open file descriptor
56 into the buffer pointed to by
60 specifies the size of that buffer.
64 structure is declared as follows:
69 unsigned long d_ino; /* Inode number */
70 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
71 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
72 char d_name[]; /* Filename (null\-terminated) */
73 /* length is actually (d_reclen \- 2 \-
74 offsetof(struct linux_dirent, d_name)) */
76 char pad; // Zero padding byte
77 char d_type; // File type (only since Linux
78 // 2.6.4); offset is (d_reclen \- 1)
87 is the distance from the start of the directory to the start of the next
90 is the size of this entire
93 is a null-terminated filename.
96 is a byte at the end of the structure that indicates the file type.
97 It contains one of the following values (defined in
101 This is a block device.
104 This is a character device.
110 This is a named pipe (FIFO).
113 This is a symbolic link.
116 This is a regular file.
119 This is a UNIX domain socket.
122 The file type is unknown.
126 field is implemented since Linux 2.6.4.
127 It occupies a space that was previously a zero-filled padding byte in the
130 Thus, on kernels up to and including 2.6.3,
131 attempting to access this field always provides the value 0
136 .\" The same sentence is in readdir.2
137 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
138 have full support for returning the file type in
140 All applications must properly handle a return of
145 system call did not handle large filesystems and large file offsets.
146 Consequently, Linux 2.4 added
148 with wider types for the
163 except that its second argument is a pointer to a buffer containing
164 structures of the following type:
168 struct linux_dirent64 {
169 ino64_t d_ino; /* 64\-bit inode number */
170 off64_t d_off; /* 64\-bit offset to next structure */
171 unsigned short d_reclen; /* Size of this dirent */
172 unsigned char d_type; /* File type */
173 char d_name[]; /* Filename (null\-terminated) */
178 On success, the number of bytes read is returned.
179 On end of directory, 0 is returned.
180 On error, \-1 is returned, and
182 is set to indicate the error.
186 Invalid file descriptor
190 Argument points outside the calling process's address space.
193 Result buffer is too small.
199 File descriptor does not refer to a directory.
202 .\" SVr4 documents additional ENOLINK, EIO error conditions.
206 was added in glibc 2.30;
207 Glibc does not provide a wrapper for
213 on earlier glibc versions) using
215 In that case you will need to define the
221 Probably, you want to use
223 instead of these system calls.
225 These calls supersede
228 .\" FIXME The example program needs to be revised, since it uses the older
229 .\" getdents() system call and the structure with smaller field widths.
230 The program below demonstrates the use of
232 The following output shows an example of what we see when running this
233 program on an ext2 directory:
237 .RB "$" " ./a.out /testfs/"
238 -\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=120 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
239 inode# file type d_reclen d_off d_name
242 11 directory 24 44 lost+found
244 228929 directory 16 68 sub
245 16353 directory 16 80 sub2
246 130817 directory 16 4096 sub3
253 #include <dirent.h> /* Defines DT_* constants */
259 #include <sys/stat.h>
260 #include <sys/syscall.h>
262 #define handle_error(msg) \e
263 do { perror(msg); exit(EXIT_FAILURE); } while (0)
265 struct linux_dirent {
268 unsigned short d_reclen;
272 #define BUF_SIZE 1024
275 main(int argc, char *argv[])
280 struct linux_dirent *d;
283 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
285 handle_error("open");
288 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
290 handle_error("getdents");
295 printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
296 printf("inode# file type d_reclen d_off d_name\en");
297 for (long bpos = 0; bpos < nread;) {
298 d = (struct linux_dirent *) (buf + bpos);
299 printf("%8ld ", d\->d_ino);
300 d_type = *(buf + bpos + d\->d_reclen \- 1);
301 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
302 (d_type == DT_DIR) ? "directory" :
303 (d_type == DT_FIFO) ? "FIFO" :
304 (d_type == DT_SOCK) ? "socket" :
305 (d_type == DT_LNK) ? "symlink" :
306 (d_type == DT_BLK) ? "block dev" :
307 (d_type == DT_CHR) ? "char dev" : "???");
308 printf("%4d %10jd %s\en", d\->d_reclen,
309 (intmax_t) d\->d_off, d\->d_name);
310 bpos += d\->d_reclen;