Debian Codegraph @

upstream/debian-trixie/man3/scandir.3

raw
1 '\" t
2 .\" Copyright (C) 1993 David Metcalfe ([email protected])
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" 386BSD man pages
10 .\" Modified Sat Jul 24 18:26:16 1993 by Rik Faith ([email protected])
11 .\" Modified Thu Apr 11 17:11:33 1996 by Andries Brouwer ([email protected]):
12 .\" Corrected type of compar routines, as suggested by
13 .\" Miguel Barreiro ([email protected]). Added example.
14 .\" Modified Sun Sep 24 20:15:46 2000 by aeb, following Petter Reinholdtsen.
15 .\" Modified 2001-12-26 by aeb, following Joey. Added versionsort.
16 .\"
17 .\" The pieces on scandirat(3) were copyright and licensed as follows.
18 .\"
19 .\" Copyright (c) 2012, Mark R. Bannister <[email protected]>
20 .\" based on text in mkfifoat.3 Copyright (c) 2006, Michael Kerrisk
21 .\"
22 .\" SPDX-License-Identifier: GPL-2.0-or-later
23 .\"
24 .TH scandir 3 2024-06-15 "Linux man-pages 6.9.1"
25 .SH NAME
26 scandir, scandirat, alphasort, versionsort \- scan
27 a directory for matching entries
28 .SH LIBRARY
29 Standard C library
30 .RI ( libc ", " \-lc )
31 .SH SYNOPSIS
32 .nf
33 .B #include <dirent.h>
34 .P
35 .BI "int scandir(const char *restrict " dirp ,
36 .BI " struct dirent ***restrict " namelist ,
37 .BI " int (*" filter ")(const struct dirent *),"
38 .BI " int (*" compar ")(const struct dirent **,"
39 .B " const struct dirent **));"
40 .P
41 .BI "int alphasort(const struct dirent **" a ", const struct dirent **" b );
42 .BI "int versionsort(const struct dirent **" a ", const struct dirent **" b );
43 .P
44 .BR "#include <fcntl.h>" " /* Definition of AT_* constants */"
45 .B #include <dirent.h>
46 .P
47 .BI "int scandirat(int " dirfd ", const char *restrict " dirp ,
48 .BI " struct dirent ***restrict " namelist ,
49 .BI " int (*" filter ")(const struct dirent *),"
50 .BI " int (*" compar ")(const struct dirent **,"
51 .B " const struct dirent **));"
52 .fi
53 .P
54 .RS -4
55 Feature Test Macro Requirements for glibc (see
56 .BR feature_test_macros (7)):
57 .RE
58 .P
59 .BR scandir (),
60 .BR alphasort ():
61 .nf
62 /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
63 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
64 .fi
65 .P
66 .BR versionsort ():
67 .nf
68 _GNU_SOURCE
69 .fi
70 .P
71 .BR scandirat ():
72 .nf
73 _GNU_SOURCE
74 .fi
75 .SH DESCRIPTION
76 The
77 .BR scandir ()
78 function scans the directory \fIdirp\fP, calling
79 \fIfilter\fP() on each directory entry.
80 Entries for which
81 \fIfilter\fP() returns nonzero are stored in strings allocated via
82 .BR malloc (3),
83 sorted using
84 .BR qsort (3)
85 with the comparison
86 function \fIcompar\fP(), and collected in array \fInamelist\fP
87 which is allocated via
88 .BR malloc (3).
89 If \fIfilter\fP is NULL, all entries are selected.
90 .P
91 The
92 .BR alphasort ()
93 and
94 .BR versionsort ()
95 functions can be used as the comparison function
96 .IR compar ().
97 The former sorts directory entries using
98 .BR strcoll (3),
99 the latter using
100 .BR strverscmp (3)
101 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
102 .SS scandirat()
103 The
104 .BR scandirat ()
105 function operates in exactly the same way as
106 .BR scandir (),
107 except for the differences described here.
108 .P
109 If the pathname given in
110 .I dirp
111 is relative, then it is interpreted relative to the directory
112 referred to by the file descriptor
113 .I dirfd
114 (rather than relative to the current working directory of
115 the calling process, as is done by
116 .BR scandir ()
117 for a relative pathname).
118 .P
119 If
120 .I dirp
121 is relative and
122 .I dirfd
123 is the special value
124 .BR AT_FDCWD ,
125 then
126 .I dirp
127 is interpreted relative to the current working
128 directory of the calling process (like
129 .BR scandir ()).
130 .P
131 If
132 .I dirp
133 is absolute, then
134 .I dirfd
135 is ignored.
136 .P
137 See
138 .BR openat (2)
139 for an explanation of the need for
140 .BR scandirat ().
141 .SH RETURN VALUE
142 The
143 .BR scandir ()
144 function returns the number of directory entries
145 selected.
146 On error, \-1 is returned, with
147 .I errno
148 set to indicate the error.
149 .P
150 The
151 .BR alphasort ()
152 and
153 .BR versionsort ()
154 functions return an integer less than, equal to,
155 or greater than zero if the first argument is considered to be
156 respectively less than, equal to, or greater than the second.
157 .SH ERRORS
158 .TP
159 .B EBADF
160 .RB ( scandirat ())
161 .I dirp
162 is relative but
163 .I dirfd
164 is neither
165 .B AT_FDCWD
166 nor a valid file descriptor.
167 .TP
168 .B ENOENT
169 The path in \fIdirp\fR does not exist.
170 .TP
171 .B ENOMEM
172 Insufficient memory to complete the operation.
173 .TP
174 .B ENOTDIR
175 The path in \fIdirp\fR is not a directory.
176 .TP
177 .B ENOTDIR
178 .RB ( scandirat ())
179 .I dirp
180 is a relative pathname and
181 .I dirfd
182 is a file descriptor referring to a file other than a directory.
183 .SH ATTRIBUTES
184 For an explanation of the terms used in this section, see
185 .BR attributes (7).
186 .TS
187 allbox;
188 lbx lb lb
189 l l l.
190 Interface Attribute Value
191 T{
192 .na
193 .nh
194 .BR scandir (),
195 .BR scandirat ()
196 T} Thread safety MT-Safe
197 T{
198 .na
199 .nh
200 .BR alphasort (),
201 .BR versionsort ()
202 T} Thread safety MT-Safe locale
203 .TE
204 .SH STANDARDS
205 .TP
206 .BR alphasort ()
207 .TQ
208 .BR scandir ()
209 POSIX.1-2008.
210 .TP
211 .BR versionsort ()
212 .TQ
213 .BR scandirat ()
214 GNU.
215 .SH HISTORY
216 .TP
217 .BR alphasort ()
218 .TQ
219 .BR scandir ()
220 4.3BSD, POSIX.1-2008.
221 .TP
222 .BR versionsort ()
223 glibc 2.1.
224 .TP
225 .BR scandirat ()
226 glibc 2.15.
227 .\" .P
228 .\" The functions
229 .\" .BR scandir ()
230 .\" and
231 .\" .BR alphasort ()
232 .\" are from 4.3BSD, and have been available under Linux since libc4.
233 .\" Libc4 and libc5 use the more precise prototype
234 .\" .sp
235 .\" .nf
236 .\" int alphasort(const struct dirent ** a,
237 .\" const struct dirent **b);
238 .\" .fi
239 .\" .sp
240 .\" but glibc 2.0 returns to the imprecise BSD prototype.
241 .SH NOTES
242 Since glibc 2.1,
243 .BR alphasort ()
244 calls
245 .BR strcoll (3);
246 earlier it used
247 .BR strcmp (3).
248 .P
249 Before glibc 2.10, the two arguments of
250 .BR alphasort ()
251 and
252 .BR versionsort ()
253 were typed as
254 .IR "const void\ *" .
255 When
256 .BR alphasort ()
257 was standardized in POSIX.1-2008,
258 the argument type was specified as the type-safe
259 .IR "const struct dirent\ **",
260 and glibc 2.10 changed the definition of
261 .BR alphasort ()
262 (and the nonstandard
263 .BR versionsort ())
264 to match the standard.
265 .SH EXAMPLES
266 The program below prints a list of the files in the current directory
267 in reverse order.
268 .\"
269 .SS Program source
270 \&
271 .\" SRC BEGIN (scandir.c)
272 .EX
273 #define _DEFAULT_SOURCE
274 #include <dirent.h>
275 #include <stdio.h>
276 #include <stdlib.h>
277 \&
278 int
279 main(void)
280 {
281 struct dirent **namelist;
282 int n;
283 \&
284 n = scandir(".", &namelist, NULL, alphasort);
285 if (n == \-1) {
286 perror("scandir");
287 exit(EXIT_FAILURE);
288 }
289 \&
290 while (n\-\-) {
291 printf("%s\[rs]n", namelist[n]\->d_name);
292 free(namelist[n]);
293 }
294 free(namelist);
295 \&
296 exit(EXIT_SUCCESS);
297 }
298 .EE
299 .\" SRC END
300 .SH SEE ALSO
301 .BR closedir (3),
302 .BR fnmatch (3),
303 .BR opendir (3),
304 .BR readdir (3),
305 .BR rewinddir (3),
306 .BR seekdir (3),
307 .BR strcmp (3),
308 .BR strcoll (3),
309 .BR strverscmp (3),
310 .BR telldir (3)