OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / 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  2009-02-10 "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 ENOMEM
106 Insufficient memory to complete the operation.
107 .SH VERSIONS
108 .BR versionsort ()
109 was added to glibc in version 2.1.
110 .SH "CONFORMING TO"
111 .BR alphasort ()
112 and
113 .BR scandir ()
114 are specified in POSIX.1-2008, and are widely available.
115 .BR versionsort ()
116 is a GNU extension.
117 .LP
118 The functions
119 .BR scandir ()
120 and
121 .BR alphasort ()
122 are from 4.3BSD, and have been available under Linux since libc4.
123 Libc4 and libc5 use the more precise prototype
124 .sp
125 .nf
126     int alphasort(const struct dirent ** a,
127                   const struct dirent **b);
128 .fi
129 .sp
130 but glibc 2.0 returns to the imprecise BSD prototype.
131 .LP
132 The function
133 .BR versionsort ()
134 is a GNU extension, available since glibc 2.1.
135 .LP
136 Since glibc 2.1,
137 .BR alphasort ()
138 calls
139 .BR strcoll (3);
140 earlier it used
141 .BR strcmp (3).
142 .SH EXAMPLE
143 .nf
144 #define _SVID_SOURCE
145 /* print files in current directory in reverse order */
146 #include <dirent.h>
147
148 int
149 main(void)
150 {
151     struct dirent **namelist;
152     int n;
153
154     n = scandir(".", &namelist, 0, alphasort);
155     if (n < 0)
156         perror("scandir");
157     else {
158         while (n\-\-) {
159             printf("%s\en", namelist[n]\->d_name);
160             free(namelist[n]);
161         }
162         free(namelist);
163     }
164 }
165 .fi
166 .SH "SEE ALSO"
167 .BR closedir (3),
168 .BR fnmatch (3),
169 .BR opendir (3),
170 .BR readdir (3),
171 .BR rewinddir (3),
172 .BR seekdir (3),
173 .BR strcmp (3),
174 .BR strcoll (3),
175 .BR strverscmp (3),
176 .BR telldir (3)