Home | History | Annotate | Line # | Download | only in gen
scandir.c revision 1.25.6.1
      1  1.25.6.1  wrstuden /*	$NetBSD: scandir.c,v 1.25.6.1 2007/09/03 07:01:18 wrstuden 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.25.6.1  wrstuden __RCSID("$NetBSD: scandir.c,v 1.25.6.1 2007/09/03 07:01:18 wrstuden 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.25.6.1  wrstuden /*
     59  1.25.6.1  wrstuden  * Compute an estimate of the number of entries in a directory based on
     60  1.25.6.1  wrstuden  * the file size. Returns the estimated number of entries or 0 on failure.
     61  1.25.6.1  wrstuden  */
     62  1.25.6.1  wrstuden static size_t
     63  1.25.6.1  wrstuden dirsize(int fd, size_t olen)
     64  1.25.6.1  wrstuden {
     65  1.25.6.1  wrstuden 	struct stat stb;
     66  1.25.6.1  wrstuden 	size_t nlen;
     67  1.25.6.1  wrstuden 
     68  1.25.6.1  wrstuden 	if (fstat(fd, &stb) == -1)
     69  1.25.6.1  wrstuden 		return 0;
     70  1.25.6.1  wrstuden 	/*
     71  1.25.6.1  wrstuden 	 * Estimate the array size by taking the size of the directory file
     72  1.25.6.1  wrstuden 	 * and dividing it by a multiple of the minimum size entry.
     73  1.25.6.1  wrstuden 	 */
     74  1.25.6.1  wrstuden 	nlen = (size_t)(stb.st_size / _DIRENT_MINSIZE((struct dirent *)0));
     75  1.25.6.1  wrstuden 	/*
     76  1.25.6.1  wrstuden 	 * If the size turns up 0, switch to an alternate strategy and use the
     77  1.25.6.1  wrstuden 	 * file size as the number of entries like ZFS returns. If that turns
     78  1.25.6.1  wrstuden 	 * out to be 0 too return a minimum of 10 entries, plus the old length.
     79  1.25.6.1  wrstuden 	 */
     80  1.25.6.1  wrstuden 	if (nlen == 0)
     81  1.25.6.1  wrstuden 		nlen = (size_t)(stb.st_size ? stb.st_size : 10);
     82  1.25.6.1  wrstuden 	return olen + nlen;
     83  1.25.6.1  wrstuden }
     84  1.25.6.1  wrstuden 
     85      1.25  christos int
     86  1.25.6.1  wrstuden scandir(const char *dirname, struct dirent ***namelist,
     87  1.25.6.1  wrstuden     int (*selectfn)(const struct dirent *),
     88  1.25.6.1  wrstuden     int (*dcomp)(const void *, const void *))
     89      1.25  christos {
     90      1.25  christos 	struct dirent *d, *p, **names, **newnames;
     91      1.25  christos 	size_t nitems, arraysz;
     92      1.25  christos 	DIR *dirp;
     93      1.25  christos 
     94      1.25  christos 	_DIAGASSERT(dirname != NULL);
     95      1.25  christos 	_DIAGASSERT(namelist != NULL);
     96      1.25  christos 
     97      1.25  christos 	if ((dirp = opendir(dirname)) == NULL)
     98  1.25.6.1  wrstuden 		return -1;
     99  1.25.6.1  wrstuden 
    100  1.25.6.1  wrstuden 	if ((arraysz = dirsize(dirp->dd_fd, 0)) == 0)
    101      1.25  christos 		goto bad;
    102      1.25  christos 
    103  1.25.6.1  wrstuden 	names = malloc(arraysz * sizeof(*names));
    104      1.25  christos 	if (names == NULL)
    105      1.25  christos 		goto bad;
    106      1.25  christos 
    107      1.25  christos 	nitems = 0;
    108      1.25  christos 	while ((d = readdir(dirp)) != NULL) {
    109      1.25  christos 		if (selectfn != NULL && !(*selectfn)(d))
    110      1.25  christos 			continue;	/* just selected names */
    111      1.25  christos 
    112      1.25  christos 		/*
    113      1.25  christos 		 * Check to make sure the array has space left and
    114      1.25  christos 		 * realloc the maximum size.
    115      1.25  christos 		 */
    116      1.25  christos 		if (nitems >= arraysz) {
    117  1.25.6.1  wrstuden 			if ((arraysz = dirsize(dirp->dd_fd, arraysz)) == 0)
    118  1.25.6.1  wrstuden 				goto bad2;
    119  1.25.6.1  wrstuden 			newnames = realloc(names, arraysz * sizeof(*names));
    120      1.25  christos 			if (newnames == NULL)
    121      1.25  christos 				goto bad2;
    122      1.25  christos 			names = newnames;
    123      1.25  christos 		}
    124      1.25  christos 
    125      1.25  christos 		/*
    126      1.25  christos 		 * Make a minimum size copy of the data
    127      1.25  christos 		 */
    128  1.25.6.1  wrstuden 		p = malloc((size_t)_DIRENT_SIZE(d));
    129      1.25  christos 		if (p == NULL)
    130      1.25  christos 			goto bad2;
    131      1.25  christos 		p->d_fileno = d->d_fileno;
    132      1.25  christos 		p->d_reclen = d->d_reclen;
    133      1.25  christos 		p->d_type = d->d_type;
    134      1.25  christos 		p->d_namlen = d->d_namlen;
    135  1.25.6.1  wrstuden 		(void)memmove(p->d_name, d->d_name, (size_t)(p->d_namlen + 1));
    136      1.25  christos 		names[nitems++] = p;
    137      1.25  christos 	}
    138  1.25.6.1  wrstuden 	(void)closedir(dirp);
    139      1.25  christos 	if (nitems && dcomp != NULL)
    140  1.25.6.1  wrstuden 		qsort(names, nitems, sizeof(*names), dcomp);
    141      1.25  christos 	*namelist = names;
    142  1.25.6.1  wrstuden 	return nitems;
    143      1.25  christos 
    144      1.25  christos bad2:
    145      1.25  christos 	while (nitems-- > 0)
    146      1.25  christos 		free(names[nitems]);
    147      1.25  christos 	free(names);
    148      1.25  christos bad:
    149  1.25.6.1  wrstuden 	(void)closedir(dirp);
    150  1.25.6.1  wrstuden 	return -1;
    151      1.25  christos }
    152