1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
24 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
25 .\" Derived from 'readdir.2'.
26 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .TH GETDENTS 2 2009-07-04 "Linux" "Linux Programmer's Manual"
29 getdents \- get directory entries
32 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
33 .BI " unsigned int " count );
36 This is not the function you are interested in.
39 for the POSIX conforming C library interface.
40 This page documents the bare kernel system call interface.
46 structures from the directory
47 referred to by the open file descriptor
49 into the buffer pointed to by
53 specifies the size of that buffer.
57 structure is declared as follows:
62 unsigned long d_ino; /* Inode number */
63 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
64 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
65 char d_name[]; /* Filename (null-terminated) */
66 /* length is actually (d_reclen \- 2 \-
67 offsetof(struct linux_dirent, d_name) */
69 char pad; // Zero padding byte */
70 char d_type; // File type (only since Linux 2.6.4;
71 // offset is (d_reclen \- 1))
81 is the distance from the start of the directory to the start of the next
84 is the size of this entire
87 is a null-terminated filename.
90 is a byte at the end of the structure that indicates the file type.
91 It contains one of the following values (defined in
95 This is a block device.
98 This is a character device.
104 This is a named pipe (FIFO).
107 This is a symbolic link.
110 This is a regular file.
113 This is a Unix domain socket.
116 The file type is unknown.
120 field is implemented since Linux 2.6.4.
121 It occupies a space that was previously a zero-filled padding byte in the
124 Thus, on kernels before 2.6.3,
125 attempting to access this field always provides the value 0
130 .\" The same sentence is in readdir.2
131 only some file systems (among them: Btrfs, ext2, etx3, and ext4)
132 have full support for returning the file type in
134 All applications must properly handle a return of
137 On success, the number of bytes read is returned.
138 On end of directory, 0 is returned.
139 On error, \-1 is returned, and
141 is set appropriately.
145 Invalid file descriptor
149 Argument points outside the calling process's address space.
152 Result buffer is too small.
158 File descriptor does not refer to a directory.
161 .\" SVr4 documents additional ENOLINK, EIO error conditions.
163 Glibc does not provide a wrapper for this system call; call it using
165 You will need to define the
172 The program below demonstrates the use of
174 The following output shows an example of what we see when running this
175 program on an ext2 directory:
179 .RB "$" " ./a.out /testfs/"
180 --------------- nread=120 ---------------
181 i-node# file type d_reclen d_off d_name
184 11 directory 24 44 lost+found
186 228929 directory 16 68 sub
187 16353 directory 16 80 sub2
188 130817 directory 16 4096 sub3
195 #include <dirent.h> /* Defines DT_* constants */
200 #include <sys/stat.h>
201 #include <sys/syscall.h>
203 #define handle_error(msg) \\
204 do { perror(msg); exit(EXIT_FAILURE); } while (0)
206 struct linux_dirent {
209 unsigned short d_reclen;
213 #define BUF_SIZE 1024
216 main(int argc, char *argv[])
220 struct linux_dirent *d;
224 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
226 handle_error("open");
229 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
231 handle_error("getdents");
236 printf("\--------------- nread=%d ---------------\\n", nread);
237 printf("i\-node# file type d_reclen d_off d_name\\n");
238 for (bpos = 0; bpos < nread;) {
239 d = (struct linux_dirent *) (buf + bpos);
240 printf("%8ld ", d\->d_ino);
241 d_type = *(buf + bpos + d\->d_reclen - 1);
242 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
243 (d_type == DT_DIR) ? "directory" :
244 (d_type == DT_FIFO) ? "FIFO" :
245 (d_type == DT_SOCK) ? "socket" :
246 (d_type == DT_LNK) ? "symlink" :
247 (d_type == DT_BLK) ? "block dev" :
248 (d_type == DT_CHR) ? "char dev" : "???");
249 printf("%4d %10lld %s\\n", d\->d_reclen,
250 (long long) d\->d_off, (char *) d->d_name);
251 bpos += d\->d_reclen;