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 .TH SCANDIR 3 2012-03-20 "GNU" "Linux Programmer's Manual"
36 scandir, alphasort, versionsort \- scan a directory for matching entries
39 .B #include <dirent.h>
41 .BI "int scandir(const char *" dirp ", struct dirent ***" namelist ,
43 .BI "int (*" filter ")(const struct dirent *),"
44 .BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
47 .BI "int alphasort(const void *" a ", const void *" b );
49 .BI "int versionsort(const void *" a ", const void *" b );
53 Feature Test Macro Requirements for glibc (see
54 .BR feature_test_macros (7)):
59 _BSD_SOURCE || _SVID_SOURCE
66 function scans the directory \fIdirp\fP, calling
67 \fIfilter\fP() on each directory entry.
69 \fIfilter\fP() returns nonzero are stored in strings allocated via
74 function \fIcompar\fP(), and collected in array \fInamelist\fP
75 which is allocated via
77 If \fIfilter\fP is NULL, all entries are selected.
83 functions can be used as the comparison function
85 The former sorts directory entries using
89 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
93 function returns the number of directory entries
94 selected or \-1 if an error occurs.
100 functions return an integer less than, equal to,
101 or greater than zero if the first argument is considered to be
102 respectively less than, equal to, or greater than the second.
106 The path in \fIdirp\fR does not exist.
109 Insufficient memory to complete the operation.
112 The path in \fIdirp\fR is not a directory.
115 was added to glibc in version 2.1.
120 are specified in POSIX.1-2008, and are widely available.
128 are from 4.3BSD, and have been available under Linux since libc4.
129 Libc4 and libc5 use the more precise prototype
132 int alphasort(const struct dirent ** a,
133 const struct dirent **b);
136 but glibc 2.0 returns to the imprecise BSD prototype.
140 is a GNU extension, available since glibc 2.1.
151 /* print files in current directory in reverse order */
157 struct dirent **namelist;
160 n = scandir(".", &namelist, 0, alphasort);
165 printf("%s\en", namelist[n]\->d_name);