move from sys to gen
[unix-history] / usr / src / lib / libc / gen / scandir.3
CommitLineData
ae59e04c 1.\" Copyright (c) 1983, 1991 Regents of the University of California.
25aac6e1 2.\" All rights reserved.
460316d1 3.\"
91cff1e1 4.\" %sccs.include.redist.man%
25aac6e1 5.\"
ae59e04c 6.\" @(#)scandir.3 6.8 (Berkeley) %G%
460316d1 7.\"
ae59e04c
CL
8.Dd
9.Dt SCANDIR 3
10.Os BSD 4.2
11.Sh NAME
12.Nm scandir ,
13.Nm alphasort
14.Nd scan a directory
15.Sh SYNOPSIS
16.Fd #include <sys/types.h>
17.Fd #include <dirent.h>
18.Ft int
19.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \\*(lp*select\\*(rp\\*(lpstruct dirent *\\*(rp" "int \\*(lp*compar\\*(rp\\*(lpconst void *, const void *\\*(rp"
20.Ft int
21.Fn alphasort "const void *d1" "const void *d2"
22.Sh DESCRIPTION
23The
24.Fn scandir
25function
460316d1 26reads the directory
ae59e04c 27.Fa dirname
460316d1
KM
28and builds an array of pointers to directory
29entries using
ae59e04c 30.Xr malloc 3 .
4754ea9c
KB
31It returns the number of entries in the array.
32A pointer to the array of directory entries is stored in the location
33referenced by
ae59e04c
CL
34.Fa namelist .
35.Pp
460316d1 36The
ae59e04c 37.Fa select
460316d1 38parameter is a pointer to a user supplied subroutine which is called by
ae59e04c 39.Fn scandir
460316d1
KM
40to select which entries are to be included in the array.
41The select routine is passed a
42pointer to a directory entry and should return a non-zero
43value if the directory entry is to be included in the array.
44If
ae59e04c 45.Fa select
460316d1 46is null, then all the directory entries will be included.
ae59e04c 47.Pp
460316d1 48The
ae59e04c 49.Fa compar
460316d1 50parameter is a pointer to a user supplied subroutine which is passed to
ae59e04c 51.Xr qsort 3
643c10cf
KB
52to sort the completed array.
53If this pointer is null, the array is not sorted.
ae59e04c
CL
54.Pp
55The
56.Fn alphasort
57function
460316d1 58is a routine which can be used for the
ae59e04c 59.Fa compar
460316d1 60parameter to sort the array alphabetically.
ae59e04c 61.Pp
460316d1 62The memory allocated for the array can be deallocated with
ae59e04c 63.Xr free 3 ,
643c10cf 64by freeing each pointer in the array and then the array itself.
ae59e04c 65.Sh DIAGNOSTICS
460316d1 66Returns \-1 if the directory cannot be opened for reading or if
ae59e04c 67.Xr malloc 3
460316d1 68cannot allocate enough memory to hold all the data structures.
ae59e04c
CL
69.Sh SEE ALSO
70.Xr directory 3 ,
71.Xr malloc 3 ,
72.Xr qsort 3 ,
73.Xr dir 5
74.Sh HISTORY
75The
76.Fn scandir
77and
78.Fn alphasort
79functions appeared in
80.Bx 4.2 .