Home | History | Annotate | Line # | Download | only in gen
scandir.c revision 1.27.14.1
      1  1.27.14.1  pgoyette /*	$NetBSD: scandir.c,v 1.27.14.1 2017/01/07 08:56:03 pgoyette Exp $	*/
      2        1.6       cgd 
      3       1.25  christos /*
      4       1.25  christos  * Copyright (c) 1983, 1993
      5       1.25  christos  *	The Regents of the University of California.  All rights reserved.
      6       1.25  christos  *
      7       1.25  christos  * Redistribution and use in source and binary forms, with or without
      8       1.25  christos  * modification, are permitted provided that the following conditions
      9       1.25  christos  * are met:
     10       1.25  christos  * 1. Redistributions of source code must retain the above copyright
     11       1.25  christos  *    notice, this list of conditions and the following disclaimer.
     12       1.25  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.25  christos  *    notice, this list of conditions and the following disclaimer in the
     14       1.25  christos  *    documentation and/or other materials provided with the distribution.
     15       1.25  christos  * 3. Neither the name of the University nor the names of its contributors
     16       1.25  christos  *    may be used to endorse or promote products derived from this software
     17       1.25  christos  *    without specific prior written permission.
     18       1.25  christos  *
     19       1.25  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.25  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.25  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.25  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.25  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.25  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.25  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.25  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.25  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.25  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.25  christos  * SUCH DAMAGE.
     30       1.25  christos  */
     31       1.25  christos 
     32       1.25  christos #include <sys/cdefs.h>
     33       1.25  christos #if defined(LIBC_SCCS) && !defined(lint)
     34       1.25  christos #if 0
     35       1.25  christos static char sccsid[] = "@(#)scandir.c	8.3 (Berkeley) 1/2/94";
     36       1.25  christos #else
     37  1.27.14.1  pgoyette __RCSID("$NetBSD: scandir.c,v 1.27.14.1 2017/01/07 08:56:03 pgoyette Exp $");
     38       1.25  christos #endif
     39       1.25  christos #endif /* LIBC_SCCS and not lint */
     40       1.25  christos 
     41       1.25  christos /*
     42       1.25  christos  * Scan the directory dirname calling selectfn to make a list of selected
     43       1.25  christos  * directory entries then sort using qsort and compare routine dcomp.
     44       1.25  christos  * Returns the number of entries and a pointer to a list of pointers to
     45       1.25  christos  * struct dirent (through namelist). Returns -1 if there were any errors.
     46       1.25  christos  */
     47       1.25  christos 
     48       1.25  christos #include "namespace.h"
     49       1.25  christos #include <sys/types.h>
     50        1.1       cgd #include <sys/stat.h>
     51       1.15     lukem 
     52       1.25  christos #include <assert.h>
     53       1.25  christos #include <errno.h>
     54       1.25  christos #include <dirent.h>
     55       1.25  christos #include <stdlib.h>
     56       1.25  christos #include <string.h>
     57       1.25  christos 
     58       1.26  christos /*
     59       1.26  christos  * Compute an estimate of the number of entries in a directory based on
     60       1.26  christos  * the file size. Returns the estimated number of entries or 0 on failure.
     61       1.26  christos  */
     62       1.26  christos static size_t
     63       1.26  christos dirsize(int fd, size_t olen)
     64       1.26  christos {
     65       1.26  christos 	struct stat stb;
     66       1.26  christos 	size_t nlen;
     67       1.26  christos 
     68       1.26  christos 	if (fstat(fd, &stb) == -1)
     69       1.26  christos 		return 0;
     70       1.26  christos 	/*
     71       1.26  christos 	 * Estimate the array size by taking the size of the directory file
     72       1.26  christos 	 * and dividing it by a multiple of the minimum size entry.
     73       1.26  christos 	 */
     74       1.26  christos 	nlen = (size_t)(stb.st_size / _DIRENT_MINSIZE((struct dirent *)0));
     75       1.26  christos 	/*
     76       1.26  christos 	 * If the size turns up 0, switch to an alternate strategy and use the
     77       1.26  christos 	 * file size as the number of entries like ZFS returns. If that turns
     78       1.26  christos 	 * out to be 0 too return a minimum of 10 entries, plus the old length.
     79       1.26  christos 	 */
     80       1.26  christos 	if (nlen == 0)
     81       1.26  christos 		nlen = (size_t)(stb.st_size ? stb.st_size : 10);
     82       1.26  christos 	return olen + nlen;
     83       1.26  christos }
     84       1.26  christos 
     85  1.27.14.1  pgoyette #ifndef COMPARARG
     86  1.27.14.1  pgoyette #define COMPARARG struct dirent **
     87  1.27.14.1  pgoyette #endif
     88  1.27.14.1  pgoyette 
     89       1.25  christos int
     90       1.26  christos scandir(const char *dirname, struct dirent ***namelist,
     91       1.26  christos     int (*selectfn)(const struct dirent *),
     92  1.27.14.1  pgoyette     int (*dcomp)(const COMPARARG, const COMPARARG))
     93       1.25  christos {
     94       1.25  christos 	struct dirent *d, *p, **names, **newnames;
     95       1.25  christos 	size_t nitems, arraysz;
     96       1.25  christos 	DIR *dirp;
     97       1.25  christos 
     98       1.25  christos 	_DIAGASSERT(dirname != NULL);
     99       1.25  christos 	_DIAGASSERT(namelist != NULL);
    100       1.25  christos 
    101       1.25  christos 	if ((dirp = opendir(dirname)) == NULL)
    102       1.26  christos 		return -1;
    103       1.26  christos 
    104       1.26  christos 	if ((arraysz = dirsize(dirp->dd_fd, 0)) == 0)
    105       1.25  christos 		goto bad;
    106       1.25  christos 
    107       1.26  christos 	names = malloc(arraysz * sizeof(*names));
    108       1.25  christos 	if (names == NULL)
    109       1.25  christos 		goto bad;
    110       1.25  christos 
    111       1.25  christos 	nitems = 0;
    112       1.25  christos 	while ((d = readdir(dirp)) != NULL) {
    113       1.25  christos 		if (selectfn != NULL && !(*selectfn)(d))
    114       1.25  christos 			continue;	/* just selected names */
    115       1.25  christos 
    116       1.25  christos 		/*
    117       1.25  christos 		 * Check to make sure the array has space left and
    118       1.25  christos 		 * realloc the maximum size.
    119       1.25  christos 		 */
    120       1.25  christos 		if (nitems >= arraysz) {
    121       1.26  christos 			if ((arraysz = dirsize(dirp->dd_fd, arraysz)) == 0)
    122       1.26  christos 				goto bad2;
    123       1.26  christos 			newnames = realloc(names, arraysz * sizeof(*names));
    124       1.25  christos 			if (newnames == NULL)
    125       1.25  christos 				goto bad2;
    126       1.25  christos 			names = newnames;
    127       1.25  christos 		}
    128       1.25  christos 
    129       1.25  christos 		/*
    130       1.25  christos 		 * Make a minimum size copy of the data
    131       1.25  christos 		 */
    132       1.26  christos 		p = malloc((size_t)_DIRENT_SIZE(d));
    133       1.25  christos 		if (p == NULL)
    134       1.25  christos 			goto bad2;
    135       1.25  christos 		p->d_fileno = d->d_fileno;
    136       1.25  christos 		p->d_reclen = d->d_reclen;
    137       1.25  christos 		p->d_type = d->d_type;
    138       1.25  christos 		p->d_namlen = d->d_namlen;
    139       1.26  christos 		(void)memmove(p->d_name, d->d_name, (size_t)(p->d_namlen + 1));
    140       1.25  christos 		names[nitems++] = p;
    141       1.25  christos 	}
    142       1.26  christos 	(void)closedir(dirp);
    143       1.25  christos 	if (nitems && dcomp != NULL)
    144  1.27.14.1  pgoyette 		qsort(names, nitems, sizeof(*names),
    145  1.27.14.1  pgoyette 		      (int (*)(const void *, const void *))dcomp);
    146       1.25  christos 	*namelist = names;
    147       1.27  christos 	_DIAGASSERT(__type_fit(int, nitems));
    148       1.27  christos 	return (int)nitems;
    149       1.25  christos 
    150       1.25  christos bad2:
    151       1.25  christos 	while (nitems-- > 0)
    152       1.25  christos 		free(names[nitems]);
    153       1.25  christos 	free(names);
    154       1.25  christos bad:
    155       1.26  christos 	(void)closedir(dirp);
    156       1.26  christos 	return -1;
    157       1.25  christos }
    158