]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getdents.2
getdents.2: srcfix: Add FIXME
[thirdparty/man-pages.git] / man2 / getdents.2
1 .\" Copyright (C) 1995 Andries Brouwer (aeb@cwi.nl)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
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 2010-11-21 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 getdents \- get directory entries
30 .SH SYNOPSIS
31 .nf
32 .BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
33 .BI " unsigned int " count );
34 .fi
35 .SH DESCRIPTION
36 This is not the function you are interested in.
37 Look at
38 .BR readdir (3)
39 for the POSIX conforming C library interface.
40 This page documents the bare kernel system call interface.
41 .PP
42 The system call
43 .BR getdents ()
44 reads several
45 .I linux_dirent
46 structures from the directory
47 referred to by the open file descriptor
48 .I fd
49 into the buffer pointed to by
50 .IR dirp .
51 The argument
52 .I count
53 specifies the size of that buffer.
54 .PP
55 The
56 .I linux_dirent
57 structure is declared as follows:
58 .PP
59 .in +4n
60 .nf
61 struct linux_dirent {
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) */
68 /*
69 char pad; // Zero padding byte
70 char d_type; // File type (only since Linux 2.6.4;
71 // offset is (d_reclen \- 1))
72 */
73
74 }
75 .fi
76 .in
77 .PP
78 .I d_ino
79 is an inode number.
80 .I d_off
81 is the distance from the start of the directory to the start of the next
82 .IR linux_dirent .
83 .I d_reclen
84 is the size of this entire
85 .IR linux_dirent .
86 .I d_name
87 is a null-terminated filename.
88
89 .I d_type
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
92 .IR <dirent.h> ):
93 .TP 12
94 .B DT_BLK
95 This is a block device.
96 .TP
97 .B DT_CHR
98 This is a character device.
99 .TP
100 .B DT_DIR
101 This is a directory.
102 .TP
103 .B DT_FIFO
104 This is a named pipe (FIFO).
105 .TP
106 .B DT_LNK
107 This is a symbolic link.
108 .TP
109 .B DT_REG
110 This is a regular file.
111 .TP
112 .B DT_SOCK
113 This is a UNIX domain socket.
114 .TP
115 .B DT_UNKNOWN
116 The file type is unknown.
117 .PP
118 The
119 .I d_type
120 field is implemented since Linux 2.6.4.
121 It occupies a space that was previously a zero-filled padding byte in the
122 .IR linux_dirent
123 structure.
124 Thus, on kernels before 2.6.3,
125 attempting to access this field always provides the value 0
126 .RB ( DT_UNKNOWN ).
127 .PP
128 Currently,
129 .\" kernel 2.6.27
130 .\" The same sentence is in readdir.2
131 only some file systems (among them: Btrfs, ext2, ext3, and ext4)
132 have full support for returning the file type in
133 .IR d_type .
134 All applications must properly handle a return of
135 .BR DT_UNKNOWN .
136 .SH "RETURN VALUE"
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
140 .I errno
141 is set appropriately.
142 .SH ERRORS
143 .TP
144 .B EBADF
145 Invalid file descriptor
146 .IR fd .
147 .TP
148 .B EFAULT
149 Argument points outside the calling process's address space.
150 .TP
151 .B EINVAL
152 Result buffer is too small.
153 .TP
154 .B ENOENT
155 No such directory.
156 .TP
157 .B ENOTDIR
158 File descriptor does not refer to a directory.
159 .SH "CONFORMING TO"
160 SVr4.
161 .\" SVr4 documents additional ENOLINK, EIO error conditions.
162 .SH NOTES
163 Glibc does not provide a wrapper for this system call; call it using
164 .BR syscall (2).
165 You will need to define the
166 .I linux_dirent
167 structure yourself.
168
169 This call supersedes
170 .BR readdir (2).
171
172 The original Linux
173 .BR getdents ()
174 system call did not handle large file systems and large file offsets.
175 Consequently, Linux 2.4 added
176 .BR getdents64 (),
177 with wider types for the
178 .I d_ino
179 and
180 .I d_off
181 fields employed in the
182 .IR linux_dirent
183 structure.
184 .SH EXAMPLE
185 .\" FIXME: This program uses the older getdents(0 system call
186 .\" and the structure with smaller field widths.
187 The program below demonstrates the use of
188 .BR getdents ().
189 The following output shows an example of what we see when running this
190 program on an ext2 directory:
191
192 .in +4n
193 .nf
194 .RB "$" " ./a.out /testfs/"
195 --------------- nread=120 ---------------
196 i-node# file type d_reclen d_off d_name
197 2 directory 16 12 .
198 2 directory 16 24 ..
199 11 directory 24 44 lost+found
200 12 regular 16 56 a
201 228929 directory 16 68 sub
202 16353 directory 16 80 sub2
203 130817 directory 16 4096 sub3
204 .fi
205 .in
206 .SS Program source
207 \&
208 .nf
209 #define _GNU_SOURCE
210 #include <dirent.h> /* Defines DT_* constants */
211 #include <fcntl.h>
212 #include <stdio.h>
213 #include <unistd.h>
214 #include <stdlib.h>
215 #include <sys/stat.h>
216 #include <sys/syscall.h>
217
218 #define handle_error(msg) \\
219 do { perror(msg); exit(EXIT_FAILURE); } while (0)
220
221 struct linux_dirent {
222 long d_ino;
223 off_t d_off;
224 unsigned short d_reclen;
225 char d_name[];
226 };
227
228 #define BUF_SIZE 1024
229
230 int
231 main(int argc, char *argv[])
232 {
233 int fd, nread;
234 char buf[BUF_SIZE];
235 struct linux_dirent *d;
236 int bpos;
237 char d_type;
238
239 fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
240 if (fd == \-1)
241 handle_error("open");
242
243 for ( ; ; ) {
244 nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
245 if (nread == \-1)
246 handle_error("getdents");
247
248 if (nread == 0)
249 break;
250
251 printf("\--------------- nread=%d ---------------\\n", nread);
252 printf("i\-node# file type d_reclen d_off d_name\\n");
253 for (bpos = 0; bpos < nread;) {
254 d = (struct linux_dirent *) (buf + bpos);
255 printf("%8ld ", d\->d_ino);
256 d_type = *(buf + bpos + d\->d_reclen - 1);
257 printf("%\-10s ", (d_type == DT_REG) ? "regular" :
258 (d_type == DT_DIR) ? "directory" :
259 (d_type == DT_FIFO) ? "FIFO" :
260 (d_type == DT_SOCK) ? "socket" :
261 (d_type == DT_LNK) ? "symlink" :
262 (d_type == DT_BLK) ? "block dev" :
263 (d_type == DT_CHR) ? "char dev" : "???");
264 printf("%4d %10lld %s\\n", d\->d_reclen,
265 (long long) d\->d_off, (char *) d->d_name);
266 bpos += d\->d_reclen;
267 }
268 }
269
270 exit(EXIT_SUCCESS);
271 }
272 .fi
273 .SH "SEE ALSO"
274 .BR readdir (2),
275 .BR readdir (3)