glob.c 12 KB

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