xref: /csrg-svn/lib/libc/gen/scandir.3 (revision 48352)
1*48352Scael.\" Copyright (c) 1983, 1991 Regents of the University of California.
235309Sbostic.\" All rights reserved.
320586Smckusick.\"
443571Strent.\" %sccs.include.redist.man%
520586Smckusick.\"
6*48352Scael.\"     @(#)scandir.3	6.8 (Berkeley) 04/19/91
735309Sbostic.\"
8*48352Scael.Dd
9*48352Scael.Dt SCANDIR 3
10*48352Scael.Os BSD 4.2
11*48352Scael.Sh NAME
12*48352Scael.Nm scandir ,
13*48352Scael.Nm alphasort
14*48352Scael.Nd scan a directory
15*48352Scael.Sh SYNOPSIS
16*48352Scael.Fd #include <sys/types.h>
17*48352Scael.Fd #include <dirent.h>
18*48352Scael.Ft int
19*48352Scael.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*48352Scael.Ft int
21*48352Scael.Fn alphasort "const void *d1" "const void *d2"
22*48352Scael.Sh DESCRIPTION
23*48352ScaelThe
24*48352Scael.Fn scandir
25*48352Scaelfunction
2620586Smckusickreads the directory
27*48352Scael.Fa dirname
2820586Smckusickand builds an array of pointers to directory
2920586Smckusickentries using
30*48352Scael.Xr malloc 3 .
3143536SbosticIt returns the number of entries in the array.
3243536SbosticA pointer to the array of directory entries is stored in the location
3343536Sbosticreferenced by
34*48352Scael.Fa namelist .
35*48352Scael.Pp
3620586SmckusickThe
37*48352Scael.Fa select
3820586Smckusickparameter is a pointer to a user supplied subroutine which is called by
39*48352Scael.Fn scandir
4020586Smckusickto select which entries are to be included in the array.
4120586SmckusickThe select routine is passed a
4220586Smckusickpointer to a directory entry and should return a non-zero
4320586Smckusickvalue if the directory entry is to be included in the array.
4420586SmckusickIf
45*48352Scael.Fa select
4620586Smckusickis null, then all the directory entries will be included.
47*48352Scael.Pp
4820586SmckusickThe
49*48352Scael.Fa compar
5020586Smckusickparameter is a pointer to a user supplied subroutine which is passed to
51*48352Scael.Xr qsort 3
5243610Sbosticto sort the completed array.
5343610SbosticIf this pointer is null, the array is not sorted.
54*48352Scael.Pp
55*48352ScaelThe
56*48352Scael.Fn alphasort
57*48352Scaelfunction
5820586Smckusickis a routine which can be used for the
59*48352Scael.Fa compar
6020586Smckusickparameter to sort the array alphabetically.
61*48352Scael.Pp
6220586SmckusickThe memory allocated for the array can be deallocated with
63*48352Scael.Xr free 3 ,
6443610Sbosticby freeing each pointer in the array and then the array itself.
65*48352Scael.Sh DIAGNOSTICS
6620586SmckusickReturns \-1 if the directory cannot be opened for reading or if
67*48352Scael.Xr malloc 3
6820586Smckusickcannot allocate enough memory to hold all the data structures.
69*48352Scael.Sh SEE ALSO
70*48352Scael.Xr directory 3 ,
71*48352Scael.Xr malloc 3 ,
72*48352Scael.Xr qsort 3 ,
73*48352Scael.Xr dir 5
74*48352Scael.Sh HISTORY
75*48352ScaelThe
76*48352Scael.Fn scandir
77*48352Scaeland
78*48352Scael.Fn alphasort
79*48352Scaelfunctions appeared in
80*48352Scael.Bx 4.2 .
81