Skip to content

Commit a1d9905

Browse files
committed
Refactor part of glob.c as only ever used with Windows
1 parent f9c69bc commit a1d9905

File tree

2 files changed

+70
-99
lines changed

2 files changed

+70
-99
lines changed

win32/glob.c

Lines changed: 63 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,23 @@
5858
* gl_matchc:
5959
* Number of matches in the current invocation of glob.
6060
*/
61-
#ifdef PHP_WIN32
62-
#if _MSC_VER < 1800
63-
# define _POSIX_
64-
# include <limits.h>
65-
# undef _POSIX_
66-
#else
61+
6762
/* 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
63+
#ifndef ARG_MAX
64+
# define ARG_MAX 14500
7265
#endif
7366

74-
#include "php.h"
67+
#include "glob.h"
68+
#include <stdbool.h>
69+
#include <stdint.h>
7570
#include <sys/stat.h>
7671

7772
#include <ctype.h>
78-
#ifndef PHP_WIN32
79-
#include <sys/param.h>
80-
#include <dirent.h>
81-
#include <pwd.h>
82-
#include <unistd.h>
83-
#endif
8473
#include <errno.h>
85-
#include "glob.h"
8674
#include <stdio.h>
8775
#include <stdlib.h>
8876
#include <string.h>
77+
#include "php.h"
8978

9079
#define DOLLAR '$'
9180
#define DOT '.'
@@ -96,7 +85,8 @@
9685
#define QUOTE '\\'
9786
#define RANGE '-'
9887
#define RBRACKET ']'
99-
#define SEP DEFAULT_SLASH
88+
/* As this is Windows we use a backslash for the separator */
89+
#define SEP '\\'
10090
#define STAR '*'
10191
#define TILDE '~'
10292
#define UNDERSCORE '_'
@@ -137,11 +127,11 @@ typedef char Char;
137127
#define ismeta(c) (((c)&M_QUOTE) != 0)
138128

139129
static int compare(const void *, const void *);
140-
static int g_Ctoc(const Char *, char *, u_int);
141-
static int g_lstat(Char *, zend_stat_t *, glob_t *);
130+
static bool g_Ctoc(const Char *, char *, u_int);
131+
static int g_lstat(Char *, php_win32_ioutil_stat_t *, glob_t *);
142132
static DIR *g_opendir(Char *, glob_t *);
143133
static Char *g_strchr(Char *, int);
144-
static int g_stat(Char *, zend_stat_t *, glob_t *);
134+
static int g_stat(Char *, php_win32_ioutil_stat_t *, glob_t *);
145135
static int glob0(const Char *, glob_t *);
146136
static int glob1(Char *, Char *, glob_t *, size_t *);
147137
static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
@@ -152,7 +142,7 @@ static int globextend(const Char *, glob_t *, size_t *);
152142
static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
153143
static int globexp1(const Char *, glob_t *);
154144
static int globexp2(const Char *, const Char *, glob_t *, int *);
155-
static int match(Char *, Char *, Char *);
145+
static bool match(Char *, Char *, Char *);
156146
#ifdef DEBUG
157147
static void qprintf(const char *, Char *);
158148
#endif
@@ -341,9 +331,6 @@ static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv
341331
*/
342332
static const Char *globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
343333
{
344-
#ifndef PHP_WIN32
345-
struct passwd *pwd;
346-
#endif
347334
char *h;
348335
const Char *p;
349336
Char *b, *eb;
@@ -359,38 +346,19 @@ static const Char *globtilde(const Char *pattern, Char *patbuf, size_t patbuf_le
359346

360347
*h = EOS;
361348

362-
#if 0
363-
if (h == (char *)eb)
364-
return what;
365-
#endif
366-
367349
if (((char *) patbuf)[0] == EOS) {
368350
/*
369351
* handle a plain ~ or ~/ by expanding $HOME
370352
* first and then trying the password file
371353
*/
372354
if ((h = getenv("HOME")) == NULL) {
373-
#ifndef PHP_WIN32
374-
if ((pwd = getpwuid(getuid())) == NULL)
375-
return pattern;
376-
else
377-
h = pwd->pw_dir;
378-
#else
379355
return pattern;
380-
#endif
381356
}
382357
} else {
383358
/*
384359
* Expand a ~user
385360
*/
386-
#ifndef PHP_WIN32
387-
if ((pwd = getpwnam((char*) patbuf)) == NULL)
388-
return pattern;
389-
else
390-
h = pwd->pw_dir;
391-
#else
392361
return pattern;
393-
#endif
394362
}
395363

396364
/* Copy the home directory */
@@ -504,11 +472,7 @@ static int compare(const void *p, const void *q)
504472
return(strcmp(*(char **)p, *(char **)q));
505473
}
506474

507-
static int
508-
glob1(pattern, pattern_last, pglob, limitp)
509-
Char *pattern, *pattern_last;
510-
glob_t *pglob;
511-
size_t *limitp;
475+
static int glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
512476
{
513477
Char pathbuf[MAXPATHLEN];
514478

@@ -520,6 +484,11 @@ glob1(pattern, pattern_last, pglob, limitp)
520484
pattern, pattern_last, pglob, limitp));
521485
}
522486

487+
static inline bool glob_is_slash(Char c)
488+
{
489+
return c == '/' || c == '\\';
490+
}
491+
523492
/*
524493
* The functions glob2 and glob3 are mutually recursive; there is one level
525494
* of recursion for each segment in the pattern that contains one or more
@@ -528,7 +497,7 @@ glob1(pattern, pattern_last, pglob, limitp)
528497
static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, Char *pattern,
529498
Char *pattern_last, glob_t *pglob, size_t *limitp)
530499
{
531-
zend_stat_t sb = {0};
500+
php_win32_ioutil_stat_t sb = {0};
532501
Char *p, *q;
533502
int anymeta;
534503

@@ -543,7 +512,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend
543512
return(0);
544513

545514
if (((pglob->gl_flags & GLOB_MARK) &&
546-
!IS_SLASH(pathend[-1])) && (S_ISDIR(sb.st_mode) ||
515+
!glob_is_slash(pathend[-1])) && (S_ISDIR(sb.st_mode) ||
547516
(S_ISLNK(sb.st_mode) &&
548517
(g_stat(pathbuf, &sb, pglob) == 0) &&
549518
S_ISDIR(sb.st_mode)))) {
@@ -559,7 +528,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend
559528
/* Find end of next segment, copy tentatively to pathend. */
560529
q = pathend;
561530
p = pattern;
562-
while (*p != EOS && !IS_SLASH(*p)) {
531+
while (*p != EOS && !glob_is_slash(*p)) {
563532
if (ismeta(*p))
564533
anymeta = 1;
565534
if (q+1 > pathend_last)
@@ -570,7 +539,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend
570539
if (!anymeta) { /* No expansion, do next segment. */
571540
pathend = q;
572541
pattern = p;
573-
while (IS_SLASH(*pattern)) {
542+
while (glob_is_slash(*pattern)) {
574543
if (pathend+1 > pathend_last)
575544
return (1);
576545
*pathend++ = *pattern++;
@@ -727,50 +696,58 @@ static int globextend(const Char *path, glob_t *pglob, size_t *limitp)
727696
* pattern matching function for filenames. Each occurrence of the *
728697
* pattern causes a recursion level.
729698
*/
730-
static int match(Char *name, Char *pat, Char *patend)
699+
static bool match(Char *name, Char *pat, Char *patend)
731700
{
732-
int ok, negate_range;
701+
bool ok, negate_range;
733702
Char c, k;
734703

735704
while (pat < patend) {
736705
c = *pat++;
737706
switch (c & M_MASK) {
738707
case M_ALL:
739-
if (pat == patend)
740-
return(1);
741-
do
742-
if (match(name, pat, patend))
743-
return(1);
744-
while (*name++ != EOS)
745-
;
746-
return(0);
708+
if (pat == patend) {
709+
return true;
710+
}
711+
do {
712+
if (match(name, pat, patend)) {
713+
return true;
714+
}
715+
} while (*name++ != EOS);
716+
return false;
747717
case M_ONE:
748-
if (*name++ == EOS)
749-
return(0);
718+
if (*name++ == EOS) {
719+
return false;
720+
}
750721
break;
751722
case M_SET:
752-
ok = 0;
753-
if ((k = *name++) == EOS)
754-
return(0);
755-
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
723+
ok = false;
724+
if ((k = *name++) == EOS) {
725+
return false;
726+
}
727+
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) {
756728
++pat;
729+
}
757730
while (((c = *pat++) & M_MASK) != M_END)
758731
if ((*pat & M_MASK) == M_RNG) {
759-
if (c <= k && k <= pat[1])
760-
ok = 1;
732+
if (c <= k && k <= pat[1]) {
733+
ok = true;
734+
}
761735
pat += 2;
762-
} else if (c == k)
763-
ok = 1;
764-
if (ok == negate_range)
765-
return(0);
736+
} else if (c == k) {
737+
ok = true;
738+
}
739+
if (ok == negate_range) {
740+
return false;
741+
}
766742
break;
767743
default:
768-
if (*name++ != c)
769-
return(0);
744+
if (*name++ != c) {
745+
return false;
746+
}
770747
break;
771748
}
772749
}
773-
return(*name == EOS);
750+
return *name == EOS;
774751
}
775752

776753
/* Free allocated data belonging to a glob_t structure. */
@@ -806,26 +783,26 @@ static DIR * g_opendir(Char *str, glob_t *pglob)
806783
return(opendir(buf));
807784
}
808785

809-
static int g_lstat(Char *fn, zend_stat_t *sb, glob_t *pglob)
786+
static int g_lstat(Char *fn, php_win32_ioutil_stat_t *sb, glob_t *pglob)
810787
{
811788
char buf[MAXPATHLEN];
812789

813790
if (g_Ctoc(fn, buf, sizeof(buf)))
814791
return(-1);
815792
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
816793
return((*pglob->gl_lstat)(buf, sb));
817-
return(php_sys_lstat(buf, sb));
794+
return(php_win32_ioutil_lstat(buf, sb));
818795
}
819796

820-
static int g_stat(Char *fn, zend_stat_t *sb, glob_t *pglob)
797+
static int g_stat(Char *fn, php_win32_ioutil_stat_t *sb, glob_t *pglob)
821798
{
822799
char buf[MAXPATHLEN];
823800

824801
if (g_Ctoc(fn, buf, sizeof(buf)))
825802
return(-1);
826803
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
827804
return((*pglob->gl_stat)(buf, sb));
828-
return(php_sys_stat(buf, sb));
805+
return(php_win32_ioutil_stat(buf, sb));
829806
}
830807

831808
static Char *g_strchr(Char *str, int ch)
@@ -837,14 +814,14 @@ static Char *g_strchr(Char *str, int ch)
837814
return (NULL);
838815
}
839816

840-
static int g_Ctoc(const Char *str, char *buf, u_int len)
817+
static bool g_Ctoc(const Char *str, char *buf, u_int len)
841818
{
842819

843820
while (len--) {
844821
if ((*buf++ = (char) *str++) == EOS)
845-
return (0);
822+
return false;
846823
}
847-
return (1);
824+
return true;
848825
}
849826

850827
#ifdef DEBUG

win32/glob.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@
4242
#ifndef _GLOB_H_
4343
#define _GLOB_H_
4444

45-
#ifndef PHP_WIN32
46-
# include <sys/cdefs.h>
47-
#endif
48-
49-
#include "Zend/zend_stream.h"
45+
#include <stddef.h>
46+
#include "ioutil.h"
5047

5148
typedef struct {
52-
int gl_pathc; /* Count of total paths so far. */
53-
int gl_matchc; /* Count of paths matching pattern. */
54-
int gl_offs; /* Reserved at beginning of gl_pathv. */
49+
size_t gl_pathc; /* Count of total paths so far. */
50+
unsigned int gl_matchc; /* Count of paths matching pattern. */
51+
size_t gl_offs; /* Reserved at beginning of gl_pathv. */
5552
int gl_flags; /* Copy of flags parameter to glob. */
5653
char **gl_pathv; /* List of paths matching pattern. */
5754
/* Copy of errfunc parameter to glob. */
@@ -65,8 +62,8 @@ typedef struct {
6562
void (*gl_closedir)(void *);
6663
struct dirent *(*gl_readdir)(void *);
6764
void *(*gl_opendir)(const char *);
68-
int (*gl_lstat)(const char *, zend_stat_t *);
69-
int (*gl_stat)(const char *, zend_stat_t *);
65+
int (*gl_lstat)(const char *, php_win32_ioutil_stat_t *);
66+
int (*gl_stat)(const char *, php_win32_ioutil_stat_t *);
7067
} glob_t;
7168

7269
/* Flags */
@@ -76,8 +73,6 @@ typedef struct {
7673
#define GLOB_MARK 0x0008 /* Append / to matching directories. */
7774
#define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */
7875
#define GLOB_NOSORT 0x0020 /* Don't sort. */
79-
80-
#ifndef _POSIX_SOURCE
8176
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
8277
#define GLOB_BRACE 0x0080 /* Expand braces ala csh. */
8378
#define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */
@@ -86,7 +81,6 @@ typedef struct {
8681
#define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */
8782
#define GLOB_NOESCAPE 0x1000 /* Disable backslash escaping. */
8883
#define GLOB_LIMIT 0x2000 /* Limit pattern match output to ARG_MAX */
89-
#endif
9084

9185
/* Error values returned by glob(3) */
9286
#define GLOB_NOSPACE (-1) /* Malloc call failed. */

0 commit comments

Comments
 (0)