2000-04-16 18:43:56 +04:00
|
|
|
/* $NetBSD: scandir.c,v 1.18 2000/04/16 14:43:57 mrg Exp $ */
|
1995-02-25 11:50:56 +03:00
|
|
|
|
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)
|
1995-02-25 11:50:56 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94";
|
|
|
|
#else
|
2000-04-16 18:43:56 +04:00
|
|
|
__RCSID("$NetBSD: scandir.c,v 1.18 2000/04/16 14:43:57 mrg Exp $");
|
1995-02-25 11:50:56 +03:00
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#include "namespace.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#ifdef __weak_alias
|
2000-01-23 01:19:07 +03:00
|
|
|
__weak_alias(scandir,_scandir)
|
|
|
|
__weak_alias(alphasort,_alphasort)
|
1997-07-21 18:06:24 +04:00
|
|
|
#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;
|
2000-04-16 18:43:56 +04:00
|
|
|
int (*select) __P((const struct dirent *));
|
1993-03-21 12:45:37 +03:00
|
|
|
int (*dcomp) __P((const void *, const void *));
|
|
|
|
{
|
1998-02-03 21:23:37 +03:00
|
|
|
struct dirent *d, *p, **names;
|
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;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(dirname != NULL);
|
|
|
|
_DIAGASSERT(namelist != NULL);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((dirp = opendir(dirname)) == NULL)
|
|
|
|
return(-1);
|
|
|
|
if (fstat(dirp->dd_fd, &stb) < 0)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
nitems = 0;
|
|
|
|
while ((d = readdir(dirp)) != NULL) {
|
|
|
|
if (select != NULL && !(*select)(d))
|
|
|
|
continue; /* just selected names */
|
|
|
|
/*
|
|
|
|
* Make a minimum size copy of the data
|
|
|
|
*/
|
|
|
|
p = (struct dirent *)malloc(DIRSIZ(d));
|
|
|
|
if (p == NULL)
|
|
|
|
return(-1);
|
1997-08-03 10:19:40 +04:00
|
|
|
p->d_fileno = d->d_fileno;
|
1993-03-21 12:45:37 +03:00
|
|
|
p->d_reclen = d->d_reclen;
|
1997-08-03 10:19:40 +04:00
|
|
|
p->d_type = d->d_type;
|
1993-03-21 12:45:37 +03:00
|
|
|
p->d_namlen = d->d_namlen;
|
1998-08-26 04:38:39 +04:00
|
|
|
memmove(p->d_name, d->d_name, (size_t)(p->d_namlen + 1));
|
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) {
|
|
|
|
if (fstat(dirp->dd_fd, &stb) < 0)
|
|
|
|
return(-1); /* just might have grown */
|
1998-11-13 15:31:50 +03:00
|
|
|
arraysz = (size_t)(stb.st_size / 12);
|
|
|
|
names = realloc(names,
|
|
|
|
arraysz * sizeof(struct dirent *));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (names == NULL)
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
names[nitems-1] = p;
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if (nitems && dcomp != NULL)
|
|
|
|
qsort(names, nitems, sizeof(struct dirent *), dcomp);
|
|
|
|
*namelist = names;
|
|
|
|
return(nitems);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Alphabetic order comparison routine for those who want it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
alphasort(d1, d2)
|
|
|
|
const void *d1;
|
|
|
|
const void *d2;
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(d1 != NULL);
|
|
|
|
_DIAGASSERT(d2 != NULL);
|
|
|
|
|
1998-11-13 15:31:50 +03:00
|
|
|
return(strcmp((*(const struct dirent *const *)d1)->d_name,
|
|
|
|
(*(const struct dirent *const *)d2)->d_name));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|