glob.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /* Copyright (C) 1991-2002,2003,2004,2005,2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #undef ENABLE_GLOB_BRACE_EXPANSION
  15. #undef ENABLE_GLOB_TILDE_EXPANSION
  16. #include <features.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include <dirent.h>
  24. #include <malloc.h>
  25. #include <fnmatch.h>
  26. #include <glob.h>
  27. #ifdef ENABLE_GLOB_TILDE_EXPANSION
  28. #include <pwd.h>
  29. #endif
  30. #ifdef COMPILE_GLOB64
  31. #undef stat
  32. #define stat stat64
  33. #define struct_stat64 struct stat64
  34. #define __stat64(fname, buf) stat64 (fname, buf)
  35. #define dirent dirent64
  36. #define __readdir readdir64
  37. #define __readdir64 readdir64
  38. #define glob_t glob64_t
  39. #define glob(pattern, flags, errfunc, pglob) glob64 (pattern, flags, errfunc, pglob)
  40. #define globfree(pglob) globfree64 (pglob)
  41. #else
  42. #define __readdir readdir
  43. #define __readdir64 readdir64
  44. #define struct_stat64 struct stat
  45. #define __stat64(fname, buf) stat (fname, buf)
  46. #endif
  47. /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
  48. if the `d_type' member for `struct dirent' is available.
  49. HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
  50. #if defined _DIRENT_HAVE_D_TYPE
  51. /* True if the directory entry D must be of type T. */
  52. # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
  53. /* True if the directory entry D might be a symbolic link. */
  54. # define DIRENT_MIGHT_BE_SYMLINK(d) \
  55. ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
  56. /* True if the directory entry D might be a directory. */
  57. # define DIRENT_MIGHT_BE_DIR(d) \
  58. ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
  59. #else /* !HAVE_D_TYPE */
  60. # define DIRENT_MUST_BE(d, t) false
  61. # define DIRENT_MIGHT_BE_SYMLINK(d) true
  62. # define DIRENT_MIGHT_BE_DIR(d) true
  63. #endif /* HAVE_D_TYPE */
  64. # define NAMLEN(dirent) strlen((dirent)->d_name)
  65. #ifdef _D_NAMLEN
  66. # undef NAMLEN
  67. # define NAMLEN(d) _D_NAMLEN(d)
  68. #endif
  69. # if defined _DIRENT_HAVE_D_NAMLEN
  70. # define CONVERT_D_NAMLEN(d64, d32) (d64)->d_namlen = (d32)->d_namlen;
  71. # else
  72. # define CONVERT_D_NAMLEN(d64, d32)
  73. # endif
  74. # define CONVERT_D_INO(d64, d32) (d64)->d_ino = (d32)->d_ino;
  75. # ifdef _DIRENT_HAVE_D_TYPE
  76. # define CONVERT_D_TYPE(d64, d32) (d64)->d_type = (d32)->d_type;
  77. # else
  78. # define CONVERT_D_TYPE(d64, d32)
  79. # endif
  80. # define CONVERT_DIRENT_DIRENT64(d64, d32) \
  81. memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
  82. CONVERT_D_NAMLEN (d64, d32) \
  83. CONVERT_D_INO (d64, d32) \
  84. CONVERT_D_TYPE (d64, d32)
  85. extern int __collated_compare (const void *a, const void *b) attribute_hidden;
  86. extern int __prefix_array (const char *dirname, char **array, size_t n) attribute_hidden;
  87. #if defined ENABLE_GLOB_BRACE_EXPANSION
  88. extern const char *__next_brace_sub (const char *cp, int flags) attribute_hidden;
  89. #endif
  90. #ifndef COMPILE_GLOB64
  91. /* Return nonzero if PATTERN contains any metacharacters.
  92. Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
  93. int glob_pattern_p(const char *pattern, int quote)
  94. {
  95. register const char *p;
  96. int open = 0;
  97. for (p = pattern; *p != '\0'; ++p)
  98. switch (*p)
  99. {
  100. case '?':
  101. case '*':
  102. return 1;
  103. case '\\':
  104. if (quote && p[1] != '\0')
  105. ++p;
  106. break;
  107. case '[':
  108. open = 1;
  109. break;
  110. case ']':
  111. if (open)
  112. return 1;
  113. break;
  114. }
  115. return 0;
  116. }
  117. libc_hidden_def(glob_pattern_p)
  118. /* Do a collated comparison of A and B. */
  119. int __collated_compare (const void *a, const void *b)
  120. {
  121. const char *const s1 = *(const char *const * const) a;
  122. const char *const s2 = *(const char *const * const) b;
  123. if (s1 == s2)
  124. return 0;
  125. if (s1 == NULL)
  126. return 1;
  127. if (s2 == NULL)
  128. return -1;
  129. return strcoll (s1, s2);
  130. }
  131. /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
  132. elements in place. Return nonzero if out of memory, zero if successful.
  133. A slash is inserted between DIRNAME and each elt of ARRAY,
  134. unless DIRNAME is just "/". Each old element of ARRAY is freed.
  135. If ADD_SLASH is non-zero, allocate one character more than
  136. necessary, so that a slash can be appended later. */
  137. int __prefix_array (const char *dirname, char **array, size_t n)
  138. {
  139. register size_t i;
  140. size_t dirlen = strlen (dirname);
  141. # define DIRSEP_CHAR '/'
  142. if (dirlen == 1 && dirname[0] == '/')
  143. /* DIRNAME is just "/", so normal prepending would get us "//foo".
  144. We want "/foo" instead, so don't prepend any chars from DIRNAME. */
  145. dirlen = 0;
  146. for (i = 0; i < n; ++i)
  147. {
  148. size_t eltlen = strlen (array[i]) + 1;
  149. char *new = (char *) malloc (dirlen + 1 + eltlen);
  150. if (new == NULL)
  151. {
  152. while (i > 0)
  153. free (array[--i]);
  154. return 1;
  155. }
  156. {
  157. char *endp = mempcpy (new, dirname, dirlen);
  158. *endp++ = DIRSEP_CHAR;
  159. mempcpy (endp, array[i], eltlen);
  160. }
  161. free (array[i]);
  162. array[i] = new;
  163. }
  164. return 0;
  165. }
  166. #if defined ENABLE_GLOB_BRACE_EXPANSION
  167. /* Find the end of the sub-pattern in a brace expression. */
  168. const char *
  169. __next_brace_sub (const char *cp, int flags)
  170. {
  171. unsigned int depth = 0;
  172. while (*cp != '\0')
  173. if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
  174. {
  175. if (*++cp == '\0')
  176. break;
  177. ++cp;
  178. }
  179. else
  180. {
  181. if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
  182. break;
  183. if (*cp++ == '{')
  184. depth++;
  185. }
  186. return *cp != '\0' ? cp : NULL;
  187. }
  188. #endif
  189. #endif
  190. static int
  191. link_exists_p (const char *dir, size_t dirlen, const char *fname,
  192. glob_t *pglob, int flags)
  193. {
  194. size_t fnamelen = strlen (fname);
  195. char *fullname = (char *) alloca (dirlen + 1 + fnamelen + 1);
  196. struct stat st;
  197. struct_stat64 st64;
  198. mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
  199. fname, fnamelen + 1);
  200. return (((flags & GLOB_ALTDIRFUNC)
  201. ? (*pglob->gl_stat) (fullname, &st)
  202. : __stat64 (fullname, &st64)) == 0);
  203. }
  204. /* Like `glob', but PATTERN is a final pathname component,
  205. and matches are searched for in DIRECTORY.
  206. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
  207. The GLOB_APPEND flag is assumed to be set (always appends). */
  208. static int glob_in_dir (const char *pattern, const char *directory, int flags,
  209. int (*errfunc) (const char *, int),
  210. glob_t *pglob)
  211. {
  212. size_t dirlen = strlen (directory);
  213. void *stream = NULL;
  214. struct globlink
  215. {
  216. struct globlink *next;
  217. char *name;
  218. };
  219. struct globlink *names = NULL;
  220. size_t nfound;
  221. int meta;
  222. int save;
  223. meta = glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
  224. if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
  225. {
  226. /* We need not do any tests. The PATTERN contains no meta
  227. characters and we must not return an error therefore the
  228. result will always contain exactly one name. */
  229. flags |= GLOB_NOCHECK;
  230. nfound = 0;
  231. }
  232. else if (meta == 0 &&
  233. ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
  234. {
  235. /* Since we use the normal file functions we can also use stat()
  236. to verify the file is there. */
  237. struct stat st;
  238. struct_stat64 st64;
  239. size_t patlen = strlen (pattern);
  240. char *fullname = (char *) alloca (dirlen + 1 + patlen + 1);
  241. mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
  242. "/", 1),
  243. pattern, patlen + 1);
  244. if (((flags & GLOB_ALTDIRFUNC)
  245. ? (*pglob->gl_stat) (fullname, &st)
  246. : __stat64 (fullname, &st64)) == 0)
  247. /* We found this file to be existing. Now tell the rest
  248. of the function to copy this name into the result. */
  249. flags |= GLOB_NOCHECK;
  250. nfound = 0;
  251. }
  252. else
  253. {
  254. if (pattern[0] == '\0')
  255. {
  256. /* This is a special case for matching directories like in
  257. "*a/". */
  258. names = (struct globlink *) alloca (sizeof (struct globlink));
  259. names->name = (char *) malloc (1);
  260. if (names->name == NULL)
  261. goto memory_error;
  262. names->name[0] = '\0';
  263. names->next = NULL;
  264. nfound = 1;
  265. meta = 0;
  266. }
  267. else
  268. {
  269. stream = ((flags & GLOB_ALTDIRFUNC)
  270. ? (*pglob->gl_opendir) (directory)
  271. : opendir (directory));
  272. if (stream == NULL)
  273. {
  274. if (errno != ENOTDIR
  275. && ((errfunc != NULL && (*errfunc) (directory, errno))
  276. || (flags & GLOB_ERR)))
  277. return GLOB_ABORTED;
  278. nfound = 0;
  279. meta = 0;
  280. }
  281. else
  282. {
  283. int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
  284. | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
  285. );
  286. nfound = 0;
  287. flags |= GLOB_MAGCHAR;
  288. while (1)
  289. {
  290. const char *name;
  291. size_t len;
  292. #if !defined COMPILE_GLOB64
  293. struct dirent64 *d;
  294. union
  295. {
  296. struct dirent64 d64;
  297. char room [offsetof (struct dirent64, d_name[0])
  298. + NAME_MAX + 1];
  299. }
  300. d64buf;
  301. if (flags & GLOB_ALTDIRFUNC)
  302. {
  303. struct dirent *d32 = (*pglob->gl_readdir) (stream);
  304. if (d32 != NULL)
  305. {
  306. CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
  307. d = &d64buf.d64;
  308. }
  309. else
  310. d = NULL;
  311. }
  312. else
  313. d = __readdir64 (stream);
  314. #else
  315. struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
  316. ? ((struct dirent *)
  317. (*pglob->gl_readdir) (stream))
  318. : __readdir (stream));
  319. #endif
  320. if (d == NULL)
  321. break;
  322. # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
  323. if (! REAL_DIR_ENTRY (d))
  324. continue;
  325. /* If we shall match only directories use the information
  326. provided by the dirent call if possible. */
  327. if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
  328. continue;
  329. name = d->d_name;
  330. if (fnmatch (pattern, name, fnm_flags) == 0)
  331. {
  332. /* If the file we found is a symlink we have to
  333. make sure the target file exists. */
  334. if (!DIRENT_MIGHT_BE_SYMLINK (d)
  335. || link_exists_p (directory, dirlen, name, pglob,
  336. flags))
  337. {
  338. struct globlink *new = (struct globlink *)
  339. alloca (sizeof (struct globlink));
  340. len = NAMLEN (d);
  341. new->name = (char *) malloc (len + 1);
  342. if (new->name == NULL)
  343. goto memory_error;
  344. *((char *) mempcpy (new->name, name, len)) = '\0';
  345. new->next = names;
  346. names = new;
  347. ++nfound;
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. if (nfound == 0 && (flags & GLOB_NOCHECK))
  355. {
  356. size_t len = strlen (pattern);
  357. nfound = 1;
  358. names = (struct globlink *) alloca (sizeof (struct globlink));
  359. names->next = NULL;
  360. names->name = (char *) malloc (len + 1);
  361. if (names->name == NULL)
  362. goto memory_error;
  363. *((char *) mempcpy (names->name, pattern, len)) = '\0';
  364. }
  365. if (nfound != 0)
  366. {
  367. char **new_gl_pathv;
  368. new_gl_pathv
  369. = (char **) realloc (pglob->gl_pathv,
  370. (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
  371. * sizeof (char *));
  372. if (new_gl_pathv == NULL)
  373. goto memory_error;
  374. pglob->gl_pathv = new_gl_pathv;
  375. for (; names != NULL; names = names->next)
  376. pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
  377. pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
  378. pglob->gl_flags = flags;
  379. }
  380. save = errno;
  381. if (stream != NULL)
  382. {
  383. if (flags & GLOB_ALTDIRFUNC)
  384. (*pglob->gl_closedir) (stream);
  385. else
  386. closedir (stream);
  387. }
  388. __set_errno (save);
  389. return nfound == 0 ? GLOB_NOMATCH : 0;
  390. memory_error:
  391. {
  392. int save2 = errno;
  393. if (flags & GLOB_ALTDIRFUNC)
  394. (*pglob->gl_closedir) (stream);
  395. else
  396. closedir (stream);
  397. __set_errno (save2);
  398. }
  399. while (names != NULL)
  400. {
  401. free (names->name);
  402. names = names->next;
  403. }
  404. return GLOB_NOSPACE;
  405. }
  406. /* Do glob searching for PATTERN, placing results in PGLOB.
  407. The bits defined above may be set in FLAGS.
  408. If a directory cannot be opened or read and ERRFUNC is not nil,
  409. it is called with the pathname that caused the error, and the
  410. `errno' value from the failing call; if it returns non-zero
  411. `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
  412. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  413. Otherwise, `glob' returns zero. */
  414. int
  415. glob (
  416. const char *pattern,
  417. int flags,
  418. int (*errfunc) (const char *, int),
  419. glob_t *pglob)
  420. {
  421. const char *filename;
  422. const char *dirname;
  423. size_t dirlen;
  424. int status;
  425. size_t oldcount;
  426. if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  427. {
  428. __set_errno (EINVAL);
  429. return -1;
  430. }
  431. if (!(flags & GLOB_DOOFFS))
  432. /* Have to do this so `globfree' knows where to start freeing. It
  433. also makes all the code that uses gl_offs simpler. */
  434. pglob->gl_offs = 0;
  435. #if defined ENABLE_GLOB_BRACE_EXPANSION
  436. if (flags & GLOB_BRACE)
  437. {
  438. const char *begin;
  439. if (flags & GLOB_NOESCAPE)
  440. begin = strchr (pattern, '{');
  441. else
  442. {
  443. begin = pattern;
  444. while (1)
  445. {
  446. if (*begin == '\0')
  447. {
  448. begin = NULL;
  449. break;
  450. }
  451. if (*begin == '\\' && begin[1] != '\0')
  452. ++begin;
  453. else if (*begin == '{')
  454. break;
  455. ++begin;
  456. }
  457. }
  458. if (begin != NULL)
  459. {
  460. /* Allocate working buffer large enough for our work. Note that
  461. we have at least an opening and closing brace. */
  462. size_t firstc;
  463. char *alt_start;
  464. const char *p;
  465. const char *next;
  466. const char *rest;
  467. size_t rest_len;
  468. char onealt[strlen (pattern) - 1];
  469. /* We know the prefix for all sub-patterns. */
  470. alt_start = mempcpy (onealt, pattern, begin - pattern);
  471. /* Find the first sub-pattern and at the same time find the
  472. rest after the closing brace. */
  473. next = __next_brace_sub (begin + 1, flags);
  474. if (next == NULL)
  475. {
  476. /* It is an illegal expression. */
  477. return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  478. }
  479. /* Now find the end of the whole brace expression. */
  480. rest = next;
  481. while (*rest != '}')
  482. {
  483. rest = __next_brace_sub (rest + 1, flags);
  484. if (rest == NULL)
  485. {
  486. /* It is an illegal expression. */
  487. return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  488. }
  489. }
  490. /* Please note that we now can be sure the brace expression
  491. is well-formed. */
  492. rest_len = strlen (++rest) + 1;
  493. /* We have a brace expression. BEGIN points to the opening {,
  494. NEXT points past the terminator of the first element, and END
  495. points past the final }. We will accumulate result names from
  496. recursive runs for each brace alternative in the buffer using
  497. GLOB_APPEND. */
  498. if (!(flags & GLOB_APPEND))
  499. {
  500. /* This call is to set a new vector, so clear out the
  501. vector so we can append to it. */
  502. pglob->gl_pathc = 0;
  503. pglob->gl_pathv = NULL;
  504. }
  505. firstc = pglob->gl_pathc;
  506. p = begin + 1;
  507. while (1)
  508. {
  509. int result;
  510. /* Construct the new glob expression. */
  511. mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
  512. result = glob (onealt,
  513. ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
  514. | GLOB_APPEND), errfunc, pglob);
  515. /* If we got an error, return it. */
  516. if (result && result != GLOB_NOMATCH)
  517. {
  518. if (!(flags & GLOB_APPEND))
  519. {
  520. globfree (pglob);
  521. pglob->gl_pathc = 0;
  522. }
  523. return result;
  524. }
  525. if (*next == '}')
  526. /* We saw the last entry. */
  527. break;
  528. p = next + 1;
  529. next = __next_brace_sub (p, flags);
  530. /* assert (next != NULL); */
  531. }
  532. if (pglob->gl_pathc != firstc)
  533. /* We found some entries. */
  534. return 0;
  535. else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
  536. return GLOB_NOMATCH;
  537. }
  538. }
  539. #endif
  540. /* Find the filename. */
  541. filename = strrchr (pattern, '/');
  542. if (filename == NULL)
  543. {
  544. /* This can mean two things: a simple name or "~name". The latter
  545. case is nothing but a notation for a directory. */
  546. if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
  547. {
  548. dirname = pattern;
  549. dirlen = strlen (pattern);
  550. /* Set FILENAME to NULL as a special flag. This is ugly but
  551. other solutions would require much more code. We test for
  552. this special case below. */
  553. filename = NULL;
  554. }
  555. else
  556. {
  557. filename = pattern;
  558. dirname = ".";
  559. dirlen = 0;
  560. }
  561. }
  562. else if (filename == pattern)
  563. {
  564. /* "/pattern". */
  565. dirname = "/";
  566. dirlen = 1;
  567. ++filename;
  568. }
  569. else
  570. {
  571. char *newp;
  572. dirlen = filename - pattern;
  573. newp = (char *) alloca (dirlen + 1);
  574. *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
  575. dirname = newp;
  576. ++filename;
  577. if (filename[0] == '\0'
  578. && dirlen > 1)
  579. /* "pattern/". Expand "pattern", appending slashes. */
  580. {
  581. int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
  582. if (val == 0)
  583. pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
  584. | (flags & GLOB_MARK));
  585. return val;
  586. }
  587. }
  588. if (!(flags & GLOB_APPEND))
  589. {
  590. pglob->gl_pathc = 0;
  591. if (!(flags & GLOB_DOOFFS))
  592. pglob->gl_pathv = NULL;
  593. else
  594. {
  595. size_t i;
  596. pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
  597. * sizeof (char *));
  598. if (pglob->gl_pathv == NULL)
  599. return GLOB_NOSPACE;
  600. for (i = 0; i <= pglob->gl_offs; ++i)
  601. pglob->gl_pathv[i] = NULL;
  602. }
  603. }
  604. oldcount = pglob->gl_pathc + pglob->gl_offs;
  605. #if defined ENABLE_GLOB_TILDE_EXPANSION
  606. if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
  607. {
  608. if (dirname[1] == '\0' || dirname[1] == '/')
  609. {
  610. /* Look up home directory. */
  611. const char *home_dir = getenv ("HOME");
  612. if (home_dir == NULL || home_dir[0] == '\0')
  613. {
  614. int success;
  615. char *name;
  616. # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
  617. size_t buflen = GET_LOGIN_NAME_MAX () + 1;
  618. if (buflen == 0)
  619. /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
  620. a moderate value. */
  621. buflen = 20;
  622. name = (char *) alloca (buflen);
  623. success = getlogin_r (name, buflen) == 0;
  624. if (success)
  625. {
  626. struct passwd *p;
  627. # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
  628. long int pwbuflen = GETPW_R_SIZE_MAX ();
  629. char *pwtmpbuf;
  630. struct passwd pwbuf;
  631. int save = errno;
  632. pwtmpbuf = (char *) alloca (pwbuflen);
  633. while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
  634. != 0)
  635. {
  636. if (errno != ERANGE)
  637. {
  638. p = NULL;
  639. break;
  640. }
  641. pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
  642. 2 * pwbuflen);
  643. __set_errno (save);
  644. }
  645. if (p != NULL)
  646. home_dir = p->pw_dir;
  647. }
  648. }
  649. if (home_dir == NULL || home_dir[0] == '\0')
  650. {
  651. if (flags & GLOB_TILDE_CHECK)
  652. return GLOB_NOMATCH;
  653. else
  654. home_dir = "~"; /* No luck. */
  655. }
  656. /* Now construct the full directory. */
  657. if (dirname[1] == '\0')
  658. dirname = home_dir;
  659. else
  660. {
  661. char *newp;
  662. size_t home_len = strlen (home_dir);
  663. newp = (char *) alloca (home_len + dirlen);
  664. mempcpy (mempcpy (newp, home_dir, home_len),
  665. &dirname[1], dirlen);
  666. dirname = newp;
  667. }
  668. }
  669. else
  670. {
  671. char *end_name = strchr (dirname, '/');
  672. const char *user_name;
  673. const char *home_dir;
  674. if (end_name == NULL)
  675. user_name = dirname + 1;
  676. else
  677. {
  678. char *newp;
  679. newp = (char *) alloca (end_name - dirname);
  680. *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
  681. = '\0';
  682. user_name = newp;
  683. }
  684. /* Look up specific user's home directory. */
  685. {
  686. struct passwd *p;
  687. long int buflen = GETPW_R_SIZE_MAX ();
  688. char *pwtmpbuf;
  689. struct passwd pwbuf;
  690. int save = errno;
  691. pwtmpbuf = (char *) alloca (buflen);
  692. while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
  693. {
  694. if (errno != ERANGE)
  695. {
  696. p = NULL;
  697. break;
  698. }
  699. pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
  700. __set_errno (save);
  701. }
  702. if (p != NULL)
  703. home_dir = p->pw_dir;
  704. else
  705. home_dir = NULL;
  706. }
  707. /* If we found a home directory use this. */
  708. if (home_dir != NULL)
  709. {
  710. char *newp;
  711. size_t home_len = strlen (home_dir);
  712. size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
  713. newp = (char *) alloca (home_len + rest_len + 1);
  714. *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
  715. end_name, rest_len)) = '\0';
  716. dirname = newp;
  717. }
  718. else
  719. if (flags & GLOB_TILDE_CHECK)
  720. /* We have to regard it as an error if we cannot find the
  721. home directory. */
  722. return GLOB_NOMATCH;
  723. }
  724. }
  725. /* Now test whether we looked for "~" or "~NAME". In this case we
  726. can give the answer now. */
  727. if (filename == NULL)
  728. {
  729. struct stat st;
  730. struct_stat64 st64;
  731. /* Return the directory if we don't check for error or if it exists. */
  732. if ((flags & GLOB_NOCHECK)
  733. || (((flags & GLOB_ALTDIRFUNC)
  734. ? ((*pglob->gl_stat) (dirname, &st) == 0
  735. && S_ISDIR (st.st_mode))
  736. : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
  737. {
  738. int newcount = pglob->gl_pathc + pglob->gl_offs;
  739. char **new_gl_pathv;
  740. new_gl_pathv
  741. = (char **) realloc (pglob->gl_pathv,
  742. (newcount + 1 + 1) * sizeof (char *));
  743. if (new_gl_pathv == NULL)
  744. {
  745. nospace:
  746. free (pglob->gl_pathv);
  747. pglob->gl_pathv = NULL;
  748. pglob->gl_pathc = 0;
  749. return GLOB_NOSPACE;
  750. }
  751. pglob->gl_pathv = new_gl_pathv;
  752. pglob->gl_pathv[newcount] = strdup (dirname);
  753. if (pglob->gl_pathv[newcount] == NULL)
  754. goto nospace;
  755. pglob->gl_pathv[++newcount] = NULL;
  756. ++pglob->gl_pathc;
  757. pglob->gl_flags = flags;
  758. return 0;
  759. }
  760. /* Not found. */
  761. return GLOB_NOMATCH;
  762. }
  763. #endif
  764. if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
  765. {
  766. /* The directory name contains metacharacters, so we
  767. have to glob for the directory, and then glob for
  768. the pattern in each directory found. */
  769. glob_t dirs;
  770. size_t i;
  771. if ((flags & GLOB_ALTDIRFUNC) != 0)
  772. {
  773. /* Use the alternative access functions also in the recursive
  774. call. */
  775. dirs.gl_opendir = pglob->gl_opendir;
  776. dirs.gl_readdir = pglob->gl_readdir;
  777. dirs.gl_closedir = pglob->gl_closedir;
  778. dirs.gl_stat = pglob->gl_stat;
  779. dirs.gl_lstat = pglob->gl_lstat;
  780. }
  781. status = glob (dirname,
  782. ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
  783. | GLOB_ALTDIRFUNC))
  784. | GLOB_NOSORT | GLOB_ONLYDIR),
  785. errfunc, &dirs);
  786. if (status != 0)
  787. return status;
  788. /* We have successfully globbed the preceding directory name.
  789. For each name we found, call glob_in_dir on it and FILENAME,
  790. appending the results to PGLOB. */
  791. for (i = 0; i < dirs.gl_pathc; ++i)
  792. {
  793. int old_pathc;
  794. old_pathc = pglob->gl_pathc;
  795. status = glob_in_dir (filename, dirs.gl_pathv[i],
  796. ((flags | GLOB_APPEND)
  797. & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
  798. errfunc, pglob);
  799. if (status == GLOB_NOMATCH)
  800. /* No matches in this directory. Try the next. */
  801. continue;
  802. if (status != 0)
  803. {
  804. globfree (&dirs);
  805. globfree (pglob);
  806. pglob->gl_pathc = 0;
  807. return status;
  808. }
  809. /* Stick the directory on the front of each name. */
  810. if (__prefix_array (dirs.gl_pathv[i],
  811. &pglob->gl_pathv[old_pathc + pglob->gl_offs],
  812. pglob->gl_pathc - old_pathc))
  813. {
  814. globfree (&dirs);
  815. globfree (pglob);
  816. pglob->gl_pathc = 0;
  817. return GLOB_NOSPACE;
  818. }
  819. }
  820. flags |= GLOB_MAGCHAR;
  821. /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
  822. But if we have not found any matching entry and the GLOB_NOCHECK
  823. flag was set we must return the input pattern itself. */
  824. if (pglob->gl_pathc + pglob->gl_offs == oldcount)
  825. {
  826. /* No matches. */
  827. if (flags & GLOB_NOCHECK)
  828. {
  829. int newcount = pglob->gl_pathc + pglob->gl_offs;
  830. char **new_gl_pathv;
  831. new_gl_pathv = (char **) realloc (pglob->gl_pathv,
  832. (newcount + 2)
  833. * sizeof (char *));
  834. if (new_gl_pathv == NULL)
  835. {
  836. globfree (&dirs);
  837. return GLOB_NOSPACE;
  838. }
  839. pglob->gl_pathv = new_gl_pathv;
  840. pglob->gl_pathv[newcount] = strdup (pattern);
  841. if (pglob->gl_pathv[newcount] == NULL)
  842. {
  843. globfree (&dirs);
  844. globfree (pglob);
  845. pglob->gl_pathc = 0;
  846. return GLOB_NOSPACE;
  847. }
  848. ++pglob->gl_pathc;
  849. ++newcount;
  850. pglob->gl_pathv[newcount] = NULL;
  851. pglob->gl_flags = flags;
  852. }
  853. else
  854. {
  855. globfree (&dirs);
  856. return GLOB_NOMATCH;
  857. }
  858. }
  859. globfree (&dirs);
  860. }
  861. else
  862. {
  863. int old_pathc = pglob->gl_pathc;
  864. status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
  865. if (status != 0)
  866. return status;
  867. if (dirlen > 0)
  868. {
  869. /* Stick the directory on the front of each name. */
  870. if (__prefix_array (dirname,
  871. &pglob->gl_pathv[old_pathc + pglob->gl_offs],
  872. pglob->gl_pathc - old_pathc))
  873. {
  874. globfree (pglob);
  875. pglob->gl_pathc = 0;
  876. return GLOB_NOSPACE;
  877. }
  878. }
  879. }
  880. if (flags & GLOB_MARK)
  881. {
  882. /* Append slashes to directory names. */
  883. size_t i;
  884. struct stat st;
  885. struct_stat64 st64;
  886. for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
  887. if (((flags & GLOB_ALTDIRFUNC)
  888. ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
  889. && S_ISDIR (st.st_mode))
  890. : (__stat64 (pglob->gl_pathv[i], &st64) == 0
  891. && S_ISDIR (st64.st_mode))))
  892. {
  893. size_t len = strlen (pglob->gl_pathv[i]) + 2;
  894. char *new = realloc (pglob->gl_pathv[i], len);
  895. if (new == NULL)
  896. {
  897. globfree (pglob);
  898. pglob->gl_pathc = 0;
  899. return GLOB_NOSPACE;
  900. }
  901. strcpy (&new[len - 2], "/");
  902. pglob->gl_pathv[i] = new;
  903. }
  904. }
  905. if (!(flags & GLOB_NOSORT))
  906. {
  907. /* Sort the vector. */
  908. qsort (&pglob->gl_pathv[oldcount],
  909. pglob->gl_pathc + pglob->gl_offs - oldcount,
  910. sizeof (char *), __collated_compare);
  911. }
  912. return 0;
  913. }
  914. #ifdef COMPILE_GLOB64
  915. libc_hidden_def(glob64)
  916. #else
  917. libc_hidden_def(glob)
  918. #endif
  919. /* Free storage allocated in PGLOB by a previous `glob' call. */
  920. void
  921. globfree (register glob_t *pglob)
  922. {
  923. if (pglob->gl_pathv != NULL)
  924. {
  925. size_t i;
  926. for (i = 0; i < pglob->gl_pathc; ++i)
  927. if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
  928. free (pglob->gl_pathv[pglob->gl_offs + i]);
  929. free (pglob->gl_pathv);
  930. pglob->gl_pathv = NULL;
  931. }
  932. }
  933. #ifdef COMPILE_GLOB64
  934. libc_hidden_def(globfree64)
  935. #else
  936. libc_hidden_def(globfree)
  937. #endif