glob.c 13 KB

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