]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getdents.2
memusage.1, clone.2, eventfd.2, futex.2, getdents.2, ioctl_fat.2, ioctl_ns.2, kcmp...
[thirdparty/man-pages.git] / man2 / getdents.2
1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright 2008, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
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>
30 .\"
31 .TH GETDENTS 2 2017-05-03 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 getdents, getdents64 \- get directory entries
34 .SH SYNOPSIS
35 .nf
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 );
40 .fi
41 .PP
42 .IR Note :
43 There are no glibc wrappers for these system calls; see NOTES.
44 .SH DESCRIPTION
45 These are not the interfaces you are interested in.
46 Look at
47 .BR readdir (3)
48 for the POSIX-conforming C library interface.
49 This page documents the bare kernel system call interfaces.
50 .SS getdents()
51 The system call
52 .BR getdents ()
53 reads several
54 .I linux_dirent
55 structures from the directory
56 referred to by the open file descriptor
57 .I fd
58 into the buffer pointed to by
59 .IR dirp .
60 The argument
61 .I count
62 specifies the size of that buffer.
63 .PP
64 The
65 .I linux_dirent
66 structure is declared as follows:
67 .PP
68 .in +4n
69 .nf
70 struct linux_dirent {
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)) */
77 /*
78 char pad; // Zero padding byte
79 char d_type; // File type (only since Linux
80 // 2.6.4); offset is (d_reclen \- 1)
81 */
82 }
83 .fi
84 .in
85 .PP
86 .I d_ino
87 is an inode number.
88 .I d_off
89 is the distance from the start of the directory to the start of the next
90 .IR linux_dirent .
91 .I d_reclen
92 is the size of this entire
93 .IR linux_dirent .
94 .I d_name
95 is a null-terminated filename.
96 .PP
97 .I d_type
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
100 .IR <dirent.h> ):
101 .TP 12
102 .B DT_BLK
103 This is a block device.
104 .TP
105 .B DT_CHR
106 This is a character device.
107 .TP
108 .B DT_DIR
109 This is a directory.
110 .TP
111 .B DT_FIFO
112 This is a named pipe (FIFO).
113 .TP
114 .B DT_LNK
115 This is a symbolic link.
116 .TP
117 .B DT_REG
118 This is a regular file.
119 .TP
120 .B DT_SOCK
121 This is a UNIX domain socket.
122 .TP
123 .B DT_UNKNOWN
124 The file type is unknown.
125 .PP
126 The
127 .I d_type
128 field is implemented since Linux 2.6.4.
129 It occupies a space that was previously a zero-filled padding byte in the
130 .IR linux_dirent
131 structure.
132 Thus, on kernels up to and including 2.6.3,
133 attempting to access this field always provides the value 0
134 .RB ( DT_UNKNOWN ).
135 .PP
136 Currently,
137 .\" kernel 2.6.27
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
141 .IR d_type .
142 All applications must properly handle a return of
143 .BR DT_UNKNOWN .
144 .SS getdents64()
145 The original Linux
146 .BR getdents ()
147 system call did not handle large filesystems and large file offsets.
148 Consequently, Linux 2.4 added
149 .BR getdents64 (),
150 with wider types for the
151 .I d_ino
152 and
153 .I d_off
154 fields.
155 In addition,
156 .BR getdents64 ()
157 supports an explicit
158 .I d_type
159 field.
160 .PP
161 The
162 .BR getdents64 ()
163 system call is like
164 .BR getdents (),
165 except that its second argument is a pointer to a buffer containing
166 structures of the following type:
167 .PP
168 .nf
169 .in +4n
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) */
176 };
177 .in
178 .fi
179 .SH RETURN VALUE
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
183 .I errno
184 is set appropriately.
185 .SH ERRORS
186 .TP
187 .B EBADF
188 Invalid file descriptor
189 .IR fd .
190 .TP
191 .B EFAULT
192 Argument points outside the calling process's address space.
193 .TP
194 .B EINVAL
195 Result buffer is too small.
196 .TP
197 .B ENOENT
198 No such directory.
199 .TP
200 .B ENOTDIR
201 File descriptor does not refer to a directory.
202 .SH CONFORMING TO
203 SVr4.
204 .\" SVr4 documents additional ENOLINK, EIO error conditions.
205 .SH NOTES
206 Glibc does not provide a wrapper for these system calls; call them using
207 .BR syscall (2).
208 You will need to define the
209 .I linux_dirent
210 or
211 .I linux_dirent64
212 structure yourself.
213 However, you probably want to use
214 .BR readdir (3)
215 instead.
216 .PP
217 These calls supersede
218 .BR readdir (2).
219 .SH EXAMPLE
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
223 .BR getdents ().
224 The following output shows an example of what we see when running this
225 program on an ext2 directory:
226 .PP
227 .in +4n
228 .nf
229 .RB "$" " ./a.out /testfs/"
230 --------------- nread=120 ---------------
231 inode# file type d_reclen d_off d_name
232 2 directory 16 12 .
233 2 directory 16 24 ..
234 11 directory 24 44 lost+found
235 12 regular 16 56 a
236 228929 directory 16 68 sub
237 16353 directory 16 80 sub2
238 130817 directory 16 4096 sub3
239 .fi
240 .in
241 .SS Program source
242 \&
243 .EX
244 #define _GNU_SOURCE
245 #include <dirent.h> /* Defines DT_* constants */
246 #include <fcntl.h>
247 #include <stdio.h>
248 #include <unistd.h>
249 #include <stdlib.h>
250 #include <sys/stat.h>
251 #include <sys/syscall.h>
252
253 #define handle_error(msg) \\
254 do { perror(msg); exit(EXIT_FAILURE); } while (0)
255
256 struct linux_dirent {
257 long d_ino;
258 off_t d_off;
259 unsigned short d_reclen;
260 char d_name[];
261 };
262
263 #define BUF_SIZE 1024
264
265 int
266 main(int argc, char *argv[])
267 {
268 int fd, nread;
269 char buf[BUF_SIZE];
270 struct linux_dirent *d;
271 int bpos;
272 char d_type;
273
274 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
275 if (fd == \-1)
276 handle_error("open");
277
278 for ( ; ; ) {
279 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
280 if (nread == \-1)
281 handle_error("getdents");
282
283 if (nread == 0)
284 break;
285
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;
302 }
303 }
304
305 exit(EXIT_SUCCESS);
306 }
307 .EE
308 .SH SEE ALSO
309 .BR readdir (2),
310 .BR readdir (3),
311 .BR inode (7)