2 Copyright (c) 1990-2005 Info-ZIP. All rights reserved.
4 See the accompanying file LICENSE, version 2000-Apr-09 or later
5 (the contents of which are also included in unzip.h) for terms of use.
6 If, for some reason, all these files are missing, the Info-ZIP license
7 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
9 /*---------------------------------------------------------------------------
13 The match() routine recursively compares a string to a "pattern" (regular
14 expression), returning TRUE if a match is found or FALSE if not. This
15 version is specifically for use with unzip.c: as did the previous match()
16 routines from SEA and J. Kercheval, it leaves the case (upper, lower, or
17 mixed) of the string alone, but converts any uppercase characters in the
18 pattern to lowercase if indicated by the global var pInfo->lcflag (which
19 is to say, string is assumed to have been converted to lowercase already,
20 if such was necessary).
22 GRR: reversed order of text, pattern in matche() (now same as match());
23 added ignore_case/ic flags, Case() macro.
25 PaulK: replaced matche() with recmatch() from Zip, modified to have an
26 ignore_case argument; replaced test frame with simpler one.
28 ---------------------------------------------------------------------------
30 Copyright on recmatch() from Zip's util.c (although recmatch() was almost
31 certainly written by Mark Adler...ask me how I can tell :-) ):
33 Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
34 Kai Uwe Rommel and Igor Mandrichenko.
36 Permission is granted to any individual or institution to use, copy,
37 or redistribute this software so long as all of the original files are
38 included unmodified, that it is not sold for profit, and that this copy-
39 right notice is retained.
41 ---------------------------------------------------------------------------
43 Match the pattern (wildcard) against the string (fixed):
45 match(string, pattern, ignore_case, sepc);
47 returns TRUE if string matches pattern, FALSE otherwise. In the pattern:
49 `*' matches any sequence of characters (zero or more)
50 `?' matches any single character
51 [SET] matches any character in the specified set,
52 [!SET] or [^SET] matches any character not in the specified set.
54 A set is composed of characters or ranges; a range looks like ``character
55 hyphen character'' (as in 0-9 or A-Z). [0-9a-zA-Z_] is the minimal set of
56 characters allowed in the [..] pattern construct. Other characters are
57 allowed (i.e., 8-bit characters) if your system will support them.
59 To suppress the special syntactic significance of any of ``[]*?!^-\'', in-
60 side or outside a [..] construct, and match the character exactly, precede
61 it with a ``\'' (backslash).
63 Note that "*.*" and "*." are treated specially under MS-DOS if DOSWILD is
64 defined. See the DOSWILD section below for an explanation. Note also
65 that with VMSWILD defined, '%' is used instead of '?', and sets (ranges)
66 are delimited by () instead of [].
68 ---------------------------------------------------------------------------*/
71 #define __MATCH_C /* identifies this source module */
73 /* define ToLower() in here (for Unix, define ToLower to be macro (using
74 * isupper()); otherwise just use tolower() */
75 #define UNZIP_INTERNAL
78 #ifndef THEOS /* the Theos port defines its own variant of match() */
80 #if 0 /* this is not useful until it matches Amiga names insensitively */
81 #ifdef AMIGA /* some other platforms might also want to use this */
82 # define ANSI_CHARSET /* MOVE INTO UNZIP.H EVENTUALLY */
90 /* uppercase letters are values 41 thru 5A, C0 thru D6, and D8 thru DE */
91 # define IsUpper(c) (c>=0xC0 ? c<=0xDE && c!=0xD7 : c>=0x41 && c<=0x5A)
92 # define ToLower(c) (IsUpper((uch) c) ? (unsigned) c | 0x20 : (unsigned) c)
94 #define Case(x) (ic? ToLower(x) : (x))
98 # define BEG_RANGE '('
99 # define END_RANGE ')'
101 # define WILDCHAR '?'
102 # define BEG_RANGE '['
103 # define END_RANGE ']'
106 #if 0 /* GRR: add this to unzip.h someday... */
107 #if !(defined(MSDOS) && defined(DOSWILD))
108 #ifdef WILD_STOP_AT_DIR
109 #define match(s,p,ic,sc) (recmatch((ZCONST uch *)p,(ZCONST uch *)s,ic,sc) == 1)
111 #define match(s,p,ic) (recmatch((ZCONST uch *)p,(ZCONST uch *)s,ic) == 1)
113 int recmatch
OF((ZCONST uch
*pattern
, ZCONST uch
*string
,
114 int ignore_case __WDLPRO
));
117 static int recmatch
OF((ZCONST uch
*pattern
, ZCONST uch
*string
,
118 int ignore_case __WDLPRO
));
119 static char *isshexp
OF((ZCONST
char *p
));
120 static int namecmp
OF((ZCONST
char *s1
, ZCONST
char *s2
));
123 /* match() is a shell to recmatch() to return only Boolean values. */
125 int match(string
, pattern
, ignore_case __WDL
)
126 ZCONST
char *string
, *pattern
;
130 #if (defined(MSDOS) && defined(DOSWILD))
132 int j
= strlen(pattern
);
134 /*---------------------------------------------------------------------------
135 Optional MS-DOS preprocessing section: compare last three chars of the
136 wildcard to "*.*" and translate to "*" if found; else compare the last
137 two characters to "*." and, if found, scan the non-wild string for dots.
138 If in the latter case a dot is found, return failure; else translate the
139 "*." to "*". In either case, continue with the normal (Unix-like) match
140 procedure after translation. (If not enough memory, default to normal
141 match.) This causes "a*.*" and "a*." to behave as MS-DOS users expect.
142 ---------------------------------------------------------------------------*/
144 if ((dospattern
= (char *)malloc(j
+1)) != NULL
) {
145 strcpy(dospattern
, pattern
);
146 if (!strcmp(dospattern
+j
-3, "*.*")) {
147 dospattern
[j
-2] = '\0'; /* nuke the ".*" */
148 } else if (!strcmp(dospattern
+j
-2, "*.")) {
149 char *p
= MBSCHR(string
, '.');
151 if (p
) { /* found a dot: match fails */
155 dospattern
[j
-1] = '\0'; /* nuke the end "." */
157 j
= recmatch((uch
*)dospattern
, (uch
*)string
, ignore_case __WDL
);
161 #endif /* MSDOS && DOSWILD */
162 return recmatch((uch
*)pattern
, (uch
*)string
, ignore_case __WDL
) == 1;
167 static int recmatch(p
, s
, ic __WDL
)
168 ZCONST uch
*p
; /* sh pattern to match */
169 ZCONST uch
*s
; /* string to which to match it */
170 int ic
; /* true for case insensitivity */
171 __WDLDEF
/* directory sepchar for WildStopAtDir mode, or 0 */
172 /* Recursively compare the sh pattern p with the string s and return 1 if
173 * they match, and 0 or 2 if they don't or if there is a syntax error in the
174 * pattern. This routine recurses on itself no more deeply than the number
175 * of characters in the pattern. */
177 unsigned int c
; /* pattern char or start of range in [-] loop */
179 /* Get first character, the pattern for new recmatch calls follows */
182 /* If that was the end of the pattern, match if string empty too */
186 /* '?' (or '%') matches any character (but not an empty string). */
188 #ifdef WILD_STOP_AT_DIR
189 /* If uO.W_flag is non-zero, it won't match '/' */
190 return (*s
&& (!sepc
|| *s
!= (uch
)sepc
))
191 ? recmatch(p
, s
+ CLEN(s
), ic
, sepc
) : 0;
193 return *s
? recmatch(p
, s
+ CLEN(s
), ic
) : 0;
196 /* '*' matches any number of characters, including zero */
198 if (c
== '#' && *p
== '?') /* "#?" is Amiga-ese for "*" */
202 #ifdef WILD_STOP_AT_DIR
204 /* check for single "*" or double "**" */
206 if ((c
= p
[0]) == '#' && p
[1] == '?') /* "#?" is Amiga-ese for "*" */
212 /* single "*": this doesn't match the dirsep character */
213 for (; *s
&& *s
!= (uch
)sepc
; INCSTR(s
))
214 if ((c
= recmatch(p
, s
, ic
, sepc
)) != 0)
216 /* end of pattern: matched if at end of string, else continue */
219 /* continue to match if at sepc in pattern, else give up */
220 return (*p
== (uch
)sepc
|| (*p
== '\\' && p
[1] == (uch
)sepc
))
221 ? recmatch(p
, s
, ic
, sepc
) : 2;
223 /* "**": this matches slashes */
224 ++p
; /* move p behind the second '*' */
225 /* and continue with the non-W_flag code variant */
227 #endif /* WILD_STOP_AT_DIR */
230 if (isshexp((ZCONST
char *)p
) == NULL
) {
231 /* Optimization for rest of pattern being a literal string:
232 * If there are no other shell expression chars in the rest
233 * of the pattern behind the multi-char wildcard, then just
234 * compare the literal string tail.
238 srest
= s
+ (strlen((ZCONST
char *)s
) - strlen((ZCONST
char *)p
));
240 /* remaining literal string from pattern is longer than rest
241 * of test string, there can't be a match
245 /* compare the remaining literal pattern string with the last
246 * bytes of the test string to check for a match
252 /* MBCS-aware code must not scan backwards into a string from
254 * So, we have to move forward by character from our well-known
255 * character position s in the test string until we have
256 * advanced to the srest position.
260 /* In case the byte *srest is a trailing byte of a multibyte
261 * character in the test string s, we have actually advanced
262 * past the position (srest).
263 * For this case, the match has failed!
268 ? namecmp((ZCONST
char *)p
, (ZCONST
char *)q
)
269 : strcmp((ZCONST
char *)p
, (ZCONST
char *)q
)
274 ? namecmp((ZCONST
char *)p
, (ZCONST
char *)srest
)
275 : strcmp((ZCONST
char *)p
, (ZCONST
char *)srest
)
279 /* pattern contains more wildcards, continue with recursion... */
280 for (; *s
; INCSTR(s
))
281 if ((c
= recmatch(p
, s
, ic __WDL
)) != 0)
283 return 2; /* 2 means give up--match will return false */
287 /* Parse and process the list of characters and ranges in brackets */
288 if (c
== BEG_RANGE
) {
289 int e
; /* flag true if next char to be taken literally */
290 ZCONST uch
*q
; /* pointer to end of [-] group */
291 int r
; /* flag true to match anything but the range */
293 if (*s
== 0) /* need a character to match */
295 p
+= (r
= (*p
== '!' || *p
== '^')); /* see if reverse */
296 for (q
= p
, e
= 0; *q
; INCSTR(q
)) /* find closing bracket */
300 if (*q
== '\\') /* GRR: change to ^ for MS-DOS, OS/2? */
302 else if (*q
== END_RANGE
)
304 if (*q
!= END_RANGE
) /* nothing matches if bad syntax */
306 for (c
= 0, e
= (*p
== '-'); p
< q
; INCSTR(p
)) {
307 /* go through the list */
308 if (!e
&& *p
== '\\') /* set escape flag if \ */
310 else if (!e
&& *p
== '-') /* set start of range if - */
313 unsigned int cc
= Case(*s
);
316 for (c
= c
? c
: *p
; c
<= *p
; c
++) /* compare range */
317 if ((unsigned)Case(c
) == cc
) /* typecast for MSC bug */
318 return r
? 0 : recmatch(q
+ 1, s
+ 1, ic __WDL
);
319 c
= e
= 0; /* clear range, escape flags */
322 return r
? recmatch(q
+ CLEN(q
), s
+ CLEN(s
), ic __WDL
) : 0;
323 /* bracket match failed */
326 /* if escape ('\\'), just compare next character */
327 if (c
== '\\' && (c
= *p
++) == 0) /* if \ at end, then syntax error */
330 /* just a character--compare it */
332 return QMatch(Case((uch
)c
), Case(*s
)) ?
333 recmatch(p
, s
+ CLEN(s
), ic __WDL
) : 0;
335 return Case((uch
)c
) == Case(*s
) ?
336 recmatch(p
, s
+ CLEN(s
), ic __WDL
) : 0;
339 } /* end function recmatch() */
343 static char *isshexp(p
)
345 /* If p is a sh expression, a pointer to the first special character is
346 returned. Otherwise, NULL is returned. */
348 for (; *p
; INCSTR(p
))
349 if (*p
== '\\' && *(p
+1))
351 else if (*p
== WILDCHAR
|| *p
== '*' || *p
== BEG_RANGE
)
354 } /* end function isshexp() */
358 static int namecmp(s1
, s2
)
359 ZCONST
char *s1
, *s2
;
364 d
= (int)ToLower((uch
)*s1
)
365 - (int)ToLower((uch
)*s2
);
367 if (d
|| *s1
== 0 || *s2
== 0)
373 } /* end function namecmp() */
380 int iswild(p
) /* originally only used for stat()-bug workaround in */
381 ZCONST
char *p
; /* VAX C, Turbo/Borland C, Watcom C, Atari MiNT libs; */
382 { /* now used in process_zipfiles() as well */
383 for (; *p
; INCSTR(p
))
384 if (*p
== '\\' && *(p
+1))
387 else if (*p
== '?' || *p
== '*' || *p
=='#'|| *p
== '@')
390 else if (*p
== '%' || *p
== '*')
393 else if (*p
== '?' || *p
== '*' || (*p
=='#' && p
[1]=='?') || *p
== '[')
395 else if (*p
== '?' || *p
== '*' || *p
== '[')
407 } /* end function iswild() */
415 #define put(s) {fputs(s,stdout); fflush(stdout);}
420 int main(int argc
, char **argv
)
422 char pat
[256], str
[256];
425 put("Pattern (return to exit): ");
430 put("String (return for new pattern): ");
434 printf("Case sensitive: %s insensitive: %s\n",
435 match(str
, pat
, 0) ? "YES" : "NO",
436 match(str
, pat
, 1) ? "YES" : "NO");
442 #endif /* TEST_MATCH */