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