Hurd: Fix port deallocation on mknod error.
[glibc.git] / dirent / scandirat.c
blob13bd57416a5cf366a0a11ed586c5364968f0d79c
1 /* Copyright (C) 1992-1998,2000,2002,2003,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <dirent.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <bits/libc-lock.h>
25 #ifndef SCANDIRAT
26 # define SCANDIRAT scandirat
27 # define READDIR __readdir
28 # define DIRENT_TYPE struct dirent
29 #endif
31 #ifndef SKIP_SCANDIR_CANCEL
32 void
33 __scandir_cancel_handler (void *arg)
35 struct scandir_cancel_struct *cp = arg;
36 size_t i;
37 void **v = cp->v;
39 for (i = 0; i < cp->cnt; ++i)
40 free (v[i]);
41 free (v);
42 (void) __closedir (cp->dp);
44 #endif
47 int
48 SCANDIRAT (dfd, dir, namelist, select, cmp)
49 int dfd;
50 const char *dir;
51 DIRENT_TYPE ***namelist;
52 int (*select) (const DIRENT_TYPE *);
53 int (*cmp) (const DIRENT_TYPE **, const DIRENT_TYPE **);
55 DIR *dp = __opendirat (dfd, dir);
56 DIRENT_TYPE **v = NULL;
57 size_t vsize = 0;
58 struct scandir_cancel_struct c;
59 DIRENT_TYPE *d;
60 int save;
62 if (dp == NULL)
63 return -1;
65 save = errno;
66 __set_errno (0);
68 c.dp = dp;
69 c.v = NULL;
70 c.cnt = 0;
71 __libc_cleanup_push (__scandir_cancel_handler, &c);
73 while ((d = READDIR (dp)) != NULL)
75 int use_it = select == NULL;
77 if (! use_it)
79 use_it = select (d);
80 /* The select function might have changed errno. It was
81 zero before and it need to be again to make the latter
82 tests work. */
83 __set_errno (0);
86 if (use_it)
88 DIRENT_TYPE *vnew;
89 size_t dsize;
91 /* Ignore errors from select or readdir */
92 __set_errno (0);
94 if (__builtin_expect (c.cnt == vsize, 0))
96 DIRENT_TYPE **new;
97 if (vsize == 0)
98 vsize = 10;
99 else
100 vsize *= 2;
101 new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v));
102 if (new == NULL)
103 break;
104 v = new;
105 c.v = (void *) v;
108 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
109 vnew = (DIRENT_TYPE *) malloc (dsize);
110 if (vnew == NULL)
111 break;
113 v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
117 if (__builtin_expect (errno, 0) != 0)
119 save = errno;
121 while (c.cnt > 0)
122 free (v[--c.cnt]);
123 free (v);
124 c.cnt = -1;
126 else
128 /* Sort the list if we have a comparison function to sort with. */
129 if (cmp != NULL)
130 qsort (v, c.cnt, sizeof (*v),
131 (int (*) (const void *, const void *)) cmp);
133 *namelist = v;
136 __libc_cleanup_pop (0);
138 (void) __closedir (dp);
139 __set_errno (save);
141 return c.cnt;
143 libc_hidden_def (SCANDIRAT)