]> Savannah Git Hosting - gnulib.git/commitdiff
windows-spawn: Export an auxiliary function.
authorBruno Haible <bruno@clisp.org>
Thu, 24 Dec 2020 21:18:10 +0000 (22:18 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 24 Dec 2020 21:18:10 +0000 (22:18 +0100)
* lib/windows-spawn.h (compose_command): New declaration.
* lib/windows-spawn.c (compose_command): New function, extracted from
spawnpvech.
(spawnpvech): Use it.

ChangeLog
lib/windows-spawn.c
lib/windows-spawn.h

index b716a268a62e6030d6d177c5e7831b4f94a10f01..ba8a7d904694ac4f9b33861620e0b59f79916217 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2020-12-24  Bruno Haible  <bruno@clisp.org>
+
+       windows-spawn: Export an auxiliary function.
+       * lib/windows-spawn.h (compose_command): New declaration.
+       * lib/windows-spawn.c (compose_command): New function, extracted from
+       spawnpvech.
+       (spawnpvech): Use it.
+
 2020-12-24  Bruno Haible  <bruno@clisp.org>
 
        posix_spawn* tests: Add support for native Windows.
index 0385b1ce1e46d3d0ce9c516b7e6242a7bfa2ff23..cc29196ecfe70acce5cbcc26f990f12a79c8021e 100644 (file)
@@ -203,6 +203,47 @@ prepare_spawn (const char * const *argv, char **mem_to_free)
   return new_argv;
 }
 
+char *
+compose_command (const char * const *argv)
+{
+  /* Just concatenate the argv[] strings, separated by spaces.  */
+  char *command;
+
+  /* Determine the size of the needed block of memory.  */
+  size_t total_size = 0;
+  const char * const *ap;
+  const char *p;
+  for (ap = argv; (p = *ap) != NULL; ap++)
+    total_size += strlen (p) + 1;
+  size_t command_size = (total_size > 0 ? total_size : 1);
+
+  /* Allocate the block of memory.  */
+  command = (char *) malloc (command_size);
+  if (command == NULL)
+    {
+      errno = ENOMEM;
+      return NULL;
+    }
+
+  /* Fill it.  */
+  if (total_size > 0)
+    {
+      char *cp = command;
+      for (ap = argv; (p = *ap) != NULL; ap++)
+        {
+          size_t size = strlen (p) + 1;
+          memcpy (cp, p, size - 1);
+          cp += size;
+          cp[-1] = ' ';
+        }
+      cp[-1] = '\0';
+    }
+  else
+    *command = '\0';
+
+  return command;
+}
+
 intptr_t
 spawnpvech (int mode,
             const char *progname, const char * const *argv,
@@ -227,35 +268,10 @@ spawnpvech (int mode,
   if (resolved_progname == NULL)
     return -1;
 
-  /* Compose the command.
-     Just concatenate the argv[] strings, separated by spaces.  */
-  char *command;
-  {
-    /* Determine the size of the needed block of memory.  */
-    size_t total_size = 0;
-    const char * const *ap;
-    const char *p;
-    for (ap = argv; (p = *ap) != NULL; ap++)
-      total_size += strlen (p) + 1;
-    size_t command_size = (total_size > 0 ? total_size : 1);
-    command = (char *) malloc (command_size);
-    if (command == NULL)
-      goto out_of_memory_1;
-    if (total_size > 0)
-      {
-        char *cp = command;
-        for (ap = argv; (p = *ap) != NULL; ap++)
-          {
-            size_t size = strlen (p) + 1;
-            memcpy (cp, p, size - 1);
-            cp += size;
-            cp[-1] = ' ';
-          }
-        cp[-1] = '\0';
-      }
-    else
-      *command = '\0';
-  }
+  /* Compose the command.  */
+  char *command = compose_command (argv);
+  if (command == NULL)
+    goto out_of_memory_1;
 
   /* Copy *ENVP into a contiguous block of memory.  */
   char *envblock;
index 77720d5559d77af1a636de757f3268fb8ffe5415..cb9d3fab12a4110bd34e6e249e652f9d7a85f986 100644 (file)
 extern const char ** prepare_spawn (const char * const *argv,
                                     char **mem_to_free);
 
+/* Composes the command to be passed to CreateProcess().
+   ARGV must contain appropriately quoted arguments, as returned by
+   prepare_spawn.
+   Returns a freshly allocated string.  In case of memory allocation failure,
+   NULL is returned, with errno set.  */
+extern char * compose_command (const char * const *argv);
+
 /* Creates a subprocess.
    MODE is either P_WAIT or P_NOWAIT.
    PROGNAME is the program to invoke.