tst-gnuglob.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.
  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. 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.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #include <dirent.h>
  19. #include <errno.h>
  20. #include <error.h>
  21. #include <glob.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. // #define DEBUG
  27. #ifdef DEBUG
  28. # define PRINTF(fmt, args...) printf (fmt, ##args)
  29. #else
  30. # define PRINTF(fmt, args...)
  31. #endif
  32. #ifdef GLOB_ALTDIRFUNC
  33. static struct
  34. {
  35. const char *name;
  36. int level;
  37. int type;
  38. } filesystem[] =
  39. {
  40. { ".", 1, DT_DIR },
  41. { "..", 1, DT_DIR },
  42. { "file1lev1", 1, DT_REG },
  43. { "file2lev1", 1, DT_UNKNOWN },
  44. { "dir1lev1", 1, DT_UNKNOWN },
  45. { ".", 2, DT_DIR },
  46. { "..", 2, DT_DIR },
  47. { "file1lev2", 2, DT_REG },
  48. { "dir1lev2", 2, DT_DIR },
  49. { ".", 3, DT_DIR },
  50. { "..", 3, DT_DIR },
  51. { "dir2lev2", 2, DT_DIR },
  52. { ".", 3, DT_DIR },
  53. { "..", 3, DT_DIR },
  54. { ".foo", 3, DT_REG },
  55. { "dir1lev3", 3, DT_DIR },
  56. { ".", 4, DT_DIR },
  57. { "..", 4, DT_DIR },
  58. { "file1lev4", 4, DT_REG },
  59. { "file1lev3", 3, DT_REG },
  60. { "file2lev3", 3, DT_REG },
  61. { "file2lev2", 2, DT_REG },
  62. { "file3lev2", 2, DT_REG },
  63. { "dir3lev2", 2, DT_DIR },
  64. { ".", 3, DT_DIR },
  65. { "..", 3, DT_DIR },
  66. { "file3lev3", 3, DT_REG },
  67. { "file4lev3", 3, DT_REG },
  68. { "dir2lev1", 1, DT_DIR },
  69. { ".", 2, DT_DIR },
  70. { "..", 2, DT_DIR },
  71. { "dir1lev2", 2, DT_UNKNOWN },
  72. { ".", 3, DT_DIR },
  73. { "..", 3, DT_DIR },
  74. { ".foo", 3, DT_REG },
  75. { ".dir", 3, DT_DIR },
  76. { ".", 4, DT_DIR },
  77. { "..", 4, DT_DIR },
  78. { "hidden", 4, DT_REG }
  79. };
  80. #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
  81. typedef struct
  82. {
  83. int level;
  84. int idx;
  85. struct dirent d;
  86. char room_for_dirent[NAME_MAX];
  87. } my_DIR;
  88. static long int
  89. find_file (const char *s)
  90. {
  91. int level = 1;
  92. long int idx = 0;
  93. if (strcmp (s, ".") == 0)
  94. return 0;
  95. if (s[0] == '.' && s[1] == '/')
  96. s += 2;
  97. while (*s != '\0')
  98. {
  99. char *endp = strchrnul (s, '/');
  100. PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
  101. while (idx < nfiles && filesystem[idx].level >= level)
  102. {
  103. if (filesystem[idx].level == level
  104. && memcmp (s, filesystem[idx].name, endp - s) == 0
  105. && filesystem[idx].name[endp - s] == '\0')
  106. break;
  107. ++idx;
  108. }
  109. if (idx == nfiles || filesystem[idx].level < level)
  110. {
  111. errno = ENOENT;
  112. return -1;
  113. }
  114. if (*endp == '\0')
  115. return idx + 1;
  116. if (filesystem[idx].type != DT_DIR
  117. && (idx + 1 >= nfiles
  118. || filesystem[idx].level >= filesystem[idx + 1].level))
  119. {
  120. errno = ENOTDIR;
  121. return -1;
  122. }
  123. ++idx;
  124. s = endp + 1;
  125. ++level;
  126. }
  127. errno = ENOENT;
  128. return -1;
  129. }
  130. static void *
  131. my_opendir (const char *s)
  132. {
  133. long int idx = find_file (s);
  134. my_DIR *dir;
  135. if (idx == -1)
  136. {
  137. PRINTF ("my_opendir(\"%s\") == NULL\n", s);
  138. return NULL;
  139. }
  140. dir = (my_DIR *) malloc (sizeof (my_DIR));
  141. if (dir == NULL)
  142. error (EXIT_FAILURE, errno, "cannot allocate directory handle");
  143. dir->level = filesystem[idx].level;
  144. dir->idx = idx;
  145. PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
  146. s, filesystem[idx].level, idx);
  147. return dir;
  148. }
  149. static struct dirent *
  150. my_readdir (void *gdir)
  151. {
  152. my_DIR *dir = gdir;
  153. if (dir->idx == -1)
  154. {
  155. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
  156. dir->level, (long int) dir->idx);
  157. return NULL;
  158. }
  159. while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
  160. ++dir->idx;
  161. if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
  162. {
  163. dir->idx = -1;
  164. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
  165. dir->level, (long int) dir->idx);
  166. return NULL;
  167. }
  168. dir->d.d_ino = dir->idx;
  169. #ifdef _DIRENT_HAVE_D_TYPE
  170. dir->d.d_type = filesystem[dir->idx].type;
  171. #endif
  172. strcpy (dir->d.d_name, filesystem[dir->idx].name);
  173. #ifdef _DIRENT_HAVE_D_TYPE
  174. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
  175. dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
  176. dir->d.d_name);
  177. #else
  178. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
  179. dir->level, (long int) dir->idx, dir->d.d_ino,
  180. dir->d.d_name);
  181. #endif
  182. ++dir->idx;
  183. return &dir->d;
  184. }
  185. static void
  186. my_closedir (void *dir)
  187. {
  188. PRINTF ("my_closedir ()\n");
  189. free (dir);
  190. }
  191. /* We use this function for lstat as well since we don't have any. */
  192. static int
  193. my_stat (const char *name, struct stat *st)
  194. {
  195. long int idx = find_file (name);
  196. if (idx == -1)
  197. {
  198. PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name, strerror (errno));
  199. return -1;
  200. }
  201. memset (st, '\0', sizeof (*st));
  202. if (filesystem[idx].type == DT_UNKNOWN)
  203. st->st_mode = DTTOIF (idx + 1 < nfiles
  204. && filesystem[idx].level < filesystem[idx + 1].level
  205. ? DT_DIR : DT_REG) | 0777;
  206. else
  207. st->st_mode = DTTOIF (filesystem[idx].type) | 0777;
  208. PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
  209. return 0;
  210. }
  211. static const char *glob_errstring[] =
  212. {
  213. [GLOB_NOSPACE] = "out of memory",
  214. [GLOB_ABORTED] = "read error",
  215. [GLOB_NOMATCH] = "no matches found"
  216. };
  217. #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
  218. static const char *
  219. flagstr (int flags)
  220. {
  221. const char *strs[] =
  222. {
  223. "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
  224. "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
  225. "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
  226. "GLOB_ONLYDIR", "GLOB_TILDECHECK"
  227. };
  228. #define nstrs (sizeof (strs) / sizeof (strs[0]))
  229. static char buf[100];
  230. char *cp = buf;
  231. int cnt;
  232. for (cnt = 0; cnt < nstrs; ++cnt)
  233. if (flags & (1 << cnt))
  234. {
  235. flags &= ~(1 << cnt);
  236. if (cp != buf)
  237. *cp++ = '|';
  238. cp = stpcpy (cp, strs[cnt]);
  239. }
  240. if (flags != 0)
  241. {
  242. if (cp != buf)
  243. *cp++ = '|';
  244. sprintf (cp, "%#x", flags);
  245. }
  246. return buf;
  247. }
  248. static int
  249. test_result (const char *fmt, int flags, glob_t *gl, const char *str[])
  250. {
  251. size_t cnt;
  252. int result = 0;
  253. printf ("results for glob (\"%s\", %s)\n", fmt, flagstr (flags));
  254. for (cnt = 0; cnt < gl->gl_pathc && str[cnt] != NULL; ++cnt)
  255. {
  256. int ok = strcmp (gl->gl_pathv[cnt], str[cnt]) == 0;
  257. const char *errstr = "";
  258. if (! ok)
  259. {
  260. size_t inner;
  261. for (inner = 0; str[inner] != NULL; ++inner)
  262. if (strcmp (gl->gl_pathv[cnt], str[inner]) == 0)
  263. break;
  264. if (str[inner] == NULL)
  265. errstr = ok ? "" : " *** WRONG";
  266. else
  267. errstr = ok ? "" : " * wrong position";
  268. result = 1;
  269. }
  270. printf (" %s%s\n", gl->gl_pathv[cnt], errstr);
  271. }
  272. puts ("");
  273. if (str[cnt] != NULL || cnt < gl->gl_pathc)
  274. {
  275. puts (" *** incorrect number of entries");
  276. result = 1;
  277. }
  278. return result;
  279. }
  280. int
  281. main (void)
  282. {
  283. glob_t gl;
  284. int errval;
  285. int result = 0;
  286. const char *fmt;
  287. int flags;
  288. memset (&gl, '\0', sizeof (gl));
  289. gl.gl_closedir = my_closedir;
  290. gl.gl_readdir = my_readdir;
  291. gl.gl_opendir = my_opendir;
  292. gl.gl_lstat = my_stat;
  293. gl.gl_stat = my_stat;
  294. #define test(a, b, c...) \
  295. fmt = a; \
  296. flags = b; \
  297. errval = glob (fmt, flags, NULL, &gl); \
  298. if (errval != 0) \
  299. { \
  300. printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
  301. errval >= 0 && errval < nglob_errstring \
  302. ? glob_errstring[errval] : "???"); \
  303. result = 1; \
  304. } \
  305. else \
  306. result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL })
  307. test ("*/*/*", GLOB_ALTDIRFUNC,
  308. "dir1lev1/dir2lev2/dir1lev3",
  309. "dir1lev1/dir2lev2/file1lev3",
  310. "dir1lev1/dir2lev2/file2lev3",
  311. "dir1lev1/dir3lev2/file3lev3",
  312. "dir1lev1/dir3lev2/file4lev3");
  313. test ("*/*/*", GLOB_ALTDIRFUNC | GLOB_PERIOD,
  314. "dir1lev1/dir1lev2/.",
  315. "dir1lev1/dir1lev2/..",
  316. "dir1lev1/dir2lev2/.",
  317. "dir1lev1/dir2lev2/..",
  318. "dir1lev1/dir2lev2/.foo",
  319. "dir1lev1/dir2lev2/dir1lev3",
  320. "dir1lev1/dir2lev2/file1lev3",
  321. "dir1lev1/dir2lev2/file2lev3",
  322. "dir1lev1/dir3lev2/.",
  323. "dir1lev1/dir3lev2/..",
  324. "dir1lev1/dir3lev2/file3lev3",
  325. "dir1lev1/dir3lev2/file4lev3",
  326. "dir2lev1/dir1lev2/.",
  327. "dir2lev1/dir1lev2/..",
  328. "dir2lev1/dir1lev2/.dir",
  329. "dir2lev1/dir1lev2/.foo");
  330. test ("*/*/.*", GLOB_ALTDIRFUNC,
  331. "dir1lev1/dir1lev2/.",
  332. "dir1lev1/dir1lev2/..",
  333. "dir1lev1/dir2lev2/.",
  334. "dir1lev1/dir2lev2/..",
  335. "dir1lev1/dir2lev2/.foo",
  336. "dir1lev1/dir3lev2/.",
  337. "dir1lev1/dir3lev2/..",
  338. "dir2lev1/dir1lev2/.",
  339. "dir2lev1/dir1lev2/..",
  340. "dir2lev1/dir1lev2/.dir",
  341. "dir2lev1/dir1lev2/.foo");
  342. test ("*1*/*2*/.*", GLOB_ALTDIRFUNC,
  343. "dir1lev1/dir1lev2/.",
  344. "dir1lev1/dir1lev2/..",
  345. "dir1lev1/dir2lev2/.",
  346. "dir1lev1/dir2lev2/..",
  347. "dir1lev1/dir2lev2/.foo",
  348. "dir1lev1/dir3lev2/.",
  349. "dir1lev1/dir3lev2/..",
  350. "dir2lev1/dir1lev2/.",
  351. "dir2lev1/dir1lev2/..",
  352. "dir2lev1/dir1lev2/.dir",
  353. "dir2lev1/dir1lev2/.foo");
  354. test ("*1*/*1*/.*", GLOB_ALTDIRFUNC,
  355. "dir1lev1/dir1lev2/.",
  356. "dir1lev1/dir1lev2/..",
  357. "dir2lev1/dir1lev2/.",
  358. "dir2lev1/dir1lev2/..",
  359. "dir2lev1/dir1lev2/.dir",
  360. "dir2lev1/dir1lev2/.foo");
  361. globfree (&gl);
  362. return result;
  363. }
  364. #else
  365. int main(void) { return 0; }
  366. #endif