OSDN Git Service

9e355aa66039cfbe2dd7ac00e9c5ff1137efe32c
[linuxjm/LDP_man-pages.git] / original / man3 / scandir.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
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.
33 .\"
34 .\" FIXME glibc 2.15 adds scandirat()
35 .\"
36 .TH SCANDIR 3  2011-09-08 "GNU" "Linux Programmer's Manual"
37 .SH NAME
38 scandir, alphasort, versionsort \- scan a directory for matching entries
39 .SH SYNOPSIS
40 .nf
41 .B #include <dirent.h>
42 .sp
43 .BI "int scandir(const char *" dirp ", struct dirent ***" namelist ,
44 .RS
45 .BI "int (*" filter ")(const struct dirent *),"
46 .BI "int (*" compar ")(const struct dirent **, const struct dirent **));"
47 .RE
48 .sp
49 .BI "int alphasort(const void *" a ", const void *" b );
50 .sp
51 .BI "int versionsort(const void *" a ", const void *" b );
52 .fi
53 .sp
54 .in -4n
55 Feature Test Macro Requirements for glibc (see
56 .BR feature_test_macros (7)):
57 .in
58 .sp
59 .BR scandir (),
60 .BR alphasort ():
61 _BSD_SOURCE || _SVID_SOURCE
62 .br
63 .BR versionsort ():
64 _GNU_SOURCE
65 .SH DESCRIPTION
66 The
67 .BR scandir ()
68 function scans the directory \fIdirp\fP, calling
69 \fIfilter\fP() on each directory entry.
70 Entries for which
71 \fIfilter\fP() returns nonzero are stored in strings allocated via
72 .BR malloc (3),
73 sorted using
74 .BR qsort (3)
75 with the comparison
76 function \fIcompar\fP(), and collected in array \fInamelist\fP
77 which is allocated via
78 .BR malloc (3).
79 If \fIfilter\fP is NULL, all entries are selected.
80 .LP
81 The
82 .BR alphasort ()
83 and
84 .BR versionsort ()
85 functions can be used as the comparison function
86 .IR compar ().
87 The former sorts directory entries using
88 .BR strcoll (3),
89 the latter using
90 .BR strverscmp (3)
91 on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
92 .SH "RETURN VALUE"
93 The
94 .BR scandir ()
95 function returns the number of directory entries
96 selected or \-1 if an error occurs.
97 .PP
98 The
99 .BR alphasort ()
100 and
101 .BR versionsort ()
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.
105 .SH ERRORS
106 .TP
107 .B ENOENT
108 The path in \fIdirp\fR does not exist.
109 .TP
110 .B ENOMEM
111 Insufficient memory to complete the operation.
112 .TP
113 .B ENOTDIR
114 The path in \fIdirp\fR is not a directory.
115 .SH VERSIONS
116 .BR versionsort ()
117 was added to glibc in version 2.1.
118 .SH "CONFORMING TO"
119 .BR alphasort ()
120 and
121 .BR scandir ()
122 are specified in POSIX.1-2008, and are widely available.
123 .BR versionsort ()
124 is a GNU extension.
125 .LP
126 The functions
127 .BR scandir ()
128 and
129 .BR alphasort ()
130 are from 4.3BSD, and have been available under Linux since libc4.
131 Libc4 and libc5 use the more precise prototype
132 .sp
133 .nf
134     int alphasort(const struct dirent ** a,
135                   const struct dirent **b);
136 .fi
137 .sp
138 but glibc 2.0 returns to the imprecise BSD prototype.
139 .LP
140 The function
141 .BR versionsort ()
142 is a GNU extension, available since glibc 2.1.
143 .LP
144 Since glibc 2.1,
145 .BR alphasort ()
146 calls
147 .BR strcoll (3);
148 earlier it used
149 .BR strcmp (3).
150 .SH EXAMPLE
151 .nf
152 #define _SVID_SOURCE
153 /* print files in current directory in reverse order */
154 #include <dirent.h>
155
156 int
157 main(void)
158 {
159     struct dirent **namelist;
160     int n;
161
162     n = scandir(".", &namelist, 0, alphasort);
163     if (n < 0)
164         perror("scandir");
165     else {
166         while (n\-\-) {
167             printf("%s\en", namelist[n]\->d_name);
168             free(namelist[n]);
169         }
170         free(namelist);
171     }
172 }
173 .fi
174 .SH "SEE ALSO"
175 .BR closedir (3),
176 .BR fnmatch (3),
177 .BR opendir (3),
178 .BR readdir (3),
179 .BR rewinddir (3),
180 .BR seekdir (3),
181 .BR strcmp (3),
182 .BR strcoll (3),
183 .BR strverscmp (3),
184 .BR telldir (3)