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.9 2008/03/16 19:08:53 das Exp $");
+__FBSDID("$FreeBSD$");
/*
* Scan the directory dirname calling select to make a list of selected
(((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 <rdar://problem/10293482> */
+#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);
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
}
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
+