1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2000-2011 Erik Andersen <andersen@uclibc.org>
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
12 #include "dirstream.h"
15 # define __SCANDIR scandir
16 # define __DIRENT_TYPE struct dirent
17 # define __READDIR readdir
20 int __SCANDIR(const char *dir, __DIRENT_TYPE ***namelist,
21 int (*selector) (const __DIRENT_TYPE *),
22 int (*compar) (const __DIRENT_TYPE **, const __DIRENT_TYPE **))
24 DIR *dp = opendir (dir);
25 __DIRENT_TYPE *current;
26 __DIRENT_TYPE **names = NULL;
27 size_t names_size = 0, pos;
37 while ((current = __READDIR (dp)) != NULL) {
38 int use_it = selector == NULL;
42 use_it = (*selector) (current);
43 /* The selector function might have changed errno.
44 * It was zero before and it need to be again to make
45 * the latter tests work. */
54 /* Ignore errors from selector or readdir */
57 if (unlikely(pos == names_size))
64 new = (__DIRENT_TYPE **) realloc (names,
65 names_size * sizeof (__DIRENT_TYPE *));
71 dsize = ¤t->d_name[_D_ALLOC_NAMLEN(current)] - (char*)current;
72 vnew = (__DIRENT_TYPE *) malloc (dsize);
76 names[pos++] = (__DIRENT_TYPE *) memcpy (vnew, current, dsize);
80 if (unlikely(errno != 0))
94 /* Sort the list if we have a comparison function to sort with. */
96 qsort (names, pos, sizeof (__DIRENT_TYPE *), (comparison_fn_t) compar);
100 #if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
101 strong_alias_untyped(scandir,scandir64)