1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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.
36 .\" The pieces on scandirat(3) were copyright and licensed as follows.
38 .\" Copyright (c) 2012, Mark R. Bannister <cambridge@users.sourceforge.net>
39 .\" based on text in mkfifoat.3 Copyright (c) 2006, Michael Kerrisk
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.
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.
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.
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/>.
62 .TH SCANDIR 3 2017-09-15 "GNU" "Linux Programmer's Manual"
64 scandir, scandirat, alphasort, versionsort \- scan
65 a directory for matching entries
68 .B #include <dirent.h>
70 .BI "int scandir(const char *" dirp ", struct dirent ***" namelist ,
72 .BI "int (*" filter ")(const struct dirent *),"
73 .BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
76 .BI "int alphasort(const struct dirent **" a ", const struct dirent **" b );
78 .BI "int versionsort(const struct dirent **" a ", const struct dirent **" b );
80 .BR "#include <fcntl.h>" " /* Definition of AT_* constants */"
81 .B #include <dirent.h>
84 .BI "int scandirat(int " dirfd ", const char *" dirp ","
85 .BI "struct dirent ***" namelist ,
88 .BI "int (*" filter ")(const struct dirent *),"
89 .BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
94 Feature Test Macro Requirements for glibc (see
95 .BR feature_test_macros (7)):
104 /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200809L
105 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
117 function scans the directory \fIdirp\fP, calling
118 \fIfilter\fP() on each directory entry.
120 \fIfilter\fP() returns nonzero are stored in strings allocated via
125 function \fIcompar\fP(), and collected in array \fInamelist\fP
126 which is allocated via
128 If \fIfilter\fP is NULL, all entries are selected.
134 functions can be used as the comparison function
136 The former sorts directory entries using
140 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
144 function operates in exactly the same way as
146 except for the differences described here.
148 If the pathname given in
150 is relative, then it is interpreted relative to the directory
151 referred to by the file descriptor
153 (rather than relative to the current working directory of
154 the calling process, as is done by
156 for a relative pathname).
166 is interpreted relative to the current working
167 directory of the calling process (like
178 for an explanation of the need for
183 function returns the number of directory entries
185 On error, \-1 is returned, with
187 set to indicate the cause of the error.
193 functions return an integer less than, equal to,
194 or greater than zero if the first argument is considered to be
195 respectively less than, equal to, or greater than the second.
199 The path in \fIdirp\fR does not exist.
202 Insufficient memory to complete the operation.
205 The path in \fIdirp\fR is not a directory.
207 The following additional errors can occur for
212 is not a valid file descriptor.
216 is a relative path and
218 is a file descriptor referring to a file other than a directory.
221 was added to glibc in version 2.1.
224 was added to glibc in version 2.15.
226 For an explanation of the terms used in this section, see
232 Interface Attribute Value
236 T} Thread safety MT-Safe
240 T} Thread safety MT-Safe locale
246 4.3BSD, POSIX.1-2008.
257 .\" are from 4.3BSD, and have been available under Linux since libc4.
258 .\" Libc4 and libc5 use the more precise prototype
261 .\" int alphasort(const struct dirent ** a,
262 .\" const struct dirent **b);
265 .\" but glibc 2.0 returns to the imprecise BSD prototype.
274 Before glibc 2.10, the two arguments of
279 .IR "const void\ *" .
282 was standardized in POSIX.1-2008,
283 the argument type was specified as the type-safe
284 .IR "const struct dirent\ **",
285 and glibc 2.10 changed the definition of
289 to match the standard.
291 The program below prints a list of the files in the current directory
297 #define _DEFAULT_SOURCE
305 struct dirent **namelist;
308 n = scandir(".", &namelist, NULL, alphasort);
315 printf("%s\en", namelist[n]\->d_name);