]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/scandir-compat11.c
libc: further adoption of SPDX licensing ID tags.
[FreeBSD/FreeBSD.git] / lib / libc / gen / scandir-compat11.c
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:
30  * $FreeBSD$
31  */
32
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)scandir.c   8.3 (Berkeley) 1/2/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40  * Scan the directory dirname calling select to make a list of selected
41  * directory entries then sort using qsort and compare routine dcomp.
42  * Returns the number of entries and a pointer to a list of pointers to
43  * struct dirent (through namelist). Returns -1 if there were any errors.
44  */
45
46 #include "namespace.h"
47 #define _WANT_FREEBSD11_DIRENT
48 #include <dirent.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "un-namespace.h"
52
53 #include "gen-compat.h"
54
55 #ifdef  I_AM_SCANDIR_B
56 #include "block_abi.h"
57 #define SELECT(x)       CALL_BLOCK(select, x)
58 #ifndef __BLOCKS__
59 void
60 qsort_b(void *, size_t, size_t, void*);
61 #endif
62 #else
63 #define SELECT(x)       select(x)
64 #endif
65
66 static int freebsd11_alphasort_thunk(void *thunk, const void *p1,
67     const void *p2);
68
69 int
70 #ifdef I_AM_SCANDIR_B
71 freebsd11_scandir_b(const char *dirname, struct freebsd11_dirent ***namelist,
72     DECLARE_BLOCK(int, select, const struct freebsd11_dirent *),
73     DECLARE_BLOCK(int, dcomp, const struct freebsd11_dirent **,
74     const struct freebsd11_dirent **))
75 #else
76 freebsd11_scandir(const char *dirname, struct freebsd11_dirent ***namelist,
77     int (*select)(const struct freebsd11_dirent *),
78     int (*dcomp)(const struct freebsd11_dirent **,
79         const struct freebsd11_dirent **))
80 #endif
81 {
82         struct freebsd11_dirent *d, *p, **names = NULL;
83         size_t arraysz, numitems;
84         DIR *dirp;
85
86         if ((dirp = opendir(dirname)) == NULL)
87                 return(-1);
88
89         numitems = 0;
90         arraysz = 32;   /* initial estimate of the array size */
91         names = (struct freebsd11_dirent **)malloc(
92             arraysz * sizeof(struct freebsd11_dirent *));
93         if (names == NULL)
94                 goto fail;
95
96         while ((d = freebsd11_readdir(dirp)) != NULL) {
97                 if (select != NULL && !SELECT(d))
98                         continue;       /* just selected names */
99                 /*
100                  * Make a minimum size copy of the data
101                  */
102                 p = (struct freebsd11_dirent *)malloc(FREEBSD11_DIRSIZ(d));
103                 if (p == NULL)
104                         goto fail;
105                 p->d_fileno = d->d_fileno;
106                 p->d_type = d->d_type;
107                 p->d_reclen = d->d_reclen;
108                 p->d_namlen = d->d_namlen;
109                 bcopy(d->d_name, p->d_name, p->d_namlen + 1);
110                 /*
111                  * Check to make sure the array has space left and
112                  * realloc the maximum size.
113                  */
114                 if (numitems >= arraysz) {
115                         struct freebsd11_dirent **names2;
116
117                         names2 = reallocarray(names, arraysz,
118                             2 * sizeof(struct freebsd11_dirent *));
119                         if (names2 == NULL) {
120                                 free(p);
121                                 goto fail;
122                         }
123                         names = names2;
124                         arraysz *= 2;
125                 }
126                 names[numitems++] = p;
127         }
128         closedir(dirp);
129         if (numitems && dcomp != NULL)
130 #ifdef I_AM_SCANDIR_B
131                 qsort_b(names, numitems, sizeof(struct freebsd11_dirent *),
132                     (void*)dcomp);
133 #else
134                 qsort_r(names, numitems, sizeof(struct freebsd11_dirent *),
135                     &dcomp, freebsd11_alphasort_thunk);
136 #endif
137         *namelist = names;
138         return (numitems);
139
140 fail:
141         while (numitems > 0)
142                 free(names[--numitems]);
143         free(names);
144         closedir(dirp);
145         return (-1);
146 }
147
148 /*
149  * Alphabetic order comparison routine for those who want it.
150  * POSIX 2008 requires that alphasort() uses strcoll().
151  */
152 int
153 freebsd11_alphasort(const struct freebsd11_dirent **d1,
154     const struct freebsd11_dirent **d2)
155 {
156
157         return (strcoll((*d1)->d_name, (*d2)->d_name));
158 }
159
160 static int
161 freebsd11_alphasort_thunk(void *thunk, const void *p1, const void *p2)
162 {
163         int (*dc)(const struct freebsd11_dirent **, const struct
164             freebsd11_dirent **);
165
166         dc = *(int (**)(const struct freebsd11_dirent **,
167             const struct freebsd11_dirent **))thunk;
168         return (dc((const struct freebsd11_dirent **)p1,
169             (const struct freebsd11_dirent **)p2));
170 }
171
172 __sym_compat(alphasort, freebsd11_alphasort, FBSD_1.0);
173 __sym_compat(scandir, freebsd11_scandir, FBSD_1.0);
174 __sym_compat(scandir_b, freebsd11_scandir_b, FBSD_1.4);