]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/misc/bug-glob2.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / misc / bug-glob2.c
1 /* Test glob memory management.
2    for the filesystem access functions.
3    Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <errno.h>
21 #include <error.h>
22 #include <dirent.h>
23 #include <glob.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 // #define DEBUG
30 #ifdef DEBUG
31 # define PRINTF(fmt, args...) \
32   do                                    \
33     {                                   \
34       int save_errno = errno;           \
35       printf (fmt, ##args);             \
36       errno = save_errno;               \
37     } while (0)
38 #else
39 # define PRINTF(fmt, args...)
40 #endif
41
42
43 #ifdef GLOB_ALTDIRFUNC
44 static struct
45 {
46   const char *name;
47   int level;
48   int type;
49   mode_t mode;
50 } filesystem[] =
51 {
52   { ".", 1, DT_DIR, 0755 },
53   { "..", 1, DT_DIR, 0755 },
54   { "dir", 1, DT_DIR, 0755 },
55     { ".", 2, DT_DIR, 0755 },
56     { "..", 2, DT_DIR, 0755 },
57     { "readable", 2, DT_DIR, 0755 },
58       { ".", 3, DT_DIR, 0755 },
59       { "..", 3, DT_DIR, 0755 },
60       { "a", 3, DT_REG, 0644 },
61     { "unreadable", 2, DT_DIR, 0111 },
62       { ".", 3, DT_DIR, 0111 },
63       { "..", 3, DT_DIR, 0755 },
64       { "a", 3, DT_REG, 0644 },
65     { "zz-readable", 2, DT_DIR, 0755 },
66       { ".", 3, DT_DIR, 0755 },
67       { "..", 3, DT_DIR, 0755 },
68       { "a", 3, DT_REG, 0644 }
69 };
70 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
71
72
73 typedef struct
74 {
75   int level;
76   int idx;
77   struct dirent d;
78   char room_for_dirent[NAME_MAX];
79 } my_DIR;
80
81
82 static long int
83 find_file (const char *s)
84 {
85   int level = 1;
86   long int idx = 0;
87
88   if (strcmp (s, ".") == 0)
89     return 0;
90
91   if (s[0] == '.' && s[1] == '/')
92     s += 2;
93
94   while (*s != '\0')
95     {
96       char *endp = strchrnul (s, '/');
97
98       PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
99
100       while (idx < nfiles && filesystem[idx].level >= level)
101         {
102           if (filesystem[idx].level == level
103               && memcmp (s, filesystem[idx].name, endp - s) == 0
104               && filesystem[idx].name[endp - s] == '\0')
105             break;
106           ++idx;
107         }
108
109       if (idx == nfiles || filesystem[idx].level < level)
110         {
111           errno = ENOENT;
112           return -1;
113         }
114
115       if (*endp == '\0')
116         return idx + 1;
117
118       if (filesystem[idx].type != DT_DIR
119           && (idx + 1 >= nfiles
120               || filesystem[idx].level >= filesystem[idx + 1].level))
121         {
122           errno = ENOTDIR;
123           return -1;
124         }
125
126       ++idx;
127
128       s = endp + 1;
129       ++level;
130     }
131
132   errno = ENOENT;
133   return -1;
134 }
135
136
137 static void *
138 my_opendir (const char *s)
139 {
140   long int idx = find_file (s);
141   my_DIR *dir;
142
143   if (idx == -1)
144     {
145       PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
146       return NULL;
147     }
148
149   if ((filesystem[idx].mode & 0400) == 0)
150     {
151       errno = EACCES;
152       PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
153       return NULL;
154     }
155
156   dir = (my_DIR *) malloc (sizeof (my_DIR));
157   if (dir == NULL)
158     {
159       printf ("cannot allocate directory handle: %m\n");
160       exit (EXIT_FAILURE);
161     }
162
163   dir->level = filesystem[idx].level;
164   dir->idx = idx;
165
166   PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
167           s, filesystem[idx].level, idx);
168
169   return dir;
170 }
171
172
173 static struct dirent *
174 my_readdir (void *gdir)
175 {
176   my_DIR *dir = gdir;
177
178   if (dir->idx == -1)
179     {
180       PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
181               dir->level, (long int) dir->idx);
182       return NULL;
183     }
184
185   while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
186     ++dir->idx;
187
188   if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
189     {
190       dir->idx = -1;
191       PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
192               dir->level, (long int) dir->idx);
193       return NULL;
194     }
195
196   dir->d.d_ino = dir->idx;
197
198 #ifdef _DIRENT_HAVE_D_TYPE
199   dir->d.d_type = filesystem[dir->idx].type;
200 #endif
201
202   strcpy (dir->d.d_name, filesystem[dir->idx].name);
203
204 #ifdef _DIRENT_HAVE_D_TYPE
205   PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
206           dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
207           dir->d.d_name);
208 #else
209   PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
210           dir->level, (long int) dir->idx, dir->d.d_ino,
211           dir->d.d_name);
212 #endif
213
214   ++dir->idx;
215
216   return &dir->d;
217 }
218
219
220 static void
221 my_closedir (void *dir)
222 {
223   PRINTF ("my_closedir ()\n");
224   free (dir);
225 }
226
227
228 /* We use this function for lstat as well since we don't have any.  */
229 static int
230 my_stat (const char *name, struct stat *st)
231 {
232   long int idx = find_file (name);
233
234   if (idx == -1)
235     {
236       PRINTF ("my_stat (\"%s\", ...) = -1 (%m)\n", name);
237       return -1;
238     }
239
240   memset (st, '\0', sizeof (*st));
241
242   if (filesystem[idx].type == DT_UNKNOWN)
243     st->st_mode = DTTOIF (idx + 1 < nfiles
244                           && filesystem[idx].level < filesystem[idx + 1].level
245                           ? DT_DIR : DT_REG) | filesystem[idx].mode;
246   else
247     st->st_mode = DTTOIF (filesystem[idx].type) | filesystem[idx].mode;
248
249   PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
250
251   return 0;
252 }
253
254
255 static void
256 init_glob_altdirfuncs (glob_t *pglob)
257 {
258   pglob->gl_closedir = my_closedir;
259   pglob->gl_readdir = my_readdir;
260   pglob->gl_opendir = my_opendir;
261   pglob->gl_lstat = my_stat;
262   pglob->gl_stat = my_stat;
263 }
264
265
266 static int
267 do_test (void)
268 {
269   glob_t gl;
270   memset (&gl, 0, sizeof (gl));
271   init_glob_altdirfuncs (&gl);
272
273   if (glob ("dir/*able/*", GLOB_ERR | GLOB_ALTDIRFUNC, NULL, &gl)
274       != GLOB_ABORTED)
275     {
276       puts ("glob did not fail with GLOB_ABORTED");
277       exit (EXIT_FAILURE);
278     }
279
280   globfree (&gl);
281
282   memset (&gl, 0, sizeof (gl));
283   init_glob_altdirfuncs (&gl);
284
285   gl.gl_offs = 3;
286   if (glob ("dir2/*", GLOB_DOOFFS, NULL, &gl) != GLOB_NOMATCH)
287     {
288       puts ("glob did not fail with GLOB_NOMATCH");
289       exit (EXIT_FAILURE);
290     }
291
292   globfree (&gl);
293
294   return 0;
295 }
296 #else
297 static int do_test (void) { return 0; }
298 #endif
299
300 #define TEST_FUNCTION do_test ()
301 #include "../test-skeleton.c"