glob.c 27 KB

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