Changes.old: Add missing entry in 5.13 changelog
[man-pages.git] / man3 / scandir.3
blobce586142f965e9f172ebaba4c9dc599f0673353b
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
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 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Sat Jul 24 18:26:16 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Modified Thu Apr 11 17:11:33 1996 by Andries Brouwer (aeb@cwi.nl):
31 .\"     Corrected type of compar routines, as suggested by
32 .\"     Miguel Barreiro (enano@avalon.yaix.es).  Added example.
33 .\" Modified Sun Sep 24 20:15:46 2000 by aeb, following Petter Reinholdtsen.
34 .\" Modified 2001-12-26 by aeb, following Joey. Added versionsort.
35 .\"
36 .\" The pieces on scandirat(3) were copyright and licensed as follows.
37 .\"
38 .\" Copyright (c) 2012, Mark R. Bannister <cambridge@users.sourceforge.net>
39 .\"        based on text in mkfifoat.3 Copyright (c) 2006, Michael Kerrisk
40 .\"
41 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
42 .\" This is free documentation; you can redistribute it and/or
43 .\" modify it under the terms of the GNU General Public License as
44 .\" published by the Free Software Foundation; either version 2 of
45 .\" the License, or (at your option) any later version.
46 .\"
47 .\" The GNU General Public License's references to "object code"
48 .\" and "executables" are to be interpreted as the output of any
49 .\" document formatting or typesetting system, including
50 .\" intermediate and printed output.
51 .\"
52 .\" This manual is distributed in the hope that it will be useful,
53 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
54 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55 .\" GNU General Public License for more details.
56 .\"
57 .\" You should have received a copy of the GNU General Public
58 .\" License along with this manual; if not, see
59 .\" <http://www.gnu.org/licenses/>.
60 .\" %%%LICENSE_END
61 .\"
62 .TH SCANDIR 3  2021-08-27 "GNU" "Linux Programmer's Manual"
63 .SH NAME
64 scandir, scandirat, alphasort, versionsort \- scan
65 a directory for matching entries
66 .SH SYNOPSIS
67 .nf
68 .B #include <dirent.h>
69 .PP
70 .BI "int scandir(const char *restrict " dirp ,
71 .BI "            struct dirent ***restrict " namelist ,
72 .BI "            int (*" filter ")(const struct dirent *),"
73 .BI "            int (*" compar ")(const struct dirent **,"
74 .BR "                          const struct dirent **));"
75 .PP
76 .BI "int alphasort(const struct dirent **" a ", const struct dirent **" b );
77 .BI "int versionsort(const struct dirent **" a ", const struct dirent **" b );
78 .PP
79 .BR "#include <fcntl.h>" "          /* Definition of AT_* constants */"
80 .B #include <dirent.h>
81 .PP
82 .BI "int scandirat(int " dirfd ", const char *restrict " dirp ,
83 .BI "            struct dirent ***restrict " namelist ,
84 .BI "            int (*" filter ")(const struct dirent *),"
85 .BI "            int (*" compar ")(const struct dirent **,"
86 .BI "                          const struct dirent **));"
87 .fi
88 .PP
89 .RS -4
90 Feature Test Macro Requirements for glibc (see
91 .BR feature_test_macros (7)):
92 .RE
93 .PP
94 .BR scandir (),
95 .BR alphasort ():
96 .nf
97     /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
98         || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
99 .fi
101 .BR versionsort ():
103     _GNU_SOURCE
106 .BR scandirat ():
108     _GNU_SOURCE
110 .SH DESCRIPTION
112 .BR scandir ()
113 function scans the directory \fIdirp\fP, calling
114 \fIfilter\fP() on each directory entry.
115 Entries for which
116 \fIfilter\fP() returns nonzero are stored in strings allocated via
117 .BR malloc (3),
118 sorted using
119 .BR qsort (3)
120 with the comparison
121 function \fIcompar\fP(), and collected in array \fInamelist\fP
122 which is allocated via
123 .BR malloc (3).
124 If \fIfilter\fP is NULL, all entries are selected.
127 .BR alphasort ()
129 .BR versionsort ()
130 functions can be used as the comparison function
131 .IR compar ().
132 The former sorts directory entries using
133 .BR strcoll (3),
134 the latter using
135 .BR strverscmp (3)
136 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
137 .SS scandirat()
139 .BR scandirat ()
140 function operates in exactly the same way as
141 .BR scandir (),
142 except for the differences described here.
144 If the pathname given in
145 .I dirp
146 is relative, then it is interpreted relative to the directory
147 referred to by the file descriptor
148 .I dirfd
149 (rather than relative to the current working directory of
150 the calling process, as is done by
151 .BR scandir ()
152 for a relative pathname).
155 .I dirp
156 is relative and
157 .I dirfd
158 is the special value
159 .BR AT_FDCWD ,
160 then
161 .I dirp
162 is interpreted relative to the current working
163 directory of the calling process (like
164 .BR scandir ()).
167 .I dirp
168 is absolute, then
169 .I dirfd
170 is ignored.
173 .BR openat (2)
174 for an explanation of the need for
175 .BR scandirat ().
176 .SH RETURN VALUE
178 .BR scandir ()
179 function returns the number of directory entries
180 selected.
181 On error, \-1 is returned, with
182 .I errno
183 set to indicate the error.
186 .BR alphasort ()
188 .BR versionsort ()
189 functions return an integer less than, equal to,
190 or greater than zero if the first argument is considered to be
191 respectively less than, equal to, or greater than the second.
192 .SH ERRORS
194 .B EBADF
195 .RB ( scandirat ())
196 .I dirp
197 is relative but
198 .I dirfd
199 is neither
200 .B AT_FDCWD
201 nor a valid file descriptor.
203 .B ENOENT
204 The path in \fIdirp\fR does not exist.
206 .B ENOMEM
207 Insufficient memory to complete the operation.
209 .B ENOTDIR
210 The path in \fIdirp\fR is not a directory.
212 .B ENOTDIR
213 .RB ( scandirat ())
214 .I dirp
215 is a relative pathname and
216 .I dirfd
217 is a file descriptor referring to a file other than a directory.
218 .SH VERSIONS
219 .BR versionsort ()
220 was added to glibc in version 2.1.
222 .BR scandirat ()
223 was added to glibc in version 2.15.
224 .SH ATTRIBUTES
225 For an explanation of the terms used in this section, see
226 .BR attributes (7).
227 .ad l
230 allbox;
231 lbx lb lb
232 l l l.
233 Interface       Attribute       Value
235 .BR scandir (),
236 .BR scandirat ()
237 T}      Thread safety   MT-Safe
239 .BR alphasort (),
240 .BR versionsort ()
241 T}      Thread safety   MT-Safe locale
245 .sp 1
246 .SH CONFORMING TO
247 .BR alphasort (),
248 .BR scandir ():
249 4.3BSD, POSIX.1-2008.
251 .BR versionsort ()
253 .BR scandirat ()
254 are GNU extensions.
255 .\" .LP
256 .\" The functions
257 .\" .BR scandir ()
258 .\" and
259 .\" .BR alphasort ()
260 .\" are from 4.3BSD, and have been available under Linux since libc4.
261 .\" Libc4 and libc5 use the more precise prototype
262 .\" .sp
263 .\" .nf
264 .\"    int alphasort(const struct dirent ** a,
265 .\"                  const struct dirent **b);
266 .\" .fi
267 .\" .sp
268 .\" but glibc 2.0 returns to the imprecise BSD prototype.
269 .SH NOTES
270 Since glibc 2.1,
271 .BR alphasort ()
272 calls
273 .BR strcoll (3);
274 earlier it used
275 .BR strcmp (3).
277 Before glibc 2.10, the two arguments of
278 .BR alphasort ()
280 .BR versionsort ()
281 were typed as
282 .IR "const void\ *" .
283 When
284 .BR alphasort ()
285 was standardized in POSIX.1-2008,
286 the argument type was specified as the type-safe
287 .IR "const struct dirent\ **",
288 and glibc 2.10 changed the definition of
289 .BR alphasort ()
290 (and the nonstandard
291 .BR versionsort ())
292 to match the standard.
293 .SH EXAMPLES
294 The program below prints a list of the files in the current directory
295 in reverse order.
297 .SS Program source
300 #define _DEFAULT_SOURCE
301 #include <dirent.h>
302 #include <stdio.h>
303 #include <stdlib.h>
306 main(void)
308     struct dirent **namelist;
309     int n;
311     n = scandir(".", &namelist, NULL, alphasort);
312     if (n == \-1) {
313         perror("scandir");
314         exit(EXIT_FAILURE);
315     }
317     while (n\-\-) {
318         printf("%s\en", namelist[n]\->d_name);
319         free(namelist[n]);
320     }
321     free(namelist);
323     exit(EXIT_SUCCESS);
326 .SH SEE ALSO
327 .BR closedir (3),
328 .BR fnmatch (3),
329 .BR opendir (3),
330 .BR readdir (3),
331 .BR rewinddir (3),
332 .BR seekdir (3),
333 .BR strcmp (3),
334 .BR strcoll (3),
335 .BR strverscmp (3),
336 .BR telldir (3)