OSDN Git Service

Address MinGW-Issue #39658; declare rand_s() function.
[mingw/mingw-org-wsl.git] / mingwrt / setargv.c
index 09e543c..712ad83 100644 (file)
@@ -8,7 +8,7 @@
  * $Id$
  *
  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
- * Copyright (C) 2014, MinGW.org Project
+ * Copyright (C) 2014, 2017, 2018, MinGW.org Project
  *
  * ---------------------------------------------------------------------------
  *
@@ -33,6 +33,8 @@
  * ---------------------------------------------------------------------------
  *
  */
+#define _ISOC99_SOURCE
+
 #include <glob.h>
 #include <string.h>
 #include <ctype.h>
@@ -91,7 +93,7 @@ void __mingw32_setargv( const char *cmdline )
 {
   /* Implementation of the MinGW replacement command line interpreter.
    */
-  char cmdbuf[1 + strlen( cmdline ) << 1];
+  char cmdbuf[(1 + strlen( cmdline )) << 1];
   int c, gotarg = 0, quoted = 0, bracket = 0, bslash = 0;
   char *argptr = cmdbuf; const char *cmdptr = cmdline;
   glob_t gl_argv;
@@ -99,9 +101,7 @@ void __mingw32_setargv( const char *cmdline )
   /* Capture any non-default globbing options, which the user may have
    * specified via a custom setting for _CRT_glob.
    */
-  int gl_opts = GLOB_NOCHECK;
-  if( _CRT_glob & __CRT_GLOB_CASE_SENSITIVE__ )
-    gl_opts |= GLOB_CASEMATCH;
+  int gl_opts = GLOB_NOCHECK | (_CRT_glob & (GLOB_CASEMATCH | GLOB_BRACE));
 
   /* We explicitly DO NOT use the GLOB_DOOFFS capability; ensure that
    * the associated field, in the glob_t structure, is initialized to
@@ -111,7 +111,7 @@ void __mingw32_setargv( const char *cmdline )
 
   /* Scan the command line, and prepare it for globbing.
    */
-  while( c = *cmdptr++ )
+  while( (c = *cmdptr++) != '\0' )
   {
     /* Got a character to process...
      */
@@ -121,12 +121,18 @@ void __mingw32_setargv( const char *cmdline )
        * need special handling.
        */
       case '\\':
-       /* We don't (yet) know if this is a literal backslash,
-        * (directory separator), or an escape for a following
-        * quote character; just note its presence, until we
-        * have looked far enough ahead to decide.
-        */
-       ++bslash;
+       if( quoted == '\'' )
+         /* Backslashes within single quotes are always literal.
+          */
+         *argptr++ = '\\';
+
+       else
+         /* We don't (yet) know if this is a literal backslash,
+          * (directory separator), or an escape for a following
+          * quote character; just note its presence, until we
+          * have looked far enough ahead to decide.
+          */
+         ++bslash;
        break;
 
       case '[':
@@ -137,9 +143,14 @@ void __mingw32_setargv( const char *cmdline )
         */
        bracket = (_CRT_glob & ARGV_NOGROUP) ? 0 : ARGV_NOGROUP;
 
-      case '*':
-      case '?':
-       /* These standard globbing tokens...
+      case '*': case '?':
+       /* These standard globbing tokens,...
+        */
+      case '{': case ',': case '}':
+       /* ...this additional triplet, non-standard, but required
+        * to support GNU's GLOB_BRACE extension; (strictly we need
+        * to consider these only if GLOB_BRACE is enabled, but it
+        * should do no harm to consider them regardless),...
         */
       case ARGV_ESCAPE:
        /* ...and the escape character itself, need to be escaped
@@ -181,9 +192,9 @@ void __mingw32_setargv( const char *cmdline )
         * literally, after flushing out any pending backslashes.
         */
        argptr = backslash( bslash, argptr );
-       if( (quoted == 0) && isspace( c ) )
+       if( (quoted == 0) && isblank( c ) )
        {
-         /* The one exception is any white space character,
+         /* The one exception is any blank or tab character,
           * when it is not contained within quotes; this acts
           * as an argument separator, (or is simply discarded
           * if there is no argument already collected)...