>From f8199b3ae12a94f79994e1c1e2389fe024ffa6fb Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 24 Dec 2020 22:18:10 +0100 Subject: [PATCH 3/7] 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. --- ChangeLog | 8 ++++++ lib/windows-spawn.c | 74 ++++++++++++++++++++++++++++++++--------------------- lib/windows-spawn.h | 7 +++++ 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index b716a26..ba8a7d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2020-12-24 Bruno Haible + 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 + posix_spawn* tests: Add support for native Windows. * tests/test-posix_spawn-open1.c (DATA_FILENAME): Treat native Windows like Cygwin. diff --git a/lib/windows-spawn.c b/lib/windows-spawn.c index 0385b1c..cc29196 100644 --- a/lib/windows-spawn.c +++ b/lib/windows-spawn.c @@ -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; diff --git a/lib/windows-spawn.h b/lib/windows-spawn.h index 77720d5..cb9d3fa 100644 --- a/lib/windows-spawn.h +++ b/lib/windows-spawn.h @@ -65,6 +65,13 @@ 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. -- 2.7.4