Update.
[glibc.git] / posix / tst-gnuglob.c
blob31eaed35d52563d5b5d453637ea2c9a4ebbdb609
1 /* Test the GNU extensions in glob which allow the user to provide callbacks
2 for the filesystem access functions.
3 Copyright (C) 2001 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <dirent.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <glob.h>
25 #include <mcheck.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
32 /* #define DEBUG */
33 #ifdef DEBUG
34 # define PRINTF(fmt, args...) printf (fmt, ##args)
35 #else
36 # define PRINTF(fmt, args...)
37 #endif
40 static struct
42 const char *name;
43 int level;
44 int type;
45 } filesystem[] =
47 { ".", 1, DT_DIR },
48 { "..", 1, DT_DIR },
49 { "file1lev1", 1, DT_REG },
50 { "file2lev1", 1, DT_UNKNOWN },
51 { "dir1lev1", 1, DT_UNKNOWN },
52 { ".", 2, DT_DIR },
53 { "..", 2, DT_DIR },
54 { "file1lev2", 2, DT_REG },
55 { "dir1lev2", 2, DT_DIR },
56 { ".", 3, DT_DIR },
57 { "..", 3, DT_DIR },
58 { "dir2lev2", 2, DT_DIR },
59 { ".", 3, DT_DIR },
60 { "..", 3, DT_DIR },
61 { ".foo", 3, DT_REG },
62 { "dir1lev3", 3, DT_DIR },
63 { ".", 4, DT_DIR },
64 { "..", 4, DT_DIR },
65 { "file1lev4", 4, DT_REG },
66 { "file1lev3", 3, DT_REG },
67 { "file2lev3", 3, DT_REG },
68 { "file2lev2", 2, DT_REG },
69 { "file3lev2", 2, DT_REG },
70 { "dir3lev2", 2, DT_DIR },
71 { ".", 3, DT_DIR },
72 { "..", 3, DT_DIR },
73 { "file3lev3", 3, DT_REG },
74 { "file4lev3", 3, DT_REG },
75 { "dir2lev1", 1, DT_DIR },
76 { ".", 2, DT_DIR },
77 { "..", 2, DT_DIR },
78 { "dir1lev2", 2, DT_UNKNOWN },
79 { ".", 3, DT_DIR },
80 { "..", 3, DT_DIR },
81 { ".foo", 3, DT_REG },
82 { ".dir", 3, DT_DIR },
83 { ".", 4, DT_DIR },
84 { "..", 4, DT_DIR },
85 { "hidden", 4, DT_REG }
87 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
90 typedef struct
92 int level;
93 int idx;
94 struct dirent d;
95 } my_DIR;
98 static long int
99 find_file (const char *s)
101 int level = 1;
102 long int idx = 0;
104 if (strcmp (s, ".") == 0)
105 return 0;
107 while (*s != '\0')
109 char *endp = strchrnul (s, '/');
111 PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
113 while (idx < nfiles && filesystem[idx].level >= level)
115 if (filesystem[idx].level == level
116 && memcmp (s, filesystem[idx].name, endp - s) == 0
117 && filesystem[idx].name[endp - s] == '\0')
118 break;
119 ++idx;
122 if (idx == nfiles || filesystem[idx].level < level)
124 errno = ENOENT;
125 return -1;
127 if (filesystem[idx].type != DT_DIR
128 && (idx + 1 >= nfiles
129 || filesystem[idx].level >= filesystem[idx + 1].level))
131 errno = ENOTDIR;
132 return -1;
135 ++idx;
137 if (*endp == '\0')
138 return idx;
140 s = endp + 1;
141 ++level;
144 errno = ENOENT;
145 return -1;
149 static void *
150 my_opendir (const char *s)
152 long int idx = find_file (s);
153 my_DIR *dir;
156 if (idx == -1)
158 PRINTF ("my_opendir(\"%s\") == NULL\n", s);
159 return NULL;
162 dir = (my_DIR *) malloc (sizeof (my_DIR));
163 if (dir == NULL)
164 error (EXIT_FAILURE, errno, "cannot allocate directory handle");
166 dir->level = filesystem[idx].level;
167 dir->idx = idx;
169 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
170 s, filesystem[idx].level, idx);
172 return dir;
176 static struct dirent *
177 my_readdir (void *gdir)
179 my_DIR *dir = gdir;
181 if (dir->idx == -1)
183 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
184 dir->level, (long int) dir->idx);
185 return NULL;
188 while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
189 ++dir->idx;
191 if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
193 dir->idx = -1;
194 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
195 dir->level, (long int) dir->idx);
196 return NULL;
199 dir->d.d_ino = dir->idx;
201 #ifdef _DIRENT_HAVE_D_TYPE
202 dir->d.d_type = filesystem[dir->idx].type;
203 #endif
205 strcpy (dir->d.d_name, filesystem[dir->idx].name);
207 #ifdef _DIRENT_HAVE_D_TYPE
208 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
209 dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
210 dir->d.d_name);
211 #else
212 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
213 dir->level, (long int) dir->idx, dir->d.d_ino,
214 dir->d.d_name);
215 #endif
217 ++dir->idx;
219 return &dir->d;
223 static void
224 my_closedir (void *dir)
226 PRINTF ("my_closedir ()\n");
227 free (dir);
231 /* We use this function for lstat as well since we don't have any. */
232 static int
233 my_stat (const char *name, struct stat *st)
235 long int idx = find_file (name);
237 if (idx == -1)
239 PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name, strerror (errno));
240 return -1;
243 memset (st, '\0', sizeof (*st));
245 if (filesystem[idx].type == DT_UNKNOWN)
246 st->st_mode = DTTOIF (idx + 1 < nfiles
247 && filesystem[idx].level < filesystem[idx + 1].level
248 ? DT_DIR : DT_REG) | 0777;
249 else
250 st->st_mode = DTTOIF (filesystem[idx].type) | 0777;
252 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
254 return 0;
258 static const char *glob_errstring[] =
260 [GLOB_NOSPACE] = "out of memory",
261 [GLOB_ABORTED] = "read error",
262 [GLOB_NOMATCH] = "no matches found"
264 #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
267 static const char *
268 flagstr (int flags)
270 const char *strs[] =
272 "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
273 "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
274 "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
275 "GLOB_ONLYDIR", "GLOB_TILDECHECK"
277 #define nstrs (sizeof (strs) / sizeof (strs[0]))
278 static char buf[100];
279 char *cp = buf;
280 int cnt;
282 for (cnt = 0; cnt < nstrs; ++cnt)
283 if (flags & (1 << cnt))
285 flags &= ~(1 << cnt);
286 if (cp != buf)
287 *cp++ = '|';
288 cp = stpcpy (cp, strs[cnt]);
291 if (flags != 0)
293 if (cp != buf)
294 *cp++ = '|';
295 sprintf (cp, "%#x", flags);
298 return buf;
302 static int
303 test_result (const char *fmt, int flags, glob_t *gl, const char *str[])
305 size_t cnt;
306 int result = 0;
308 printf ("results for glob (\"%s\", %s)\n", fmt, flagstr (flags));
309 for (cnt = 0; cnt < gl->gl_pathc && str[cnt] != NULL; ++cnt)
311 int ok = strcmp (gl->gl_pathv[cnt], str[cnt]) == 0;
312 const char *errstr = "";
314 if (! ok)
316 size_t inner;
318 for (inner = 0; str[inner] != NULL; ++inner)
319 if (strcmp (gl->gl_pathv[cnt], str[inner]) == 0)
320 break;
322 if (str[inner] == NULL)
323 errstr = ok ? "" : " *** WRONG";
324 else
325 errstr = ok ? "" : " * wrong position";
327 result = 1;
330 printf (" %s%s\n", gl->gl_pathv[cnt], errstr);
332 puts ("");
334 if (str[cnt] != NULL || cnt < gl->gl_pathc)
336 puts (" *** incorrect number of entries");
337 result = 1;
340 return result;
345 main (void)
347 glob_t gl;
348 int errval;
349 int result = 0;
350 const char *fmt;
351 int flags;
353 mtrace ();
355 memset (&gl, '\0', sizeof (gl));
357 gl.gl_closedir = my_closedir;
358 gl.gl_readdir = my_readdir;
359 gl.gl_opendir = my_opendir;
360 gl.gl_lstat = my_stat;
361 gl.gl_stat = my_stat;
363 #define test(a, b, c...) \
364 fmt = a; \
365 flags = b; \
366 errval = glob (fmt, flags, NULL, &gl); \
367 if (errval != 0) \
369 printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
370 errval >= 0 && errval < nglob_errstring \
371 ? glob_errstring[errval] : "???"); \
372 result = 1; \
374 else \
375 result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL })
377 test ("*/*/*", GLOB_ALTDIRFUNC,
378 "dir1lev1/dir2lev2/dir1lev3",
379 "dir1lev1/dir2lev2/file1lev3",
380 "dir1lev1/dir2lev2/file2lev3",
381 "dir1lev1/dir3lev2/file3lev3",
382 "dir1lev1/dir3lev2/file4lev3");
384 test ("*/*/*", GLOB_ALTDIRFUNC | GLOB_PERIOD,
385 "dir1lev1/dir1lev2/.",
386 "dir1lev1/dir1lev2/..",
387 "dir1lev1/dir2lev2/.",
388 "dir1lev1/dir2lev2/..",
389 "dir1lev1/dir2lev2/.foo",
390 "dir1lev1/dir2lev2/dir1lev3",
391 "dir1lev1/dir2lev2/file1lev3",
392 "dir1lev1/dir2lev2/file2lev3",
393 "dir1lev1/dir3lev2/.",
394 "dir1lev1/dir3lev2/..",
395 "dir1lev1/dir3lev2/file3lev3",
396 "dir1lev1/dir3lev2/file4lev3",
397 "dir2lev1/dir1lev2/.",
398 "dir2lev1/dir1lev2/..",
399 "dir2lev1/dir1lev2/.dir",
400 "dir2lev1/dir1lev2/.foo");
402 test ("*/*/.*", GLOB_ALTDIRFUNC,
403 "dir1lev1/dir1lev2/.",
404 "dir1lev1/dir1lev2/..",
405 "dir1lev1/dir2lev2/.",
406 "dir1lev1/dir2lev2/..",
407 "dir1lev1/dir2lev2/.foo",
408 "dir1lev1/dir3lev2/.",
409 "dir1lev1/dir3lev2/..",
410 "dir2lev1/dir1lev2/.",
411 "dir2lev1/dir1lev2/..",
412 "dir2lev1/dir1lev2/.dir",
413 "dir2lev1/dir1lev2/.foo");
415 test ("*1*/*2*/.*", GLOB_ALTDIRFUNC,
416 "dir1lev1/dir1lev2/.",
417 "dir1lev1/dir1lev2/..",
418 "dir1lev1/dir2lev2/.",
419 "dir1lev1/dir2lev2/..",
420 "dir1lev1/dir2lev2/.foo",
421 "dir1lev1/dir3lev2/.",
422 "dir1lev1/dir3lev2/..",
423 "dir2lev1/dir1lev2/.",
424 "dir2lev1/dir1lev2/..",
425 "dir2lev1/dir1lev2/.dir",
426 "dir2lev1/dir1lev2/.foo");
428 test ("*1*/*1*/.*", GLOB_ALTDIRFUNC,
429 "dir1lev1/dir1lev2/.",
430 "dir1lev1/dir1lev2/..",
431 "dir2lev1/dir1lev2/.",
432 "dir2lev1/dir1lev2/..",
433 "dir2lev1/dir1lev2/.dir",
434 "dir2lev1/dir1lev2/.foo");
436 globfree (&gl);
438 return result;