1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright 2008, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
27 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
28 .\" Derived from 'readdir.2'.
29 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
31 .TH GETDENTS 2 2021-03-22 "Linux" "Linux Programmer's Manual"
33 getdents, getdents64 \- get directory entries
36 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
37 .B #include <unistd.h>
39 .BI "long syscall(SYS_getdents, unsigned int " fd \
40 ", struct linux_dirent *" dirp ,
41 .BI " unsigned int " count );
43 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
44 .BR "#include <dirent.h>"
46 .BI "ssize_t getdents64(int " fd ", void *" dirp ", size_t " count );
50 glibc provides no wrapper for
52 necessitating the use of
56 There is no definition of
57 .I struct linux_dirent
60 These are not the interfaces you are interested in.
63 for the POSIX-conforming C library interface.
64 This page documents the bare kernel system call interfaces.
70 structures from the directory
71 referred to by the open file descriptor
73 into the buffer pointed to by
77 specifies the size of that buffer.
81 structure is declared as follows:
86 unsigned long d_ino; /* Inode number */
87 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
88 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
89 char d_name[]; /* Filename (null\-terminated) */
90 /* length is actually (d_reclen \- 2 \-
91 offsetof(struct linux_dirent, d_name)) */
93 char pad; // Zero padding byte
94 char d_type; // File type (only since Linux
95 // 2.6.4); offset is (d_reclen \- 1)
104 is the distance from the start of the directory to the start of the next
107 is the size of this entire
110 is a null-terminated filename.
113 is a byte at the end of the structure that indicates the file type.
114 It contains one of the following values (defined in
118 This is a block device.
121 This is a character device.
127 This is a named pipe (FIFO).
130 This is a symbolic link.
133 This is a regular file.
136 This is a UNIX domain socket.
139 The file type is unknown.
143 field is implemented since Linux 2.6.4.
144 It occupies a space that was previously a zero-filled padding byte in the
147 Thus, on kernels up to and including 2.6.3,
148 attempting to access this field always provides the value 0
153 .\" The same sentence is in readdir.2
154 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
155 have full support for returning the file type in
157 All applications must properly handle a return of
162 system call did not handle large filesystems and large file offsets.
163 Consequently, Linux 2.4 added
165 with wider types for the
180 except that its second argument is a pointer to a buffer containing
181 structures of the following type:
185 struct linux_dirent64 {
186 ino64_t d_ino; /* 64\-bit inode number */
187 off64_t d_off; /* 64\-bit offset to next structure */
188 unsigned short d_reclen; /* Size of this dirent */
189 unsigned char d_type; /* File type */
190 char d_name[]; /* Filename (null\-terminated) */
195 On success, the number of bytes read is returned.
196 On end of directory, 0 is returned.
197 On error, \-1 is returned, and
199 is set to indicate the error.
203 Invalid file descriptor
207 Argument points outside the calling process's address space.
210 Result buffer is too small.
216 File descriptor does not refer to a directory.
219 .\" SVr4 documents additional ENOLINK, EIO error conditions.
223 was added in glibc 2.30;
224 Glibc does not provide a wrapper for
230 on earlier glibc versions) using
232 In that case you will need to define the
238 Probably, you want to use
240 instead of these system calls.
242 These calls supersede
245 .\" FIXME The example program needs to be revised, since it uses the older
246 .\" getdents() system call and the structure with smaller field widths.
247 The program below demonstrates the use of
249 The following output shows an example of what we see when running this
250 program on an ext2 directory:
254 .RB "$" " ./a.out /testfs/"
255 -\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=120 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
256 inode# file type d_reclen d_off d_name
259 11 directory 24 44 lost+found
261 228929 directory 16 68 sub
262 16353 directory 16 80 sub2
263 130817 directory 16 4096 sub3
270 #include <dirent.h> /* Defines DT_* constants */
276 #include <sys/stat.h>
277 #include <sys/syscall.h>
279 #define handle_error(msg) \e
280 do { perror(msg); exit(EXIT_FAILURE); } while (0)
282 struct linux_dirent {
285 unsigned short d_reclen;
289 #define BUF_SIZE 1024
292 main(int argc, char *argv[])
297 struct linux_dirent *d;
300 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
302 handle_error("open");
305 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
307 handle_error("getdents");
312 printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
313 printf("inode# file type d_reclen d_off d_name\en");
314 for (long bpos = 0; bpos < nread;) {
315 d = (struct linux_dirent *) (buf + bpos);
316 printf("%8ld ", d\->d_ino);
317 d_type = *(buf + bpos + d\->d_reclen \- 1);
318 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
319 (d_type == DT_DIR) ? "directory" :
320 (d_type == DT_FIFO) ? "FIFO" :
321 (d_type == DT_SOCK) ? "socket" :
322 (d_type == DT_LNK) ? "symlink" :
323 (d_type == DT_BLK) ? "block dev" :
324 (d_type == DT_CHR) ? "char dev" : "???");
325 printf("%4d %10jd %s\en", d\->d_reclen,
326 (intmax_t) d\->d_off, d\->d_name);
327 bpos += d\->d_reclen;