]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/scandir.3
ffix
[thirdparty/man-pages.git] / man3 / scandir.3
CommitLineData
fea681da
MK
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 2001-12-26 "GNU" "Linux Programmer's Manual"
35.SH NAME
36scandir, 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 *" dir ", 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.br
49.BI "int versionsort(const void *" a ", const void *" b );
50.fi
51.SH DESCRIPTION
63aa9df0
MK
52The \fBscandir\fP() function scans the directory \fIdir\fP, calling
53\fIfilter\fP() on each directory entry. Entries for which
54\fIfilter\fP() returns non-zero are stored in strings allocated via
55\fBmalloc\fP(), sorted using \fBqsort\fP() with the comparison
56function \fIcompar\fP(), and collected in array \fInamelist\fP
57which is allocated via \fBmalloc\fP().
fea681da
MK
58If \fIfilter\fP is NULL, all entries are selected.
59.LP
60The
63aa9df0 61.BR alphasort ()
fea681da 62and
63aa9df0 63.BR versionsort ()
fea681da 64functions can be used as the comparison function
63aa9df0 65.IR compar ().
fea681da
MK
66The former sorts directory entries using
67.BR strcoll (3),
68the latter using
4d52e8f8 69.BR strverscmp (3)
8c383102 70on the strings \fI(*a)\->d_name\fP and \fI(*b)\->d_name\fP.
fea681da 71.SH "RETURN VALUE"
63aa9df0 72The \fBscandir\fP() function returns the number of directory entries
fea681da
MK
73selected or \-1 if an error occurs.
74.PP
75The
63aa9df0 76.BR alphasort ()
fea681da 77and
63aa9df0 78.BR versionsort ()
fea681da
MK
79functions return an integer less than, equal to,
80or greater than zero if the first argument is considered to be
81respectively less than, equal to, or greater than the second.
82.SH ERRORS
83.TP
84.B ENOMEM
85Insufficient memory to complete the operation.
86.SH "CONFORMING TO"
a7fadb55 87None of these functions is in POSIX.1-2001, but
63aa9df0 88.BR alphasort ()
fecfb777
MK
89and
90.BR scandir ()
91are under consideration for a future revision to POSIX.1.
fea681da
MK
92.LP
93The functions
63aa9df0 94.BR scandir ()
fea681da 95and
63aa9df0 96.BR alphasort ()
b14d4aa5 97are from 4.3BSD, and have been available under Linux since libc4.
fea681da
MK
98Libc4 and libc5 use the more precise prototype
99.sp
100.nf
fecfb777
MK
101 int alphasort(const struct dirent ** a,
102 const struct dirent **b);
fea681da
MK
103.fi
104.sp
105but glibc 2.0 returns to the imprecise BSD prototype.
106.LP
107The function
63aa9df0 108.BR versionsort ()
fea681da 109is a GNU extension, available since glibc 2.1.
fecfb777 110.LP
fea681da 111Since glibc 2.1,
63aa9df0 112.BR alphasort ()
fea681da
MK
113calls
114.BR strcoll (3);
115earlier it used
116.BR strcmp (3).
117.SH EXAMPLE
118.nf
119/* print files in current directory in reverse order */
120#include <dirent.h>
cf0a9ace
MK
121
122int
123main(void)
124{
fea681da
MK
125 struct dirent **namelist;
126 int n;
127
128 n = scandir(".", &namelist, 0, alphasort);
129 if (n < 0)
130 perror("scandir");
131 else {
2bc2f479
MK
132 while(n\-\-) {
133 printf("%s\en", namelist[n]\->d_name);
fea681da
MK
134 free(namelist[n]);
135 }
136 free(namelist);
137 }
138}
139.fi
140.SH "SEE ALSO"
141.BR closedir (3),
142.BR fnmatch (3),
143.BR opendir (3),
144.BR readdir (3),
145.BR rewinddir (3),
146.BR seekdir (3),
147.BR strcmp (3),
148.BR strcoll (3),
149.BR strverscmp (3),
150.BR telldir (3)