glob.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /*
  2. * Copyright (c) 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Guido van Rossum.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. /* $Id$ */
  37. /*
  38. * glob(3) -- a superset of the one defined in POSIX 1003.2.
  39. *
  40. * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
  41. *
  42. * Optional extra services, controlled by flags not defined by POSIX:
  43. *
  44. * GLOB_QUOTE:
  45. * Escaping convention: \ inhibits any special meaning the following
  46. * character might have (except \ at end of string is retained).
  47. * GLOB_MAGCHAR:
  48. * Set in gl_flags if pattern contained a globbing character.
  49. * GLOB_NOMAGIC:
  50. * Same as GLOB_NOCHECK, but it will only append pattern if it did
  51. * not contain any magic characters. [Used in csh style globbing]
  52. * GLOB_ALTDIRFUNC:
  53. * Use alternately specified directory access functions.
  54. * GLOB_TILDE:
  55. * expand ~user/foo to the /home/dir/of/user/foo
  56. * GLOB_BRACE:
  57. * expand {1,2}{a,b} to 1a 1b 2a 2b
  58. * gl_matchc:
  59. * Number of matches in the current invocation of glob.
  60. */
  61. #ifdef PHP_WIN32
  62. #if _MSC_VER < 1800
  63. # define _POSIX_
  64. # include <limits.h>
  65. # undef _POSIX_
  66. #else
  67. /* Visual Studio 2013 removed all the _POSIX_ defines, but we depend on some */
  68. # ifndef ARG_MAX
  69. # define ARG_MAX 14500
  70. # endif
  71. #endif
  72. #ifndef S_ISDIR
  73. #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
  74. #endif
  75. #ifndef S_ISLNK
  76. #define S_ISLNK(m) (0)
  77. #endif
  78. #endif
  79. #include "php.h"
  80. #include <sys/stat.h>
  81. #include <ctype.h>
  82. #ifndef PHP_WIN32
  83. #include <sys/param.h>
  84. #include <dirent.h>
  85. #include <pwd.h>
  86. #include <unistd.h>
  87. #endif
  88. #include <errno.h>
  89. #include "glob.h"
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #define DOLLAR '$'
  94. #define DOT '.'
  95. #define EOS '\0'
  96. #define LBRACKET '['
  97. #define NOT '!'
  98. #define QUESTION '?'
  99. #define QUOTE '\\'
  100. #define RANGE '-'
  101. #define RBRACKET ']'
  102. #define SEP DEFAULT_SLASH
  103. #define STAR '*'
  104. #define TILDE '~'
  105. #define UNDERSCORE '_'
  106. #define LBRACE '{'
  107. #define RBRACE '}'
  108. #define SLASH '/'
  109. #define COMMA ','
  110. #ifndef DEBUG
  111. #define M_QUOTE 0x8000
  112. #define M_PROTECT 0x4000
  113. #define M_MASK 0xffff
  114. #define M_ASCII 0x00ff
  115. typedef u_short Char;
  116. #else
  117. #define M_QUOTE 0x80
  118. #define M_PROTECT 0x40
  119. #define M_MASK 0xff
  120. #define M_ASCII 0x7f
  121. typedef char Char;
  122. #endif
  123. #define CHAR(c) ((Char)((c)&M_ASCII))
  124. #define META(c) ((Char)((c)|M_QUOTE))
  125. #define M_ALL META('*')
  126. #define M_END META(']')
  127. #define M_NOT META('!')
  128. #define M_ONE META('?')
  129. #define M_RNG META('-')
  130. #define M_SET META('[')
  131. #define ismeta(c) (((c)&M_QUOTE) != 0)
  132. static int compare(const void *, const void *);
  133. static int g_Ctoc(const Char *, char *, u_int);
  134. static int g_lstat(Char *, struct stat *, glob_t *);
  135. static DIR *g_opendir(Char *, glob_t *);
  136. static Char *g_strchr(Char *, int);
  137. static int g_stat(Char *, struct stat *, glob_t *);
  138. static int glob0(const Char *, glob_t *);
  139. static int glob1(Char *, Char *, glob_t *, size_t *);
  140. static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
  141. glob_t *, size_t *);
  142. static int glob3(Char *, Char *, Char *, Char *, Char *, Char *,
  143. Char *, Char *, glob_t *, size_t *);
  144. static int globextend(const Char *, glob_t *, size_t *);
  145. static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
  146. static int globexp1(const Char *, glob_t *);
  147. static int globexp2(const Char *, const Char *, glob_t *, int *);
  148. static int match(Char *, Char *, Char *);
  149. #ifdef DEBUG
  150. static void qprintf(const char *, Char *);
  151. #endif
  152. PHPAPI int
  153. glob(pattern, flags, errfunc, pglob)
  154. const char *pattern;
  155. int flags, (*errfunc)(const char *, int);
  156. glob_t *pglob;
  157. {
  158. const u_char *patnext;
  159. int c;
  160. Char *bufnext, *bufend, patbuf[MAXPATHLEN];
  161. #ifdef PHP_WIN32
  162. /* Force skipping escape sequences on windows
  163. * due to the ambiguity with path backslashes
  164. */
  165. flags |= GLOB_NOESCAPE;
  166. #endif
  167. patnext = (u_char *) pattern;
  168. if (!(flags & GLOB_APPEND)) {
  169. pglob->gl_pathc = 0;
  170. pglob->gl_pathv = NULL;
  171. if (!(flags & GLOB_DOOFFS))
  172. pglob->gl_offs = 0;
  173. }
  174. pglob->gl_flags = flags & ~GLOB_MAGCHAR;
  175. pglob->gl_errfunc = errfunc;
  176. pglob->gl_matchc = 0;
  177. bufnext = patbuf;
  178. bufend = bufnext + MAXPATHLEN - 1;
  179. if (flags & GLOB_NOESCAPE)
  180. while (bufnext < bufend && (c = *patnext++) != EOS)
  181. *bufnext++ = c;
  182. else {
  183. /* Protect the quoted characters. */
  184. while (bufnext < bufend && (c = *patnext++) != EOS)
  185. if (c == QUOTE) {
  186. if ((c = *patnext++) == EOS) {
  187. c = QUOTE;
  188. --patnext;
  189. }
  190. *bufnext++ = c | M_PROTECT;
  191. } else
  192. *bufnext++ = c;
  193. }
  194. *bufnext = EOS;
  195. if (flags & GLOB_BRACE)
  196. return globexp1(patbuf, pglob);
  197. else
  198. return glob0(patbuf, pglob);
  199. }
  200. /*
  201. * Expand recursively a glob {} pattern. When there is no more expansion
  202. * invoke the standard globbing routine to glob the rest of the magic
  203. * characters
  204. */
  205. static int
  206. globexp1(pattern, pglob)
  207. const Char *pattern;
  208. glob_t *pglob;
  209. {
  210. const Char* ptr = pattern;
  211. int rv;
  212. /* Protect a single {}, for find(1), like csh */
  213. if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
  214. return glob0(pattern, pglob);
  215. while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
  216. if (!globexp2(ptr, pattern, pglob, &rv))
  217. return rv;
  218. return glob0(pattern, pglob);
  219. }
  220. /*
  221. * Recursive brace globbing helper. Tries to expand a single brace.
  222. * If it succeeds then it invokes globexp1 with the new pattern.
  223. * If it fails then it tries to glob the rest of the pattern and returns.
  224. */
  225. static int
  226. globexp2(ptr, pattern, pglob, rv)
  227. const Char *ptr, *pattern;
  228. glob_t *pglob;
  229. int *rv;
  230. {
  231. int i;
  232. Char *lm, *ls;
  233. const Char *pe, *pm, *pl;
  234. Char patbuf[MAXPATHLEN];
  235. /* copy part up to the brace */
  236. for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
  237. ;
  238. *lm = EOS;
  239. ls = lm;
  240. /* Find the balanced brace */
  241. for (i = 0, pe = ++ptr; *pe; pe++)
  242. if (*pe == LBRACKET) {
  243. /* Ignore everything between [] */
  244. for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
  245. ;
  246. if (*pe == EOS) {
  247. /*
  248. * We could not find a matching RBRACKET.
  249. * Ignore and just look for RBRACE
  250. */
  251. pe = pm;
  252. }
  253. } else if (*pe == LBRACE)
  254. i++;
  255. else if (*pe == RBRACE) {
  256. if (i == 0)
  257. break;
  258. i--;
  259. }
  260. /* Non matching braces; just glob the pattern */
  261. if (i != 0 || *pe == EOS) {
  262. *rv = glob0(patbuf, pglob);
  263. return 0;
  264. }
  265. for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
  266. const Char *pb;
  267. switch (*pm) {
  268. case LBRACKET:
  269. /* Ignore everything between [] */
  270. for (pb = pm++; *pm != RBRACKET && *pm != EOS; pm++)
  271. ;
  272. if (*pm == EOS) {
  273. /*
  274. * We could not find a matching RBRACKET.
  275. * Ignore and just look for RBRACE
  276. */
  277. pm = pb;
  278. }
  279. break;
  280. case LBRACE:
  281. i++;
  282. break;
  283. case RBRACE:
  284. if (i) {
  285. i--;
  286. break;
  287. }
  288. /* FALLTHROUGH */
  289. case COMMA:
  290. if (i && *pm == COMMA)
  291. break;
  292. else {
  293. /* Append the current string */
  294. for (lm = ls; (pl < pm); *lm++ = *pl++)
  295. ;
  296. /*
  297. * Append the rest of the pattern after the
  298. * closing brace
  299. */
  300. for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
  301. ;
  302. /* Expand the current pattern */
  303. #ifdef DEBUG
  304. qprintf("globexp2:", patbuf);
  305. #endif
  306. *rv = globexp1(patbuf, pglob);
  307. /* move after the comma, to the next string */
  308. pl = pm + 1;
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. *rv = 0;
  316. return 0;
  317. }
  318. /*
  319. * expand tilde from the passwd file.
  320. */
  321. static const Char *
  322. globtilde(pattern, patbuf, patbuf_len, pglob)
  323. const Char *pattern;
  324. Char *patbuf;
  325. size_t patbuf_len;
  326. glob_t *pglob;
  327. {
  328. #ifndef PHP_WIN32
  329. struct passwd *pwd;
  330. #endif
  331. char *h;
  332. const Char *p;
  333. Char *b, *eb;
  334. if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
  335. return pattern;
  336. /* Copy up to the end of the string or / */
  337. eb = &patbuf[patbuf_len - 1];
  338. for (p = pattern + 1, h = (char *) patbuf;
  339. h < (char *)eb && *p && *p != SLASH; *h++ = (char) *p++)
  340. ;
  341. *h = EOS;
  342. #if 0
  343. if (h == (char *)eb)
  344. return what;
  345. #endif
  346. if (((char *) patbuf)[0] == EOS) {
  347. /*
  348. * handle a plain ~ or ~/ by expanding $HOME
  349. * first and then trying the password file
  350. */
  351. if ((h = getenv("HOME")) == NULL) {
  352. #ifndef PHP_WIN32
  353. if ((pwd = getpwuid(getuid())) == NULL)
  354. return pattern;
  355. else
  356. h = pwd->pw_dir;
  357. #else
  358. return pattern;
  359. #endif
  360. }
  361. } else {
  362. /*
  363. * Expand a ~user
  364. */
  365. #ifndef PHP_WIN32
  366. if ((pwd = getpwnam((char*) patbuf)) == NULL)
  367. return pattern;
  368. else
  369. h = pwd->pw_dir;
  370. #else
  371. return pattern;
  372. #endif
  373. }
  374. /* Copy the home directory */
  375. for (b = patbuf; b < eb && *h; *b++ = *h++)
  376. ;
  377. /* Append the rest of the pattern */
  378. while (b < eb && (*b++ = *p++) != EOS)
  379. ;
  380. *b = EOS;
  381. return patbuf;
  382. }
  383. /*
  384. * The main glob() routine: compiles the pattern (optionally processing
  385. * quotes), calls glob1() to do the real pattern matching, and finally
  386. * sorts the list (unless unsorted operation is requested). Returns 0
  387. * if things went well, nonzero if errors occurred. It is not an error
  388. * to find no matches.
  389. */
  390. static int
  391. glob0(pattern, pglob)
  392. const Char *pattern;
  393. glob_t *pglob;
  394. {
  395. const Char *qpatnext;
  396. int c, err, oldpathc;
  397. Char *bufnext, patbuf[MAXPATHLEN];
  398. size_t limit = 0;
  399. qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
  400. oldpathc = pglob->gl_pathc;
  401. bufnext = patbuf;
  402. /* We don't need to check for buffer overflow any more. */
  403. while ((c = *qpatnext++) != EOS) {
  404. switch (c) {
  405. case LBRACKET:
  406. c = *qpatnext;
  407. if (c == NOT)
  408. ++qpatnext;
  409. if (*qpatnext == EOS ||
  410. g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
  411. *bufnext++ = LBRACKET;
  412. if (c == NOT)
  413. --qpatnext;
  414. break;
  415. }
  416. *bufnext++ = M_SET;
  417. if (c == NOT)
  418. *bufnext++ = M_NOT;
  419. c = *qpatnext++;
  420. do {
  421. *bufnext++ = CHAR(c);
  422. if (*qpatnext == RANGE &&
  423. (c = qpatnext[1]) != RBRACKET) {
  424. *bufnext++ = M_RNG;
  425. *bufnext++ = CHAR(c);
  426. qpatnext += 2;
  427. }
  428. } while ((c = *qpatnext++) != RBRACKET);
  429. pglob->gl_flags |= GLOB_MAGCHAR;
  430. *bufnext++ = M_END;
  431. break;
  432. case QUESTION:
  433. pglob->gl_flags |= GLOB_MAGCHAR;
  434. *bufnext++ = M_ONE;
  435. break;
  436. case STAR:
  437. pglob->gl_flags |= GLOB_MAGCHAR;
  438. /* collapse adjacent stars to one,
  439. * to avoid exponential behavior
  440. */
  441. if (bufnext == patbuf || bufnext[-1] != M_ALL)
  442. *bufnext++ = M_ALL;
  443. break;
  444. default:
  445. *bufnext++ = CHAR(c);
  446. break;
  447. }
  448. }
  449. *bufnext = EOS;
  450. #ifdef DEBUG
  451. qprintf("glob0:", patbuf);
  452. #endif
  453. if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
  454. return(err);
  455. /*
  456. * If there was no match we are going to append the pattern
  457. * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
  458. * and the pattern did not contain any magic characters
  459. * GLOB_NOMAGIC is there just for compatibility with csh.
  460. */
  461. if (pglob->gl_pathc == oldpathc) {
  462. if ((pglob->gl_flags & GLOB_NOCHECK) ||
  463. ((pglob->gl_flags & GLOB_NOMAGIC) &&
  464. !(pglob->gl_flags & GLOB_MAGCHAR)))
  465. return(globextend(pattern, pglob, &limit));
  466. else
  467. return(GLOB_NOMATCH);
  468. }
  469. if (!(pglob->gl_flags & GLOB_NOSORT))
  470. qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
  471. pglob->gl_pathc - oldpathc, sizeof(char *), compare);
  472. return(0);
  473. }
  474. static int
  475. compare(const void *p, const void *q)
  476. {
  477. return(strcmp(*(char **)p, *(char **)q));
  478. }
  479. static int
  480. glob1(pattern, pattern_last, pglob, limitp)
  481. Char *pattern, *pattern_last;
  482. glob_t *pglob;
  483. size_t *limitp;
  484. {
  485. Char pathbuf[MAXPATHLEN];
  486. /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
  487. if (*pattern == EOS)
  488. return(0);
  489. return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
  490. pathbuf, pathbuf+MAXPATHLEN-1,
  491. pattern, pattern_last, pglob, limitp));
  492. }
  493. /*
  494. * The functions glob2 and glob3 are mutually recursive; there is one level
  495. * of recursion for each segment in the pattern that contains one or more
  496. * meta characters.
  497. */
  498. static int
  499. glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
  500. pattern_last, pglob, limitp)
  501. Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
  502. Char *pattern, *pattern_last;
  503. glob_t *pglob;
  504. size_t *limitp;
  505. {
  506. struct stat sb;
  507. Char *p, *q;
  508. int anymeta;
  509. /*
  510. * Loop over pattern segments until end of pattern or until
  511. * segment with meta character found.
  512. */
  513. for (anymeta = 0;;) {
  514. if (*pattern == EOS) { /* End of pattern? */
  515. *pathend = EOS;
  516. if (g_lstat(pathbuf, &sb, pglob))
  517. return(0);
  518. if (((pglob->gl_flags & GLOB_MARK) &&
  519. !IS_SLASH(pathend[-1])) && (S_ISDIR(sb.st_mode) ||
  520. (S_ISLNK(sb.st_mode) &&
  521. (g_stat(pathbuf, &sb, pglob) == 0) &&
  522. S_ISDIR(sb.st_mode)))) {
  523. if (pathend+1 > pathend_last)
  524. return (1);
  525. *pathend++ = SEP;
  526. *pathend = EOS;
  527. }
  528. ++pglob->gl_matchc;
  529. return(globextend(pathbuf, pglob, limitp));
  530. }
  531. /* Find end of next segment, copy tentatively to pathend. */
  532. q = pathend;
  533. p = pattern;
  534. while (*p != EOS && !IS_SLASH(*p)) {
  535. if (ismeta(*p))
  536. anymeta = 1;
  537. if (q+1 > pathend_last)
  538. return (1);
  539. *q++ = *p++;
  540. }
  541. if (!anymeta) { /* No expansion, do next segment. */
  542. pathend = q;
  543. pattern = p;
  544. while (IS_SLASH(*pattern)) {
  545. if (pathend+1 > pathend_last)
  546. return (1);
  547. *pathend++ = *pattern++;
  548. }
  549. } else
  550. /* Need expansion, recurse. */
  551. return(glob3(pathbuf, pathbuf_last, pathend,
  552. pathend_last, pattern, pattern_last,
  553. p, pattern_last, pglob, limitp));
  554. }
  555. /* NOTREACHED */
  556. }
  557. static int
  558. glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
  559. restpattern, restpattern_last, pglob, limitp)
  560. Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
  561. Char *pattern, *pattern_last, *restpattern, *restpattern_last;
  562. glob_t *pglob;
  563. size_t *limitp;
  564. {
  565. register struct dirent *dp;
  566. DIR *dirp;
  567. int err;
  568. char buf[MAXPATHLEN];
  569. /*
  570. * The readdirfunc declaration can't be prototyped, because it is
  571. * assigned, below, to two functions which are prototyped in glob.h
  572. * and dirent.h as taking pointers to differently typed opaque
  573. * structures.
  574. */
  575. struct dirent *(*readdirfunc)();
  576. if (pathend > pathend_last)
  577. return (1);
  578. *pathend = EOS;
  579. errno = 0;
  580. if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
  581. /* TODO: don't call for ENOENT or ENOTDIR? */
  582. if (pglob->gl_errfunc) {
  583. if (g_Ctoc(pathbuf, buf, sizeof(buf)))
  584. return(GLOB_ABORTED);
  585. if (pglob->gl_errfunc(buf, errno) ||
  586. pglob->gl_flags & GLOB_ERR)
  587. return(GLOB_ABORTED);
  588. }
  589. return(0);
  590. }
  591. err = 0;
  592. /* Search directory for matching names. */
  593. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  594. readdirfunc = pglob->gl_readdir;
  595. else
  596. readdirfunc = readdir;
  597. while ((dp = (*readdirfunc)(dirp))) {
  598. register u_char *sc;
  599. register Char *dc;
  600. /* Initial DOT must be matched literally. */
  601. if (dp->d_name[0] == DOT && *pattern != DOT)
  602. continue;
  603. dc = pathend;
  604. sc = (u_char *) dp->d_name;
  605. while (dc < pathend_last && (*dc++ = *sc++) != EOS)
  606. ;
  607. if (dc >= pathend_last) {
  608. *dc = EOS;
  609. err = 1;
  610. break;
  611. }
  612. if (!match(pathend, pattern, restpattern)) {
  613. *pathend = EOS;
  614. continue;
  615. }
  616. err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
  617. restpattern, restpattern_last, pglob, limitp);
  618. if (err)
  619. break;
  620. }
  621. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  622. (*pglob->gl_closedir)(dirp);
  623. else
  624. closedir(dirp);
  625. return(err);
  626. }
  627. /*
  628. * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
  629. * add the new item, and update gl_pathc.
  630. *
  631. * This assumes the BSD realloc, which only copies the block when its size
  632. * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
  633. * behavior.
  634. *
  635. * Return 0 if new item added, error code if memory couldn't be allocated.
  636. *
  637. * Invariant of the glob_t structure:
  638. * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
  639. * gl_pathv points to (gl_offs + gl_pathc + 1) items.
  640. */
  641. static int
  642. globextend(path, pglob, limitp)
  643. const Char *path;
  644. glob_t *pglob;
  645. size_t *limitp;
  646. {
  647. register char **pathv;
  648. register int i;
  649. u_int newsize, len;
  650. char *copy;
  651. const Char *p;
  652. newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
  653. pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
  654. malloc(newsize);
  655. if (pathv == NULL) {
  656. if (pglob->gl_pathv) {
  657. free(pglob->gl_pathv);
  658. pglob->gl_pathv = NULL;
  659. }
  660. return(GLOB_NOSPACE);
  661. }
  662. if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
  663. /* first time around -- clear initial gl_offs items */
  664. pathv += pglob->gl_offs;
  665. for (i = pglob->gl_offs; --i >= 0; )
  666. *--pathv = NULL;
  667. }
  668. pglob->gl_pathv = pathv;
  669. for (p = path; *p++;)
  670. ;
  671. len = (size_t)(p - path);
  672. *limitp += len;
  673. if ((copy = malloc(len)) != NULL) {
  674. if (g_Ctoc(path, copy, len)) {
  675. free(copy);
  676. return(GLOB_NOSPACE);
  677. }
  678. pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
  679. }
  680. pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
  681. if ((pglob->gl_flags & GLOB_LIMIT) &&
  682. newsize + *limitp >= ARG_MAX) {
  683. errno = 0;
  684. return(GLOB_NOSPACE);
  685. }
  686. return(copy == NULL ? GLOB_NOSPACE : 0);
  687. }
  688. /*
  689. * pattern matching function for filenames. Each occurrence of the *
  690. * pattern causes a recursion level.
  691. */
  692. static int
  693. match(name, pat, patend)
  694. register Char *name, *pat, *patend;
  695. {
  696. int ok, negate_range;
  697. Char c, k;
  698. while (pat < patend) {
  699. c = *pat++;
  700. switch (c & M_MASK) {
  701. case M_ALL:
  702. if (pat == patend)
  703. return(1);
  704. do
  705. if (match(name, pat, patend))
  706. return(1);
  707. while (*name++ != EOS)
  708. ;
  709. return(0);
  710. case M_ONE:
  711. if (*name++ == EOS)
  712. return(0);
  713. break;
  714. case M_SET:
  715. ok = 0;
  716. if ((k = *name++) == EOS)
  717. return(0);
  718. if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
  719. ++pat;
  720. while (((c = *pat++) & M_MASK) != M_END)
  721. if ((*pat & M_MASK) == M_RNG) {
  722. if (c <= k && k <= pat[1])
  723. ok = 1;
  724. pat += 2;
  725. } else if (c == k)
  726. ok = 1;
  727. if (ok == negate_range)
  728. return(0);
  729. break;
  730. default:
  731. if (*name++ != c)
  732. return(0);
  733. break;
  734. }
  735. }
  736. return(*name == EOS);
  737. }
  738. /* Free allocated data belonging to a glob_t structure. */
  739. PHPAPI void
  740. globfree(pglob)
  741. glob_t *pglob;
  742. {
  743. register int i;
  744. register char **pp;
  745. if (pglob->gl_pathv != NULL) {
  746. pp = pglob->gl_pathv + pglob->gl_offs;
  747. for (i = pglob->gl_pathc; i--; ++pp)
  748. if (*pp)
  749. free(*pp);
  750. free(pglob->gl_pathv);
  751. pglob->gl_pathv = NULL;
  752. }
  753. }
  754. static DIR *
  755. g_opendir(str, pglob)
  756. register Char *str;
  757. glob_t *pglob;
  758. {
  759. char buf[MAXPATHLEN];
  760. if (!*str)
  761. strlcpy(buf, ".", sizeof buf);
  762. else {
  763. if (g_Ctoc(str, buf, sizeof(buf)))
  764. return(NULL);
  765. }
  766. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  767. return((*pglob->gl_opendir)(buf));
  768. return(opendir(buf));
  769. }
  770. static int
  771. g_lstat(fn, sb, pglob)
  772. register Char *fn;
  773. struct stat *sb;
  774. glob_t *pglob;
  775. {
  776. char buf[MAXPATHLEN];
  777. if (g_Ctoc(fn, buf, sizeof(buf)))
  778. return(-1);
  779. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  780. return((*pglob->gl_lstat)(buf, sb));
  781. return(php_sys_lstat(buf, sb));
  782. }
  783. static int
  784. g_stat(fn, sb, pglob)
  785. register Char *fn;
  786. struct stat *sb;
  787. glob_t *pglob;
  788. {
  789. char buf[MAXPATHLEN];
  790. if (g_Ctoc(fn, buf, sizeof(buf)))
  791. return(-1);
  792. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  793. return((*pglob->gl_stat)(buf, sb));
  794. return(php_sys_stat(buf, sb));
  795. }
  796. static Char *
  797. g_strchr(str, ch)
  798. Char *str;
  799. int ch;
  800. {
  801. do {
  802. if (*str == ch)
  803. return (str);
  804. } while (*str++);
  805. return (NULL);
  806. }
  807. static int
  808. g_Ctoc(str, buf, len)
  809. register const Char *str;
  810. char *buf;
  811. u_int len;
  812. {
  813. while (len--) {
  814. if ((*buf++ = (char) *str++) == EOS)
  815. return (0);
  816. }
  817. return (1);
  818. }
  819. #ifdef DEBUG
  820. static void
  821. qprintf(str, s)
  822. const char *str;
  823. register Char *s;
  824. {
  825. register Char *p;
  826. (void)printf("%s:\n", str);
  827. for (p = s; *p; p++)
  828. (void)printf("%c", CHAR(*p));
  829. (void)printf("\n");
  830. for (p = s; *p; p++)
  831. (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
  832. (void)printf("\n");
  833. for (p = s; *p; p++)
  834. (void)printf("%c", ismeta(*p) ? '_' : ' ');
  835. (void)printf("\n");
  836. }
  837. #endif