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