glob.c 13 KB

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