X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/9385eb3d10ebe5eb398c52040ec3dbfba9b0cdcf..HEAD:/gen/FreeBSD/scandir.c?ds=sidebyside diff --git a/gen/FreeBSD/scandir.c b/gen/FreeBSD/scandir.c index cf79bce..b47983e 100644 --- a/gen/FreeBSD/scandir.c +++ b/gen/FreeBSD/scandir.c @@ -10,10 +10,6 @@ * 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. @@ -35,7 +31,7 @@ static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94"; #endif /* LIBC_SCCS and not lint */ #include -__FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.7 2002/02/01 01:32:19 obrien Exp $"); +__FBSDID("$FreeBSD$"); /* * Scan the directory dirname calling select to make a list of selected @@ -45,8 +41,6 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.7 2002/02/01 01:32:19 obrien */ #include "namespace.h" -#include -#include #include #include #include @@ -64,34 +58,38 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.7 2002/02/01 01:32:19 obrien (((dp)->d_namlen + 1 + 3) &~ 3)) int -scandir(dirname, namelist, select, dcomp) - const char *dirname; - struct dirent ***namelist; - int (*select)(struct dirent *); - int (*dcomp)(const void *, const void *); +#ifdef I_AM_SCANDIR_B +scandir_b(const char *dirname, struct dirent ***namelist, + int (^select)(const struct dirent *), + int (^_dcomp)(const struct dirent **, const struct dirent **)) +#else +scandir(const char *dirname, struct dirent ***namelist, + int (*select)(const struct dirent *), + int (*_dcomp)(const struct dirent **, const struct dirent **)) +#endif { struct dirent *d, *p, **names = NULL; size_t nitems = 0; - struct stat stb; long arraysz; DIR *dirp; + /* see */ +#ifdef I_AM_SCANDIR_B + int (^dcomp)(const void *, const void *) = (void *)_dcomp; +#else + int (*dcomp)(const void *, const void *) = (void *)_dcomp; +#endif + 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; while ((d = readdir(dirp)) != NULL) { - if (select != NULL && !(*select)(d)) + if (select != NULL && !select(d)) continue; /* just selected names */ /* * Make a minimum size copy of the data @@ -109,42 +107,47 @@ scandir(dirname, namelist, select, dcomp) * 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; } closedir(dirp); if (nitems && dcomp != NULL) +#ifdef I_AM_SCANDIR_B + qsort_b(names, nitems, sizeof(struct dirent *), dcomp); +#else qsort(names, nitems, sizeof(struct dirent *), dcomp); +#endif *namelist = names; - return(nitems); + return (int) (nitems); fail: while (nitems > 0) free(names[--nitems]); free(names); closedir(dirp); - return -1; + return (-1); } +#ifndef I_AM_SCANDIR_B /* * Alphabetic order comparison routine for those who want it. + * POSIX 2008 requires that alphasort() uses strcoll(). */ int -alphasort(d1, d2) - const void *d1; - const void *d2; +alphasort(const struct dirent **d1, const struct dirent **d2) { - return(strcmp((*(struct dirent **)d1)->d_name, - (*(struct dirent **)d2)->d_name)); + + return (strcoll((*d1)->d_name, (*d2)->d_name)); } +#endif // I_AM_SCANDIR_B +