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