X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/1f2f436a38f7ae2d39a943ad2898d8fed4ed2e58..6dccf0e0b5e80b7b6176e8d332e646175431bb3d:/gen/FreeBSD/scandir.c diff --git a/gen/FreeBSD/scandir.c b/gen/FreeBSD/scandir.c index e21bd09..b47983e 100644 --- a/gen/FreeBSD/scandir.c +++ b/gen/FreeBSD/scandir.c @@ -31,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.9 2008/03/16 19:08:53 das Exp $"); +__FBSDID("$FreeBSD$"); /* * Scan the directory dirname calling select to make a list of selected @@ -58,17 +58,28 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.9 2008/03/16 19:08:53 das Exp (((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; 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); @@ -78,7 +89,7 @@ scandir(dirname, namelist, select, dcomp) 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 @@ -111,26 +122,32 @@ scandir(dirname, namelist, select, dcomp) } 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 +