1 /* Test the GNU extensions in glob which allow the user to provide callbacks
2 for the filesystem access functions.
3 Copyright (C) 2001-2002 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
33 # define PRINTF(fmt, args...) printf (fmt, ##args)
35 # define PRINTF(fmt, args...)
39 #ifdef GLOB_ALTDIRFUNC
49 { "file1lev1", 1, DT_REG
},
50 { "file2lev1", 1, DT_UNKNOWN
},
51 { "dir1lev1", 1, DT_UNKNOWN
},
54 { "file1lev2", 2, DT_REG
},
55 { "dir1lev2", 2, DT_DIR
},
58 { "dir2lev2", 2, DT_DIR
},
61 { ".foo", 3, DT_REG
},
62 { "dir1lev3", 3, 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
},
73 { "file3lev3", 3, DT_REG
},
74 { "file4lev3", 3, DT_REG
},
75 { "dir2lev1", 1, DT_DIR
},
78 { "dir1lev2", 2, DT_UNKNOWN
},
81 { ".foo", 3, DT_REG
},
82 { ".dir", 3, DT_DIR
},
85 { "hidden", 4, DT_REG
}
87 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
95 char room_for_dirent
[NAME_MAX
];
100 find_file (const char *s
)
105 if (strcmp (s
, ".") == 0)
108 if (s
[0] == '.' && s
[1] == '/')
113 char *endp
= strchrnul (s
, '/');
115 PRINTF ("looking for %.*s, level %d\n", (int) (endp
- s
), s
, level
);
117 while (idx
< nfiles
&& filesystem
[idx
].level
>= level
)
119 if (filesystem
[idx
].level
== level
120 && memcmp (s
, filesystem
[idx
].name
, endp
- s
) == 0
121 && filesystem
[idx
].name
[endp
- s
] == '\0')
126 if (idx
== nfiles
|| filesystem
[idx
].level
< level
)
135 if (filesystem
[idx
].type
!= DT_DIR
136 && (idx
+ 1 >= nfiles
137 || filesystem
[idx
].level
>= filesystem
[idx
+ 1].level
))
155 my_opendir (const char *s
)
157 long int idx
= find_file (s
);
163 PRINTF ("my_opendir(\"%s\") == NULL\n", s
);
167 dir
= (my_DIR
*) malloc (sizeof (my_DIR
));
169 error (EXIT_FAILURE
, errno
, "cannot allocate directory handle");
171 dir
->level
= filesystem
[idx
].level
;
174 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
175 s
, filesystem
[idx
].level
, idx
);
181 static struct dirent
*
182 my_readdir (void *gdir
)
188 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
189 dir
->level
, (long int) dir
->idx
);
193 while (dir
->idx
< nfiles
&& filesystem
[dir
->idx
].level
> dir
->level
)
196 if (dir
->idx
== nfiles
|| filesystem
[dir
->idx
].level
< dir
->level
)
199 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
200 dir
->level
, (long int) dir
->idx
);
204 dir
->d
.d_ino
= dir
->idx
;
206 #ifdef _DIRENT_HAVE_D_TYPE
207 dir
->d
.d_type
= filesystem
[dir
->idx
].type
;
210 strcpy (dir
->d
.d_name
, filesystem
[dir
->idx
].name
);
212 #ifdef _DIRENT_HAVE_D_TYPE
213 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
214 dir
->level
, (long int) dir
->idx
, dir
->d
.d_ino
, dir
->d
.d_type
,
217 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
218 dir
->level
, (long int) dir
->idx
, dir
->d
.d_ino
,
229 my_closedir (void *dir
)
231 PRINTF ("my_closedir ()\n");
236 /* We use this function for lstat as well since we don't have any. */
238 my_stat (const char *name
, struct stat
*st
)
240 long int idx
= find_file (name
);
244 PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name
, strerror (errno
));
248 memset (st
, '\0', sizeof (*st
));
250 if (filesystem
[idx
].type
== DT_UNKNOWN
)
251 st
->st_mode
= DTTOIF (idx
+ 1 < nfiles
252 && filesystem
[idx
].level
< filesystem
[idx
+ 1].level
253 ? DT_DIR
: DT_REG
) | 0777;
255 st
->st_mode
= DTTOIF (filesystem
[idx
].type
) | 0777;
257 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name
, st
->st_mode
);
263 static const char *glob_errstring
[] =
265 [GLOB_NOSPACE
] = "out of memory",
266 [GLOB_ABORTED
] = "read error",
267 [GLOB_NOMATCH
] = "no matches found"
269 #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
277 "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
278 "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
279 "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
280 "GLOB_ONLYDIR", "GLOB_TILDECHECK"
282 #define nstrs (sizeof (strs) / sizeof (strs[0]))
283 static char buf
[100];
287 for (cnt
= 0; cnt
< nstrs
; ++cnt
)
288 if (flags
& (1 << cnt
))
290 flags
&= ~(1 << cnt
);
293 cp
= stpcpy (cp
, strs
[cnt
]);
300 sprintf (cp
, "%#x", flags
);
308 test_result (const char *fmt
, int flags
, glob_t
*gl
, const char *str
[])
313 printf ("results for glob (\"%s\", %s)\n", fmt
, flagstr (flags
));
314 for (cnt
= 0; cnt
< gl
->gl_pathc
&& str
[cnt
] != NULL
; ++cnt
)
316 int ok
= strcmp (gl
->gl_pathv
[cnt
], str
[cnt
]) == 0;
317 const char *errstr
= "";
323 for (inner
= 0; str
[inner
] != NULL
; ++inner
)
324 if (strcmp (gl
->gl_pathv
[cnt
], str
[inner
]) == 0)
327 if (str
[inner
] == NULL
)
328 errstr
= ok
? "" : " *** WRONG";
330 errstr
= ok
? "" : " * wrong position";
335 printf (" %s%s\n", gl
->gl_pathv
[cnt
], errstr
);
339 if (str
[cnt
] != NULL
|| cnt
< gl
->gl_pathc
)
341 puts (" *** incorrect number of entries");
358 memset (&gl
, '\0', sizeof (gl
));
360 gl
.gl_closedir
= my_closedir
;
361 gl
.gl_readdir
= my_readdir
;
362 gl
.gl_opendir
= my_opendir
;
363 gl
.gl_lstat
= my_stat
;
364 gl
.gl_stat
= my_stat
;
366 #define test(a, b, c...) \
369 errval = glob (fmt, flags, NULL, &gl); \
372 printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
373 errval >= 0 && errval < nglob_errstring \
374 ? glob_errstring[errval] : "???"); \
378 result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL })
380 test ("*/*/*", GLOB_ALTDIRFUNC
,
381 "dir1lev1/dir2lev2/dir1lev3",
382 "dir1lev1/dir2lev2/file1lev3",
383 "dir1lev1/dir2lev2/file2lev3",
384 "dir1lev1/dir3lev2/file3lev3",
385 "dir1lev1/dir3lev2/file4lev3");
387 test ("*/*/*", GLOB_ALTDIRFUNC
| GLOB_PERIOD
,
388 "dir1lev1/dir1lev2/.",
389 "dir1lev1/dir1lev2/..",
390 "dir1lev1/dir2lev2/.",
391 "dir1lev1/dir2lev2/..",
392 "dir1lev1/dir2lev2/.foo",
393 "dir1lev1/dir2lev2/dir1lev3",
394 "dir1lev1/dir2lev2/file1lev3",
395 "dir1lev1/dir2lev2/file2lev3",
396 "dir1lev1/dir3lev2/.",
397 "dir1lev1/dir3lev2/..",
398 "dir1lev1/dir3lev2/file3lev3",
399 "dir1lev1/dir3lev2/file4lev3",
400 "dir2lev1/dir1lev2/.",
401 "dir2lev1/dir1lev2/..",
402 "dir2lev1/dir1lev2/.dir",
403 "dir2lev1/dir1lev2/.foo");
405 test ("*/*/.*", GLOB_ALTDIRFUNC
,
406 "dir1lev1/dir1lev2/.",
407 "dir1lev1/dir1lev2/..",
408 "dir1lev1/dir2lev2/.",
409 "dir1lev1/dir2lev2/..",
410 "dir1lev1/dir2lev2/.foo",
411 "dir1lev1/dir3lev2/.",
412 "dir1lev1/dir3lev2/..",
413 "dir2lev1/dir1lev2/.",
414 "dir2lev1/dir1lev2/..",
415 "dir2lev1/dir1lev2/.dir",
416 "dir2lev1/dir1lev2/.foo");
418 test ("*1*/*2*/.*", GLOB_ALTDIRFUNC
,
419 "dir1lev1/dir1lev2/.",
420 "dir1lev1/dir1lev2/..",
421 "dir1lev1/dir2lev2/.",
422 "dir1lev1/dir2lev2/..",
423 "dir1lev1/dir2lev2/.foo",
424 "dir1lev1/dir3lev2/.",
425 "dir1lev1/dir3lev2/..",
426 "dir2lev1/dir1lev2/.",
427 "dir2lev1/dir1lev2/..",
428 "dir2lev1/dir1lev2/.dir",
429 "dir2lev1/dir1lev2/.foo");
431 test ("*1*/*1*/.*", GLOB_ALTDIRFUNC
,
432 "dir1lev1/dir1lev2/.",
433 "dir1lev1/dir1lev2/..",
434 "dir2lev1/dir1lev2/.",
435 "dir2lev1/dir1lev2/..",
436 "dir2lev1/dir1lev2/.dir",
437 "dir2lev1/dir1lev2/.foo");
445 int main(void) { return 0; }