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>
29 .TH GETDENTS 2 2012-08-03 "Linux" "Linux Programmer's Manual"
31 getdents \- get directory entries
34 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
35 .BI " unsigned int " count );
39 There is no glibc wrapper for this system call; see NOTES.
41 This is not the function you are interested in.
44 for the POSIX conforming C library interface.
45 This page documents the bare kernel system call interface.
51 structures from the directory
52 referred to by the open file descriptor
54 into the buffer pointed to by
58 specifies the size of that buffer.
62 structure is declared as follows:
67 unsigned long d_ino; /* Inode number */
68 unsigned long d_off; /* Offset to next \fIlinux_dirent\fP */
69 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
70 char d_name[]; /* Filename (null-terminated) */
71 /* length is actually (d_reclen \- 2 \-
72 offsetof(struct linux_dirent, d_name) */
74 char pad; // Zero padding byte
75 char d_type; // File type (only since Linux 2.6.4;
76 // offset is (d_reclen \- 1))
86 is the distance from the start of the directory to the start of the next
89 is the size of this entire
92 is a null-terminated filename.
95 is a byte at the end of the structure that indicates the file type.
96 It contains one of the following values (defined in
100 This is a block device.
103 This is a character device.
109 This is a named pipe (FIFO).
112 This is a symbolic link.
115 This is a regular file.
118 This is a UNIX domain socket.
121 The file type is unknown.
125 field is implemented since Linux 2.6.4.
126 It occupies a space that was previously a zero-filled padding byte in the
129 Thus, on kernels before 2.6.3,
130 attempting to access this field always provides the value 0
135 .\" The same sentence is in readdir.2
136 only some file systems (among them: Btrfs, ext2, ext3, and ext4)
137 have full support for returning the file type in
139 All applications must properly handle a return of
142 On success, the number of bytes read is returned.
143 On end of directory, 0 is returned.
144 On error, \-1 is returned, and
146 is set appropriately.
150 Invalid file descriptor
154 Argument points outside the calling process's address space.
157 Result buffer is too small.
163 File descriptor does not refer to a directory.
166 .\" SVr4 documents additional ENOLINK, EIO error conditions.
168 Glibc does not provide a wrapper for this system call; call it using
170 You will need to define the
173 However, you probably want to use
182 system call did not handle large file systems and large file offsets.
183 Consequently, Linux 2.4 added
185 with wider types for the
189 fields employed in the
193 .\" FIXME: This program uses the older getdents(0 system call
194 .\" and the structure with smaller field widths.
195 The program below demonstrates the use of
197 The following output shows an example of what we see when running this
198 program on an ext2 directory:
202 .RB "$" " ./a.out /testfs/"
203 --------------- nread=120 ---------------
204 i-node# file type d_reclen d_off d_name
207 11 directory 24 44 lost+found
209 228929 directory 16 68 sub
210 16353 directory 16 80 sub2
211 130817 directory 16 4096 sub3
218 #include <dirent.h> /* Defines DT_* constants */
223 #include <sys/stat.h>
224 #include <sys/syscall.h>
226 #define handle_error(msg) \\
227 do { perror(msg); exit(EXIT_FAILURE); } while (0)
229 struct linux_dirent {
232 unsigned short d_reclen;
236 #define BUF_SIZE 1024
239 main(int argc, char *argv[])
243 struct linux_dirent *d;
247 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
249 handle_error("open");
252 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
254 handle_error("getdents");
259 printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\\n", nread);
260 printf("i\-node# file type d_reclen d_off d_name\\n");
261 for (bpos = 0; bpos < nread;) {
262 d = (struct linux_dirent *) (buf + bpos);
263 printf("%8ld ", d\->d_ino);
264 d_type = *(buf + bpos + d\->d_reclen \- 1);
265 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
266 (d_type == DT_DIR) ? "directory" :
267 (d_type == DT_FIFO) ? "FIFO" :
268 (d_type == DT_SOCK) ? "socket" :
269 (d_type == DT_LNK) ? "symlink" :
270 (d_type == DT_BLK) ? "block dev" :
271 (d_type == DT_CHR) ? "char dev" : "???");
272 printf("%4d %10lld %s\\n", d\->d_reclen,
273 (long long) d\->d_off, d\->d_name);
274 bpos += d\->d_reclen;