* 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.
static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.7 2002/02/01 01:32:19 obrien Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.9 2008/03/16 19:08:53 das Exp $");
/*
* Scan the directory dirname calling select to make a list of selected
*/
#include "namespace.h"
-#include <sys/types.h>
-#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include "un-namespace.h"
/*
- * The DIRSIZ macro is the minimum record length which will hold the directory
+ * The _GENERIC_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.
*/
-#undef DIRSIZ
-#define DIRSIZ(dp) \
- ((sizeof(struct dirent) - sizeof(dp)->d_name) + \
- (((dp)->d_namlen + 1 + 3) &~ 3))
int
-scandir(dirname, namelist, select, dcomp)
+scandir(dirname, namelist, select, _dcomp)
const char *dirname;
struct dirent ***namelist;
- int (*select)(struct dirent *);
- int (*dcomp)(const void *, const void *);
+ int (*select)(const struct dirent *);
+ int (*_dcomp)(const struct dirent **, const struct dirent **);
{
struct dirent *d, *p, **names = NULL;
size_t nitems = 0;
- struct stat stb;
long arraysz;
DIR *dirp;
+ int (*dcomp)(const void *, const void *) = (void *)_dcomp; /* see <rdar://problem/10293482> */
if ((dirp = opendir(dirname)) == NULL)
return(-1);
- if (_fstat(dirp->dd_fd, &stb) < 0)
- goto fail;
- /*
- * estimate the array size by taking the size of the directory file
- * and dividing it by a multiple of the minimum size entry.
- */
- arraysz = (stb.st_size / 24);
+ arraysz = 32; /* initial estimate of the array size */
names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
if (names == NULL)
goto fail;
/*
* Make a minimum size copy of the data
*/
- p = (struct dirent *)malloc(DIRSIZ(d));
+ p = (struct dirent *)malloc(_GENERIC_DIRSIZ(d));
if (p == NULL)
goto fail;
p->d_fileno = d->d_fileno;
* realloc the maximum size.
*/
if (nitems >= arraysz) {
- const int inc = 10; /* increase by this much */
struct dirent **names2;
names2 = (struct dirent **)realloc((char *)names,
- (arraysz + inc) * sizeof(struct dirent *));
+ (arraysz * 2) * sizeof(struct dirent *));
if (names2 == NULL) {
free(p);
goto fail;
}
names = names2;
- arraysz += inc;
+ arraysz *= 2;
}
names[nitems++] = p;
}
*/
int
alphasort(d1, d2)
- const void *d1;
- const void *d2;
+ const struct dirent **d1;
+ const struct dirent **d2;
{
return(strcmp((*(struct dirent **)d1)->d_name,
(*(struct dirent **)d2)->d_name));