Bash-4.2 patch 3
[platform/upstream/bash.git] / lib / glob / smatch.c
index 8c54702..061142b 100644 (file)
@@ -1,23 +1,23 @@
 /* strmatch.c -- ksh-like extended pattern matching for the shell and filename
                globbing. */
 
-/* Copyright (C) 1991-2002 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2011 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
    
-   Bash is free software; you can redistribute it and/or modify it under
-   the terms of the GNU General Public License as published by the Free
-   Software Foundation; either version 2, or (at your option) any later
-   version.
-             
-   Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-   WARRANTY; without even the implied warranty of MERCHANTABILITY or
-   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-   for more details.
-                        
-   You should have received a copy of the GNU General Public License along
-   with Bash; see the file COPYING.  If not, write to the Free Software
-   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include <config.h>
 
@@ -93,14 +93,16 @@ collequiv (c1, c2)
 
 static int
 collsym (s, len)
-     char *s;
+     CHAR *s;
      int len;
 {
   register struct _collsym *csp;
+  char *x;
 
+  x = (char *)s;
   for (csp = posix_collsyms; csp->name; csp++)
     {
-      if (STREQN(csp->name, s, len) && csp->name[len] == '\0')
+      if (STREQN(csp->name, x, len) && csp->name[len] == '\0')
        return (csp->code);
     }
   if (len == 1)
@@ -239,13 +241,14 @@ is_cclass (c, name)
 #  define STREQ(s1, s2) ((wcscmp (s1, s2) == 0))
 #  define STREQN(a, b, n) ((a)[0] == (b)[0] && wcsncmp(a, b, n) == 0)
 
+extern char *mbsmbchar __P((const char *));
+
 static int
 rangecmp_wc (c1, c2)
      wint_t c1, c2;
 {
   static wchar_t s1[2] = { L' ', L'\0' };
   static wchar_t s2[2] = { L' ', L'\0' };
-  int ret;
 
   if (c1 == c2)
     return 0;
@@ -313,7 +316,7 @@ is_wcclass (wc, name)
 
   memset (&state, '\0', sizeof (mbstate_t));
   mbs = (char *) malloc (wcslen(name) * MB_CUR_MAX + 1);
-  mbslength = wcsrtombs(mbs, (const wchar_t **)&name, (wcslen(name) * MB_CUR_MAX + 1), &state);
+  mbslength = wcsrtombs (mbs, (const wchar_t **)&name, (wcslen(name) * MB_CUR_MAX + 1), &state);
 
   if (mbslength == (size_t)-1 || mbslength == (size_t)-2)
     {
@@ -362,44 +365,35 @@ xstrmatch (pattern, string, flags)
 {
 #if HANDLE_MULTIBYTE
   int ret;
-  mbstate_t ps;
   size_t n;
-  char *pattern_bak;
   wchar_t *wpattern, *wstring;
+  size_t plen, slen, mplen, mslen;
 
-  if (MB_CUR_MAX == 1)
-    return (internal_strmatch (pattern, string, flags));
+#if 0
+  plen = strlen (pattern);
+  mplen = mbstrlen (pattern);
+  if (plen == mplen && strlen (string) == mbstrlen (string))
+#else
+  if (mbsmbchar (string) == 0 && mbsmbchar (pattern) == 0)
+#endif
+    return (internal_strmatch ((unsigned char *)pattern, (unsigned char *)string, flags));
 
-  pattern_bak = (char *)xmalloc (strlen (pattern) + 1);
-  strcpy (pattern_bak, pattern);
+  if (MB_CUR_MAX == 1)
+    return (internal_strmatch ((unsigned char *)pattern, (unsigned char *)string, flags));
 
-  memset (&ps, '\0', sizeof (mbstate_t));
-  n = xmbsrtowcs (NULL, (const char **)&pattern, 0, &ps);
+  n = xdupmbstowcs (&wpattern, NULL, pattern);
   if (n == (size_t)-1 || n == (size_t)-2)
-    {
-      free (pattern_bak);
-      return (internal_strmatch ((unsigned char *)pattern, (unsigned char *)string, flags));
-    }
+    return (internal_strmatch ((unsigned char *)pattern, (unsigned char *)string, flags));
 
-  wpattern = (wchar_t *)xmalloc ((n + 1) * sizeof (wchar_t));
-  (void) xmbsrtowcs (wpattern, (const char **)&pattern, n + 1, &ps);
-
-  memset (&ps, '\0', sizeof (mbstate_t));
-  n = xmbsrtowcs (NULL, (const char **)&string, 0, &ps);
+  n = xdupmbstowcs (&wstring, NULL, string);
   if (n == (size_t)-1 || n == (size_t)-2)
     {
       free (wpattern);
-      ret = internal_strmatch (pattern_bak, string, flags);
-      free (pattern_bak);
-      return ret;
+      return (internal_strmatch ((unsigned char *)pattern, (unsigned char *)string, flags));
     }
 
-  wstring = (wchar_t *)xmalloc ((n + 1) * sizeof (wchar_t));
-  (void) xmbsrtowcs (wstring, (const char **)&string, n + 1, &ps);
-
   ret = internal_wstrmatch (wpattern, wstring, flags);
 
-  free (pattern_bak);
   free (wpattern);
   free (wstring);