OSDN Git Service

Honour GLOB_CASEMATCH for globbing sets; cf. issue [#2327].
[mingw/mingw-org-wsl.git] / mingwrt / mingwex / glob.c
index 6de4880..1f064e1 100644 (file)
@@ -7,7 +7,7 @@
  * $Id$
  *
  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
- * Copyright (C) 2011-2014, MinGW.org Project.
+ * Copyright (C) 2011-2014, 2017, MinGW.org Project.
  *
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -227,6 +227,15 @@ static const char *glob_set_adjusted( const char *pattern, int flags )
   return ++p;
 }
 
+GLOB_INLINE int glob_case_match( int flags, int check, int match )
+{
+  /* Local helper function, used to facilitate the case insensitive
+   * glob character matching appropriate for MS-Windows systems.
+   */
+  return (flags & GLOB_CASEMATCH) ? check - match
+    : tolower( check ) - tolower( match );
+}
+
 static const char *glob_in_set( const char *set, int test, int flags )
 {
   /* Check if the single character "test" is present in the set
@@ -283,7 +292,7 @@ static const char *glob_in_set( const char *set, int test, int flags )
        /* ...in incremental collating sequence order, to the next
         * character following the '-'...
         */
-       if( lastc++ == test )
+       if( glob_case_match( flags, lastc++, test ) == 0 )
          /*
           * ...returning immediately on a successful match...
           */
@@ -295,7 +304,7 @@ static const char *glob_in_set( const char *set, int test, int flags )
         * range may have been specified in decrementing collating
         * sequence order...
         */
-       if( lastc-- == test )
+       if( glob_case_match( flags, lastc--, test ) == 0 )
          /*
           * ...once again, return immediately on a successful match.
           */
@@ -316,7 +325,7 @@ static const char *glob_in_set( const char *set, int test, int flags )
        */
       return NULL;
 
-    if( c == test )
+    if( glob_case_match( flags, c, test ) == 0 )
       /*
        * We found the test character within the set; adjust the pattern
        * reference, to resume after the end of the set, and return the
@@ -337,15 +346,6 @@ static const char *glob_in_set( const char *set, int test, int flags )
   return NULL;
 }
 
-GLOB_INLINE int glob_case_match( int flags, int check, int match )
-{
-  /* Local helper function, used to facilitate the case insensitive
-   * glob character matching appropriate for MS-Windows systems.
-   */
-  return (flags & GLOB_CASEMATCH) ? check - match
-    : tolower( check ) - tolower( match );
-}
-
 static int glob_strcmp( const char *pattern, const char *text, int flags )
 {
   /* Compare "text" to a specified globbing "pattern" using semantics
@@ -1039,9 +1039,22 @@ __mingw_glob( const char *pattern, int flags, int (*errfn)(), glob_t *gl_data )
   /* Module entry point for the glob() function.
    */
   int status;
+
   /* First, consult the glob "registry", to ensure that the
    * glob data structure passed by the caller, has been properly
-   * initialised.
+   * initialised.  (Note that this implementation gratuitously uses
+   * gl_data->gl_offs, irrespective of specification of GLOB_DOOFFS
+   * in the flags; while the user must accept responsibility for the
+   * initialisation of gl_data->gl_offs when specifying GLOB_DOOFFS,
+   * this is not the case when GLOB_DOOFFS is not specified; in the
+   * latter case, WE must assume the responsibility, ensuring that
+   * the required zero value is assigned BEFORE registration).
+   */
+  if( (gl_data != NULL) && ((flags & GLOB_DOOFFS) == 0) )
+    gl_data->gl_offs = 0;
+
+  /* With this pre-registration requirement satisfied, we may now
+   * proceed to register the provided gl_data structure.
    */
   gl_data = glob_registry( GLOB_INIT, gl_data );