1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright 2008, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Written 11 June 1995 by Andries Brouwer <aeb@cwi.nl>
7 .\" Modified 22 July 1995 by Michael Chastain <mec@duracef.shout.net>:
8 .\" Derived from 'readdir.2'.
9 .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
11 .TH getdents 2 (date) "Linux man-pages (unreleased)"
13 getdents, getdents64 \- get directory entries
16 .RI ( libc ", " \-lc )
19 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
20 .B #include <unistd.h>
22 .BI "long syscall(SYS_getdents, unsigned int " fd \
23 ", struct linux_dirent *" dirp ,
24 .BI " unsigned int " count );
26 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
27 .B #include <dirent.h>
29 .BI "ssize_t getdents64(int " fd ", void " dirp [. count "], size_t " count );
33 glibc provides no wrapper for
35 necessitating the use of
39 There is no definition of
40 .I struct linux_dirent
43 These are not the interfaces you are interested in.
46 for the POSIX-conforming C library interface.
47 This page documents the bare kernel system call interfaces.
53 structures from the directory
54 referred to by the open file descriptor
56 into the buffer pointed to by
60 specifies the size of that buffer.
64 structure is declared as follows:
69 unsigned long d_ino; /* Inode number */
70 unsigned long d_off; /* Not an offset; see below */
71 unsigned short d_reclen; /* Length of this \fIlinux_dirent\fP */
72 char d_name[]; /* Filename (null\-terminated) */
73 /* length is actually (d_reclen \- 2 \-
74 offsetof(struct linux_dirent, d_name)) */
76 char pad; // Zero padding byte
77 char d_type; // File type (only since Linux
78 // 2.6.4); offset is (d_reclen \- 1)
87 is a filesystem-specific value with no specific meaning to user space,
88 though on older filesystems it used to be
89 the distance from the start of the directory to the start of the next
94 is the size of this entire
97 is a null-terminated filename.
100 is a byte at the end of the structure that indicates the file type.
101 It contains one of the following values (defined in
105 This is a block device.
108 This is a character device.
114 This is a named pipe (FIFO).
117 This is a symbolic link.
120 This is a regular file.
123 This is a UNIX domain socket.
126 The file type is unknown.
130 field is implemented since Linux 2.6.4.
131 It occupies a space that was previously a zero-filled padding byte in the
134 Thus, on kernels up to and including Linux 2.6.3,
135 attempting to access this field always provides the value 0
140 .\" The same sentence is in readdir.2
141 only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
142 have full support for returning the file type in
144 All applications must properly handle a return of
149 system call did not handle large filesystems and large file offsets.
150 Consequently, Linux 2.4 added
152 with wider types for the
167 except that its second argument is a pointer to a buffer containing
168 structures of the following type:
172 struct linux_dirent64 {
173 ino64_t d_ino; /* 64\-bit inode number */
174 off64_t d_off; /* Not an offset; see getdents() */
175 unsigned short d_reclen; /* Size of this dirent */
176 unsigned char d_type; /* File type */
177 char d_name[]; /* Filename (null\-terminated) */
182 On success, the number of bytes read is returned.
183 On end of directory, 0 is returned.
184 On error, \-1 is returned, and
186 is set to indicate the error.
190 Invalid file descriptor
194 Argument points outside the calling process's address space.
197 Result buffer is too small.
203 File descriptor does not refer to a directory.
208 .\" SVr4 documents additional ENOLINK, EIO error conditions.
213 glibc does not provide a wrapper for
219 In that case you will need to define the
225 Probably, you want to use
227 instead of these system calls.
229 These calls supersede
232 .\" FIXME The example program needs to be revised, since it uses the older
233 .\" getdents() system call and the structure with smaller field widths.
234 The program below demonstrates the use of
236 The following output shows an example of what we see when running this
237 program on an ext2 directory:
241 .RB "$" " ./a.out /testfs/"
242 -\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=120 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
243 inode# file type d_reclen d_off d_name
246 11 directory 24 44 lost+found
248 228929 directory 16 68 sub
249 16353 directory 16 80 sub2
250 130817 directory 16 4096 sub3
255 .\" SRC BEGIN (getdents.c)
258 #include <dirent.h> /* Defines DT_* constants */
264 #include <sys/syscall.h>
267 struct linux_dirent {
270 unsigned short d_reclen;
274 #define BUF_SIZE 1024
277 main(int argc, char *argv[])
283 struct linux_dirent *d;
285 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
287 err(EXIT_FAILURE, "open");
290 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
292 err(EXIT_FAILURE, "getdents");
297 printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%ld \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
298 printf("inode# file type d_reclen d_off d_name\en");
299 for (size_t bpos = 0; bpos < nread;) {
300 d = (struct linux_dirent *) (buf + bpos);
301 printf("%8lu ", d\->d_ino);
302 d_type = *(buf + bpos + d\->d_reclen \- 1);
303 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
304 (d_type == DT_DIR) ? "directory" :
305 (d_type == DT_FIFO) ? "FIFO" :
306 (d_type == DT_SOCK) ? "socket" :
307 (d_type == DT_LNK) ? "symlink" :
308 (d_type == DT_BLK) ? "block dev" :
309 (d_type == DT_CHR) ? "char dev" : "???");
310 printf("%4d %10jd %s\en", d\->d_reclen,
311 (intmax_t) d\->d_off, d\->d_name);
312 bpos += d\->d_reclen;