xref: /freebsd/lib/libc/gen/scandir.3 (revision aa5e19a9b3f33bab7a04d749601706d8f4499b25)
158f0484fSRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
12fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
1358f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1458f0484fSRodney W. Grimes.\"    without specific prior written permission.
1558f0484fSRodney W. Grimes.\"
1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes.\" SUCH DAMAGE.
2758f0484fSRodney W. Grimes.\"
2858f0484fSRodney W. Grimes.\"     @(#)scandir.3	8.1 (Berkeley) 6/4/93
297f3dea24SPeter Wemm.\" $FreeBSD$
3058f0484fSRodney W. Grimes.\"
314176dd52SKonstantin Belousov.Dd January 3, 2010
3258f0484fSRodney W. Grimes.Dt SCANDIR 3
33a307d598SRuslan Ermilov.Os
3458f0484fSRodney W. Grimes.Sh NAME
3558f0484fSRodney W. Grimes.Nm scandir ,
3658f0484fSRodney W. Grimes.Nm alphasort
3758f0484fSRodney W. Grimes.Nd scan a directory
3825bb73e0SAlexey Zelkin.Sh LIBRARY
3925bb73e0SAlexey Zelkin.Lb libc
4058f0484fSRodney W. Grimes.Sh SYNOPSIS
4132eef9aeSRuslan Ermilov.In dirent.h
4258f0484fSRodney W. Grimes.Ft int
43*aa5e19a9SKonstantin Belousov.Fo scandir
44*aa5e19a9SKonstantin Belousov.Fa "const char *dirname"
45*aa5e19a9SKonstantin Belousov.Fa "struct dirent ***namelist"
46*aa5e19a9SKonstantin Belousov.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp"
47*aa5e19a9SKonstantin Belousov.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
48*aa5e19a9SKonstantin Belousov.Fc
4958f0484fSRodney W. Grimes.Ft int
50*aa5e19a9SKonstantin Belousov.Fo scandir_b
51*aa5e19a9SKonstantin Belousov.Fa "const char *dirname"
52*aa5e19a9SKonstantin Belousov.Fa "struct dirent ***namelist"
53*aa5e19a9SKonstantin Belousov.Fa "int \*(lp*select\^(rp\*(lpconst struct dirent *\*(rp"
54*aa5e19a9SKonstantin Belousov.Fa "int \*(lp^compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
55*aa5e19a9SKonstantin Belousov.Fc
5646cdc140SDavid Chisnall.Ft int
574176dd52SKonstantin Belousov.Fn alphasort "const struct dirent **d1" "const struct dirent **d2"
5858f0484fSRodney W. Grimes.Sh DESCRIPTION
5958f0484fSRodney W. GrimesThe
6058f0484fSRodney W. Grimes.Fn scandir
6158f0484fSRodney W. Grimesfunction
6258f0484fSRodney W. Grimesreads the directory
6358f0484fSRodney W. Grimes.Fa dirname
6458f0484fSRodney W. Grimesand builds an array of pointers to directory
6558f0484fSRodney W. Grimesentries using
6658f0484fSRodney W. Grimes.Xr malloc 3 .
6758f0484fSRodney W. GrimesIt returns the number of entries in the array.
6858f0484fSRodney W. GrimesA pointer to the array of directory entries is stored in the location
6958f0484fSRodney W. Grimesreferenced by
7058f0484fSRodney W. Grimes.Fa namelist .
7158f0484fSRodney W. Grimes.Pp
7258f0484fSRodney W. GrimesThe
7358f0484fSRodney W. Grimes.Fa select
742efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is called by
7558f0484fSRodney W. Grimes.Fn scandir
7658f0484fSRodney W. Grimesto select which entries are to be included in the array.
7758f0484fSRodney W. GrimesThe select routine is passed a
7858f0484fSRodney W. Grimespointer to a directory entry and should return a non-zero
7958f0484fSRodney W. Grimesvalue if the directory entry is to be included in the array.
8058f0484fSRodney W. GrimesIf
8158f0484fSRodney W. Grimes.Fa select
8258f0484fSRodney W. Grimesis null, then all the directory entries will be included.
8358f0484fSRodney W. Grimes.Pp
8458f0484fSRodney W. GrimesThe
8558f0484fSRodney W. Grimes.Fa compar
862efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is passed to
8758f0484fSRodney W. Grimes.Xr qsort 3
8858f0484fSRodney W. Grimesto sort the completed array.
8958f0484fSRodney W. GrimesIf this pointer is null, the array is not sorted.
9058f0484fSRodney W. Grimes.Pp
9158f0484fSRodney W. GrimesThe
9258f0484fSRodney W. Grimes.Fn alphasort
9358f0484fSRodney W. Grimesfunction
9458f0484fSRodney W. Grimesis a routine which can be used for the
9558f0484fSRodney W. Grimes.Fa compar
96e5732e18SAndrey A. Chernovargument to sort the array alphabetically using
97e5732e18SAndrey A. Chernov.Xr strcoll 3 .
9858f0484fSRodney W. Grimes.Pp
9958f0484fSRodney W. GrimesThe memory allocated for the array can be deallocated with
10058f0484fSRodney W. Grimes.Xr free 3 ,
10158f0484fSRodney W. Grimesby freeing each pointer in the array and then the array itself.
10246cdc140SDavid Chisnall.Pp
10346cdc140SDavid ChisnallThe
10446cdc140SDavid Chisnall.Fn scandir_b
10546cdc140SDavid Chisnallfunction behaves in the same way as
10646cdc140SDavid Chisnall.Fn scandir ,
10746cdc140SDavid Chisnallbut takes blocks as arguments instead of function pointers and calls
10846cdc140SDavid Chisnall.Fn qsort_b
10946cdc140SDavid Chisnallrather than
11046cdc140SDavid Chisnall.Fn qsort .
11158f0484fSRodney W. Grimes.Sh DIAGNOSTICS
11258f0484fSRodney W. GrimesReturns \-1 if the directory cannot be opened for reading or if
11358f0484fSRodney W. Grimes.Xr malloc 3
11458f0484fSRodney W. Grimescannot allocate enough memory to hold all the data structures.
11558f0484fSRodney W. Grimes.Sh SEE ALSO
11658f0484fSRodney W. Grimes.Xr directory 3 ,
11758f0484fSRodney W. Grimes.Xr malloc 3 ,
11858f0484fSRodney W. Grimes.Xr qsort 3 ,
1190aee91e1SChristian Brueffer.Xr strcoll 3 ,
1200aee91e1SChristian Brueffer.Xr dir 5
12158f0484fSRodney W. Grimes.Sh HISTORY
12258f0484fSRodney W. GrimesThe
12358f0484fSRodney W. Grimes.Fn scandir
12458f0484fSRodney W. Grimesand
12558f0484fSRodney W. Grimes.Fn alphasort
12658f0484fSRodney W. Grimesfunctions appeared in
12758f0484fSRodney W. Grimes.Bx 4.2 .
128