654eff
diff -up unzip60/match.c.recmatch unzip60/match.c
654eff
--- unzip60/match.c.recmatch	2005-08-14 13:00:36.000000000 -0400
654eff
+++ unzip60/match.c	2013-05-28 10:29:57.949077543 -0400
654eff
@@ -27,16 +27,14 @@
654eff
 
654eff
   ---------------------------------------------------------------------------
654eff
 
654eff
-  Copyright on recmatch() from Zip's util.c (although recmatch() was almost
654eff
-  certainly written by Mark Adler...ask me how I can tell :-) ):
654eff
+  Copyright on recmatch() from Zip's util.c
654eff
+	 Copyright (c) 1990-2005 Info-ZIP.  All rights reserved.
654eff
 
654eff
-     Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
654eff
-     Kai Uwe Rommel and Igor Mandrichenko.
654eff
+	 See the accompanying file LICENSE, version 2004-May-22 or later
654eff
+	 for terms of use.
654eff
+	 If, for some reason, both of these files are missing, the Info-ZIP license
654eff
+	 also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html  
654eff
 
654eff
-     Permission is granted to any individual or institution to use, copy,
654eff
-     or redistribute this software so long as all of the original files are
654eff
-     included unmodified, that it is not sold for profit, and that this copy-
654eff
-     right notice is retained.
654eff
 
654eff
   ---------------------------------------------------------------------------
654eff
 
654eff
@@ -53,7 +51,7 @@
654eff
 
654eff
   A set is composed of characters or ranges; a range looks like ``character
654eff
   hyphen character'' (as in 0-9 or A-Z).  [0-9a-zA-Z_] is the minimal set of
654eff
-  characters allowed in the [..] pattern construct.  Other characters are
654eff
+  characters ALlowed in the [..] pattern construct.  Other characters are
654eff
   allowed (i.e., 8-bit characters) if your system will support them.
654eff
 
654eff
   To suppress the special syntactic significance of any of ``[]*?!^-\'', in-
654eff
@@ -101,8 +99,32 @@
654eff
 #  define WILDCHAR   '?'
654eff
 #  define BEG_RANGE  '['
654eff
 #  define END_RANGE  ']'
654eff
+#  define WILDCHR_SINGLE '?'
654eff
+#  define DIRSEP_CHR '/'
654eff
+#  define WILDCHR_MULTI '*'
654eff
 #endif
654eff
 
654eff
+#ifdef WILD_STOP_AT_DIR
654eff
+   int wild_stop_at_dir = 1; /* default wildcards do not include / in matches */
654eff
+#else
654eff
+   int wild_stop_at_dir = 0; /* default wildcards do include / in matches */
654eff
+#endif
654eff
+
654eff
+
654eff
+
654eff
+/*
654eff
+ * case mapping functions. case_map is used to ignore case in comparisons,
654eff
+ * to_up is used to force upper case even on Unix (for dosify option).
654eff
+ */
654eff
+#ifdef USE_CASE_MAP
654eff
+#  define case_map(c) upper[(c) & 0xff]
654eff
+#  define to_up(c)    upper[(c) & 0xff]
654eff
+#else
654eff
+#  define case_map(c) (c)
654eff
+#  define to_up(c)    ((c) >= 'a' && (c) <= 'z' ? (c)-'a'+'A' : (c))
654eff
+#endif /* USE_CASE_MAP */
654eff
+
654eff
+
654eff
 #if 0                /* GRR:  add this to unzip.h someday... */
654eff
 #if !(defined(MSDOS) && defined(DOSWILD))
654eff
 #ifdef WILD_STOP_AT_DIR
654eff
@@ -114,8 +136,8 @@ int recmatch OF((ZCONST uch *pattern, ZC
654eff
                  int ignore_case __WDLPRO));
654eff
 #endif
654eff
 #endif /* 0 */
654eff
-static int recmatch OF((ZCONST uch *pattern, ZCONST uch *string,
654eff
-                        int ignore_case __WDLPRO));
654eff
+static int recmatch OF((ZCONST char *, ZCONST char *, 
654eff
+                        int));
654eff
 static char *isshexp OF((ZCONST char *p));
654eff
 static int namecmp OF((ZCONST char *s1, ZCONST char *s2));
654eff
 
654eff
@@ -154,192 +176,240 @@ int match(string, pattern, ignore_case _
654eff
             }
654eff
             dospattern[j-1] = '\0';                    /* nuke the end "." */
654eff
         }
654eff
-        j = recmatch((uch *)dospattern, (uch *)string, ignore_case __WDL);
654eff
+        j = recmatch(dospattern, string, ignore_case);
654eff
         free(dospattern);
654eff
         return j == 1;
654eff
     } else
654eff
 #endif /* MSDOS && DOSWILD */
654eff
-    return recmatch((uch *)pattern, (uch *)string, ignore_case __WDL) == 1;
654eff
+    return recmatch(pattern, string, ignore_case) == 1;
654eff
 }
654eff
 
654eff
+#ifdef _MBCS
654eff
+
654eff
+char *___tmp_ptr;
654eff
 
654eff
+#endif
654eff
 
654eff
-static int recmatch(p, s, ic __WDL)
654eff
-    ZCONST uch *p;        /* sh pattern to match */
654eff
-    ZCONST uch *s;        /* string to which to match it */
654eff
-    int ic;               /* true for case insensitivity */
654eff
-    __WDLDEF              /* directory sepchar for WildStopAtDir mode, or 0 */
654eff
+static int recmatch(p, s, cs)
654eff
+ZCONST char *p;         /* sh pattern to match */
654eff
+ZCONST char *s;         /* string to match it to */
654eff
+int cs;                 /* flag: force case-sensitive matching */
654eff
 /* Recursively compare the sh pattern p with the string s and return 1 if
654eff
- * they match, and 0 or 2 if they don't or if there is a syntax error in the
654eff
- * pattern.  This routine recurses on itself no more deeply than the number
654eff
- * of characters in the pattern. */
654eff
+   they match, and 0 or 2 if they don't or if there is a syntax error in the
654eff
+   pattern.  This routine recurses on itself no deeper than the number of
654eff
+   characters in the pattern. */
654eff
 {
654eff
-    unsigned int c;       /* pattern char or start of range in [-] loop */
654eff
+  int c;                /* pattern char or start of range in [-] loop */
654eff
+  /* Get first character, the pattern for new recmatch calls follows */
654eff
+ /* borrowed from Zip's global.c */
654eff
+ int no_wild = 0; 
654eff
+ int allow_regex=1;
654eff
+  /* This fix provided by akt@m5.dion.ne.jp for Japanese.
654eff
+     See 21 July 2006 mail.
654eff
+     It only applies when p is pointing to a doublebyte character and
654eff
+     things like / and wildcards are not doublebyte.  This probably
654eff
+     should not be needed. */
654eff
 
654eff
-    /* Get first character, the pattern for new recmatch calls follows */
654eff
-    c = *p; INCSTR(p);
654eff
+#ifdef _MBCS
654eff
+  if (CLEN(p) == 2) {
654eff
+    if (CLEN(s) == 2) {
654eff
+      return (*p == *s && *(p+1) == *(s+1)) ?
654eff
+        recmatch(p + 2, s + 2, cs) : 0;
654eff
+    } else {
654eff
+      return 0;
654eff
+    }
654eff
+  }
654eff
+#endif /* ?_MBCS */
654eff
 
654eff
-    /* If that was the end of the pattern, match if string empty too */
654eff
-    if (c == 0)
654eff
-        return *s == 0;
654eff
+  c = *POSTINCSTR(p);
654eff
 
654eff
-    /* '?' (or '%') matches any character (but not an empty string). */
654eff
-    if (c == WILDCHAR)
654eff
-#ifdef WILD_STOP_AT_DIR
654eff
-        /* If uO.W_flag is non-zero, it won't match '/' */
654eff
-        return (*s && (!sepc || *s != (uch)sepc))
654eff
-               ? recmatch(p, s + CLEN(s), ic, sepc) : 0;
654eff
-#else
654eff
-        return *s ? recmatch(p, s + CLEN(s), ic) : 0;
654eff
-#endif
654eff
+  /* If that was the end of the pattern, match if string empty too */
654eff
+  if (c == 0)
654eff
+    return *s == 0;
654eff
+
654eff
+  /* '?' (or '%' or '#') matches any character (but not an empty string) */
654eff
+  if (c == WILDCHR_SINGLE) {
654eff
+    if (wild_stop_at_dir)
654eff
+      return (*s && *s != DIRSEP_CHR) ? recmatch(p, s + CLEN(s), cs) : 0;
654eff
+    else
654eff
+      return *s ? recmatch(p, s + CLEN(s), cs) : 0;
654eff
+  }
654eff
 
654eff
-    /* '*' matches any number of characters, including zero */
654eff
+  /* WILDCHR_MULTI ('*') matches any number of characters, including zero */
654eff
 #ifdef AMIGA
654eff
-    if (c == '#' && *p == '?')     /* "#?" is Amiga-ese for "*" */
654eff
-        c = '*', p++;
654eff
+  if (!no_wild && c == '#' && *p == '?')            /* "#?" is Amiga-ese for "*" */
654eff
+    c = WILDCHR_MULTI, p++;
654eff
 #endif /* AMIGA */
654eff
-    if (c == '*') {
654eff
-#ifdef WILD_STOP_AT_DIR
654eff
-        if (sepc) {
654eff
-          /* check for single "*" or double "**" */
654eff
-#  ifdef AMIGA
654eff
-          if ((c = p[0]) == '#' && p[1] == '?') /* "#?" is Amiga-ese for "*" */
654eff
-            c = '*', p++;
654eff
-          if (c != '*') {
654eff
-#  else /* !AMIGA */
654eff
-          if (*p != '*') {
654eff
-#  endif /* ?AMIGA */
654eff
-            /* single "*": this doesn't match the dirsep character */
654eff
-            for (; *s && *s != (uch)sepc; INCSTR(s))
654eff
-                if ((c = recmatch(p, s, ic, sepc)) != 0)
654eff
-                    return (int)c;
654eff
-            /* end of pattern: matched if at end of string, else continue */
654eff
-            if (*p == '\0')
654eff
-                return (*s == 0);
654eff
-            /* continue to match if at sepc in pattern, else give up */
654eff
-            return (*p == (uch)sepc || (*p == '\\' && p[1] == (uch)sepc))
654eff
-                   ? recmatch(p, s, ic, sepc) : 2;
654eff
-          }
654eff
-          /* "**": this matches slashes */
654eff
-          ++p;        /* move p behind the second '*' */
654eff
-          /* and continue with the non-W_flag code variant */
654eff
-        }
654eff
-#endif /* WILD_STOP_AT_DIR */
654eff
+  if (!no_wild && c == WILDCHR_MULTI)
654eff
+  {
654eff
+    if (wild_stop_at_dir) {
654eff
+      /* Check for an immediately following WILDCHR_MULTI */
654eff
+# ifdef AMIGA
654eff
+      if ((c = p[0]) == '#' && p[1] == '?') /* "#?" is Amiga-ese for "*" */
654eff
+        c = WILDCHR_MULTI, p++;
654eff
+      if (c != WILDCHR_MULTI) {
654eff
+# else /* !AMIGA */
654eff
+      if (*p != WILDCHR_MULTI) {
654eff
+# endif /* ?AMIGA */
654eff
+        /* Single WILDCHR_MULTI ('*'): this doesn't match slashes */
654eff
+        for (; *s && *s != DIRSEP_CHR; INCSTR(s))
654eff
+          if ((c = recmatch(p, s, cs)) != 0)
654eff
+            return c;
654eff
+        /* end of pattern: matched if at end of string, else continue */
654eff
         if (*p == 0)
654eff
-            return 1;
654eff
-        if (isshexp((ZCONST char *)p) == NULL) {
654eff
-            /* Optimization for rest of pattern being a literal string:
654eff
-             * If there are no other shell expression chars in the rest
654eff
-             * of the pattern behind the multi-char wildcard, then just
654eff
-             * compare the literal string tail.
654eff
-             */
654eff
-            ZCONST uch *srest;
654eff
-
654eff
-            srest = s + (strlen((ZCONST char *)s) - strlen((ZCONST char *)p));
654eff
-            if (srest - s < 0)
654eff
-                /* remaining literal string from pattern is longer than rest
654eff
-                 * of test string, there can't be a match
654eff
-                 */
654eff
-                return 0;
654eff
-            else
654eff
-              /* compare the remaining literal pattern string with the last
654eff
-               * bytes of the test string to check for a match
654eff
-               */
654eff
+          return (*s == 0);
654eff
+        /* continue to match if at DIRSEP_CHR in pattern, else give up */
654eff
+        return (*p == DIRSEP_CHR || (*p == '\\' && p[1] == DIRSEP_CHR))
654eff
+               ? recmatch(p, s, cs) : 2;
654eff
+      }
654eff
+      /* Two consecutive WILDCHR_MULTI ("**"): this matches DIRSEP_CHR ('/') */
654eff
+      p++;        /* move p past the second WILDCHR_MULTI */
654eff
+      /* continue with the normal non-WILD_STOP_AT_DIR code */
654eff
+    } /* wild_stop_at_dir */
654eff
+
654eff
+    /* Not wild_stop_at_dir */
654eff
+    if (*p == 0)
654eff
+      return 1;
654eff
+    if (!isshexp((char *)p))
654eff
+    {
654eff
+      /* optimization for rest of pattern being a literal string */
654eff
+
654eff
+      /* optimization to handle patterns like *.txt */
654eff
+      /* if the first char in the pattern is '*' and there */
654eff
+      /* are no other shell expression chars, i.e. a literal string */
654eff
+      /* then just compare the literal string at the end */
654eff
+
654eff
+      ZCONST char *srest;
654eff
+
654eff
+      srest = s + (strlen(s) - strlen(p));
654eff
+      if (srest - s < 0)
654eff
+        /* remaining literal string from pattern is longer than rest of
654eff
+           test string, there can't be a match
654eff
+         */
654eff
+        return 0;
654eff
+      else
654eff
+        /* compare the remaining literal pattern string with the last bytes
654eff
+           of the test string to check for a match */
654eff
 #ifdef _MBCS
654eff
-            {
654eff
-                ZCONST uch *q = s;
654eff
+      {
654eff
+        ZCONST char *q = s;
654eff
 
654eff
-                /* MBCS-aware code must not scan backwards into a string from
654eff
-                 * the end.
654eff
-                 * So, we have to move forward by character from our well-known
654eff
-                 * character position s in the test string until we have
654eff
-                 * advanced to the srest position.
654eff
-                 */
654eff
-                while (q < srest)
654eff
-                  INCSTR(q);
654eff
-                /* In case the byte *srest is a trailing byte of a multibyte
654eff
-                 * character in the test string s, we have actually advanced
654eff
-                 * past the position (srest).
654eff
-                 * For this case, the match has failed!
654eff
-                 */
654eff
-                if (q != srest)
654eff
-                    return 0;
654eff
-                return ((ic
654eff
-                         ? namecmp((ZCONST char *)p, (ZCONST char *)q)
654eff
-                         : strcmp((ZCONST char *)p, (ZCONST char *)q)
654eff
-                        ) == 0);
654eff
-            }
654eff
+        /* MBCS-aware code must not scan backwards into a string from
654eff
+         * the end.
654eff
+         * So, we have to move forward by character from our well-known
654eff
+         * character position s in the test string until we have advanced
654eff
+         * to the srest position.
654eff
+         */
654eff
+        while (q < srest)
654eff
+          INCSTR(q);
654eff
+        /* In case the byte *srest is a trailing byte of a multibyte
654eff
+         * character, we have actually advanced past the position (srest).
654eff
+         * For this case, the match has failed!
654eff
+         */
654eff
+        if (q != srest)
654eff
+          return 0;
654eff
+        return ((cs ? strcmp(p, q) : namecmp(p, q)) == 0);
654eff
+      }
654eff
 #else /* !_MBCS */
654eff
-                return ((ic
654eff
-                         ? namecmp((ZCONST char *)p, (ZCONST char *)srest)
654eff
-                         : strcmp((ZCONST char *)p, (ZCONST char *)srest)
654eff
-                        ) == 0);
654eff
+        return ((cs ? strcmp(p, srest) : namecmp(p, srest)) == 0);
654eff
 #endif /* ?_MBCS */
654eff
-        } else {
654eff
-            /* pattern contains more wildcards, continue with recursion... */
654eff
-            for (; *s; INCSTR(s))
654eff
-                if ((c = recmatch(p, s, ic __WDL)) != 0)
654eff
-                    return (int)c;
654eff
-            return 2;  /* 2 means give up--match will return false */
654eff
-        }
654eff
     }
654eff
-
654eff
-    /* Parse and process the list of characters and ranges in brackets */
654eff
-    if (c == BEG_RANGE) {
654eff
-        int e;          /* flag true if next char to be taken literally */
654eff
-        ZCONST uch *q;  /* pointer to end of [-] group */
654eff
-        int r;          /* flag true to match anything but the range */
654eff
-
654eff
-        if (*s == 0)                            /* need a character to match */
654eff
-            return 0;
654eff
-        p += (r = (*p == '!' || *p == '^'));    /* see if reverse */
654eff
-        for (q = p, e = 0; *q; INCSTR(q))       /* find closing bracket */
654eff
-            if (e)
654eff
-                e = 0;
654eff
-            else
654eff
-                if (*q == '\\')      /* GRR:  change to ^ for MS-DOS, OS/2? */
654eff
-                    e = 1;
654eff
-                else if (*q == END_RANGE)
654eff
-                    break;
654eff
-        if (*q != END_RANGE)         /* nothing matches if bad syntax */
654eff
-            return 0;
654eff
-        for (c = 0, e = (*p == '-'); p < q; INCSTR(p)) {
654eff
-            /* go through the list */
654eff
-            if (!e && *p == '\\')               /* set escape flag if \ */
654eff
-                e = 1;
654eff
-            else if (!e && *p == '-')           /* set start of range if - */
654eff
-                c = *(p-1);
654eff
-            else {
654eff
-                unsigned int cc = Case(*s);
654eff
-
654eff
-                if (*(p+1) != '-')
654eff
-                    for (c = c ? c : *p; c <= *p; c++)  /* compare range */
654eff
-                        if ((unsigned)Case(c) == cc) /* typecast for MSC bug */
654eff
-                            return r ? 0 : recmatch(q + 1, s + 1, ic __WDL);
654eff
-                c = e = 0;   /* clear range, escape flags */
654eff
-            }
654eff
-        }
654eff
-        return r ? recmatch(q + CLEN(q), s + CLEN(s), ic __WDL) : 0;
654eff
-                                        /* bracket match failed */
654eff
+    else
654eff
+    {
654eff
+      /* pattern contains more wildcards, continue with recursion... */
654eff
+      for (; *s; INCSTR(s))
654eff
+        if ((c = recmatch(p, s, cs)) != 0)
654eff
+          return c;
654eff
+      return 2;           /* 2 means give up--shmatch will return false */
654eff
     }
654eff
+  }
654eff
 
654eff
-    /* if escape ('\\'), just compare next character */
654eff
-    if (c == '\\' && (c = *p++) == 0)     /* if \ at end, then syntax error */
654eff
-        return 0;
654eff
+#ifndef VMS             /* No bracket matching in VMS */
654eff
+  /* Parse and process the list of characters and ranges in brackets */
654eff
+  if (!no_wild && allow_regex && c == '[')
654eff
+  {
654eff
+    int e;              /* flag true if next char to be taken literally */
654eff
+    ZCONST char *q;     /* pointer to end of [-] group */
654eff
+    int r;              /* flag true to match anything but the range */
654eff
+
654eff
+    if (*s == 0)                        /* need a character to match */
654eff
+      return 0;
654eff
+    p += (r = (*p == '!' || *p == '^')); /* see if reverse */
654eff
+    for (q = p, e = 0; *q; q++)         /* find closing bracket */
654eff
+      if (e)
654eff
+        e = 0;
654eff
+      else
654eff
+        if (*q == '\\')
654eff
+          e = 1;
654eff
+        else if (*q == ']')
654eff
+          break;
654eff
+    if (*q != ']')                      /* nothing matches if bad syntax */
654eff
+      return 0;
654eff
+    for (c = 0, e = *p == '-'; p < q; p++)      /* go through the list */
654eff
+    {
654eff
+      if (e == 0 && *p == '\\')         /* set escape flag if \ */
654eff
+        e = 1;
654eff
+      else if (e == 0 && *p == '-')     /* set start of range if - */
654eff
+        c = *(p-1);
654eff
+      else
654eff
+      {
654eff
+        uch cc = (cs ? (uch)*s : case_map((uch)*s));
654eff
+        uch uc = (uch) c;
654eff
+        if (*(p+1) != '-')
654eff
+          for (uc = uc ? uc : (uch)*p; uc <= (uch)*p; uc++)
654eff
+            /* compare range */
654eff
+            if ((cs ? uc : case_map(uc)) == cc)
654eff
+              return r ? 0 : recmatch(q + CLEN(q), s + CLEN(s), cs);
654eff
+        c = e = 0;                      /* clear range, escape flags */
654eff
+      }
654eff
+    }
654eff
+    return r ? recmatch(q + CLEN(q), s + CLEN(s), cs) : 0;
654eff
+                                        /* bracket match failed */
654eff
+  }
654eff
+#endif /* !VMS */
654eff
 
654eff
-    /* just a character--compare it */
654eff
-#ifdef QDOS
654eff
-    return QMatch(Case((uch)c), Case(*s)) ?
654eff
-           recmatch(p, s + CLEN(s), ic __WDL) : 0;
654eff
-#else
654eff
-    return Case((uch)c) == Case(*s) ?
654eff
-           recmatch(p, s + CLEN(s), ic __WDL) : 0;
654eff
-#endif
654eff
+  /* If escape ('\'), just compare next character */
654eff
+  if (!no_wild && c == '\\')
654eff
+    if ((c = *p++) == '\0')             /* if \ at end, then syntax error */
654eff
+      return 0;
654eff
+
654eff
+#ifdef VMS
654eff
+  /* 2005-11-06 SMS.
654eff
+     Handle "..." wildcard in p with "." or "]" in s.
654eff
+  */
654eff
+  if ((c == '.') && (*p == '.') && (*(p+ CLEN( p)) == '.') &&
654eff
+   ((*s == '.') || (*s == ']')))
654eff
+  {
654eff
+    /* Match "...]" with "]".  Continue after "]" in both. */
654eff
+    if ((*(p+ 2* CLEN( p)) == ']') && (*s == ']'))
654eff
+      return recmatch( (p+ 3* CLEN( p)), (s+ CLEN( s)), cs);
654eff
+
654eff
+    /* Else, look for a reduced match in s, until "]" in or end of s. */
654eff
+    for (; *s && (*s != ']'); INCSTR(s))
654eff
+      if (*s == '.')
654eff
+        /* If reduced match, then continue after "..." in p, "." in s. */
654eff
+        if ((c = recmatch( (p+ CLEN( p)), s, cs)) != 0)
654eff
+          return (int)c;
654eff
+
654eff
+    /* Match "...]" with "]".  Continue after "]" in both. */
654eff
+    if ((*(p+ 2* CLEN( p)) == ']') && (*s == ']'))
654eff
+      return recmatch( (p+ 3* CLEN( p)), (s+ CLEN( s)), cs);
654eff
+
654eff
+    /* No reduced match.  Quit. */
654eff
+    return 2;
654eff
+  }
654eff
+
654eff
+#endif /* def VMS */
654eff
+
654eff
+  /* Just a character--compare it */
654eff
+  return (cs ? c == *s : case_map((uch)c) == case_map((uch)*s)) ?
654eff
+          recmatch(p, s + CLEN(s), cs) : 0;
654eff
+}
654eff
 
654eff
-} /* end function recmatch() */
654eff
 
654eff
 
654eff
 
654eff
+/*************************************************************************************************/
654eff
 static char *isshexp(p)
654eff
 ZCONST char *p;
654eff
 /* If p is a sh expression, a pointer to the first special character is