1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" Modified Sat Jul 24 18:26:16 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Modified Thu Apr 11 17:11:33 1996 by Andries Brouwer (aeb@cwi.nl):
29 .\" Corrected type of compar routines, as suggested by
30 .\" Miguel Barreiro (enano@avalon.yaix.es). Added example.
31 .\" Modified Sun Sep 24 20:15:46 2000 by aeb, following Petter Reinholdtsen.
32 .\" Modified 2001-12-26 by aeb, following Joey. Added versionsort.
34 .\" FIXME glibc 2.15 adds scandirat()
36 .TH SCANDIR 3 2011-09-08 "GNU" "Linux Programmer's Manual"
38 scandir, alphasort, versionsort \- scan a directory for matching entries
41 .B #include <dirent.h>
43 .BI "int scandir(const char *" dirp ", struct dirent ***" namelist ,
45 .BI "int (*" filter ")(const struct dirent *),"
46 .BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
49 .BI "int alphasort(const void *" a ", const void *" b );
51 .BI "int versionsort(const void *" a ", const void *" b );
55 Feature Test Macro Requirements for glibc (see
56 .BR feature_test_macros (7)):
61 _BSD_SOURCE || _SVID_SOURCE
68 function scans the directory \fIdirp\fP, calling
69 \fIfilter\fP() on each directory entry.
71 \fIfilter\fP() returns nonzero are stored in strings allocated via
76 function \fIcompar\fP(), and collected in array \fInamelist\fP
77 which is allocated via
79 If \fIfilter\fP is NULL, all entries are selected.
85 functions can be used as the comparison function
87 The former sorts directory entries using
91 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
95 function returns the number of directory entries
96 selected or \-1 if an error occurs.
102 functions return an integer less than, equal to,
103 or greater than zero if the first argument is considered to be
104 respectively less than, equal to, or greater than the second.
108 The path in \fIdirp\fR does not exist.
111 Insufficient memory to complete the operation.
114 The path in \fIdirp\fR is not a directory.
117 was added to glibc in version 2.1.
122 are specified in POSIX.1-2008, and are widely available.
130 are from 4.3BSD, and have been available under Linux since libc4.
131 Libc4 and libc5 use the more precise prototype
134 int alphasort(const struct dirent ** a,
135 const struct dirent **b);
138 but glibc 2.0 returns to the imprecise BSD prototype.
142 is a GNU extension, available since glibc 2.1.
153 /* print files in current directory in reverse order */
159 struct dirent **namelist;
162 n = scandir(".", &namelist, 0, alphasort);
167 printf("%s\en", namelist[n]\->d_name);