glob.c 27 KB

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