]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/scandir.3
err.3: EXAMPLES: use EXIT_FAILURE rather than 1 as exit status
[thirdparty/man-pages.git] / man3 / scandir.3
CommitLineData
fea681da
MK
1.\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 12.\"
fea681da
MK
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.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da
MK
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.\"
ef097103
MK
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.\"
4b8c67d9 62.TH SCANDIR 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da 63.SH NAME
ef097103
MK
64scandir, scandirat, alphasort, versionsort \- scan
65a directory for matching entries
fea681da
MK
66.SH SYNOPSIS
67.nf
68.B #include <dirent.h>
68e4db0a 69.PP
6dea120e 70.BI "int scandir(const char *" dirp ", struct dirent ***" namelist ,
fea681da 71.RS
a3887f6e
MK
72.BI "int (*" filter ")(const struct dirent *),"
73.BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
fea681da 74.RE
68e4db0a 75.PP
ecf9e5f8 76.BI "int alphasort(const struct dirent **" a ", const struct dirent **" b );
68e4db0a 77.PP
ecf9e5f8 78.BI "int versionsort(const struct dirent **" a ", const struct dirent **" b );
ef097103
MK
79
80.BR "#include <fcntl.h>" " /* Definition of AT_* constants */"
81.B #include <dirent.h>
68e4db0a 82.PP
ef097103
MK
83.fi
84.BI "int scandirat(int " dirfd ", const char *" dirp ","
85.BI "struct dirent ***" namelist ,
86.nf
87.RS
88.BI "int (*" filter ")(const struct dirent *),"
89.BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
90.RE
fea681da 91.fi
68e4db0a 92.PP
cc4615cc
MK
93.in -4n
94Feature Test Macro Requirements for glibc (see
95.BR feature_test_macros (7)):
96.in
68e4db0a 97.PP
cc4615cc
MK
98.BR scandir (),
99.BR alphasort ():
4a8961c3
MK
100.br
101.RS 4
102.PD 0
78e3b458 103.ad b
a30128b4
MK
104/* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200809L
105 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
4a8961c3
MK
106.PD
107.RE
68e4db0a 108.PP
cc4615cc
MK
109.BR versionsort ():
110_GNU_SOURCE
68e4db0a 111.PP
ef097103
MK
112.BR scandirat ():
113_GNU_SOURCE
fea681da 114.SH DESCRIPTION
60a90ecd
MK
115The
116.BR scandir ()
6dea120e 117function scans the directory \fIdirp\fP, calling
c13182ef
MK
118\fIfilter\fP() on each directory entry.
119Entries for which
c7094399 120\fIfilter\fP() returns nonzero are stored in strings allocated via
60a90ecd
MK
121.BR malloc (3),
122sorted using
123.BR qsort (3)
124with the comparison
63aa9df0 125function \fIcompar\fP(), and collected in array \fInamelist\fP
60a90ecd
MK
126which is allocated via
127.BR malloc (3).
fea681da 128If \fIfilter\fP is NULL, all entries are selected.
dd3568a1 129.PP
fea681da 130The
63aa9df0 131.BR alphasort ()
fea681da 132and
63aa9df0 133.BR versionsort ()
fea681da 134functions can be used as the comparison function
63aa9df0 135.IR compar ().
fea681da
MK
136The former sorts directory entries using
137.BR strcoll (3),
138the latter using
4d52e8f8 139.BR strverscmp (3)
8c383102 140on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
ef097103
MK
141.SS scandirat()
142The
143.BR scandirat ()
78e3b458 144function operates in exactly the same way as
d0aa8720 145.BR scandir (),
ef097103 146except for the differences described here.
847e0d88 147.PP
ef097103
MK
148If the pathname given in
149.I dirp
150is relative, then it is interpreted relative to the directory
151referred to by the file descriptor
152.I dirfd
153(rather than relative to the current working directory of
154the calling process, as is done by
d0aa8720 155.BR scandir ()
ef097103 156for a relative pathname).
847e0d88 157.PP
ef097103
MK
158If
159.I dirp
160is relative and
161.I dirfd
162is the special value
163.BR AT_FDCWD ,
164then
165.I dirp
166is interpreted relative to the current working
167directory of the calling process (like
d0aa8720 168.BR scandir ()).
847e0d88 169.PP
ef097103
MK
170If
171.I dirp
172is absolute, then
173.I dirfd
174is ignored.
175.PP
176See
177.BR openat (2)
178for an explanation of the need for
179.BR scandirat ().
47297adb 180.SH RETURN VALUE
60a90ecd
MK
181The
182.BR scandir ()
183function returns the number of directory entries
fd88c684
MK
184selected.
185On error, \-1 is returned, with
186.I errno
187set to indicate the cause of the error.
fea681da
MK
188.PP
189The
63aa9df0 190.BR alphasort ()
fea681da 191and
63aa9df0 192.BR versionsort ()
c13182ef
MK
193functions return an integer less than, equal to,
194or greater than zero if the first argument is considered to be
fea681da
MK
195respectively less than, equal to, or greater than the second.
196.SH ERRORS
197.TP
cb06e4a5
MF
198.B ENOENT
199The path in \fIdirp\fR does not exist.
200.TP
fea681da
MK
201.B ENOMEM
202Insufficient memory to complete the operation.
cb06e4a5
MF
203.TP
204.B ENOTDIR
205The path in \fIdirp\fR is not a directory.
ef097103
MK
206.PP
207The following additional errors can occur for
208.BR scandirat ():
209.TP
210.B EBADF
211.I dirfd
212is not a valid file descriptor.
213.TP
214.B ENOTDIR
215.I dirp
216is a relative path and
217.I dirfd
218is a file descriptor referring to a file other than a directory.
7a4b8d36
MK
219.SH VERSIONS
220.BR versionsort ()
221was added to glibc in version 2.1.
847e0d88 222.PP
ef097103
MK
223.BR scandirat ()
224was added to glibc in version 2.15.
9ec9e827
ZL
225.SH ATTRIBUTES
226For an explanation of the terms used in this section, see
227.BR attributes (7).
228.TS
229allbox;
230lbw26 lb lb
231l l l.
232Interface Attribute Value
233T{
234.BR scandir (),
235.BR scandirat ()
236T} Thread safety MT-Safe
237T{
238.BR alphasort (),
239.BR versionsort ()
240T} Thread safety MT-Safe locale
241.TE
847e0d88 242.sp 1
47297adb 243.SH CONFORMING TO
b86e869f
MK
244.BR alphasort (),
245.BR scandir ():
4046f0e0 2464.3BSD, POSIX.1-2008.
847e0d88 247.PP
2da325d7 248.BR versionsort ()
b86e869f 249and
ef097103 250.BR scandirat ()
b86e869f 251are GNU extensions.
4046f0e0
MK
252.\" .LP
253.\" The functions
254.\" .BR scandir ()
255.\" and
256.\" .BR alphasort ()
257.\" are from 4.3BSD, and have been available under Linux since libc4.
258.\" Libc4 and libc5 use the more precise prototype
259.\" .sp
260.\" .nf
261.\" int alphasort(const struct dirent ** a,
262.\" const struct dirent **b);
263.\" .fi
264.\" .sp
265.\" but glibc 2.0 returns to the imprecise BSD prototype.
6f695bc0 266.SH NOTES
fea681da 267Since glibc 2.1,
63aa9df0 268.BR alphasort ()
fea681da
MK
269calls
270.BR strcoll (3);
271earlier it used
272.BR strcmp (3).
847e0d88 273.PP
57cc00ec
MK
274Before glibc 2.10, the two arguments of
275.BR alphasort ()
276and
277.BR versionsort ()
278were typed as
279.IR "const void\ *" .
280When
281.BR alphasort ()
282was standardized in POSIX.1-2008,
283the argument type was specified as the type-safe
284.IR "const struct dirent\ **",
285and glibc 2.10 changed the definition of
286.BR alphasort ()
287(and the nonstandard
288.BR versionsort ())
289to match the standard.
a14af333 290.SH EXAMPLES
e1d2d774
MK
291The program below prints a list of the files in the current directory
292in reverse order.
293.\"
294.SS Program source
295\&
e7d0bb47 296.EX
5e84ae6c 297#define _DEFAULT_SOURCE
fea681da 298#include <dirent.h>
5e84ae6c
MK
299#include <stdio.h>
300#include <stdlib.h>
cf0a9ace
MK
301
302int
303main(void)
304{
fea681da
MK
305 struct dirent **namelist;
306 int n;
307
3326bd19 308 n = scandir(".", &namelist, NULL, alphasort);
9a96fb65 309 if (n == -1) {
fea681da 310 perror("scandir");
9a96fb65 311 exit(EXIT_FAILURE);
fea681da 312 }
9a96fb65
MK
313
314 while (n\-\-) {
315 printf("%s\en", namelist[n]\->d_name);
316 free(namelist[n]);
317 }
318 free(namelist);
319
5e84ae6c 320 exit(EXIT_SUCCESS);
fea681da 321}
e7d0bb47 322.EE
47297adb 323.SH SEE ALSO
fea681da
MK
324.BR closedir (3),
325.BR fnmatch (3),
326.BR opendir (3),
327.BR readdir (3),
328.BR rewinddir (3),
329.BR seekdir (3),
330.BR strcmp (3),
331.BR strcoll (3),
332.BR strverscmp (3),
333.BR telldir (3)