1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
26 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
27 .\" Derived from 'readdir.2'.
28 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
30 .TH GETDENTS 2 2012-08-03 "Linux" "Linux Programmer's Manual"
32 getdents \- get directory entries
35 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
36 .BI " unsigned int " count );
40 There is no glibc wrapper for this system call; see NOTES.
42 This is not the function you are interested in.
45 for the POSIX conforming C library interface.
46 This page documents the bare kernel system call interface.
52 structures from the directory
53 referred to by the open file descriptor
55 into the buffer pointed to by
59 specifies the size of that buffer.
63 structure is declared as follows:
68 unsigned long d_ino; /* Inode number */
69 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
70 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
71 char d_name[]; /* Filename (null-terminated) */
72 /* length is actually (d_reclen \- 2 \-
73 offsetof(struct linux_dirent, d_name)) */
75 char pad; // Zero padding byte
76 char d_type; // File type (only since Linux
77 // 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 before 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 file systems (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
143 On success, the number of bytes read is returned.
144 On end of directory, 0 is returned.
145 On error, \-1 is returned, and
147 is set appropriately.
151 Invalid file descriptor
155 Argument points outside the calling process's address space.
158 Result buffer is too small.
164 File descriptor does not refer to a directory.
167 .\" SVr4 documents additional ENOLINK, EIO error conditions.
169 Glibc does not provide a wrapper for this system call; call it using
171 You will need to define the
174 However, you probably want to use
183 system call did not handle large file systems and large file offsets.
184 Consequently, Linux 2.4 added
186 with wider types for the
190 fields employed in the
194 .\" FIXME: This program uses the older getdents() system call
195 .\" and the structure with smaller field widths.
196 The program below demonstrates the use of
198 The following output shows an example of what we see when running this
199 program on an ext2 directory:
203 .RB "$" " ./a.out /testfs/"
204 --------------- nread=120 ---------------
205 i-node# file type d_reclen d_off d_name
208 11 directory 24 44 lost+found
210 228929 directory 16 68 sub
211 16353 directory 16 80 sub2
212 130817 directory 16 4096 sub3
219 #include <dirent.h> /* Defines DT_* constants */
224 #include <sys/stat.h>
225 #include <sys/syscall.h>
227 #define handle_error(msg) \\
228 do { perror(msg); exit(EXIT_FAILURE); } while (0)
230 struct linux_dirent {
233 unsigned short d_reclen;
237 #define BUF_SIZE 1024
240 main(int argc, char *argv[])
244 struct linux_dirent *d;
248 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
250 handle_error("open");
253 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
255 handle_error("getdents");
260 printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\\n", nread);
261 printf("i\-node# file type d_reclen d_off d_name\\n");
262 for (bpos = 0; bpos < nread;) {
263 d = (struct linux_dirent *) (buf + bpos);
264 printf("%8ld ", d\->d_ino);
265 d_type = *(buf + bpos + d\->d_reclen \- 1);
266 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
267 (d_type == DT_DIR) ? "directory" :
268 (d_type == DT_FIFO) ? "FIFO" :
269 (d_type == DT_SOCK) ? "socket" :
270 (d_type == DT_LNK) ? "symlink" :
271 (d_type == DT_BLK) ? "block dev" :
272 (d_type == DT_CHR) ? "char dev" : "???");
273 printf("%4d %10lld %s\\n", d\->d_reclen,
274 (long long) d\->d_off, d\->d_name);
275 bpos += d\->d_reclen;