1 /* Template for tests of the GNU extension GLOB_ALTDIRFUNC.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 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.
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.
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/>. */
20 /* To use this skeleton, the following macros need to be defined
21 before inclusion of this file:
23 GLOB_FUNC The glob function to test (glob or glob64)
24 GLOB_TYPE The glob type expected by the function (glob_t, glob64_t)
25 GLOBFREE_FUNC The corresponding deallocation function
26 DIRENT_STRUCT The struct tag of the dirent type
27 STAT_STRUCT The struct tag of the stat type
39 #include <support/test-driver.h>
51 { "file1lev1", 1, DT_REG
},
52 { "file2lev1", 1, DT_UNKNOWN
},
53 { "dir1lev1", 1, DT_UNKNOWN
},
56 { "file1lev2", 2, DT_REG
},
57 { "dir1lev2", 2, DT_DIR
},
60 { "dir2lev2", 2, DT_DIR
},
63 { ".foo", 3, DT_REG
},
64 { "dir1lev3", 3, DT_DIR
},
67 { "file1lev4", 4, DT_REG
},
68 { "file1lev3", 3, DT_REG
},
69 { "file2lev3", 3, DT_REG
},
70 { "file2lev2", 2, DT_REG
},
71 { "file3lev2", 2, DT_REG
},
72 { "dir3lev2", 2, DT_DIR
},
75 { "file3lev3", 3, DT_REG
},
76 { "file4lev3", 3, DT_REG
},
77 { "dir2lev1", 1, DT_DIR
},
80 { "dir1lev2", 2, DT_UNKNOWN
},
83 { ".foo", 3, DT_REG
},
84 { ".dir", 3, DT_DIR
},
87 { "hidden", 4, DT_REG
}
89 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
96 struct DIRENT_STRUCT d
;
97 char room_for_dirent
[NAME_MAX
];
102 find_file (const char *s
)
117 if (strcmp (s
, ".") == 0)
120 if (s
[0] == '.' && s
[1] == '/')
125 char *endp
= strchrnul (s
, '/');
128 printf ("info: looking for %.*s, level %d\n",
129 (int) (endp
- s
), s
, level
);
131 while (idx
< nfiles
&& filesystem
[idx
].level
>= level
)
133 if (filesystem
[idx
].level
== level
134 && memcmp (s
, filesystem
[idx
].name
, endp
- s
) == 0
135 && filesystem
[idx
].name
[endp
- s
] == '\0')
140 if (idx
== nfiles
|| filesystem
[idx
].level
< level
)
149 if (filesystem
[idx
].type
!= DT_DIR
150 && (idx
+ 1 >= nfiles
151 || filesystem
[idx
].level
>= filesystem
[idx
+ 1].level
))
169 my_opendir (const char *s
)
171 long int idx
= find_file (s
);
175 if (idx
== -1 || filesystem
[idx
].type
!= DT_DIR
)
177 if (test_verbose
> 0)
178 printf ("info: my_opendir(\"%s\") == NULL\n", s
);
182 dir
= (my_DIR
*) malloc (sizeof (my_DIR
));
184 error (EXIT_FAILURE
, errno
, "cannot allocate directory handle");
186 dir
->level
= filesystem
[idx
].level
;
189 if (test_verbose
> 0)
190 printf ("info: my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
191 s
, filesystem
[idx
].level
, idx
);
197 static struct DIRENT_STRUCT
*
198 my_readdir (void *gdir
)
204 if (test_verbose
> 0)
205 printf ("info: my_readdir ({ level: %d, idx: %ld }) = NULL\n",
206 dir
->level
, (long int) dir
->idx
);
210 while (dir
->idx
< nfiles
&& filesystem
[dir
->idx
].level
> dir
->level
)
213 if (dir
->idx
== nfiles
|| filesystem
[dir
->idx
].level
< dir
->level
)
216 if (test_verbose
> 0)
217 printf ("info: my_readdir ({ level: %d, idx: %ld }) = NULL\n",
218 dir
->level
, (long int) dir
->idx
);
222 dir
->d
.d_ino
= 1; /* glob should not skip this entry. */
224 dir
->d
.d_type
= filesystem
[dir
->idx
].type
;
226 strcpy (dir
->d
.d_name
, filesystem
[dir
->idx
].name
);
228 if (test_verbose
> 0)
229 printf ("info: my_readdir ({ level: %d, idx: %ld })"
230 " = { d_ino: %lld, d_type: %d, d_name: \"%s\" }\n",
231 dir
->level
, (long int) dir
->idx
,
232 (long long) dir
->d
.d_ino
, dir
->d
.d_type
,
242 my_closedir (void *dir
)
244 if (test_verbose
> 0)
245 printf ("info: my_closedir ()\n");
250 /* We use this function for lstat as well since we don't have any. */
252 my_stat (const char *name
, struct STAT_STRUCT
*st
)
254 long int idx
= find_file (name
);
258 if (test_verbose
> 0)
259 printf ("info: my_stat (\"%s\", ...) = -1 (%s)\n",
260 name
, strerror (errno
));
264 memset (st
, '\0', sizeof (*st
));
266 if (filesystem
[idx
].type
== DT_UNKNOWN
)
267 st
->st_mode
= DTTOIF (idx
+ 1 < nfiles
268 && filesystem
[idx
].level
< filesystem
[idx
+ 1].level
269 ? DT_DIR
: DT_REG
) | 0777;
271 st
->st_mode
= DTTOIF (filesystem
[idx
].type
) | 0777;
273 if (test_verbose
> 0)
274 printf ("info: my_stat (\"%s\", { st_mode: %o }) = 0\n", name
, st
->st_mode
);
280 static const char *glob_errstring
[] =
282 [GLOB_NOSPACE
] = "out of memory",
283 [GLOB_ABORTED
] = "read error",
284 [GLOB_NOMATCH
] = "no matches found"
286 #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
292 static const char *const strs
[] =
294 "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
295 "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
296 "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
297 "GLOB_ONLYDIR", "GLOB_TILDECHECK"
299 #define nstrs (sizeof (strs) / sizeof (strs[0]))
300 static char buf
[100];
304 for (cnt
= 0; cnt
< nstrs
; ++cnt
)
305 if (flags
& (1 << cnt
))
307 flags
&= ~(1 << cnt
);
310 cp
= stpcpy (cp
, strs
[cnt
]);
317 sprintf (cp
, "%#x", flags
);
328 static const char *const strs
[] =
330 [GLOB_NOSPACE
] = "GLOB_NOSPACE",
331 [GLOB_ABORTED
] = "GLOB_ABORTED",
332 [GLOB_NOMATCH
] = "GLOB_NOMATCH",
333 [GLOB_NOSYS
] = "GLOB_NOSYS"
335 #define nstrs (sizeof (strs) / sizeof (strs[0]))
336 static char buf
[100];
337 if (val
< 0 || val
>= nstrs
|| strs
[val
] == NULL
)
339 snprintf (buf
, sizeof (buf
), "GLOB_??? (%d)", val
);
348 test_result (const char *fmt
, int flags
, GLOB_TYPE
*gl
, const char *str
[])
353 printf ("results for glob (\"%s\", %s)\n", fmt
, flagstr (flags
));
354 for (cnt
= 0; cnt
< gl
->gl_pathc
&& str
[cnt
] != NULL
; ++cnt
)
356 int ok
= strcmp (gl
->gl_pathv
[cnt
], str
[cnt
]) == 0;
357 const char *errstr
= "";
363 for (inner
= 0; str
[inner
] != NULL
; ++inner
)
364 if (strcmp (gl
->gl_pathv
[cnt
], str
[inner
]) == 0)
367 if (str
[inner
] == NULL
)
368 errstr
= ok
? "" : " *** WRONG";
370 errstr
= ok
? "" : " * wrong position";
375 printf (" %s%s\n", gl
->gl_pathv
[cnt
], errstr
);
379 if (str
[cnt
] != NULL
|| cnt
< gl
->gl_pathc
)
381 puts (" *** incorrect number of entries");
400 memset (&gl
, '\0', sizeof (gl
));
402 gl
.gl_closedir
= my_closedir
;
403 gl
.gl_readdir
= my_readdir
;
404 gl
.gl_opendir
= my_opendir
;
405 gl
.gl_lstat
= my_stat
;
406 gl
.gl_stat
= my_stat
;
408 #define test(a, b, r, c...) \
410 flags = GLOB_ALTDIRFUNC | b; \
411 errval = GLOB_FUNC (fmt, flags, NULL, &gl); \
415 printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
416 errval >= 0 && errval < nglob_errstring \
417 ? glob_errstring[errval] : "???"); \
419 printf ("glob (\"%s\", %s) did not fail\n", fmt, flagstr (flags)); \
423 result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL }); \
425 printf ("result for glob (\"%s\", %s) = %s\n\n", fmt, flagstr (flags), \
429 "dir1lev1/dir2lev2/dir1lev3",
430 "dir1lev1/dir2lev2/file1lev3",
431 "dir1lev1/dir2lev2/file2lev3",
432 "dir1lev1/dir3lev2/file3lev3",
433 "dir1lev1/dir3lev2/file4lev3");
435 test ("*/*/*", GLOB_PERIOD
, 0,
436 "dir1lev1/dir1lev2/.",
437 "dir1lev1/dir1lev2/..",
438 "dir1lev1/dir2lev2/.",
439 "dir1lev1/dir2lev2/..",
440 "dir1lev1/dir2lev2/.foo",
441 "dir1lev1/dir2lev2/dir1lev3",
442 "dir1lev1/dir2lev2/file1lev3",
443 "dir1lev1/dir2lev2/file2lev3",
444 "dir1lev1/dir3lev2/.",
445 "dir1lev1/dir3lev2/..",
446 "dir1lev1/dir3lev2/file3lev3",
447 "dir1lev1/dir3lev2/file4lev3",
448 "dir2lev1/dir1lev2/.",
449 "dir2lev1/dir1lev2/..",
450 "dir2lev1/dir1lev2/.dir",
451 "dir2lev1/dir1lev2/.foo");
453 test ("*/*/.*", 0, 0,
454 "dir1lev1/dir1lev2/.",
455 "dir1lev1/dir1lev2/..",
456 "dir1lev1/dir2lev2/.",
457 "dir1lev1/dir2lev2/..",
458 "dir1lev1/dir2lev2/.foo",
459 "dir1lev1/dir3lev2/.",
460 "dir1lev1/dir3lev2/..",
461 "dir2lev1/dir1lev2/.",
462 "dir2lev1/dir1lev2/..",
463 "dir2lev1/dir1lev2/.dir",
464 "dir2lev1/dir1lev2/.foo");
466 test ("*1*/*2*/.*", 0, 0,
467 "dir1lev1/dir1lev2/.",
468 "dir1lev1/dir1lev2/..",
469 "dir1lev1/dir2lev2/.",
470 "dir1lev1/dir2lev2/..",
471 "dir1lev1/dir2lev2/.foo",
472 "dir1lev1/dir3lev2/.",
473 "dir1lev1/dir3lev2/..",
474 "dir2lev1/dir1lev2/.",
475 "dir2lev1/dir1lev2/..",
476 "dir2lev1/dir1lev2/.dir",
477 "dir2lev1/dir1lev2/.foo");
479 test ("*1*/*1*/.*", 0, 0,
480 "dir1lev1/dir1lev2/.",
481 "dir1lev1/dir1lev2/..",
482 "dir2lev1/dir1lev2/.",
483 "dir2lev1/dir1lev2/..",
484 "dir2lev1/dir1lev2/.dir",
485 "dir2lev1/dir1lev2/.foo");
494 "dir1lev1/dir1lev2/",
495 "dir1lev1/dir2lev2/",
496 "dir1lev1/dir3lev2/",
497 "dir2lev1/dir1lev2/");
499 test ("", 0, GLOB_NOMATCH
, NULL
);
501 test ("", GLOB_NOCHECK
, 0, "");
508 #include <support/test-driver.c>