xref: /freebsd/lib/libc/gen/scandir.3 (revision 0aee91e1fb311f4f92779bb2c7ef0c1fe10d8d35)
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.
1258f0484fSRodney W. Grimes.\" 4. 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
43e3303e90SUlrich Spörlein.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp" "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
4458f0484fSRodney W. Grimes.Ft int
4546cdc140SDavid Chisnall.Fn scandir_b "const char *dirname" "struct dirent ***namelist" "int \*(lp*select\^(rp\*(lpconst struct dirent *\*(rp" "int \*(lp^compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
4646cdc140SDavid Chisnall.Ft int
474176dd52SKonstantin Belousov.Fn alphasort "const struct dirent **d1" "const struct dirent **d2"
4858f0484fSRodney W. Grimes.Sh DESCRIPTION
4958f0484fSRodney W. GrimesThe
5058f0484fSRodney W. Grimes.Fn scandir
5158f0484fSRodney W. Grimesfunction
5258f0484fSRodney W. Grimesreads the directory
5358f0484fSRodney W. Grimes.Fa dirname
5458f0484fSRodney W. Grimesand builds an array of pointers to directory
5558f0484fSRodney W. Grimesentries using
5658f0484fSRodney W. Grimes.Xr malloc 3 .
5758f0484fSRodney W. GrimesIt returns the number of entries in the array.
5858f0484fSRodney W. GrimesA pointer to the array of directory entries is stored in the location
5958f0484fSRodney W. Grimesreferenced by
6058f0484fSRodney W. Grimes.Fa namelist .
6158f0484fSRodney W. Grimes.Pp
6258f0484fSRodney W. GrimesThe
6358f0484fSRodney W. Grimes.Fa select
642efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is called by
6558f0484fSRodney W. Grimes.Fn scandir
6658f0484fSRodney W. Grimesto select which entries are to be included in the array.
6758f0484fSRodney W. GrimesThe select routine is passed a
6858f0484fSRodney W. Grimespointer to a directory entry and should return a non-zero
6958f0484fSRodney W. Grimesvalue if the directory entry is to be included in the array.
7058f0484fSRodney W. GrimesIf
7158f0484fSRodney W. Grimes.Fa select
7258f0484fSRodney W. Grimesis null, then all the directory entries will be included.
7358f0484fSRodney W. Grimes.Pp
7458f0484fSRodney W. GrimesThe
7558f0484fSRodney W. Grimes.Fa compar
762efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is passed to
7758f0484fSRodney W. Grimes.Xr qsort 3
7858f0484fSRodney W. Grimesto sort the completed array.
7958f0484fSRodney W. GrimesIf this pointer is null, the array is not sorted.
8058f0484fSRodney W. Grimes.Pp
8158f0484fSRodney W. GrimesThe
8258f0484fSRodney W. Grimes.Fn alphasort
8358f0484fSRodney W. Grimesfunction
8458f0484fSRodney W. Grimesis a routine which can be used for the
8558f0484fSRodney W. Grimes.Fa compar
86e5732e18SAndrey A. Chernovargument to sort the array alphabetically using
87e5732e18SAndrey A. Chernov.Xr strcoll 3 .
8858f0484fSRodney W. Grimes.Pp
8958f0484fSRodney W. GrimesThe memory allocated for the array can be deallocated with
9058f0484fSRodney W. Grimes.Xr free 3 ,
9158f0484fSRodney W. Grimesby freeing each pointer in the array and then the array itself.
9246cdc140SDavid Chisnall.Pp
9346cdc140SDavid ChisnallThe
9446cdc140SDavid Chisnall.Fn scandir_b
9546cdc140SDavid Chisnallfunction behaves in the same way as
9646cdc140SDavid Chisnall.Fn scandir ,
9746cdc140SDavid Chisnallbut takes blocks as arguments instead of function pointers and calls
9846cdc140SDavid Chisnall.Fn qsort_b
9946cdc140SDavid Chisnallrather than
10046cdc140SDavid Chisnall.Fn qsort .
10158f0484fSRodney W. Grimes.Sh DIAGNOSTICS
10258f0484fSRodney W. GrimesReturns \-1 if the directory cannot be opened for reading or if
10358f0484fSRodney W. Grimes.Xr malloc 3
10458f0484fSRodney W. Grimescannot allocate enough memory to hold all the data structures.
10558f0484fSRodney W. Grimes.Sh SEE ALSO
10658f0484fSRodney W. Grimes.Xr directory 3 ,
10758f0484fSRodney W. Grimes.Xr malloc 3 ,
10858f0484fSRodney W. Grimes.Xr qsort 3 ,
109*0aee91e1SChristian Brueffer.Xr strcoll 3 ,
110*0aee91e1SChristian Brueffer.Xr dir 5
11158f0484fSRodney W. Grimes.Sh HISTORY
11258f0484fSRodney W. GrimesThe
11358f0484fSRodney W. Grimes.Fn scandir
11458f0484fSRodney W. Grimesand
11558f0484fSRodney W. Grimes.Fn alphasort
11658f0484fSRodney W. Grimesfunctions appeared in
11758f0484fSRodney W. Grimes.Bx 4.2 .
118