glob.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. int oldcount;
  166. #ifdef SHELL
  167. {
  168. /* Make globbing interruptible in the bash shell. */
  169. extern int interrupt_state;
  170. if (interrupt_state)
  171. {
  172. globfree (&dirs);
  173. globfree (&files);
  174. return GLOB_ABEND;
  175. }
  176. }
  177. #endif /* SHELL. */
  178. oldcount = pglob->gl_pathc;
  179. status = glob_in_dir (filename, dirs.gl_pathv[i],
  180. (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
  181. errfunc, pglob);
  182. if (status == GLOB_NOMATCH)
  183. /* No matches in this directory. Try the next. */
  184. continue;
  185. if (status != 0)
  186. {
  187. globfree (&dirs);
  188. globfree (pglob);
  189. return status;
  190. }
  191. /* Stick the directory on the front of each name. */
  192. if (prefix_array (dirs.gl_pathv[i],
  193. &pglob->gl_pathv[oldcount],
  194. pglob->gl_pathc - oldcount,
  195. flags & GLOB_MARK))
  196. {
  197. globfree (&dirs);
  198. globfree (pglob);
  199. return GLOB_NOSPACE;
  200. }
  201. }
  202. flags |= GLOB_MAGCHAR;
  203. if (pglob->gl_pathc == oldcount)
  204. {
  205. /* No matches. */
  206. if (flags & GLOB_NOCHECK)
  207. {
  208. size_t len = strlen (pattern) + 1;
  209. char *patcopy = (char *) malloc (len);
  210. if (patcopy == NULL)
  211. return GLOB_NOSPACE;
  212. memcpy (patcopy, pattern, len);
  213. pglob->gl_pathv
  214. = (char **) realloc (pglob->gl_pathv,
  215. (pglob->gl_pathc +
  216. ((flags & GLOB_DOOFFS) ?
  217. pglob->gl_offs : 0) +
  218. 1 + 1) *
  219. sizeof (char *));
  220. if (pglob->gl_pathv == NULL)
  221. {
  222. free (patcopy);
  223. return GLOB_NOSPACE;
  224. }
  225. if (flags & GLOB_DOOFFS)
  226. while (pglob->gl_pathc < pglob->gl_offs)
  227. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  228. pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
  229. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  230. pglob->gl_flags = flags;
  231. }
  232. else
  233. {
  234. return GLOB_NOMATCH;
  235. }
  236. }
  237. }
  238. else
  239. {
  240. status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
  241. if (status != 0)
  242. return status;
  243. if (dirlen > 0)
  244. {
  245. /* Stick the directory on the front of each name. */
  246. if (prefix_array (dirname,
  247. &pglob->gl_pathv[oldcount],
  248. pglob->gl_pathc - oldcount,
  249. flags & GLOB_MARK))
  250. {
  251. globfree (pglob);
  252. return GLOB_NOSPACE;
  253. }
  254. }
  255. }
  256. if (flags & GLOB_MARK)
  257. {
  258. /* Append slashes to directory names. glob_in_dir has already
  259. allocated the extra character for us. */
  260. int i;
  261. struct stat st;
  262. for (i = oldcount; i < pglob->gl_pathc; ++i)
  263. if (lstat (pglob->gl_pathv[i], &st) == 0 &&
  264. S_ISDIR (st.st_mode))
  265. strcat (pglob->gl_pathv[i], "/");
  266. }
  267. if (!(flags & GLOB_NOSORT))
  268. /* Sort the vector. */
  269. qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
  270. pglob->gl_pathc - oldcount,
  271. sizeof (char *), (__compar_fn_t)collated_compare);
  272. return 0;
  273. }
  274. #ifdef __GLOB64
  275. libc_hidden_def(glob64)
  276. #else
  277. libc_hidden_def(glob)
  278. #endif
  279. /* Free storage allocated in PGLOB by a previous `glob' call. */
  280. void
  281. globfree (pglob)
  282. register glob_t *pglob;
  283. {
  284. if (pglob->gl_pathv != NULL)
  285. {
  286. register int i = pglob->gl_flags & GLOB_DOOFFS? pglob->gl_offs : 0;
  287. for (; i < pglob->gl_pathc; ++i)
  288. if (pglob->gl_pathv[i] != NULL)
  289. free ((__ptr_t) pglob->gl_pathv[i]);
  290. free ((__ptr_t) pglob->gl_pathv);
  291. }
  292. }
  293. #ifdef __GLOB64
  294. libc_hidden_def(globfree64)
  295. #else
  296. libc_hidden_def(globfree)
  297. #endif
  298. /* Do a collated comparison of A and B. */
  299. static int
  300. collated_compare (a, b)
  301. const __ptr_t a;
  302. const __ptr_t b;
  303. {
  304. const char *const s1 = *(const char *const *) a;
  305. const char *const s2 = *(const char *const *) b;
  306. if (s1 == s2)
  307. return 0;
  308. if (s1 == NULL)
  309. return 1;
  310. if (s2 == NULL)
  311. return -1;
  312. return strcoll (s1, s2);
  313. }
  314. /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
  315. elements in place. Return nonzero if out of memory, zero if successful.
  316. A slash is inserted between DIRNAME and each elt of ARRAY,
  317. unless DIRNAME is just "/". Each old element of ARRAY is freed.
  318. If ADD_SLASH is non-zero, allocate one character more than
  319. necessary, so that a slash can be appended later. */
  320. static int
  321. prefix_array (dirname, array, n, add_slash)
  322. const char *dirname;
  323. char **array;
  324. size_t n;
  325. int add_slash;
  326. {
  327. register size_t i;
  328. size_t dirlen = strlen (dirname);
  329. if (dirlen == 1 && dirname[0] == '/')
  330. /* DIRNAME is just "/", so normal prepending would get us "//foo".
  331. We want "/foo" instead, so don't prepend any chars from DIRNAME. */
  332. dirlen = 0;
  333. for (i = 0; i < n; ++i)
  334. {
  335. size_t eltlen = strlen (array[i]) + 1;
  336. char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0));
  337. if (new == NULL)
  338. {
  339. while (i > 0)
  340. free ((__ptr_t) array[--i]);
  341. return 1;
  342. }
  343. memcpy (new, dirname, dirlen);
  344. new[dirlen] = '/';
  345. memcpy (&new[dirlen + 1], array[i], eltlen);
  346. free ((__ptr_t) array[i]);
  347. array[i] = new;
  348. }
  349. return 0;
  350. }
  351. /* Like `glob', but PATTERN is a final pathname component,
  352. and matches are searched for in DIRECTORY.
  353. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
  354. The GLOB_APPEND flag is assumed to be set (always appends). */
  355. static int
  356. glob_in_dir (pattern, directory, flags, errfunc, pglob)
  357. const char *pattern;
  358. const char *directory;
  359. int flags;
  360. int (*errfunc) __P ((const char *, int));
  361. glob_t *pglob;
  362. {
  363. __ptr_t stream;
  364. struct globlink
  365. {
  366. struct globlink *next;
  367. char *name;
  368. };
  369. struct globlink *names = NULL;
  370. size_t nfound = 0;
  371. int meta;
  372. stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
  373. : (__ptr_t) opendir (directory));
  374. if (stream == NULL)
  375. {
  376. if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
  377. (flags & GLOB_ERR))
  378. return GLOB_ABORTED;
  379. }
  380. meta = glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
  381. if (meta)
  382. flags |= GLOB_MAGCHAR;
  383. while (1)
  384. {
  385. const char *name;
  386. size_t len;
  387. if (__glob_readdir_hook)
  388. {
  389. name = (*__glob_readdir_hook) (stream);
  390. if (name == NULL)
  391. break;
  392. len = 0;
  393. }
  394. else
  395. {
  396. struct dirent *d = __readdir ((DIR *) stream);
  397. if (d == NULL)
  398. break;
  399. if (! (d->d_ino != 0))
  400. continue;
  401. name = d->d_name;
  402. #ifdef _DIRENT_HAVE_D_NAMLEN
  403. len = d->d_namlen;
  404. #else
  405. len = 0;
  406. #endif
  407. }
  408. if ((!meta && strcmp (pattern, name) == 0)
  409. || fnmatch (pattern, name,
  410. (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
  411. ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
  412. {
  413. struct globlink *new
  414. = (struct globlink *) alloca (sizeof (struct globlink));
  415. if (len == 0)
  416. len = strlen (name);
  417. new->name
  418. = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
  419. if (new->name == NULL)
  420. goto memory_error;
  421. memcpy ((__ptr_t) new->name, name, len);
  422. new->name[len] = '\0';
  423. new->next = names;
  424. names = new;
  425. ++nfound;
  426. if (!meta)
  427. break;
  428. }
  429. }
  430. if (nfound == 0 && (flags & GLOB_NOCHECK))
  431. {
  432. size_t len = strlen (pattern);
  433. nfound = 1;
  434. names = (struct globlink *) alloca (sizeof (struct globlink));
  435. names->next = NULL;
  436. names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1);
  437. if (names->name == NULL)
  438. goto memory_error;
  439. memcpy (names->name, pattern, len);
  440. names->name[len] = '\0';
  441. }
  442. pglob->gl_pathv
  443. = (char **) realloc (pglob->gl_pathv,
  444. (pglob->gl_pathc +
  445. ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
  446. nfound + 1) *
  447. sizeof (char *));
  448. if (pglob->gl_pathv == NULL)
  449. goto memory_error;
  450. if (flags & GLOB_DOOFFS)
  451. while (pglob->gl_pathc < pglob->gl_offs)
  452. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  453. for (; names != NULL; names = names->next)
  454. pglob->gl_pathv[pglob->gl_pathc++] = names->name;
  455. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  456. pglob->gl_flags = flags;
  457. {
  458. int save = errno;
  459. if (__glob_closedir_hook)
  460. (*__glob_closedir_hook) (stream);
  461. else
  462. (void) closedir ((DIR *) stream);
  463. errno = save;
  464. }
  465. return nfound == 0 ? GLOB_NOMATCH : 0;
  466. memory_error:
  467. {
  468. int save = errno;
  469. if (__glob_closedir_hook)
  470. (*__glob_closedir_hook) (stream);
  471. else
  472. (void) closedir ((DIR *) stream);
  473. errno = save;
  474. }
  475. while (names != NULL)
  476. {
  477. if (names->name != NULL)
  478. free ((__ptr_t) names->name);
  479. names = names->next;
  480. }
  481. return GLOB_NOSPACE;
  482. }