NetBSD/lib/libc/gen/scandir.c

170 lines
5.1 KiB
C
Raw Normal View History

2001-11-21 03:48:28 +03:00
/* $NetBSD: scandir.c,v 1.21 2001/11/21 00:48:28 enami Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-07-27 09:37:08 +04:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-07-13 23:45:36 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94";
#else
2001-11-21 03:48:28 +03:00
__RCSID("$NetBSD: scandir.c,v 1.21 2001/11/21 00:48:28 enami Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* LIBC_SCCS and not lint */
/*
* Scan the directory dirname calling select to make a list of selected
* directory entries then sort using qsort and compare routine dcomp.
* Returns the number of entries and a pointer to a list of pointers to
* struct dirent (through namelist). Returns -1 if there were any errors.
*/
#include "namespace.h"
1993-03-21 12:45:37 +03:00
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
1993-03-21 12:45:37 +03:00
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#ifdef __weak_alias
__weak_alias(scandir,_scandir)
__weak_alias(alphasort,_alphasort)
#endif
1993-03-21 12:45:37 +03:00
/*
1994-07-27 09:37:08 +04:00
* The DIRSIZ macro is the minimum record length which will hold the directory
* entry. This requires the amount of space in struct dirent without the
* d_name field, plus enough space for the name and a terminating nul byte
* (dp->d_namlen + 1), rounded up to a 4 byte boundary.
1993-03-21 12:45:37 +03:00
*/
#undef DIRSIZ
1994-07-27 09:37:08 +04:00
#define DIRSIZ(dp) \
((sizeof(struct dirent) - sizeof(dp)->d_name) + \
(((dp)->d_namlen + 1 + 3) &~ 3))
1993-03-21 12:45:37 +03:00
int
scandir(dirname, namelist, select, dcomp)
const char *dirname;
struct dirent ***namelist;
int (*select) __P((const struct dirent *));
1993-03-21 12:45:37 +03:00
int (*dcomp) __P((const void *, const void *));
{
struct dirent *d, *p, **names, **newnames;
1998-11-13 15:31:50 +03:00
size_t nitems, arraysz;
1993-03-21 12:45:37 +03:00
struct stat stb;
DIR *dirp;
_DIAGASSERT(dirname != NULL);
_DIAGASSERT(namelist != NULL);
1993-03-21 12:45:37 +03:00
if ((dirp = opendir(dirname)) == NULL)
2001-11-21 03:48:28 +03:00
return (-1);
1993-03-21 12:45:37 +03:00
if (fstat(dirp->dd_fd, &stb) < 0)
2001-10-25 06:02:02 +04:00
goto bad;
1993-03-21 12:45:37 +03:00
/*
* estimate the array size by taking the size of the directory file
* and dividing it by a multiple of the minimum size entry.
*/
1998-11-13 15:31:50 +03:00
arraysz = (size_t)(stb.st_size / 24);
names = malloc(arraysz * sizeof(struct dirent *));
1993-03-21 12:45:37 +03:00
if (names == NULL)
2001-10-25 06:02:02 +04:00
goto bad;
1993-03-21 12:45:37 +03:00
nitems = 0;
1993-03-21 12:45:37 +03:00
while ((d = readdir(dirp)) != NULL) {
if (select != NULL && !(*select)(d))
continue; /* just selected names */
1993-03-21 12:45:37 +03:00
/*
* Check to make sure the array has space left and
* realloc the maximum size.
*/
if (nitems >= arraysz) {
1993-03-21 12:45:37 +03:00
if (fstat(dirp->dd_fd, &stb) < 0)
2001-10-25 06:02:02 +04:00
goto bad2; /* just might have grown */
1998-11-13 15:31:50 +03:00
arraysz = (size_t)(stb.st_size / 12);
2001-10-25 06:02:02 +04:00
newnames = realloc(names,
1998-11-13 15:31:50 +03:00
arraysz * sizeof(struct dirent *));
2001-10-25 06:02:02 +04:00
if (newnames == NULL)
goto bad2;
names = newnames;
1993-03-21 12:45:37 +03:00
}
/*
* Make a minimum size copy of the data
*/
p = (struct dirent *)malloc(DIRSIZ(d));
if (p == NULL)
goto bad2;
p->d_fileno = d->d_fileno;
p->d_reclen = d->d_reclen;
p->d_type = d->d_type;
p->d_namlen = d->d_namlen;
memmove(p->d_name, d->d_name, (size_t)(p->d_namlen + 1));
names[nitems++] = p;
1993-03-21 12:45:37 +03:00
}
closedir(dirp);
if (nitems && dcomp != NULL)
qsort(names, nitems, sizeof(struct dirent *), dcomp);
*namelist = names;
2001-11-21 03:48:28 +03:00
return (nitems);
2001-10-25 06:02:02 +04:00
bad2:
while (nitems-- > 0)
free(names[nitems]);
free(names);
2001-10-25 06:02:02 +04:00
bad:
closedir(dirp);
2001-11-21 03:48:28 +03:00
return (-1);
1993-03-21 12:45:37 +03:00
}
/*
* Alphabetic order comparison routine for those who want it.
*/
int
alphasort(d1, d2)
const void *d1;
const void *d2;
{
_DIAGASSERT(d1 != NULL);
_DIAGASSERT(d2 != NULL);
2001-11-21 03:48:28 +03:00
return (strcmp((*(const struct dirent *const *)d1)->d_name,
1998-11-13 15:31:50 +03:00
(*(const struct dirent *const *)d2)->d_name));
1993-03-21 12:45:37 +03:00
}