xref: /openbsd-src/lib/libc/gen/scandir.3 (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1.\"	$OpenBSD: scandir.3,v 1.10 2010/03/26 19:30:40 jmc Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: March 26 2010 $
31.Dt SCANDIR 3
32.Os
33.Sh NAME
34.Nm scandir ,
35.Nm alphasort
36.Nd scan a directory
37.Sh SYNOPSIS
38.Fd #include <sys/types.h>
39.Fd #include <dirent.h>
40.Ft int
41.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \*(lp*select\*(rp\*(lpstruct dirent *\*(rp" "int \*(lp*compar\*(rp\*(lpconst void *, const void *\*(rp"
42.Ft int
43.Fn alphasort "const void *d1" "const void *d2"
44.Sh DESCRIPTION
45The
46.Fn scandir
47function reads the directory
48.Fa dirname
49and builds an array of pointers to directory
50entries using
51.Xr malloc 3 .
52It returns the number of entries in the array.
53A pointer to the array of directory entries is stored in the location
54referenced by
55.Fa namelist .
56.Pp
57The
58.Fa select
59parameter is a pointer to a user-supplied subroutine which is called by
60.Fn scandir
61to select which entries are to be included in the array.
62The select routine is passed a
63pointer to a directory entry and should return a non-zero
64value if the directory entry is to be included in the array.
65If
66.Fa select
67is
68.Dv NULL ,
69then all directory entries will be included.
70.Pp
71The
72.Fa compar
73parameter is a pointer to a user-supplied subroutine which is passed to
74.Xr qsort 3
75to sort the completed array.
76If this pointer is
77.Dv NULL ,
78the array is not sorted.
79.Pp
80The
81.Fn alphasort
82function is a routine which can be used for the
83.Fa compar
84parameter to sort the array alphabetically.
85.Pp
86The memory allocated for the array can be deallocated with
87.Xr free 3 ,
88by freeing each pointer in the array and then the array itself.
89.Sh DIAGNOSTICS
90Returns \-1 if the directory cannot be opened for reading or if
91.Xr malloc 3
92cannot allocate enough memory to hold all the data structures.
93.Sh SEE ALSO
94.Xr directory 3 ,
95.Xr malloc 3 ,
96.Xr qsort 3 ,
97.Xr dir 5
98.Sh HISTORY
99The
100.Fn scandir
101and
102.Fn alphasort
103functions appeared in
104.Bx 4.2 .
105