From 3b3a5fd6bf7a3542ab8e8701c93c3d0f505c722f Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 18 May 2017 07:09:40 +0100 Subject: Move system_argv_* into their own file. --- Makefile.am | 6 +++--- dupstr.h | 2 ++ fake-lib.c | 52 -------------------------------------------------- fake-lib.h | 2 -- fake-msi.c | 1 + fake-winterop.c | 1 + subproc.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ subproc.h | 6 ++++++ 8 files changed, 72 insertions(+), 57 deletions(-) create mode 100644 subproc.c create mode 100644 subproc.h diff --git a/Makefile.am b/Makefile.am index b0141dc..806366a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,10 +5,10 @@ ACLOCAL_AMFLAGS = -I m4 lib_LTLIBRARIES = libwinterop.so.la libmsi.so.la libpreload.la libwinterop_so_la_SOURCES = fake-winterop.c fake-lib.c fake-lib.h \ -memory.c memory.h dupstr.c dupstr.h +memory.c memory.h dupstr.c dupstr.h subproc.c subproc.h -libmsi_so_la_SOURCES = fake-msi.c fake-lib.c fake-lib.h md5.c \ -memory.c memory.h version.c dupstr.c dupstr.h +libmsi_so_la_SOURCES = fake-msi.c fake-lib.c fake-lib.h md5.c \ +memory.c memory.h version.c dupstr.c dupstr.h subproc.c subproc.h libpreload_la_SOURCES = preload.c libpreload_la_LDFLAGS = -ldl diff --git a/dupstr.h b/dupstr.h index ea152c5..fd29bb1 100644 --- a/dupstr.h +++ b/dupstr.h @@ -1,6 +1,8 @@ #include +#ifndef cNULL #define cNULL ((const char *)NULL) +#endif char *dupstr(const char *str); char *dupcat(const char *str, ...); diff --git a/fake-lib.c b/fake-lib.c index cf6be49..e2bc364 100644 --- a/fake-lib.c +++ b/fake-lib.c @@ -7,10 +7,6 @@ #include #include -#include -#include -#include - #include "memory.h" #include "fake-lib.h" @@ -29,54 +25,6 @@ char *ascii(const char16_t *wstr, bool translate_slashes) return ret; } -void system_argv_array(char **args) -{ - pid_t pid = fork(); - if (pid < 0) - err(1, "fork"); - - if (pid == 0) { - execvp(args[0], args); - warn("execvp"); - _exit(127); - } - - int status; - if (waitpid(pid, &status, 0) != pid) - err(1, "waitpid"); - if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) - errx(1, "subcommand failed"); -} - -void system_argv(const char *cmd, ...) -{ - int nargs, nchars; - const char *word; - va_list ap; - - va_start(ap, cmd); - nargs = 1; /* terminating NULL */ - nchars = 0; - for (word = cmd; word; word = va_arg(ap, const char *)) { - nargs++; - nchars += 1 + strlen(word); - } - va_end(ap); - - char *args[nargs], chars[nchars]; - char **argp = args, *charp = chars; - va_start(ap, cmd); - for (word = cmd; word; word = va_arg(ap, const char *)) { - *argp++ = charp; - strcpy(charp, word); - charp += 1 + strlen(word); - } - va_end(ap); - *argp++ = NULL; - - system_argv_array(args); -} - void c16cpy(char16_t *out, uint32_t *outsize, char *s) { uint32_t retlen = 0; diff --git a/fake-lib.h b/fake-lib.h index c71b977..5ae4a91 100644 --- a/fake-lib.h +++ b/fake-lib.h @@ -2,6 +2,4 @@ #include char *ascii(const char16_t *wstr, bool translate_slashes); -void system_argv(const char *cmd, ...); -void system_argv_array(char **args); void c16cpy(char16_t *out, uint32_t *outsize, char *s); diff --git a/fake-msi.c b/fake-msi.c index 12b1489..0600671 100644 --- a/fake-msi.c +++ b/fake-msi.c @@ -16,6 +16,7 @@ #include "memory.h" #include "dupstr.h" +#include "subproc.h" #include "fake-lib.h" typedef struct MsiTypePrefix { diff --git a/fake-winterop.c b/fake-winterop.c index 6f7e3d3..1b439fe 100644 --- a/fake-winterop.c +++ b/fake-winterop.c @@ -13,6 +13,7 @@ #include "memory.h" #include "dupstr.h" +#include "subproc.h" #include "fake-lib.h" uint32_t HashPublicKeyInfo(void *pCertContext, diff --git a/subproc.c b/subproc.c new file mode 100644 index 0000000..ae4991f --- /dev/null +++ b/subproc.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include "subproc.h" + +void system_argv_array(char **args) +{ + pid_t pid = fork(); + if (pid < 0) + err(1, "fork"); + + if (pid == 0) { + execvp(args[0], args); + warn("execvp"); + _exit(127); + } + + int status; + if (waitpid(pid, &status, 0) != pid) + err(1, "waitpid"); + if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) + errx(1, "subcommand failed"); +} + +void system_argv(const char *cmd, ...) +{ + int nargs, nchars; + const char *word; + va_list ap; + + va_start(ap, cmd); + nargs = 1; /* terminating NULL */ + nchars = 0; + for (word = cmd; word; word = va_arg(ap, const char *)) { + nargs++; + nchars += 1 + strlen(word); + } + va_end(ap); + + char *args[nargs], chars[nchars]; + char **argp = args, *charp = chars; + va_start(ap, cmd); + for (word = cmd; word; word = va_arg(ap, const char *)) { + *argp++ = charp; + strcpy(charp, word); + charp += 1 + strlen(word); + } + va_end(ap); + *argp++ = NULL; + + system_argv_array(args); +} + diff --git a/subproc.h b/subproc.h new file mode 100644 index 0000000..5f2d5f9 --- /dev/null +++ b/subproc.h @@ -0,0 +1,6 @@ +#ifndef cNULL +#define cNULL ((const char *)NULL) +#endif + +void system_argv(const char *cmd, ...); +void system_argv_array(char **args); -- cgit v1.2.3-55-g6feb