1 /* Copyright (C) 1992-1998, 2000, 2002-2003, 2009-2012 Free Software
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
26 # include <bits/libc-lock.h>
29 #if ! defined __builtin_expect && __GNUC__ < 3
30 # define __builtin_expect(expr, expected) (expr)
35 #ifndef _D_EXACT_NAMLEN
36 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
38 #ifndef _D_ALLOC_NAMLEN
39 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
44 # define SCANDIR scandir
45 # define READDIR __readdir
46 # define DIRENT_TYPE struct dirent
49 # define SCANDIR scandir
50 # define READDIR readdir
51 # define DIRENT_TYPE struct dirent
52 # define __opendir opendir
53 # define __closedir closedir
54 # define __set_errno(val) errno = (val)
56 /* The results of opendir() in this file are not used with dirfd and fchdir,
57 and we do not leak fds to any single-threaded code that could use stdio,
58 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
59 FIXME - if the kernel ever adds support for multi-thread safety for
60 avoiding standard fds, then we should use opendir_safer. */
65 #ifndef SCANDIR_CANCEL
66 # define SCANDIR_CANCEL
67 struct scandir_cancel_struct
76 cancel_handler (void *arg
)
78 struct scandir_cancel_struct
*cp
= arg
;
82 for (i
= 0; i
< cp
->cnt
; ++i
)
85 (void) __closedir (cp
->dp
);
92 SCANDIR (const char *dir
,
93 DIRENT_TYPE
***namelist
,
94 int (*select
) (const DIRENT_TYPE
*),
95 int (*cmp
) (const DIRENT_TYPE
**, const DIRENT_TYPE
**))
97 DIR *dp
= __opendir (dir
);
98 DIRENT_TYPE
**v
= NULL
;
100 struct scandir_cancel_struct c
;
114 __libc_cleanup_push (cancel_handler
, &c
);
117 while ((d
= READDIR (dp
)) != NULL
)
119 int use_it
= select
== NULL
;
124 /* The select function might have changed errno. It was
125 zero before and it need to be again to make the latter
135 /* Ignore errors from select or readdir */
138 if (__builtin_expect (c
.cnt
== vsize
, 0))
145 new = (DIRENT_TYPE
**) realloc (v
, vsize
* sizeof (*v
));
152 dsize
= &d
->d_name
[_D_ALLOC_NAMLEN (d
)] - (char *) d
;
153 vnew
= (DIRENT_TYPE
*) malloc (dsize
);
157 v
[c
.cnt
++] = (DIRENT_TYPE
*) memcpy (vnew
, d
, dsize
);
161 if (__builtin_expect (errno
, 0) != 0)
172 /* Sort the list if we have a comparison function to sort with. */
174 qsort (v
, c
.cnt
, sizeof (*v
), (int (*) (const void *, const void *)) cmp
);
180 __libc_cleanup_pop (0);
183 (void) __closedir (dp
);