1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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.
16 .\" The pieces on scandirat(3) were copyright and licensed as follows.
18 .\" Copyright (c) 2012, Mark R. Bannister <cambridge@users.sourceforge.net>
19 .\" based on text in mkfifoat.3 Copyright (c) 2006, Michael Kerrisk
21 .\" SPDX-License-Identifier: GPL-2.0-or-later
23 .TH SCANDIR 3 2021-08-27 "GNU" "Linux Programmer's Manual"
25 scandir, scandirat, alphasort, versionsort \- scan
26 a directory for matching entries
29 .RI ( libc ", " \-lc )
32 .B #include <dirent.h>
34 .BI "int scandir(const char *restrict " dirp ,
35 .BI " struct dirent ***restrict " namelist ,
36 .BI " int (*" filter ")(const struct dirent *),"
37 .BI " int (*" compar ")(const struct dirent **,"
38 .BR " const struct dirent **));"
40 .BI "int alphasort(const struct dirent **" a ", const struct dirent **" b );
41 .BI "int versionsort(const struct dirent **" a ", const struct dirent **" b );
43 .BR "#include <fcntl.h>" " /* Definition of AT_* constants */"
44 .B #include <dirent.h>
46 .BI "int scandirat(int " dirfd ", const char *restrict " dirp ,
47 .BI " struct dirent ***restrict " namelist ,
48 .BI " int (*" filter ")(const struct dirent *),"
49 .BI " int (*" compar ")(const struct dirent **,"
50 .BI " const struct dirent **));"
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
61 /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
62 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
77 function scans the directory \fIdirp\fP, calling
78 \fIfilter\fP() on each directory entry.
80 \fIfilter\fP() returns nonzero are stored in strings allocated via
85 function \fIcompar\fP(), and collected in array \fInamelist\fP
86 which is allocated via
88 If \fIfilter\fP is NULL, all entries are selected.
94 functions can be used as the comparison function
96 The former sorts directory entries using
100 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
104 function operates in exactly the same way as
106 except for the differences described here.
108 If the pathname given in
110 is relative, then it is interpreted relative to the directory
111 referred to by the file descriptor
113 (rather than relative to the current working directory of
114 the calling process, as is done by
116 for a relative pathname).
126 is interpreted relative to the current working
127 directory of the calling process (like
138 for an explanation of the need for
143 function returns the number of directory entries
145 On error, \-1 is returned, with
147 set to indicate the error.
153 functions return an integer less than, equal to,
154 or greater than zero if the first argument is considered to be
155 respectively less than, equal to, or greater than the second.
165 nor a valid file descriptor.
168 The path in \fIdirp\fR does not exist.
171 Insufficient memory to complete the operation.
174 The path in \fIdirp\fR is not a directory.
179 is a relative pathname and
181 is a file descriptor referring to a file other than a directory.
184 was added to glibc in version 2.1.
187 was added to glibc in version 2.15.
189 For an explanation of the terms used in this section, see
197 Interface Attribute Value
201 T} Thread safety MT-Safe
205 T} Thread safety MT-Safe locale
213 4.3BSD, POSIX.1-2008.
224 .\" are from 4.3BSD, and have been available under Linux since libc4.
225 .\" Libc4 and libc5 use the more precise prototype
228 .\" int alphasort(const struct dirent ** a,
229 .\" const struct dirent **b);
232 .\" but glibc 2.0 returns to the imprecise BSD prototype.
241 Before glibc 2.10, the two arguments of
246 .IR "const void\ *" .
249 was standardized in POSIX.1-2008,
250 the argument type was specified as the type-safe
251 .IR "const struct dirent\ **",
252 and glibc 2.10 changed the definition of
256 to match the standard.
258 The program below prints a list of the files in the current directory
264 #define _DEFAULT_SOURCE
272 struct dirent **namelist;
275 n = scandir(".", &namelist, NULL, alphasort);
282 printf("%s\en", namelist[n]\->d_name);