58
58
* gl_matchc:
59
59
* Number of matches in the current invocation of glob.
60
60
*/
61
- #ifdef PHP_WIN32
62
- #if _MSC_VER < 1800
63
- # define _POSIX_
64
- # include <limits.h>
65
- # undef _POSIX_
66
- #else
61
+
67
62
/* 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
72
65
#endif
73
66
74
- #include "php.h"
67
+ #include "glob.h"
68
+ #include <stdbool.h>
69
+ #include <stdint.h>
75
70
#include <sys/stat.h>
76
71
77
72
#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
84
73
#include <errno.h>
85
- #include "glob.h"
86
74
#include <stdio.h>
87
75
#include <stdlib.h>
88
76
#include <string.h>
77
+ #include "php.h"
89
78
90
79
#define DOLLAR '$'
91
80
#define DOT '.'
96
85
#define QUOTE '\\'
97
86
#define RANGE '-'
98
87
#define RBRACKET ']'
99
- #define SEP DEFAULT_SLASH
88
+ /* As this is Windows we use a backslash for the separator */
89
+ #define SEP '\\'
100
90
#define STAR '*'
101
91
#define TILDE '~'
102
92
#define UNDERSCORE '_'
@@ -137,11 +127,11 @@ typedef char Char;
137
127
#define ismeta (c ) (((c)&M_QUOTE) != 0)
138
128
139
129
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 * );
142
132
static DIR * g_opendir (Char * , glob_t * );
143
133
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 * );
145
135
static int glob0 (const Char * , glob_t * );
146
136
static int glob1 (Char * , Char * , glob_t * , size_t * );
147
137
static int glob2 (Char * , Char * , Char * , Char * , Char * , Char * ,
@@ -152,7 +142,7 @@ static int globextend(const Char *, glob_t *, size_t *);
152
142
static const Char * globtilde (const Char * , Char * , size_t , glob_t * );
153
143
static int globexp1 (const Char * , glob_t * );
154
144
static int globexp2 (const Char * , const Char * , glob_t * , int * );
155
- static int match (Char * , Char * , Char * );
145
+ static bool match (Char * , Char * , Char * );
156
146
#ifdef DEBUG
157
147
static void qprintf (const char * , Char * );
158
148
#endif
@@ -341,9 +331,6 @@ static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv
341
331
*/
342
332
static const Char * globtilde (const Char * pattern , Char * patbuf , size_t patbuf_len , glob_t * pglob )
343
333
{
344
- #ifndef PHP_WIN32
345
- struct passwd * pwd ;
346
- #endif
347
334
char * h ;
348
335
const Char * p ;
349
336
Char * b , * eb ;
@@ -359,38 +346,19 @@ static const Char *globtilde(const Char *pattern, Char *patbuf, size_t patbuf_le
359
346
360
347
* h = EOS ;
361
348
362
- #if 0
363
- if (h == (char * )eb )
364
- return what ;
365
- #endif
366
-
367
349
if (((char * ) patbuf )[0 ] == EOS ) {
368
350
/*
369
351
* handle a plain ~ or ~/ by expanding $HOME
370
352
* first and then trying the password file
371
353
*/
372
354
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
379
355
return pattern ;
380
- #endif
381
356
}
382
357
} else {
383
358
/*
384
359
* Expand a ~user
385
360
*/
386
- #ifndef PHP_WIN32
387
- if ((pwd = getpwnam ((char * ) patbuf )) == NULL )
388
- return pattern ;
389
- else
390
- h = pwd -> pw_dir ;
391
- #else
392
361
return pattern ;
393
- #endif
394
362
}
395
363
396
364
/* Copy the home directory */
@@ -504,11 +472,7 @@ static int compare(const void *p, const void *q)
504
472
return (strcmp (* (char * * )p , * (char * * )q ));
505
473
}
506
474
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 )
512
476
{
513
477
Char pathbuf [MAXPATHLEN ];
514
478
@@ -520,6 +484,11 @@ glob1(pattern, pattern_last, pglob, limitp)
520
484
pattern , pattern_last , pglob , limitp ));
521
485
}
522
486
487
+ static inline bool glob_is_slash (Char c )
488
+ {
489
+ return c == '/' || c == '\\' ;
490
+ }
491
+
523
492
/*
524
493
* The functions glob2 and glob3 are mutually recursive; there is one level
525
494
* of recursion for each segment in the pattern that contains one or more
@@ -528,7 +497,7 @@ glob1(pattern, pattern_last, pglob, limitp)
528
497
static int glob2 (Char * pathbuf , Char * pathbuf_last , Char * pathend , Char * pathend_last , Char * pattern ,
529
498
Char * pattern_last , glob_t * pglob , size_t * limitp )
530
499
{
531
- zend_stat_t sb = {0 };
500
+ php_win32_ioutil_stat_t sb = {0 };
532
501
Char * p , * q ;
533
502
int anymeta ;
534
503
@@ -543,7 +512,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend
543
512
return (0 );
544
513
545
514
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 ) ||
547
516
(S_ISLNK (sb .st_mode ) &&
548
517
(g_stat (pathbuf , & sb , pglob ) == 0 ) &&
549
518
S_ISDIR (sb .st_mode )))) {
@@ -559,7 +528,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend
559
528
/* Find end of next segment, copy tentatively to pathend. */
560
529
q = pathend ;
561
530
p = pattern ;
562
- while (* p != EOS && !IS_SLASH (* p )) {
531
+ while (* p != EOS && !glob_is_slash (* p )) {
563
532
if (ismeta (* p ))
564
533
anymeta = 1 ;
565
534
if (q + 1 > pathend_last )
@@ -570,7 +539,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend
570
539
if (!anymeta ) { /* No expansion, do next segment. */
571
540
pathend = q ;
572
541
pattern = p ;
573
- while (IS_SLASH (* pattern )) {
542
+ while (glob_is_slash (* pattern )) {
574
543
if (pathend + 1 > pathend_last )
575
544
return (1 );
576
545
* pathend ++ = * pattern ++ ;
@@ -727,50 +696,58 @@ static int globextend(const Char *path, glob_t *pglob, size_t *limitp)
727
696
* pattern matching function for filenames. Each occurrence of the *
728
697
* pattern causes a recursion level.
729
698
*/
730
- static int match (Char * name , Char * pat , Char * patend )
699
+ static bool match (Char * name , Char * pat , Char * patend )
731
700
{
732
- int ok , negate_range ;
701
+ bool ok , negate_range ;
733
702
Char c , k ;
734
703
735
704
while (pat < patend ) {
736
705
c = * pat ++ ;
737
706
switch (c & M_MASK ) {
738
707
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;
747
717
case M_ONE :
748
- if (* name ++ == EOS )
749
- return (0 );
718
+ if (* name ++ == EOS ) {
719
+ return false;
720
+ }
750
721
break ;
751
722
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 ) {
756
728
++ pat ;
729
+ }
757
730
while (((c = * pat ++ ) & M_MASK ) != M_END )
758
731
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
+ }
761
735
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
+ }
766
742
break ;
767
743
default :
768
- if (* name ++ != c )
769
- return (0 );
744
+ if (* name ++ != c ) {
745
+ return false;
746
+ }
770
747
break ;
771
748
}
772
749
}
773
- return ( * name == EOS ) ;
750
+ return * name == EOS ;
774
751
}
775
752
776
753
/* Free allocated data belonging to a glob_t structure. */
@@ -806,26 +783,26 @@ static DIR * g_opendir(Char *str, glob_t *pglob)
806
783
return (opendir (buf ));
807
784
}
808
785
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 )
810
787
{
811
788
char buf [MAXPATHLEN ];
812
789
813
790
if (g_Ctoc (fn , buf , sizeof (buf )))
814
791
return (-1 );
815
792
if (pglob -> gl_flags & GLOB_ALTDIRFUNC )
816
793
return ((* pglob -> gl_lstat )(buf , sb ));
817
- return (php_sys_lstat (buf , sb ));
794
+ return (php_win32_ioutil_lstat (buf , sb ));
818
795
}
819
796
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 )
821
798
{
822
799
char buf [MAXPATHLEN ];
823
800
824
801
if (g_Ctoc (fn , buf , sizeof (buf )))
825
802
return (-1 );
826
803
if (pglob -> gl_flags & GLOB_ALTDIRFUNC )
827
804
return ((* pglob -> gl_stat )(buf , sb ));
828
- return (php_sys_stat (buf , sb ));
805
+ return (php_win32_ioutil_stat (buf , sb ));
829
806
}
830
807
831
808
static Char * g_strchr (Char * str , int ch )
@@ -837,14 +814,14 @@ static Char *g_strchr(Char *str, int ch)
837
814
return (NULL );
838
815
}
839
816
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 )
841
818
{
842
819
843
820
while (len -- ) {
844
821
if ((* buf ++ = (char ) * str ++ ) == EOS )
845
- return ( 0 ) ;
822
+ return false ;
846
823
}
847
- return ( 1 ) ;
824
+ return true ;
848
825
}
849
826
850
827
#ifdef DEBUG
0 commit comments