8 int scandir(const char *path
, struct dirent
***res
,
9 int (*sel
)(const struct dirent
*),
10 int (*cmp
)(const struct dirent
**, const struct dirent
**))
12 DIR *d
= opendir(path
);
13 struct dirent
*de
, **names
=0, **tmp
;
15 int old_errno
= errno
;
19 while ((errno
=0), (de
= readdir(d
))) {
20 if (sel
&& !sel(de
)) continue;
23 if (len
> SIZE_MAX
/sizeof *names
) break;
24 tmp
= realloc(names
, len
* sizeof *names
);
28 names
[cnt
] = malloc(de
->d_reclen
);
29 if (!names
[cnt
]) break;
30 memcpy(names
[cnt
++], de
, de
->d_reclen
);
36 if (names
) while (cnt
-->0) free(names
[cnt
]);
42 if (cmp
) qsort(names
, cnt
, sizeof *names
, (int (*)(const void *, const void *))cmp
);
47 weak_alias(scandir
, scandir64
);