Move system_argv_* into their own file.

This commit is contained in:
Simon Tatham 2017-05-18 07:09:40 +01:00
parent 48919caa7b
commit 3b3a5fd6bf
8 changed files with 72 additions and 57 deletions

View file

@ -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

View file

@ -1,6 +1,8 @@
#include <stddef.h>
#ifndef cNULL
#define cNULL ((const char *)NULL)
#endif
char *dupstr(const char *str);
char *dupcat(const char *str, ...);

View file

@ -7,10 +7,6 @@
#include <uchar.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#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;

View file

@ -2,6 +2,4 @@
#include <stdlib.h>
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);

View file

@ -16,6 +16,7 @@
#include "memory.h"
#include "dupstr.h"
#include "subproc.h"
#include "fake-lib.h"
typedef struct MsiTypePrefix {

View file

@ -13,6 +13,7 @@
#include "memory.h"
#include "dupstr.h"
#include "subproc.h"
#include "fake-lib.h"
uint32_t HashPublicKeyInfo(void *pCertContext,

59
subproc.c Normal file
View file

@ -0,0 +1,59 @@
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#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);
}

6
subproc.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef cNULL
#define cNULL ((const char *)NULL)
#endif
void system_argv(const char *cmd, ...);
void system_argv_array(char **args);