xref: /freebsd/lib/libc/gen/scandir-compat11.c (revision dc36d6f9)
169921123SKonstantin Belousov /*
269921123SKonstantin Belousov  * Copyright (c) 1983, 1993
369921123SKonstantin Belousov  *	The Regents of the University of California.  All rights reserved.
469921123SKonstantin Belousov  *
569921123SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
669921123SKonstantin Belousov  * modification, are permitted provided that the following conditions
769921123SKonstantin Belousov  * are met:
869921123SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
969921123SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1069921123SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1169921123SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1269921123SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1369921123SKonstantin Belousov  * 3. Neither the name of the University nor the names of its contributors
1469921123SKonstantin Belousov  *    may be used to endorse or promote products derived from this software
1569921123SKonstantin Belousov  *    without specific prior written permission.
1669921123SKonstantin Belousov  *
1769921123SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1869921123SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1969921123SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2069921123SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2169921123SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2269921123SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2369921123SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2469921123SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2569921123SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2669921123SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2769921123SKonstantin Belousov  * SUCH DAMAGE.
28c1920558SJohn Baldwin  * From: FreeBSD: head/lib/libc/gen/scandir.c 317372 2017-04-24 14:56:41Z pfg
2969921123SKonstantin Belousov  */
3069921123SKonstantin Belousov 
3169921123SKonstantin Belousov /*
3269921123SKonstantin Belousov  * Scan the directory dirname calling select to make a list of selected
3369921123SKonstantin Belousov  * directory entries then sort using qsort and compare routine dcomp.
3469921123SKonstantin Belousov  * Returns the number of entries and a pointer to a list of pointers to
3569921123SKonstantin Belousov  * struct dirent (through namelist). Returns -1 if there were any errors.
3669921123SKonstantin Belousov  */
3769921123SKonstantin Belousov 
3869921123SKonstantin Belousov #include "namespace.h"
3969921123SKonstantin Belousov #define	_WANT_FREEBSD11_DIRENT
4069921123SKonstantin Belousov #include <dirent.h>
4169921123SKonstantin Belousov #include <stdlib.h>
4269921123SKonstantin Belousov #include <string.h>
4369921123SKonstantin Belousov #include "un-namespace.h"
4469921123SKonstantin Belousov 
4569921123SKonstantin Belousov #include "gen-compat.h"
4669921123SKonstantin Belousov 
47cc321ccdSKonstantin Belousov /*
48cc321ccdSKonstantin Belousov  * scandir_b@FBSD_1.4 was never exported from libc.so.7 due to a
49cc321ccdSKonstantin Belousov  * mistake, so there is no use of exporting it now with some earlier
50cc321ccdSKonstantin Belousov  * symbol version.  As result, we do not need to implement compat
51cc321ccdSKonstantin Belousov  * function freebsd11_scandir_b().
52cc321ccdSKonstantin Belousov  */
53cc321ccdSKonstantin Belousov 
5469921123SKonstantin Belousov #define	SELECT(x)	select(x)
55cc321ccdSKonstantin Belousov 
56af3c7888SEd Schouten static int freebsd11_scandir_thunk_cmp(const void *p1, const void *p2,
57af3c7888SEd Schouten     void *thunk);
5869921123SKonstantin Belousov 
5969921123SKonstantin Belousov 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 **))6069921123SKonstantin Belousov freebsd11_scandir(const char *dirname, struct freebsd11_dirent ***namelist,
6169921123SKonstantin Belousov     int (*select)(const struct freebsd11_dirent *),
6269921123SKonstantin Belousov     int (*dcomp)(const struct freebsd11_dirent **,
6369921123SKonstantin Belousov 	const struct freebsd11_dirent **))
6469921123SKonstantin Belousov {
6569921123SKonstantin Belousov 	struct freebsd11_dirent *d, *p, **names = NULL;
6669921123SKonstantin Belousov 	size_t arraysz, numitems;
6769921123SKonstantin Belousov 	DIR *dirp;
6869921123SKonstantin Belousov 
6969921123SKonstantin Belousov 	if ((dirp = opendir(dirname)) == NULL)
7069921123SKonstantin Belousov 		return(-1);
7169921123SKonstantin Belousov 
7269921123SKonstantin Belousov 	numitems = 0;
7369921123SKonstantin Belousov 	arraysz = 32;	/* initial estimate of the array size */
7469921123SKonstantin Belousov 	names = (struct freebsd11_dirent **)malloc(
7569921123SKonstantin Belousov 	    arraysz * sizeof(struct freebsd11_dirent *));
7669921123SKonstantin Belousov 	if (names == NULL)
7769921123SKonstantin Belousov 		goto fail;
7869921123SKonstantin Belousov 
7969921123SKonstantin Belousov 	while ((d = freebsd11_readdir(dirp)) != NULL) {
8069921123SKonstantin Belousov 		if (select != NULL && !SELECT(d))
8169921123SKonstantin Belousov 			continue;	/* just selected names */
8269921123SKonstantin Belousov 		/*
8369921123SKonstantin Belousov 		 * Make a minimum size copy of the data
8469921123SKonstantin Belousov 		 */
8569921123SKonstantin Belousov 		p = (struct freebsd11_dirent *)malloc(FREEBSD11_DIRSIZ(d));
8669921123SKonstantin Belousov 		if (p == NULL)
8769921123SKonstantin Belousov 			goto fail;
8869921123SKonstantin Belousov 		p->d_fileno = d->d_fileno;
8969921123SKonstantin Belousov 		p->d_type = d->d_type;
9069921123SKonstantin Belousov 		p->d_reclen = d->d_reclen;
9169921123SKonstantin Belousov 		p->d_namlen = d->d_namlen;
9269921123SKonstantin Belousov 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
9369921123SKonstantin Belousov 		/*
9469921123SKonstantin Belousov 		 * Check to make sure the array has space left and
9569921123SKonstantin Belousov 		 * realloc the maximum size.
9669921123SKonstantin Belousov 		 */
9769921123SKonstantin Belousov 		if (numitems >= arraysz) {
9869921123SKonstantin Belousov 			struct freebsd11_dirent **names2;
9969921123SKonstantin Belousov 
10069921123SKonstantin Belousov 			names2 = reallocarray(names, arraysz,
10169921123SKonstantin Belousov 			    2 * sizeof(struct freebsd11_dirent *));
10269921123SKonstantin Belousov 			if (names2 == NULL) {
10369921123SKonstantin Belousov 				free(p);
10469921123SKonstantin Belousov 				goto fail;
10569921123SKonstantin Belousov 			}
10669921123SKonstantin Belousov 			names = names2;
10769921123SKonstantin Belousov 			arraysz *= 2;
10869921123SKonstantin Belousov 		}
10969921123SKonstantin Belousov 		names[numitems++] = p;
11069921123SKonstantin Belousov 	}
11169921123SKonstantin Belousov 	closedir(dirp);
11269921123SKonstantin Belousov 	if (numitems && dcomp != NULL)
11369921123SKonstantin Belousov 		qsort_r(names, numitems, sizeof(struct freebsd11_dirent *),
114af3c7888SEd Schouten 		    freebsd11_scandir_thunk_cmp, &dcomp);
11569921123SKonstantin Belousov 	*namelist = names;
11669921123SKonstantin Belousov 	return (numitems);
11769921123SKonstantin Belousov 
11869921123SKonstantin Belousov fail:
11969921123SKonstantin Belousov 	while (numitems > 0)
12069921123SKonstantin Belousov 		free(names[--numitems]);
12169921123SKonstantin Belousov 	free(names);
12269921123SKonstantin Belousov 	closedir(dirp);
12369921123SKonstantin Belousov 	return (-1);
12469921123SKonstantin Belousov }
12569921123SKonstantin Belousov 
12669921123SKonstantin Belousov /*
12769921123SKonstantin Belousov  * Alphabetic order comparison routine for those who want it.
12869921123SKonstantin Belousov  * POSIX 2008 requires that alphasort() uses strcoll().
12969921123SKonstantin Belousov  */
13069921123SKonstantin Belousov int
freebsd11_alphasort(const struct freebsd11_dirent ** d1,const struct freebsd11_dirent ** d2)13169921123SKonstantin Belousov freebsd11_alphasort(const struct freebsd11_dirent **d1,
13269921123SKonstantin Belousov     const struct freebsd11_dirent **d2)
13369921123SKonstantin Belousov {
13469921123SKonstantin Belousov 
13569921123SKonstantin Belousov 	return (strcoll((*d1)->d_name, (*d2)->d_name));
13669921123SKonstantin Belousov }
13769921123SKonstantin Belousov 
13869921123SKonstantin Belousov static int
freebsd11_scandir_thunk_cmp(const void * p1,const void * p2,void * thunk)139af3c7888SEd Schouten freebsd11_scandir_thunk_cmp(const void *p1, const void *p2, void *thunk)
14069921123SKonstantin Belousov {
14169921123SKonstantin Belousov 	int (*dc)(const struct freebsd11_dirent **, const struct
14269921123SKonstantin Belousov 	    freebsd11_dirent **);
14369921123SKonstantin Belousov 
14469921123SKonstantin Belousov 	dc = *(int (**)(const struct freebsd11_dirent **,
14569921123SKonstantin Belousov 	    const struct freebsd11_dirent **))thunk;
14669921123SKonstantin Belousov 	return (dc((const struct freebsd11_dirent **)p1,
14769921123SKonstantin Belousov 	    (const struct freebsd11_dirent **)p2));
14869921123SKonstantin Belousov }
14969921123SKonstantin Belousov 
15069921123SKonstantin Belousov __sym_compat(alphasort, freebsd11_alphasort, FBSD_1.0);
15169921123SKonstantin Belousov __sym_compat(scandir, freebsd11_scandir, FBSD_1.0);
152