* sysdeps/unix/sysv/linux/alpha/clone.S: Use HIDDEN_JUMPTARGET. ...
[glibc.git] / dirent / scandir.c
blob748a490739e39ed4f232eaf60f3a04dc09af0e08
1 /* Copyright (C) 1992-1998, 2000, 2002, 2003 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
17 02111-1307 USA. */
19 #include <dirent.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
24 #ifndef SCANDIR
25 #define SCANDIR scandir
26 #define READDIR __readdir
27 #define DIRENT_TYPE struct dirent
28 #endif
31 int
32 SCANDIR (dir, namelist, select, cmp)
33 const char *dir;
34 DIRENT_TYPE ***namelist;
35 int (*select) (const DIRENT_TYPE *);
36 int (*cmp) (const void *, const void *);
38 DIR *dp = __opendir (dir);
39 DIRENT_TYPE **v = NULL;
40 size_t vsize = 0, i;
41 DIRENT_TYPE *d;
42 int save;
44 if (dp == NULL)
45 return -1;
47 save = errno;
48 __set_errno (0);
50 i = 0;
51 while ((d = READDIR (dp)) != NULL)
53 int use_it = select == NULL;
55 if (! use_it)
57 use_it = select (d);
58 /* The select function might have changed errno. It was
59 zero before and it need to be again to make the latter
60 tests work. */
61 __set_errno (0);
64 if (use_it)
66 DIRENT_TYPE *vnew;
67 size_t dsize;
69 /* Ignore errors from select or readdir */
70 __set_errno (0);
72 if (__builtin_expect (i == vsize, 0))
74 DIRENT_TYPE **new;
75 if (vsize == 0)
76 vsize = 10;
77 else
78 vsize *= 2;
79 new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v));
80 if (new == NULL)
81 break;
82 v = new;
85 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
86 vnew = (DIRENT_TYPE *) malloc (dsize);
87 if (vnew == NULL)
88 break;
90 v[i++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
94 if (__builtin_expect (errno, 0) != 0)
96 save = errno;
98 while (i > 0)
99 free (v[--i]);
100 free (v);
102 i = -1;
104 else
106 /* Sort the list if we have a comparison function to sort with. */
107 if (cmp != NULL)
108 qsort (v, i, sizeof (*v), cmp);
110 *namelist = v;
113 (void) __closedir (dp);
114 __set_errno (save);
116 return i;