glob.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public License as
  4. published by the Free Software Foundation; either version 2 of the
  5. License, or (at your option) any later version.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; see the file COPYING.LIB. If not,
  12. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  13. Boston, MA 02111-1307, USA. */
  14. /* AIX requires this to be the first thing in the file. */
  15. #if defined _AIX && !defined __GNUC__
  16. #pragma alloca
  17. #endif
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #define HAVE_DIRENT_H
  22. /* Enable GNU extensions in glob.h. */
  23. #ifndef _GNU_SOURCE
  24. # define _GNU_SOURCE 1
  25. #endif
  26. #include <errno.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. /* Outcomment the following line for production quality code. */
  30. /* #define NDEBUG 1 */
  31. #include <assert.h>
  32. #include <stdio.h> /* Needed on stupid SunOS for assert. */
  33. /* Comment out all this code if we are using the GNU C Library, and are not
  34. actually compiling the library itself. This code is part of the GNU C
  35. Library, but also included in many other GNU distributions. Compiling
  36. and linking in this code is a waste when using the GNU C library
  37. (especially if it is a shared library). Rather than having every GNU
  38. program understand `configure --with-gnu-libc' and omit the object files,
  39. it is simpler to just do this in the source for each such file. */
  40. #define GLOB_INTERFACE_VERSION 1
  41. #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
  42. # include <gnu-versions.h>
  43. # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
  44. # define ELIDE_CODE
  45. # endif
  46. #endif
  47. #ifndef ELIDE_CODE
  48. #if defined STDC_HEADERS || defined __GNU_LIBRARY__
  49. # include <stddef.h>
  50. #endif
  51. #if defined HAVE_UNISTD_H || defined _LIBC
  52. # include <unistd.h>
  53. # ifndef POSIX
  54. # ifdef _POSIX_VERSION
  55. # define POSIX
  56. # endif
  57. # endif
  58. #endif
  59. #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
  60. # include <pwd.h>
  61. #endif
  62. #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
  63. extern int errno;
  64. #endif
  65. #ifndef __set_errno
  66. # define __set_errno(val) errno = (val)
  67. #endif
  68. #ifndef NULL
  69. # define NULL 0
  70. #endif
  71. #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
  72. # include <dirent.h>
  73. # define NAMLEN(dirent) strlen((dirent)->d_name)
  74. #else
  75. # define dirent direct
  76. # define NAMLEN(dirent) (dirent)->d_namlen
  77. # ifdef HAVE_SYS_NDIR_H
  78. # include <sys/ndir.h>
  79. # endif
  80. # ifdef HAVE_SYS_DIR_H
  81. # include <sys/dir.h>
  82. # endif
  83. # ifdef HAVE_NDIR_H
  84. # include <ndir.h>
  85. # endif
  86. # ifdef HAVE_VMSDIR_H
  87. # include "vmsdir.h"
  88. # endif /* HAVE_VMSDIR_H */
  89. #endif
  90. /* In GNU systems, <dirent.h> defines this macro for us. */
  91. #ifdef _D_NAMLEN
  92. # undef NAMLEN
  93. # define NAMLEN(d) _D_NAMLEN(d)
  94. #endif
  95. /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
  96. if the `d_type' member for `struct dirent' is available. */
  97. #ifdef _DIRENT_HAVE_D_TYPE
  98. # define HAVE_D_TYPE 1
  99. #endif
  100. #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
  101. /* Posix does not require that the d_ino field be present, and some
  102. systems do not provide it. */
  103. # define REAL_DIR_ENTRY(dp) 1
  104. #else
  105. # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
  106. #endif /* POSIX */
  107. #if defined STDC_HEADERS || defined __GNU_LIBRARY__
  108. # include <stdlib.h>
  109. # include <string.h>
  110. # define ANSI_STRING
  111. #else /* No standard headers. */
  112. extern char *getenv();
  113. # ifdef HAVE_STRING_H
  114. # include <string.h>
  115. # define ANSI_STRING
  116. # else
  117. # include <strings.h>
  118. # endif
  119. # ifdef HAVE_MEMORY_H
  120. # include <memory.h>
  121. # endif
  122. extern char *malloc(), *realloc();
  123. extern void free();
  124. extern void qsort();
  125. extern void abort(), exit();
  126. #endif /* Standard headers. */
  127. #ifndef ANSI_STRING
  128. # ifndef bzero
  129. extern void bzero();
  130. # endif
  131. # ifndef bcopy
  132. extern void bcopy();
  133. # endif
  134. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  135. # define strrchr rindex
  136. /* memset is only used for zero here, but let's be paranoid. */
  137. # define memset(s, better_be_zero, n) \
  138. ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
  139. #endif /* Not ANSI_STRING. */
  140. #if !defined HAVE_STRCOLL && !defined _LIBC
  141. # define strcoll strcmp
  142. #endif
  143. #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
  144. # define HAVE_MEMPCPY 1
  145. # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
  146. #endif
  147. #ifndef __GNU_LIBRARY__
  148. # ifdef __GNUC__
  149. __inline
  150. # endif
  151. # ifndef __SASC
  152. # ifdef WINDOWS32
  153. static void *
  154. # else
  155. static char *
  156. # endif
  157. my_realloc(p, n)
  158. char *p;
  159. unsigned int n;
  160. {
  161. /* These casts are the for sake of the broken Ultrix compiler,
  162. which warns of illegal pointer combinations otherwise. */
  163. if (p == NULL)
  164. return (char *) malloc(n);
  165. return (char *) realloc(p, n);
  166. }
  167. # define realloc my_realloc
  168. # endif /* __SASC */
  169. #endif /* __GNU_LIBRARY__ */
  170. #if !defined __alloca && !defined __GNU_LIBRARY__
  171. # ifdef __GNUC__
  172. # undef alloca
  173. # define alloca(n) __builtin_alloca (n)
  174. # else /* Not GCC. */
  175. # ifdef HAVE_ALLOCA_H
  176. # include <alloca.h>
  177. # else /* Not HAVE_ALLOCA_H. */
  178. # ifndef _AIX
  179. # ifdef WINDOWS32
  180. # include <malloc.h>
  181. # else
  182. extern char *alloca();
  183. # endif /* WINDOWS32 */
  184. # endif /* Not _AIX. */
  185. # endif /* sparc or HAVE_ALLOCA_H. */
  186. # endif /* GCC. */
  187. # define __alloca alloca
  188. #endif
  189. #ifndef __GNU_LIBRARY__
  190. # define __stat stat
  191. # ifdef STAT_MACROS_BROKEN
  192. # undef S_ISDIR
  193. # endif
  194. # ifndef S_ISDIR
  195. # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  196. # endif
  197. #endif
  198. #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
  199. # undef size_t
  200. # define size_t unsigned int
  201. #endif
  202. /* Some system header files erroneously define these.
  203. We want our own definitions from <fnmatch.h> to take precedence. */
  204. #undef FNM_PATHNAME
  205. #undef FNM_NOESCAPE
  206. #undef FNM_PERIOD
  207. #include <fnmatch.h>
  208. /* Some system header files erroneously define these.
  209. We want our own definitions from <glob.h> to take precedence. */
  210. #undef GLOB_ERR
  211. #undef GLOB_MARK
  212. #undef GLOB_NOSORT
  213. #undef GLOB_DOOFFS
  214. #undef GLOB_NOCHECK
  215. #undef GLOB_APPEND
  216. #undef GLOB_NOESCAPE
  217. #undef GLOB_PERIOD
  218. #include <glob.h>
  219. static
  220. #if __GNUC__ - 0 >= 2
  221. /*inline*/
  222. #endif
  223. const char *next_brace_sub __P((const char *begin));
  224. static int glob_in_dir __P((const char *pattern, const char *directory,
  225. int flags,
  226. int (*errfunc) __P((const char *, int)),
  227. glob_t * pglob));
  228. static int prefix_array __P((const char *prefix, char **array, size_t n));
  229. static int collated_compare __P((const __ptr_t, const __ptr_t));
  230. /* Find the end of the sub-pattern in a brace expression. We define
  231. this as an inline function if the compiler permits. */
  232. static
  233. #if __GNUC__ - 0 >= 2
  234. /*inline*/
  235. #endif
  236. const char *next_brace_sub(begin)
  237. const char *begin;
  238. {
  239. unsigned int depth = 0;
  240. const char *cp = begin;
  241. while (1) {
  242. if (depth == 0) {
  243. if (*cp != ',' && *cp != '}' && *cp != '\0') {
  244. if (*cp == '{')
  245. ++depth;
  246. ++cp;
  247. continue;
  248. }
  249. } else {
  250. while (*cp != '\0' && (*cp != '}' || depth > 0)) {
  251. if (*cp == '}')
  252. --depth;
  253. ++cp;
  254. }
  255. if (*cp == '\0')
  256. /* An incorrectly terminated brace expression. */
  257. return NULL;
  258. continue;
  259. }
  260. break;
  261. }
  262. return cp;
  263. }
  264. /* Return nonzero if PATTERN contains any metacharacters.
  265. Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
  266. int __glob_pattern_p(pattern, quote)
  267. const char *pattern;
  268. int quote;
  269. {
  270. register const char *p;
  271. int open = 0;
  272. for (p = pattern; *p != '\0'; ++p)
  273. switch (*p) {
  274. case '?':
  275. case '*':
  276. return 1;
  277. case '\\':
  278. if (quote && p[1] != '\0')
  279. ++p;
  280. break;
  281. case '[':
  282. open = 1;
  283. break;
  284. case ']':
  285. if (open)
  286. return 1;
  287. break;
  288. }
  289. return 0;
  290. }
  291. /* Do glob searching for PATTERN, placing results in PGLOB.
  292. The bits defined above may be set in FLAGS.
  293. If a directory cannot be opened or read and ERRFUNC is not nil,
  294. it is called with the pathname that caused the error, and the
  295. `errno' value from the failing call; if it returns non-zero
  296. `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
  297. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  298. Otherwise, `glob' returns zero. */
  299. int glob(pattern, flags, errfunc, pglob)
  300. const char *pattern;
  301. int flags;
  302. int (*errfunc) __P((const char *, int));
  303. glob_t *pglob;
  304. {
  305. const char *filename;
  306. char *dirname;
  307. size_t dirlen;
  308. int status;
  309. int oldcount;
  310. if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) {
  311. __set_errno(EINVAL);
  312. return -1;
  313. }
  314. if (flags & GLOB_BRACE) {
  315. const char *begin = strchr(pattern, '{');
  316. if (begin != NULL) {
  317. /* Allocate working buffer large enough for our work. Note that
  318. we have at least an opening and closing brace. */
  319. int firstc;
  320. char *alt_start;
  321. const char *p;
  322. const char *next;
  323. const char *rest;
  324. size_t rest_len;
  325. #ifdef __GNUC__
  326. char onealt[strlen(pattern) - 1];
  327. #else
  328. char *onealt = (char *) malloc(strlen(pattern) - 1);
  329. if (onealt == NULL) {
  330. if (!(flags & GLOB_APPEND))
  331. globfree(pglob);
  332. return GLOB_NOSPACE;
  333. }
  334. #endif
  335. /* We know the prefix for all sub-patterns. */
  336. #ifdef HAVE_MEMPCPY
  337. alt_start = mempcpy(onealt, pattern, begin - pattern);
  338. #else
  339. memcpy(onealt, pattern, begin - pattern);
  340. alt_start = &onealt[begin - pattern];
  341. #endif
  342. /* Find the first sub-pattern and at the same time find the
  343. rest after the closing brace. */
  344. next = next_brace_sub(begin + 1);
  345. if (next == NULL) {
  346. /* It is an illegal expression. */
  347. #ifndef __GNUC__
  348. free(onealt);
  349. #endif
  350. return glob(pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  351. }
  352. /* Now find the end of the whole brace expression. */
  353. rest = next;
  354. while (*rest != '}') {
  355. rest = next_brace_sub(rest + 1);
  356. if (rest == NULL) {
  357. /* It is an illegal expression. */
  358. #ifndef __GNUC__
  359. free(onealt);
  360. #endif
  361. return glob(pattern, flags & ~GLOB_BRACE, errfunc,
  362. pglob);
  363. }
  364. }
  365. /* Please note that we now can be sure the brace expression
  366. is well-formed. */
  367. rest_len = strlen(++rest) + 1;
  368. /* We have a brace expression. BEGIN points to the opening {,
  369. NEXT points past the terminator of the first element, and END
  370. points past the final }. We will accumulate result names from
  371. recursive runs for each brace alternative in the buffer using
  372. GLOB_APPEND. */
  373. if (!(flags & GLOB_APPEND)) {
  374. /* This call is to set a new vector, so clear out the
  375. vector so we can append to it. */
  376. pglob->gl_pathc = 0;
  377. pglob->gl_pathv = NULL;
  378. }
  379. firstc = pglob->gl_pathc;
  380. p = begin + 1;
  381. while (1) {
  382. int result;
  383. /* Construct the new glob expression. */
  384. #ifdef HAVE_MEMPCPY
  385. mempcpy(mempcpy(alt_start, p, next - p), rest, rest_len);
  386. #else
  387. memcpy(alt_start, p, next - p);
  388. memcpy(&alt_start[next - p], rest, rest_len);
  389. #endif
  390. result = glob(onealt,
  391. ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
  392. | GLOB_APPEND), errfunc, pglob);
  393. /* If we got an error, return it. */
  394. if (result && result != GLOB_NOMATCH) {
  395. #ifndef __GNUC__
  396. free(onealt);
  397. #endif
  398. if (!(flags & GLOB_APPEND))
  399. globfree(pglob);
  400. return result;
  401. }
  402. if (*next == '}')
  403. /* We saw the last entry. */
  404. break;
  405. p = next + 1;
  406. next = next_brace_sub(p);
  407. assert(next != NULL);
  408. }
  409. #ifndef __GNUC__
  410. free(onealt);
  411. #endif
  412. if (pglob->gl_pathc != firstc)
  413. /* We found some entries. */
  414. return 0;
  415. else if (!(flags & (GLOB_NOCHECK | GLOB_NOMAGIC)))
  416. return GLOB_NOMATCH;
  417. }
  418. }
  419. /* Find the filename. */
  420. filename = strrchr(pattern, '/');
  421. if (filename == NULL) {
  422. filename = pattern;
  423. #ifdef _AMIGA
  424. dirname = (char *) "";
  425. #else
  426. dirname = (char *) ".";
  427. #endif
  428. dirlen = 0;
  429. } else if (filename == pattern) {
  430. /* "/pattern". */
  431. dirname = (char *) "/";
  432. dirlen = 1;
  433. ++filename;
  434. } else {
  435. dirlen = filename - pattern;
  436. dirname = (char *) __alloca(dirlen + 1);
  437. #ifdef HAVE_MEMPCPY
  438. *((char *) mempcpy(dirname, pattern, dirlen)) = '\0';
  439. #else
  440. memcpy(dirname, pattern, dirlen);
  441. dirname[dirlen] = '\0';
  442. #endif
  443. ++filename;
  444. }
  445. if (filename[0] == '\0' && dirlen > 1)
  446. /* "pattern/". Expand "pattern", appending slashes. */
  447. {
  448. int val = glob(dirname, flags | GLOB_MARK, errfunc, pglob);
  449. if (val == 0)
  450. pglob->gl_flags =
  451. (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
  452. return val;
  453. }
  454. if (!(flags & GLOB_APPEND)) {
  455. pglob->gl_pathc = 0;
  456. pglob->gl_pathv = NULL;
  457. }
  458. oldcount = pglob->gl_pathc;
  459. #ifndef VMS
  460. if ((flags & GLOB_TILDE) && dirname[0] == '~') {
  461. if (dirname[1] == '\0' || dirname[1] == '/') {
  462. /* Look up home directory. */
  463. char *home_dir = getenv("HOME");
  464. if (home_dir == NULL || home_dir[0] == '\0')
  465. home_dir = (char *) "~"; /* No luck. */
  466. /* Now construct the full directory. */
  467. if (dirname[1] == '\0')
  468. dirname = home_dir;
  469. else {
  470. char *newp;
  471. size_t home_len = strlen(home_dir);
  472. newp = (char *) __alloca(home_len + dirlen);
  473. # ifdef HAVE_MEMPCPY
  474. mempcpy(mempcpy(newp, home_dir, home_len),
  475. &dirname[1], dirlen);
  476. # else
  477. memcpy(newp, home_dir, home_len);
  478. memcpy(&newp[home_len], &dirname[1], dirlen);
  479. # endif
  480. dirname = newp;
  481. }
  482. }
  483. # if !defined _AMIGA && !defined WINDOWS32
  484. else {
  485. char *end_name = strchr(dirname, '/');
  486. char *user_name;
  487. char *home_dir;
  488. if (end_name == NULL)
  489. user_name = dirname + 1;
  490. else {
  491. user_name = (char *) __alloca(end_name - dirname);
  492. # ifdef HAVE_MEMPCPY
  493. *((char *)
  494. mempcpy(user_name, dirname + 1, end_name - dirname)) =
  495. '\0';
  496. # else
  497. memcpy(user_name, dirname + 1, end_name - dirname);
  498. user_name[end_name - dirname - 1] = '\0';
  499. # endif
  500. }
  501. /* Look up specific user's home directory. */
  502. {
  503. //# if defined HAVE_GETPWNAM_R || defined _LIBC
  504. # if 0
  505. size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  506. char *pwtmpbuf = (char *) __alloca(buflen);
  507. struct passwd pwbuf, *p;
  508. if (__getpwnam_r(user_name, &pwbuf, pwtmpbuf, buflen, &p)
  509. >= 0)
  510. home_dir = p->pw_dir;
  511. else
  512. home_dir = NULL;
  513. # else
  514. struct passwd *p = getpwnam(user_name);
  515. if (p != NULL)
  516. home_dir = p->pw_dir;
  517. else
  518. home_dir = NULL;
  519. # endif
  520. }
  521. /* If we found a home directory use this. */
  522. if (home_dir != NULL) {
  523. char *newp;
  524. size_t home_len = strlen(home_dir);
  525. size_t rest_len = end_name == NULL ? 0 : strlen(end_name);
  526. newp = (char *) __alloca(home_len + rest_len + 1);
  527. # ifdef HAVE_MEMPCPY
  528. *((char *) mempcpy(mempcpy(newp, home_dir, home_len),
  529. end_name, rest_len)) = '\0';
  530. # else
  531. memcpy(newp, home_dir, home_len);
  532. memcpy(&newp[home_len], end_name, rest_len);
  533. newp[home_len + rest_len] = '\0';
  534. # endif
  535. dirname = newp;
  536. }
  537. }
  538. # endif /* Not Amiga && not WINDOWS32. */
  539. }
  540. #endif /* Not VMS. */
  541. if (__glob_pattern_p(dirname, !(flags & GLOB_NOESCAPE))) {
  542. /* The directory name contains metacharacters, so we
  543. have to glob for the directory, and then glob for
  544. the pattern in each directory found. */
  545. glob_t dirs;
  546. register int i;
  547. status = glob(dirname,
  548. ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE))
  549. | GLOB_NOSORT | GLOB_ONLYDIR), errfunc, &dirs);
  550. if (status != 0)
  551. return status;
  552. /* We have successfully globbed the preceding directory name.
  553. For each name we found, call glob_in_dir on it and FILENAME,
  554. appending the results to PGLOB. */
  555. for (i = 0; i < dirs.gl_pathc; ++i) {
  556. int oldcount;
  557. #ifdef SHELL
  558. {
  559. /* Make globbing interruptible in the bash shell. */
  560. extern int interrupt_state;
  561. if (interrupt_state) {
  562. globfree(&dirs);
  563. globfree(&files);
  564. return GLOB_ABORTED;
  565. }
  566. }
  567. #endif /* SHELL. */
  568. oldcount = pglob->gl_pathc;
  569. status = glob_in_dir(filename, dirs.gl_pathv[i],
  570. ((flags | GLOB_APPEND)
  571. & ~(GLOB_NOCHECK | GLOB_ERR)),
  572. errfunc, pglob);
  573. if (status == GLOB_NOMATCH)
  574. /* No matches in this directory. Try the next. */
  575. continue;
  576. if (status != 0) {
  577. globfree(&dirs);
  578. globfree(pglob);
  579. return status;
  580. }
  581. /* Stick the directory on the front of each name. */
  582. if (prefix_array(dirs.gl_pathv[i],
  583. &pglob->gl_pathv[oldcount],
  584. pglob->gl_pathc - oldcount)) {
  585. globfree(&dirs);
  586. globfree(pglob);
  587. return GLOB_NOSPACE;
  588. }
  589. }
  590. flags |= GLOB_MAGCHAR;
  591. if (pglob->gl_pathc == oldcount) {
  592. /* No matches. */
  593. if (flags & GLOB_NOCHECK) {
  594. size_t len = strlen(pattern) + 1;
  595. char *patcopy = (char *) malloc(len);
  596. if (patcopy == NULL)
  597. return GLOB_NOSPACE;
  598. memcpy(patcopy, pattern, len);
  599. pglob->gl_pathv
  600. = (char **) realloc(pglob->gl_pathv,
  601. (pglob->gl_pathc +
  602. ((flags & GLOB_DOOFFS) ?
  603. pglob->gl_offs : 0) +
  604. 1 + 1) * sizeof(char *));
  605. if (pglob->gl_pathv == NULL) {
  606. free(patcopy);
  607. return GLOB_NOSPACE;
  608. }
  609. if (flags & GLOB_DOOFFS)
  610. while (pglob->gl_pathc < pglob->gl_offs)
  611. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  612. pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
  613. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  614. pglob->gl_flags = flags;
  615. } else
  616. return GLOB_NOMATCH;
  617. }
  618. } else {
  619. status = glob_in_dir(filename, dirname, flags, errfunc, pglob);
  620. if (status != 0)
  621. return status;
  622. if (dirlen > 0) {
  623. /* Stick the directory on the front of each name. */
  624. if (prefix_array(dirname,
  625. &pglob->gl_pathv[oldcount],
  626. pglob->gl_pathc - oldcount)) {
  627. globfree(pglob);
  628. return GLOB_NOSPACE;
  629. }
  630. }
  631. }
  632. if (flags & GLOB_MARK) {
  633. /* Append slashes to directory names. */
  634. int i;
  635. struct stat st;
  636. for (i = oldcount; i < pglob->gl_pathc; ++i)
  637. if (((flags & GLOB_ALTDIRFUNC) ?
  638. (*pglob->gl_stat) (pglob->gl_pathv[i], &st) :
  639. __stat(pglob->gl_pathv[i], &st)) == 0 &&
  640. S_ISDIR(st.st_mode)) {
  641. size_t len = strlen(pglob->gl_pathv[i]) + 2;
  642. char *new = realloc(pglob->gl_pathv[i], len);
  643. if (new == NULL) {
  644. globfree(pglob);
  645. return GLOB_NOSPACE;
  646. }
  647. strcpy(&new[len - 2], "/");
  648. pglob->gl_pathv[i] = new;
  649. }
  650. }
  651. if (!(flags & GLOB_NOSORT))
  652. /* Sort the vector. */
  653. qsort((__ptr_t) & pglob->gl_pathv[oldcount],
  654. pglob->gl_pathc - oldcount,
  655. sizeof(char *), collated_compare);
  656. return 0;
  657. }
  658. /* Free storage allocated in PGLOB by a previous `glob' call. */
  659. void globfree(pglob)
  660. register glob_t *pglob;
  661. {
  662. if (pglob->gl_pathv != NULL) {
  663. register int i;
  664. for (i = 0; i < pglob->gl_pathc; ++i)
  665. if (pglob->gl_pathv[i] != NULL)
  666. free((__ptr_t) pglob->gl_pathv[i]);
  667. free((__ptr_t) pglob->gl_pathv);
  668. }
  669. }
  670. /* Do a collated comparison of A and B. */
  671. static int collated_compare(a, b)
  672. const __ptr_t a;
  673. const __ptr_t b;
  674. {
  675. const char *const s1 = *(const char *const *const) a;
  676. const char *const s2 = *(const char *const *const) b;
  677. if (s1 == s2)
  678. return 0;
  679. if (s1 == NULL)
  680. return 1;
  681. if (s2 == NULL)
  682. return -1;
  683. return strcoll(s1, s2);
  684. }
  685. /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
  686. elements in place. Return nonzero if out of memory, zero if successful.
  687. A slash is inserted between DIRNAME and each elt of ARRAY,
  688. unless DIRNAME is just "/". Each old element of ARRAY is freed. */
  689. static int prefix_array(dirname, array, n)
  690. const char *dirname;
  691. char **array;
  692. size_t n;
  693. {
  694. register size_t i;
  695. size_t dirlen = strlen(dirname);
  696. if (dirlen == 1 && dirname[0] == '/')
  697. /* DIRNAME is just "/", so normal prepending would get us "//foo".
  698. We want "/foo" instead, so don't prepend any chars from DIRNAME. */
  699. dirlen = 0;
  700. for (i = 0; i < n; ++i) {
  701. size_t eltlen = strlen(array[i]) + 1;
  702. char *new = (char *) malloc(dirlen + 1 + eltlen);
  703. if (new == NULL) {
  704. while (i > 0)
  705. free((__ptr_t) array[--i]);
  706. return 1;
  707. }
  708. #ifdef HAVE_MEMPCPY
  709. {
  710. char *endp = (char *) mempcpy(new, dirname, dirlen);
  711. *endp++ = '/';
  712. mempcpy(endp, array[i], eltlen);
  713. }
  714. #else
  715. memcpy(new, dirname, dirlen);
  716. new[dirlen] = '/';
  717. memcpy(&new[dirlen + 1], array[i], eltlen);
  718. #endif
  719. free((__ptr_t) array[i]);
  720. array[i] = new;
  721. }
  722. return 0;
  723. }
  724. /* Like `glob', but PATTERN is a final pathname component,
  725. and matches are searched for in DIRECTORY.
  726. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
  727. The GLOB_APPEND flag is assumed to be set (always appends). */
  728. static int glob_in_dir(pattern, directory, flags, errfunc, pglob)
  729. const char *pattern;
  730. const char *directory;
  731. int flags;
  732. int (*errfunc) __P((const char *, int));
  733. glob_t *pglob;
  734. {
  735. __ptr_t stream;
  736. struct globlink {
  737. struct globlink *next;
  738. char *name;
  739. };
  740. struct globlink *names = NULL;
  741. size_t nfound;
  742. int meta;
  743. int save;
  744. stream = ((flags & GLOB_ALTDIRFUNC) ?
  745. (*pglob->gl_opendir) (directory) :
  746. (__ptr_t) opendir(directory));
  747. if (stream == NULL) {
  748. if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
  749. (flags & GLOB_ERR)) return GLOB_ABORTED;
  750. nfound = 0;
  751. meta = 0;
  752. } else if (pattern[0] == '\0') {
  753. /* This is a special case for matching directories like in
  754. "*a/". */
  755. names = (struct globlink *) __alloca(sizeof(struct globlink));
  756. names->name = (char *) malloc(1);
  757. if (names->name == NULL)
  758. goto memory_error;
  759. names->name[0] = '\0';
  760. names->next = NULL;
  761. nfound = 1;
  762. meta = 0;
  763. } else {
  764. nfound = 0;
  765. meta = __glob_pattern_p(pattern, !(flags & GLOB_NOESCAPE));
  766. if (meta)
  767. flags |= GLOB_MAGCHAR;
  768. while (1) {
  769. const char *name;
  770. size_t len;
  771. struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
  772. (*pglob->gl_readdir) (stream) :
  773. readdir((DIR *) stream));
  774. if (d == NULL)
  775. break;
  776. if (!REAL_DIR_ENTRY(d))
  777. continue;
  778. #ifdef HAVE_D_TYPE
  779. /* If we shall match only directories use the information
  780. provided by the dirent call if possible. */
  781. if ((flags & GLOB_ONLYDIR)
  782. && d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
  783. continue;
  784. #endif
  785. name = d->d_name;
  786. if ((!meta && strcmp(pattern, name) == 0)
  787. || fnmatch(pattern, name,
  788. (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
  789. ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
  790. #ifdef _AMIGA
  791. | FNM_CASEFOLD
  792. #endif
  793. ) == 0) {
  794. struct globlink *new
  795. =
  796. (struct globlink *) __alloca(sizeof(struct globlink));
  797. len = NAMLEN(d);
  798. new->name = (char *) malloc(len + 1);
  799. if (new->name == NULL)
  800. goto memory_error;
  801. #ifdef HAVE_MEMPCPY
  802. *((char *) mempcpy((__ptr_t) new->name, name, len)) = '\0';
  803. #else
  804. memcpy((__ptr_t) new->name, name, len);
  805. new->name[len] = '\0';
  806. #endif
  807. new->next = names;
  808. names = new;
  809. ++nfound;
  810. if (!meta)
  811. break;
  812. }
  813. }
  814. }
  815. if (nfound == 0 && (flags & GLOB_NOMAGIC) && !meta)
  816. flags |= GLOB_NOCHECK;
  817. if (nfound == 0 && (flags & GLOB_NOCHECK)) {
  818. size_t len = strlen(pattern);
  819. nfound = 1;
  820. names = (struct globlink *) __alloca(sizeof(struct globlink));
  821. names->next = NULL;
  822. names->name = (char *) malloc(len + 1);
  823. if (names->name == NULL)
  824. goto memory_error;
  825. #ifdef HAVE_MEMPCPY
  826. *((char *) mempcpy(names->name, pattern, len)) = '\0';
  827. #else
  828. memcpy(names->name, pattern, len);
  829. names->name[len] = '\0';
  830. #endif
  831. }
  832. if (nfound != 0) {
  833. pglob->gl_pathv
  834. = (char **) realloc(pglob->gl_pathv,
  835. (pglob->gl_pathc +
  836. ((flags & GLOB_DOOFFS) ? pglob->
  837. gl_offs : 0) + nfound +
  838. 1) * sizeof(char *));
  839. if (pglob->gl_pathv == NULL)
  840. goto memory_error;
  841. if (flags & GLOB_DOOFFS)
  842. while (pglob->gl_pathc < pglob->gl_offs)
  843. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  844. for (; names != NULL; names = names->next)
  845. pglob->gl_pathv[pglob->gl_pathc++] = names->name;
  846. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  847. pglob->gl_flags = flags;
  848. }
  849. save = errno;
  850. if (flags & GLOB_ALTDIRFUNC)
  851. (*pglob->gl_closedir) (stream);
  852. else
  853. closedir((DIR *) stream);
  854. __set_errno(save);
  855. return nfound == 0 ? GLOB_NOMATCH : 0;
  856. memory_error:
  857. {
  858. int save = errno;
  859. if (flags & GLOB_ALTDIRFUNC)
  860. (*pglob->gl_closedir) (stream);
  861. else
  862. closedir((DIR *) stream);
  863. __set_errno(save);
  864. }
  865. while (names != NULL) {
  866. if (names->name != NULL)
  867. free((__ptr_t) names->name);
  868. names = names->next;
  869. }
  870. return GLOB_NOSPACE;
  871. }
  872. #endif /* Not ELIDE_CODE. */