xref: /freebsd/lib/libc/gen/scandir.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Scan the directory dirname calling select to make a list of selected
34  * directory entries then sort using qsort and compare routine dcomp.
35  * Returns the number of entries and a pointer to a list of pointers to
36  * struct dirent (through namelist). Returns -1 if there were any errors.
37  */
38 
39 #include "namespace.h"
40 #include <dirent.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "un-namespace.h"
46 
47 #ifdef	I_AM_SCANDIR_B
48 #include "block_abi.h"
49 #define	SELECT(x)	CALL_BLOCK(select, x)
50 #ifndef __BLOCKS__
51 void qsort_b(void *, size_t, size_t, void *);
52 #endif
53 #else
54 #define	SELECT(x)	select(x)
55 #endif
56 
57 #ifdef I_AM_SCANDIR_B
58 typedef DECLARE_BLOCK(int, select_block, const struct dirent *);
59 typedef DECLARE_BLOCK(int, dcomp_block, const struct dirent **,
60     const struct dirent **);
61 #else
62 static int scandir_thunk_cmp(const void *p1, const void *p2, void *thunk);
63 #endif
64 
65 static int
66 #ifdef I_AM_SCANDIR_B
67 scandir_b_dirp(DIR *dirp, struct dirent ***namelist, select_block select,
68     dcomp_block dcomp)
69 #else
70 scandir_dirp(DIR *dirp, struct dirent ***namelist,
71     int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
72     const struct dirent **))
73 #endif
74 {
75 	struct dirent *d, *p, **names = NULL;
76 	size_t arraysz, numitems;
77 
78 	numitems = 0;
79 	arraysz = 32;	/* initial estimate of the array size */
80 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
81 	if (names == NULL)
82 		goto fail;
83 
84 	while ((d = readdir(dirp)) != NULL) {
85 		if (select != NULL && !SELECT(d))
86 			continue;	/* just selected names */
87 		/*
88 		 * Make a minimum size copy of the data
89 		 */
90 		p = (struct dirent *)malloc(_GENERIC_DIRSIZ(d));
91 		if (p == NULL)
92 			goto fail;
93 		p->d_fileno = d->d_fileno;
94 		p->d_type = d->d_type;
95 		p->d_reclen = d->d_reclen;
96 		p->d_namlen = d->d_namlen;
97 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
98 		/*
99 		 * Check to make sure the array has space left and
100 		 * realloc the maximum size.
101 		 */
102 		if (numitems >= arraysz) {
103 			struct dirent **names2;
104 
105 			names2 = reallocarray(names, arraysz,
106 			    2 * sizeof(struct dirent *));
107 			if (names2 == NULL) {
108 				free(p);
109 				goto fail;
110 			}
111 			names = names2;
112 			arraysz *= 2;
113 		}
114 		names[numitems++] = p;
115 	}
116 	closedir(dirp);
117 	if (numitems && dcomp != NULL)
118 #ifdef I_AM_SCANDIR_B
119 		qsort_b(names, numitems, sizeof(struct dirent *), (void*)dcomp);
120 #else
121 		qsort_r(names, numitems, sizeof(struct dirent *),
122 		    scandir_thunk_cmp, &dcomp);
123 #endif
124 	*namelist = names;
125 	return (numitems);
126 
127 fail:
128 	while (numitems > 0)
129 		free(names[--numitems]);
130 	free(names);
131 	closedir(dirp);
132 	return (-1);
133 }
134 
135 int
136 #ifdef I_AM_SCANDIR_B
137 scandir_b(const char *dirname, struct dirent ***namelist, select_block select,
138     dcomp_block dcomp)
139 #else
140 scandir(const char *dirname, struct dirent ***namelist,
141     int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
142     const struct dirent **))
143 #endif
144 {
145 	DIR *dirp;
146 
147 	dirp = opendir(dirname);
148 	if (dirp == NULL)
149 		return (-1);
150 	return (
151 #ifdef I_AM_SCANDIR_B
152 	    scandir_b_dirp
153 #else
154 	    scandir_dirp
155 #endif
156 	    (dirp, namelist, select, dcomp));
157 }
158 
159 #ifndef I_AM_SCANDIR_B
160 int
161 scandirat(int dirfd, const char *dirname, struct dirent ***namelist,
162     int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
163     const struct dirent **))
164 {
165 	DIR *dirp;
166 	int fd;
167 
168 	fd = _openat(dirfd, dirname, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
169 	if (fd == -1)
170 		return (-1);
171 	dirp = fdopendir(fd);
172 	if (dirp == NULL) {
173 		_close(fd);
174 		return (-1);
175 	}
176 	return (scandir_dirp(dirp, namelist, select, dcomp));
177 }
178 
179 /*
180  * Alphabetic order comparison routine for those who want it.
181  * POSIX 2008 requires that alphasort() uses strcoll().
182  */
183 int
184 alphasort(const struct dirent **d1, const struct dirent **d2)
185 {
186 
187 	return (strcoll((*d1)->d_name, (*d2)->d_name));
188 }
189 
190 int
191 versionsort(const struct dirent **d1, const struct dirent **d2)
192 {
193 
194 	return (strverscmp((*d1)->d_name, (*d2)->d_name));
195 }
196 
197 static int
198 scandir_thunk_cmp(const void *p1, const void *p2, void *thunk)
199 {
200 	int (*dc)(const struct dirent **, const struct dirent **);
201 
202 	dc = *(int (**)(const struct dirent **, const struct dirent **))thunk;
203 	return (dc((const struct dirent **)p1, (const struct dirent **)p2));
204 }
205 #endif
206