--- scandir_b.c 2009-11-07 14:51:38.000000000 -0800 +++ scandir_b.c 2009-11-07 18:46:38.000000000 -0800 @@ -47,22 +47,18 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/sca #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_b(dirname, namelist, select, dcomp) const char *dirname; struct dirent ***namelist; - int (*select)(struct dirent *); - int (*dcomp)(const void *, const void *); + int (^select)(struct dirent *); + int (^dcomp)(const void *, const void *); { struct dirent *d, *p, **names = NULL; size_t nitems = 0; @@ -78,12 +74,12 @@ 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 */ - p = (struct dirent *)malloc(DIRSIZ(d)); + p = (struct dirent *)malloc(_GENERIC_DIRSIZ(d)); if (p == NULL) goto fail; p->d_fileno = d->d_fileno; @@ -111,7 +107,7 @@ scandir(dirname, namelist, select, dcomp } closedir(dirp); if (nitems && dcomp != NULL) - qsort(names, nitems, sizeof(struct dirent *), dcomp); + qsort_b(names, nitems, sizeof(struct dirent *), dcomp); *namelist = names; return(nitems); @@ -122,15 +118,3 @@ fail: closedir(dirp); return -1; } - -/* - * Alphabetic order comparison routine for those who want it. - */ -int -alphasort(d1, d2) - const void *d1; - const void *d2; -{ - return(strcmp((*(struct dirent **)d1)->d_name, - (*(struct dirent **)d2)->d_name)); -}