1 /* Copyright (C) 1992-1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Modified for uClibc by Erik Andersen
30 #include <sys/types.h>
31 #include "dirstream.h"
33 /* Experimentally off - libc_hidden_proto(memcpy) */
34 libc_hidden_proto(opendir)
35 libc_hidden_proto(closedir)
36 libc_hidden_proto(qsort)
37 libc_hidden_proto(readdir64)
39 int scandir64(const char *dir, struct dirent64 ***namelist,
40 int (*selector) (const struct dirent64 *),
41 int (*compar) (const void *, const void *))
43 DIR *dp = opendir (dir);
44 struct dirent64 *current;
45 struct dirent64 **names = NULL;
46 size_t names_size = 0, pos;
56 while ((current = readdir64 (dp)) != NULL)
57 if (selector == NULL || (*selector) (current))
59 struct dirent64 *vnew;
62 /* Ignore errors from selector or readdir64 */
65 if (unlikely(pos == names_size))
67 struct dirent64 **new;
72 new = (struct dirent64 **) realloc (names, names_size * sizeof (struct dirent64 *));
78 dsize = ¤t->d_name[_D_ALLOC_NAMLEN (current)] - (char *) current;
79 vnew = (struct dirent64 *) malloc (dsize);
83 names[pos++] = (struct dirent64 *) memcpy (vnew, current, dsize);
86 if (unlikely(errno != 0))
100 /* Sort the list if we have a comparison function to sort with. */
102 qsort (names, pos, sizeof (struct dirent64 *), compar);