xref: /freebsd/lib/libc/gen/scandir-compat11.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 /*
34  * Scan the directory dirname calling select to make a list of selected
35  * directory entries then sort using qsort and compare routine dcomp.
36  * Returns the number of entries and a pointer to a list of pointers to
37  * struct dirent (through namelist). Returns -1 if there were any errors.
38  */
39 
40 #include "namespace.h"
41 #define	_WANT_FREEBSD11_DIRENT
42 #include <dirent.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include "un-namespace.h"
46 
47 #include "gen-compat.h"
48 
49 /*
50  * scandir_b@FBSD_1.4 was never exported from libc.so.7 due to a
51  * mistake, so there is no use of exporting it now with some earlier
52  * symbol version.  As result, we do not need to implement compat
53  * function freebsd11_scandir_b().
54  */
55 
56 #define	SELECT(x)	select(x)
57 
58 static int freebsd11_scandir_thunk_cmp(const void *p1, const void *p2,
59     void *thunk);
60 
61 int
62 freebsd11_scandir(const char *dirname, struct freebsd11_dirent ***namelist,
63     int (*select)(const struct freebsd11_dirent *),
64     int (*dcomp)(const struct freebsd11_dirent **,
65 	const struct freebsd11_dirent **))
66 {
67 	struct freebsd11_dirent *d, *p, **names = NULL;
68 	size_t arraysz, numitems;
69 	DIR *dirp;
70 
71 	if ((dirp = opendir(dirname)) == NULL)
72 		return(-1);
73 
74 	numitems = 0;
75 	arraysz = 32;	/* initial estimate of the array size */
76 	names = (struct freebsd11_dirent **)malloc(
77 	    arraysz * sizeof(struct freebsd11_dirent *));
78 	if (names == NULL)
79 		goto fail;
80 
81 	while ((d = freebsd11_readdir(dirp)) != NULL) {
82 		if (select != NULL && !SELECT(d))
83 			continue;	/* just selected names */
84 		/*
85 		 * Make a minimum size copy of the data
86 		 */
87 		p = (struct freebsd11_dirent *)malloc(FREEBSD11_DIRSIZ(d));
88 		if (p == NULL)
89 			goto fail;
90 		p->d_fileno = d->d_fileno;
91 		p->d_type = d->d_type;
92 		p->d_reclen = d->d_reclen;
93 		p->d_namlen = d->d_namlen;
94 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
95 		/*
96 		 * Check to make sure the array has space left and
97 		 * realloc the maximum size.
98 		 */
99 		if (numitems >= arraysz) {
100 			struct freebsd11_dirent **names2;
101 
102 			names2 = reallocarray(names, arraysz,
103 			    2 * sizeof(struct freebsd11_dirent *));
104 			if (names2 == NULL) {
105 				free(p);
106 				goto fail;
107 			}
108 			names = names2;
109 			arraysz *= 2;
110 		}
111 		names[numitems++] = p;
112 	}
113 	closedir(dirp);
114 	if (numitems && dcomp != NULL)
115 		qsort_r(names, numitems, sizeof(struct freebsd11_dirent *),
116 		    freebsd11_scandir_thunk_cmp, &dcomp);
117 	*namelist = names;
118 	return (numitems);
119 
120 fail:
121 	while (numitems > 0)
122 		free(names[--numitems]);
123 	free(names);
124 	closedir(dirp);
125 	return (-1);
126 }
127 
128 /*
129  * Alphabetic order comparison routine for those who want it.
130  * POSIX 2008 requires that alphasort() uses strcoll().
131  */
132 int
133 freebsd11_alphasort(const struct freebsd11_dirent **d1,
134     const struct freebsd11_dirent **d2)
135 {
136 
137 	return (strcoll((*d1)->d_name, (*d2)->d_name));
138 }
139 
140 static int
141 freebsd11_scandir_thunk_cmp(const void *p1, const void *p2, void *thunk)
142 {
143 	int (*dc)(const struct freebsd11_dirent **, const struct
144 	    freebsd11_dirent **);
145 
146 	dc = *(int (**)(const struct freebsd11_dirent **,
147 	    const struct freebsd11_dirent **))thunk;
148 	return (dc((const struct freebsd11_dirent **)p1,
149 	    (const struct freebsd11_dirent **)p2));
150 }
151 
152 __sym_compat(alphasort, freebsd11_alphasort, FBSD_1.0);
153 __sym_compat(scandir, freebsd11_scandir, FBSD_1.0);
154