1.\" Copyright (c) 1983, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd August 31, 2023 29.Dt SCANDIR 3 30.Os 31.Sh NAME 32.Nm scandir , 33.Nm scandirat , 34.Nm scandir_b , 35.Nm alphasort , 36.Nm versionsort 37.Nd scan a directory 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In dirent.h 42.Ft int 43.Fo scandir 44.Fa "const char *dirname" 45.Fa "struct dirent ***namelist" 46.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp" 47.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp" 48.Fc 49.Ft int 50.Fo scandirat 51.Fa "int dirfd" 52.Fa "const char *dirname" 53.Fa "struct dirent ***namelist" 54.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp" 55.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp" 56.Fc 57.Ft int 58.Fo scandir_b 59.Fa "const char *dirname" 60.Fa "struct dirent ***namelist" 61.Fa "int \*(lp^select\*(rp\*(lpconst struct dirent *\*(rp" 62.Fa "int \*(lp^compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp" 63.Fc 64.Ft int 65.Fn alphasort "const struct dirent **d1" "const struct dirent **d2" 66.Ft int 67.Fn versionsort "const struct dirent **d1" "const struct dirent **d2" 68.Sh DESCRIPTION 69The 70.Fn scandir 71function 72reads the directory 73.Fa dirname 74and builds an array of pointers to directory 75entries using 76.Xr malloc 3 . 77It returns the number of entries in the array. 78A pointer to the array of directory entries is stored in the location 79referenced by 80.Fa namelist . 81.Pp 82The 83.Fa select 84argument is a pointer to a user supplied subroutine which is called by 85.Fn scandir 86to select which entries are to be included in the array. 87The select routine is passed a 88pointer to a directory entry and should return a non-zero 89value if the directory entry is to be included in the array. 90If 91.Fa select 92is null, then all the directory entries will be included. 93.Pp 94The 95.Fa compar 96argument is a pointer to a user supplied subroutine which is passed to 97.Xr qsort 3 98to sort the completed array. 99If this pointer is null, the array is not sorted. 100.Pp 101The 102.Fn alphasort 103function 104is a routine which can be used for the 105.Fa compar 106argument to sort the array alphabetically using 107.Xr strcoll 3 . 108.Pp 109The 110.Fn versionsort 111function is a routine which can be used for the 112.Fa compar 113argument to sort the array naturally using 114.Xr strverscmp 3 . 115.Pp 116The memory allocated for the array can be deallocated with 117.Xr free 3 , 118by freeing each pointer in the array and then the array itself. 119.Pp 120The 121.Fn scandirat 122function is similar to 123.Fn scandir , 124but takes an additional 125.Fa dirfd 126argument. 127If the supplied 128.Fa dirname 129is absolute, the function's behavior is identical to that of 130.Fn scandir , 131the 132.Fa dirfd 133argument is unused. 134If 135.Fa dirname 136is relative, 137.Fa dirfd 138must be a valid file descriptor referencing a directory, in 139which case the 140.Fa dirname 141lookup is performed relative to the directory referenced by 142.Fa dirfd . 143If 144.Fa dirfd 145has the special value 146.Va AT_FDCWD , 147then the current process directory is used as the base for 148relative lookups. 149See 150.Xr openat 2 151for additional details. 152.Pp 153The 154.Fn scandir_b 155function behaves in the same way as 156.Fn scandir , 157but takes blocks as arguments instead of function pointers and calls 158.Fn qsort_b 159rather than 160.Fn qsort . 161.Sh DIAGNOSTICS 162Returns \-1 if the directory cannot be opened for reading or if 163.Xr malloc 3 164cannot allocate enough memory to hold all the data structures. 165.Sh SEE ALSO 166.Xr openat 2 , 167.Xr directory 3 , 168.Xr malloc 3 , 169.Xr qsort 3 , 170.Xr strcoll 3 , 171.Xr strverscmp 3 , 172.Xr dir 5 173.Sh STANDARDS 174The 175.Fn versionsort 176function is a GNU extension and conforms to no standard. 177.Sh HISTORY 178The 179.Fn scandir 180and 181.Fn alphasort 182functions appeared in 183.Bx 4.2 . 184The 185.Fn scandirat 186and 187.Fn versionsort 188functions were added in 189.Fx 13.2 . 190