glob.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public License as
  4. published by the Free Software Foundation; either version 2 of the
  5. License, or (at your option) any later version.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; see the file COPYING.LIB. If
  12. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  13. Cambridge, MA 02139, USA. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <dirent.h>
  21. #include <malloc.h>
  22. #include <fnmatch.h>
  23. #include <glob.h>
  24. __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
  25. const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
  26. void (*__glob_closedir_hook) __P ((__ptr_t stream));
  27. static int glob_pattern_p __P ((const char *pattern, int quote));
  28. static int glob_in_dir __P ((const char *pattern, const char *directory,
  29. int flags,
  30. int (*errfunc) __P ((const char *, int)),
  31. glob_t *pglob));
  32. static int prefix_array __P ((const char *prefix, char **array, size_t n,
  33. int add_slash));
  34. static int collated_compare __P ((const __ptr_t, const __ptr_t));
  35. /* Do glob searching for PATTERN, placing results in PGLOB.
  36. The bits defined above may be set in FLAGS.
  37. If a directory cannot be opened or read and ERRFUNC is not nil,
  38. it is called with the pathname that caused the error, and the
  39. `errno' value from the failing call; if it returns non-zero
  40. `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
  41. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  42. Otherwise, `glob' returns zero. */
  43. int
  44. glob (pattern, flags, errfunc, pglob)
  45. const char *pattern;
  46. int flags;
  47. int (*errfunc) __P ((const char *, int));
  48. glob_t *pglob;
  49. {
  50. const char *filename;
  51. char *dirname;
  52. size_t dirlen;
  53. int status;
  54. int oldcount;
  55. if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  56. {
  57. errno = EINVAL;
  58. return -1;
  59. }
  60. /* Find the filename. */
  61. filename = strrchr (pattern, '/');
  62. if (filename == NULL)
  63. {
  64. filename = pattern;
  65. dirname = (char *) ".";
  66. dirlen = 0;
  67. }
  68. else if (filename == pattern)
  69. {
  70. /* "/pattern". */
  71. dirname = (char *) "/";
  72. dirlen = 1;
  73. ++filename;
  74. }
  75. else
  76. {
  77. dirlen = filename - pattern;
  78. dirname = (char *) alloca (dirlen + 1);
  79. memcpy (dirname, pattern, dirlen);
  80. dirname[dirlen] = '\0';
  81. ++filename;
  82. }
  83. if (filename[0] == '\0' && dirlen > 1)
  84. /* "pattern/". Expand "pattern", appending slashes. */
  85. {
  86. int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
  87. if (val == 0)
  88. pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
  89. return val;
  90. }
  91. if (!(flags & GLOB_APPEND))
  92. {
  93. pglob->gl_pathc = 0;
  94. pglob->gl_pathv = NULL;
  95. }
  96. oldcount = pglob->gl_pathc;
  97. if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
  98. {
  99. /* The directory name contains metacharacters, so we
  100. have to glob for the directory, and then glob for
  101. the pattern in each directory found. */
  102. glob_t dirs;
  103. register int i;
  104. status = glob (dirname,
  105. ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
  106. GLOB_NOSORT),
  107. errfunc, &dirs);
  108. if (status != 0)
  109. return status;
  110. /* We have successfully globbed the preceding directory name.
  111. For each name we found, call glob_in_dir on it and FILENAME,
  112. appending the results to PGLOB. */
  113. for (i = 0; i < dirs.gl_pathc; ++i)
  114. {
  115. int oldcount;
  116. #ifdef SHELL
  117. {
  118. /* Make globbing interruptible in the bash shell. */
  119. extern int interrupt_state;
  120. if (interrupt_state)
  121. {
  122. globfree (&dirs);
  123. globfree (&files);
  124. return GLOB_ABEND;
  125. }
  126. }
  127. #endif /* SHELL. */
  128. oldcount = pglob->gl_pathc;
  129. status = glob_in_dir (filename, dirs.gl_pathv[i],
  130. (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
  131. errfunc, pglob);
  132. if (status == GLOB_NOMATCH)
  133. /* No matches in this directory. Try the next. */
  134. continue;
  135. if (status != 0)
  136. {
  137. globfree (&dirs);
  138. globfree (pglob);
  139. return status;
  140. }
  141. /* Stick the directory on the front of each name. */
  142. if (prefix_array (dirs.gl_pathv[i],
  143. &pglob->gl_pathv[oldcount],
  144. pglob->gl_pathc - oldcount,
  145. flags & GLOB_MARK))
  146. {
  147. globfree (&dirs);
  148. globfree (pglob);
  149. return GLOB_NOSPACE;
  150. }
  151. }
  152. flags |= GLOB_MAGCHAR;
  153. if (pglob->gl_pathc == oldcount)
  154. {
  155. /* No matches. */
  156. if (flags & GLOB_NOCHECK)
  157. {
  158. size_t len = strlen (pattern) + 1;
  159. char *patcopy = (char *) malloc (len);
  160. if (patcopy == NULL)
  161. return GLOB_NOSPACE;
  162. memcpy (patcopy, pattern, len);
  163. pglob->gl_pathv
  164. = (char **) realloc (pglob->gl_pathv,
  165. (pglob->gl_pathc +
  166. ((flags & GLOB_DOOFFS) ?
  167. pglob->gl_offs : 0) +
  168. 1 + 1) *
  169. sizeof (char *));
  170. if (pglob->gl_pathv == NULL)
  171. {
  172. free (patcopy);
  173. return GLOB_NOSPACE;
  174. }
  175. if (flags & GLOB_DOOFFS)
  176. while (pglob->gl_pathc < pglob->gl_offs)
  177. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  178. pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
  179. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  180. pglob->gl_flags = flags;
  181. }
  182. else
  183. {
  184. return GLOB_NOMATCH;
  185. }
  186. }
  187. }
  188. else
  189. {
  190. status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
  191. if (status != 0)
  192. return status;
  193. if (dirlen > 0)
  194. {
  195. /* Stick the directory on the front of each name. */
  196. if (prefix_array (dirname,
  197. &pglob->gl_pathv[oldcount],
  198. pglob->gl_pathc - oldcount,
  199. flags & GLOB_MARK))
  200. {
  201. globfree (pglob);
  202. return GLOB_NOSPACE;
  203. }
  204. }
  205. }
  206. if (flags & GLOB_MARK)
  207. {
  208. /* Append slashes to directory names. glob_in_dir has already
  209. allocated the extra character for us. */
  210. int i;
  211. struct stat st;
  212. for (i = oldcount; i < pglob->gl_pathc; ++i)
  213. if (lstat (pglob->gl_pathv[i], &st) == 0 &&
  214. S_ISDIR (st.st_mode))
  215. strcat (pglob->gl_pathv[i], "/");
  216. }
  217. if (!(flags & GLOB_NOSORT))
  218. /* Sort the vector. */
  219. qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
  220. pglob->gl_pathc - oldcount,
  221. sizeof (char *), (__compar_fn_t)collated_compare);
  222. return 0;
  223. }
  224. /* Free storage allocated in PGLOB by a previous `glob' call. */
  225. void
  226. globfree (pglob)
  227. register glob_t *pglob;
  228. {
  229. if (pglob->gl_pathv != NULL)
  230. {
  231. register int i;
  232. for (i = 0; i < pglob->gl_pathc; ++i)
  233. if (pglob->gl_pathv[i] != NULL)
  234. free ((__ptr_t) pglob->gl_pathv[i]);
  235. free ((__ptr_t) pglob->gl_pathv);
  236. }
  237. }
  238. /* Do a collated comparison of A and B. */
  239. static int
  240. collated_compare (a, b)
  241. const __ptr_t a;
  242. const __ptr_t b;
  243. {
  244. const char *const s1 = *(const char *const * const) a;
  245. const char *const s2 = *(const char *const * const) b;
  246. if (s1 == s2)
  247. return 0;
  248. if (s1 == NULL)
  249. return 1;
  250. if (s2 == NULL)
  251. return -1;
  252. return strcoll (s1, s2);
  253. }
  254. /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
  255. elements in place. Return nonzero if out of memory, zero if successful.
  256. A slash is inserted between DIRNAME and each elt of ARRAY,
  257. unless DIRNAME is just "/". Each old element of ARRAY is freed.
  258. If ADD_SLASH is non-zero, allocate one character more than
  259. necessary, so that a slash can be appended later. */
  260. static int
  261. prefix_array (dirname, array, n, add_slash)
  262. const char *dirname;
  263. char **array;
  264. size_t n;
  265. int add_slash;
  266. {
  267. register size_t i;
  268. size_t dirlen = strlen (dirname);
  269. if (dirlen == 1 && dirname[0] == '/')
  270. /* DIRNAME is just "/", so normal prepending would get us "//foo".
  271. We want "/foo" instead, so don't prepend any chars from DIRNAME. */
  272. dirlen = 0;
  273. for (i = 0; i < n; ++i)
  274. {
  275. size_t eltlen = strlen (array[i]) + 1;
  276. char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0));
  277. if (new == NULL)
  278. {
  279. while (i > 0)
  280. free ((__ptr_t) array[--i]);
  281. return 1;
  282. }
  283. memcpy (new, dirname, dirlen);
  284. new[dirlen] = '/';
  285. memcpy (&new[dirlen + 1], array[i], eltlen);
  286. free ((__ptr_t) array[i]);
  287. array[i] = new;
  288. }
  289. return 0;
  290. }
  291. /* Return nonzero if PATTERN contains any metacharacters.
  292. Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
  293. static int
  294. glob_pattern_p (pattern, quote)
  295. const char *pattern;
  296. int quote;
  297. {
  298. register const char *p;
  299. int open = 0;
  300. for (p = pattern; *p != '\0'; ++p)
  301. switch (*p)
  302. {
  303. case '?':
  304. case '*':
  305. return 1;
  306. case '\\':
  307. if (quote)
  308. ++p;
  309. break;
  310. case '[':
  311. open = 1;
  312. break;
  313. case ']':
  314. if (open)
  315. return 1;
  316. break;
  317. }
  318. return 0;
  319. }
  320. /* Like `glob', but PATTERN is a final pathname component,
  321. and matches are searched for in DIRECTORY.
  322. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
  323. The GLOB_APPEND flag is assumed to be set (always appends). */
  324. static int
  325. glob_in_dir (pattern, directory, flags, errfunc, pglob)
  326. const char *pattern;
  327. const char *directory;
  328. int flags;
  329. int (*errfunc) __P ((const char *, int));
  330. glob_t *pglob;
  331. {
  332. __ptr_t stream;
  333. struct globlink
  334. {
  335. struct globlink *next;
  336. char *name;
  337. };
  338. struct globlink *names = NULL;
  339. size_t nfound = 0;
  340. int meta;
  341. stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
  342. : (__ptr_t) opendir (directory));
  343. if (stream == NULL)
  344. {
  345. if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
  346. (flags & GLOB_ERR))
  347. return GLOB_ABORTED;
  348. }
  349. meta = glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
  350. if (meta)
  351. flags |= GLOB_MAGCHAR;
  352. while (1)
  353. {
  354. const char *name;
  355. size_t len;
  356. if (__glob_readdir_hook)
  357. {
  358. name = (*__glob_readdir_hook) (stream);
  359. if (name == NULL)
  360. break;
  361. len = 0;
  362. }
  363. else
  364. {
  365. struct dirent *d = readdir ((DIR *) stream);
  366. if (d == NULL)
  367. break;
  368. if (! (d->d_ino != 0))
  369. continue;
  370. name = d->d_name;
  371. #ifdef HAVE_D_NAMLEN
  372. len = d->d_namlen;
  373. #else
  374. len = 0;
  375. #endif
  376. }
  377. if ((!meta && strcmp (pattern, name) == 0)
  378. || fnmatch (pattern, name,
  379. (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
  380. ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
  381. {
  382. struct globlink *new
  383. = (struct globlink *) alloca (sizeof (struct globlink));
  384. if (len == 0)
  385. len = strlen (name);
  386. new->name
  387. = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
  388. if (new->name == NULL)
  389. goto memory_error;
  390. memcpy ((__ptr_t) new->name, name, len);
  391. new->name[len] = '\0';
  392. new->next = names;
  393. names = new;
  394. ++nfound;
  395. if (!meta)
  396. break;
  397. }
  398. }
  399. if (nfound == 0 && (flags & GLOB_NOCHECK))
  400. {
  401. size_t len = strlen (pattern);
  402. nfound = 1;
  403. names = (struct globlink *) alloca (sizeof (struct globlink));
  404. names->next = NULL;
  405. names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1);
  406. if (names->name == NULL)
  407. goto memory_error;
  408. memcpy (names->name, pattern, len);
  409. names->name[len] = '\0';
  410. }
  411. pglob->gl_pathv
  412. = (char **) realloc (pglob->gl_pathv,
  413. (pglob->gl_pathc +
  414. ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
  415. nfound + 1) *
  416. sizeof (char *));
  417. if (pglob->gl_pathv == NULL)
  418. goto memory_error;
  419. if (flags & GLOB_DOOFFS)
  420. while (pglob->gl_pathc < pglob->gl_offs)
  421. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  422. for (; names != NULL; names = names->next)
  423. pglob->gl_pathv[pglob->gl_pathc++] = names->name;
  424. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  425. pglob->gl_flags = flags;
  426. {
  427. int save = errno;
  428. if (__glob_closedir_hook)
  429. (*__glob_closedir_hook) (stream);
  430. else
  431. (void) closedir ((DIR *) stream);
  432. errno = save;
  433. }
  434. return nfound == 0 ? GLOB_NOMATCH : 0;
  435. memory_error:
  436. {
  437. int save = errno;
  438. if (__glob_closedir_hook)
  439. (*__glob_closedir_hook) (stream);
  440. else
  441. (void) closedir ((DIR *) stream);
  442. errno = save;
  443. }
  444. while (names != NULL)
  445. {
  446. if (names->name != NULL)
  447. free ((__ptr_t) names->name);
  448. names = names->next;
  449. }
  450. return GLOB_NOSPACE;
  451. }