Fix ldbl-128ibm asinhl inaccuracy (bug 18020).
[glibc.git] / dirent / scandirat.c
blob004b1527f32d50ce9ce06089bed0497eeb173619
1 /* Copyright (C) 1992-2015 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, see
16 <http://www.gnu.org/licenses/>. */
18 /* We need to avoid the header declaration of scandir64, because
19 the types don't match scandir and then the compiler will
20 complain about the mismatch when we do the alias below. */
21 #define scandirat64 __renamed_scandirat64
23 #include <dirent.h>
25 #undef scandirat64
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <bits/libc-lock.h>
32 #ifndef SCANDIRAT
33 # define SCANDIRAT __scandirat
34 # define READDIR __readdir
35 # define DIRENT_TYPE struct dirent
36 # define SCANDIRAT_WEAK_ALIAS
37 #endif
39 #ifndef SKIP_SCANDIR_CANCEL
40 void
41 __scandir_cancel_handler (void *arg)
43 struct scandir_cancel_struct *cp = arg;
44 size_t i;
45 void **v = cp->v;
47 for (i = 0; i < cp->cnt; ++i)
48 free (v[i]);
49 free (v);
50 (void) __closedir (cp->dp);
52 #endif
55 int
56 SCANDIRAT (dfd, dir, namelist, select, cmp)
57 int dfd;
58 const char *dir;
59 DIRENT_TYPE ***namelist;
60 int (*select) (const DIRENT_TYPE *);
61 int (*cmp) (const DIRENT_TYPE **, const DIRENT_TYPE **);
63 DIR *dp = __opendirat (dfd, dir);
64 DIRENT_TYPE **v = NULL;
65 size_t vsize = 0;
66 struct scandir_cancel_struct c;
67 DIRENT_TYPE *d;
68 int save;
70 if (dp == NULL)
71 return -1;
73 save = errno;
74 __set_errno (0);
76 c.dp = dp;
77 c.v = NULL;
78 c.cnt = 0;
79 __libc_cleanup_push (__scandir_cancel_handler, &c);
81 while ((d = READDIR (dp)) != NULL)
83 int use_it = select == NULL;
85 if (! use_it)
87 use_it = select (d);
88 /* The select function might have changed errno. It was
89 zero before and it need to be again to make the latter
90 tests work. */
91 __set_errno (0);
94 if (use_it)
96 DIRENT_TYPE *vnew;
97 size_t dsize;
99 /* Ignore errors from select or readdir */
100 __set_errno (0);
102 if (__glibc_unlikely (c.cnt == vsize))
104 DIRENT_TYPE **new;
105 if (vsize == 0)
106 vsize = 10;
107 else
108 vsize *= 2;
109 new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v));
110 if (new == NULL)
111 break;
112 v = new;
113 c.v = (void *) v;
116 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
117 vnew = (DIRENT_TYPE *) malloc (dsize);
118 if (vnew == NULL)
119 break;
121 v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
125 if (__builtin_expect (errno, 0) != 0)
127 save = errno;
129 while (c.cnt > 0)
130 free (v[--c.cnt]);
131 free (v);
132 c.cnt = -1;
134 else
136 /* Sort the list if we have a comparison function to sort with. */
137 if (cmp != NULL)
138 qsort (v, c.cnt, sizeof (*v),
139 (int (*) (const void *, const void *)) cmp);
141 *namelist = v;
144 __libc_cleanup_pop (0);
146 (void) __closedir (dp);
147 __set_errno (save);
149 return c.cnt;
151 libc_hidden_def (SCANDIRAT)
152 #ifdef SCANDIRAT_WEAK_ALIAS
153 weak_alias (__scandirat, scandirat)
154 #endif
156 #ifdef _DIRENT_MATCHES_DIRENT64
157 weak_alias (scandirat, scandirat64)
158 #endif