xref: /freebsd/lib/libc/gen/scandir.3 (revision e5732e1803e159f932ed29ab792122f7ff53f0fe)
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
434176dd52SKonstantin Belousov.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
454176dd52SKonstantin Belousov.Fn alphasort "const struct dirent **d1" "const struct dirent **d2"
4658f0484fSRodney W. Grimes.Sh DESCRIPTION
4758f0484fSRodney W. GrimesThe
4858f0484fSRodney W. Grimes.Fn scandir
4958f0484fSRodney W. Grimesfunction
5058f0484fSRodney W. Grimesreads the directory
5158f0484fSRodney W. Grimes.Fa dirname
5258f0484fSRodney W. Grimesand builds an array of pointers to directory
5358f0484fSRodney W. Grimesentries using
5458f0484fSRodney W. Grimes.Xr malloc 3 .
5558f0484fSRodney W. GrimesIt returns the number of entries in the array.
5658f0484fSRodney W. GrimesA pointer to the array of directory entries is stored in the location
5758f0484fSRodney W. Grimesreferenced by
5858f0484fSRodney W. Grimes.Fa namelist .
5958f0484fSRodney W. Grimes.Pp
6058f0484fSRodney W. GrimesThe
6158f0484fSRodney W. Grimes.Fa select
622efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is called by
6358f0484fSRodney W. Grimes.Fn scandir
6458f0484fSRodney W. Grimesto select which entries are to be included in the array.
6558f0484fSRodney W. GrimesThe select routine is passed a
6658f0484fSRodney W. Grimespointer to a directory entry and should return a non-zero
6758f0484fSRodney W. Grimesvalue if the directory entry is to be included in the array.
6858f0484fSRodney W. GrimesIf
6958f0484fSRodney W. Grimes.Fa select
7058f0484fSRodney W. Grimesis null, then all the directory entries will be included.
7158f0484fSRodney W. Grimes.Pp
7258f0484fSRodney W. GrimesThe
7358f0484fSRodney W. Grimes.Fa compar
742efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is passed to
7558f0484fSRodney W. Grimes.Xr qsort 3
7658f0484fSRodney W. Grimesto sort the completed array.
7758f0484fSRodney W. GrimesIf this pointer is null, the array is not sorted.
7858f0484fSRodney W. Grimes.Pp
7958f0484fSRodney W. GrimesThe
8058f0484fSRodney W. Grimes.Fn alphasort
8158f0484fSRodney W. Grimesfunction
8258f0484fSRodney W. Grimesis a routine which can be used for the
8358f0484fSRodney W. Grimes.Fa compar
84e5732e18SAndrey A. Chernovargument to sort the array alphabetically using
85e5732e18SAndrey A. Chernov.Xr strcoll 3 .
8658f0484fSRodney W. Grimes.Pp
8758f0484fSRodney W. GrimesThe memory allocated for the array can be deallocated with
8858f0484fSRodney W. Grimes.Xr free 3 ,
8958f0484fSRodney W. Grimesby freeing each pointer in the array and then the array itself.
9058f0484fSRodney W. Grimes.Sh DIAGNOSTICS
9158f0484fSRodney W. GrimesReturns \-1 if the directory cannot be opened for reading or if
9258f0484fSRodney W. Grimes.Xr malloc 3
9358f0484fSRodney W. Grimescannot allocate enough memory to hold all the data structures.
9458f0484fSRodney W. Grimes.Sh SEE ALSO
9558f0484fSRodney W. Grimes.Xr directory 3 ,
9658f0484fSRodney W. Grimes.Xr malloc 3 ,
9758f0484fSRodney W. Grimes.Xr qsort 3 ,
98e5732e18SAndrey A. Chernov.Xr dir 5 ,
99e5732e18SAndrey A. Chernov.Xr strcoll 3
10058f0484fSRodney W. Grimes.Sh HISTORY
10158f0484fSRodney W. GrimesThe
10258f0484fSRodney W. Grimes.Fn scandir
10358f0484fSRodney W. Grimesand
10458f0484fSRodney W. Grimes.Fn alphasort
10558f0484fSRodney W. Grimesfunctions appeared in
10658f0484fSRodney W. Grimes.Bx 4.2 .
107