summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2020-12-24 22:18:10 +0100
committerBruno Haible <bruno@clisp.org>2020-12-24 22:18:10 +0100
commitf8199b3ae12a94f79994e1c1e2389fe024ffa6fb (patch)
tree67540697f031db5dfd76983bd5594065cc2f362a
parent8cf7746a2b5c5fe50e8369951619a87ecb3786bd (diff)
downloadgnulib-f8199b3ae12a94f79994e1c1e2389fe024ffa6fb.tar.gz
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.
-rw-r--r--ChangeLog8
-rw-r--r--lib/windows-spawn.c74
-rw-r--r--lib/windows-spawn.h7
3 files changed, 60 insertions, 29 deletions
diff --git a/ChangeLog b/ChangeLog
index b716a268a6..ba8a7d9046 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
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.
* 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 0385b1ce1e..cc29196ecf 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 77720d5559..cb9d3fab12 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.