glob.c 27 KB

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