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 2017-05-03 "Linux" "Linux Programmer's Manual"
33 getdents, getdents64 \- get directory entries
36 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
37 .BI " unsigned int " count );
38 .BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
39 .BI " unsigned int " count );
43 There are no glibc wrappers for these system calls; see NOTES.
45 These are not the interfaces you are interested in.
48 for the POSIX-conforming C library interface.
49 This page documents the bare kernel system call interfaces.
55 structures from the directory
56 referred to by the open file descriptor
58 into the buffer pointed to by
62 specifies the size of that buffer.
66 structure is declared as follows:
71 unsigned long d_ino; /* Inode number */
72 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
73 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
74 char d_name[]; /* Filename (null-terminated) */
75 /* length is actually (d_reclen \- 2 \-
76 offsetof(struct linux_dirent, d_name)) */
78 char pad; // Zero padding byte
79 char d_type; // File type (only since Linux
80 // 2.6.4); offset is (d_reclen \- 1)
89 is the distance from the start of the directory to the start of the next
92 is the size of this entire
95 is a null-terminated filename.
98 is a byte at the end of the structure that indicates the file type.
99 It contains one of the following values (defined in
103 This is a block device.
106 This is a character device.
112 This is a named pipe (FIFO).
115 This is a symbolic link.
118 This is a regular file.
121 This is a UNIX domain socket.
124 The file type is unknown.
128 field is implemented since Linux 2.6.4.
129 It occupies a space that was previously a zero-filled padding byte in the
132 Thus, on kernels up to and including 2.6.3,
133 attempting to access this field always provides the value 0
138 .\" The same sentence is in readdir.2
139 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
140 have full support for returning the file type in
142 All applications must properly handle a return of
147 system call did not handle large filesystems and large file offsets.
148 Consequently, Linux 2.4 added
150 with wider types for the
165 except that its second argument is a pointer to a buffer containing
166 structures of the following type:
170 struct linux_dirent64 {
171 ino64_t d_ino; /* 64-bit inode number */
172 off64_t d_off; /* 64-bit offset to next structure */
173 unsigned short d_reclen; /* Size of this dirent */
174 unsigned char d_type; /* File type */
175 char d_name[]; /* Filename (null-terminated) */
180 On success, the number of bytes read is returned.
181 On end of directory, 0 is returned.
182 On error, \-1 is returned, and
184 is set appropriately.
188 Invalid file descriptor
192 Argument points outside the calling process's address space.
195 Result buffer is too small.
201 File descriptor does not refer to a directory.
204 .\" SVr4 documents additional ENOLINK, EIO error conditions.
206 Glibc does not provide a wrapper for these system calls; call them using
208 You will need to define the
213 However, you probably want to use
217 These calls supersede
220 .\" FIXME The example program needs to be revised, since it uses the older
221 .\" getdents() system call and the structure with smaller field widths.
222 The program below demonstrates the use of
224 The following output shows an example of what we see when running this
225 program on an ext2 directory:
229 .RB "$" " ./a.out /testfs/"
230 --------------- nread=120 ---------------
231 inode# file type d_reclen d_off d_name
234 11 directory 24 44 lost+found
236 228929 directory 16 68 sub
237 16353 directory 16 80 sub2
238 130817 directory 16 4096 sub3
245 #include <dirent.h> /* Defines DT_* constants */
250 #include <sys/stat.h>
251 #include <sys/syscall.h>
253 #define handle_error(msg) \\
254 do { perror(msg); exit(EXIT_FAILURE); } while (0)
256 struct linux_dirent {
259 unsigned short d_reclen;
263 #define BUF_SIZE 1024
266 main(int argc, char *argv[])
270 struct linux_dirent *d;
274 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
276 handle_error("open");
279 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
281 handle_error("getdents");
286 printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\\n", nread);
287 printf("inode# file type d_reclen d_off d_name\\n");
288 for (bpos = 0; bpos < nread;) {
289 d = (struct linux_dirent *) (buf + bpos);
290 printf("%8ld ", d\->d_ino);
291 d_type = *(buf + bpos + d\->d_reclen \- 1);
292 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
293 (d_type == DT_DIR) ? "directory" :
294 (d_type == DT_FIFO) ? "FIFO" :
295 (d_type == DT_SOCK) ? "socket" :
296 (d_type == DT_LNK) ? "symlink" :
297 (d_type == DT_BLK) ? "block dev" :
298 (d_type == DT_CHR) ? "char dev" : "???");
299 printf("%4d %10lld %s\\n", d\->d_reclen,
300 (long long) d\->d_off, d\->d_name);
301 bpos += d\->d_reclen;