]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/scandir.3
scandir.3: Put detail about alphasort under a NOTES heading
[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.\"
62.TH SCANDIR 3 2014-02-21 "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>
69.sp
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
MK
74.RE
75.sp
76.BI "int alphasort(const void *" a ", const void *" b );
cc4615cc 77.sp
fea681da 78.BI "int versionsort(const void *" a ", const void *" b );
ef097103
MK
79
80.BR "#include <fcntl.h>" " /* Definition of AT_* constants */"
81.B #include <dirent.h>
82.sp
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
cc4615cc
MK
92.sp
93.in -4n
94Feature Test Macro Requirements for glibc (see
95.BR feature_test_macros (7)):
96.in
97.sp
98.BR scandir (),
99.BR alphasort ():
4a8961c3
MK
100.br
101.RS 4
102.PD 0
78e3b458 103.ad b
cc4615cc
MK
104_BSD_SOURCE || _SVID_SOURCE
105.br
4a8961c3
MK
106|| /* Since glibc 2.10: */
107.RS 4
108(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
109.RE
110.PD
111.RE
ef097103 112.sp
cc4615cc
MK
113.BR versionsort ():
114_GNU_SOURCE
ef097103
MK
115.sp
116.BR scandirat ():
117_GNU_SOURCE
fea681da 118.SH DESCRIPTION
60a90ecd
MK
119The
120.BR scandir ()
6dea120e 121function scans the directory \fIdirp\fP, calling
c13182ef
MK
122\fIfilter\fP() on each directory entry.
123Entries for which
c7094399 124\fIfilter\fP() returns nonzero are stored in strings allocated via
60a90ecd
MK
125.BR malloc (3),
126sorted using
127.BR qsort (3)
128with the comparison
63aa9df0 129function \fIcompar\fP(), and collected in array \fInamelist\fP
60a90ecd
MK
130which is allocated via
131.BR malloc (3).
fea681da
MK
132If \fIfilter\fP is NULL, all entries are selected.
133.LP
134The
63aa9df0 135.BR alphasort ()
fea681da 136and
63aa9df0 137.BR versionsort ()
fea681da 138functions can be used as the comparison function
63aa9df0 139.IR compar ().
fea681da
MK
140The former sorts directory entries using
141.BR strcoll (3),
142the latter using
4d52e8f8 143.BR strverscmp (3)
8c383102 144on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
ef097103
MK
145.SS scandirat()
146The
147.BR scandirat ()
78e3b458 148function operates in exactly the same way as
ef097103
MK
149.BR scandir (3),
150except for the differences described here.
151
152If the pathname given in
153.I dirp
154is relative, then it is interpreted relative to the directory
155referred to by the file descriptor
156.I dirfd
157(rather than relative to the current working directory of
158the calling process, as is done by
159.BR scandir (3)
160for a relative pathname).
161
162If
163.I dirp
164is relative and
165.I dirfd
166is the special value
167.BR AT_FDCWD ,
168then
169.I dirp
170is interpreted relative to the current working
171directory of the calling process (like
172.BR scandir (3)).
173
174If
175.I dirp
176is absolute, then
177.I dirfd
178is ignored.
179.PP
180See
181.BR openat (2)
182for an explanation of the need for
183.BR scandirat ().
47297adb 184.SH RETURN VALUE
60a90ecd
MK
185The
186.BR scandir ()
187function returns the number of directory entries
fd88c684
MK
188selected.
189On error, \-1 is returned, with
190.I errno
191set to indicate the cause of the error.
fea681da
MK
192.PP
193The
63aa9df0 194.BR alphasort ()
fea681da 195and
63aa9df0 196.BR versionsort ()
c13182ef
MK
197functions return an integer less than, equal to,
198or greater than zero if the first argument is considered to be
fea681da
MK
199respectively less than, equal to, or greater than the second.
200.SH ERRORS
201.TP
cb06e4a5
MF
202.B ENOENT
203The path in \fIdirp\fR does not exist.
204.TP
fea681da
MK
205.B ENOMEM
206Insufficient memory to complete the operation.
cb06e4a5
MF
207.TP
208.B ENOTDIR
209The path in \fIdirp\fR is not a directory.
ef097103
MK
210.PP
211The following additional errors can occur for
212.BR scandirat ():
213.TP
214.B EBADF
215.I dirfd
216is not a valid file descriptor.
217.TP
218.B ENOTDIR
219.I dirp
220is a relative path and
221.I dirfd
222is a file descriptor referring to a file other than a directory.
7a4b8d36
MK
223.SH VERSIONS
224.BR versionsort ()
225was added to glibc in version 2.1.
ef097103
MK
226
227.BR scandirat ()
228was added to glibc in version 2.15.
47297adb 229.SH CONFORMING TO
b86e869f
MK
230.BR alphasort (),
231.BR scandir ():
232POSIX.1-2008.
233
2da325d7 234.BR versionsort ()
b86e869f 235and
ef097103 236.BR scandirat ()
b86e869f 237are GNU extensions.
fea681da
MK
238.LP
239The functions
63aa9df0 240.BR scandir ()
fea681da 241and
63aa9df0 242.BR alphasort ()
b14d4aa5 243are from 4.3BSD, and have been available under Linux since libc4.
fea681da
MK
244Libc4 and libc5 use the more precise prototype
245.sp
246.nf
c13182ef 247 int alphasort(const struct dirent ** a,
fecfb777 248 const struct dirent **b);
fea681da
MK
249.fi
250.sp
251but glibc 2.0 returns to the imprecise BSD prototype.
6f695bc0 252.SH NOTES
fea681da 253Since glibc 2.1,
63aa9df0 254.BR alphasort ()
fea681da
MK
255calls
256.BR strcoll (3);
257earlier it used
258.BR strcmp (3).
259.SH EXAMPLE
260.nf
cc4615cc 261#define _SVID_SOURCE
fea681da
MK
262/* print files in current directory in reverse order */
263#include <dirent.h>
cf0a9ace
MK
264
265int
266main(void)
267{
fea681da
MK
268 struct dirent **namelist;
269 int n;
270
3326bd19 271 n = scandir(".", &namelist, NULL, alphasort);
fea681da
MK
272 if (n < 0)
273 perror("scandir");
274 else {
d4949190 275 while (n\-\-) {
31a6818e 276 printf("%s\en", namelist[n]\->d_name);
fea681da
MK
277 free(namelist[n]);
278 }
279 free(namelist);
280 }
281}
282.fi
47297adb 283.SH SEE ALSO
fea681da
MK
284.BR closedir (3),
285.BR fnmatch (3),
286.BR opendir (3),
287.BR readdir (3),
288.BR rewinddir (3),
289.BR seekdir (3),
290.BR strcmp (3),
291.BR strcoll (3),
292.BR strverscmp (3),
293.BR telldir (3)