glob.c 13 KB

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