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