OSDN Git Service

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