]> git.saurik.com Git - apple/hfs.git/blob - tests/systemx.c
hfs-407.200.4.tar.gz
[apple/hfs.git] / tests / systemx.c
1 //
2 // systemx.c
3 // hfs
4 //
5 // Created by Chris Suter on 8/12/15.
6 //
7 //
8
9 #include <stdarg.h>
10 #include <libgen.h>
11 #include <spawn.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 #include "systemx.h"
16 #include "test-utils.h"
17
18 int __attribute__((sentinel)) systemx(const char *prog, ...)
19 {
20 const char *args[64];
21
22 va_list ap;
23
24 const char **parg = args;
25
26 *parg++ = basename((char *)prog);
27
28 va_start(ap, prog);
29
30 bool quiet = false;
31
32 while ((*parg = va_arg(ap, char *))) {
33 if (*parg == SYSTEMX_QUIET) {
34 quiet = true;
35 } else
36 ++parg;
37 }
38
39 va_end(ap);
40
41 posix_spawn_file_actions_t facts, *pfacts = NULL;
42
43 if (quiet) {
44 posix_spawn_file_actions_init(&facts);
45 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
46 pfacts = &facts;
47 }
48
49 pid_t pid;
50 assert_no_err(posix_spawn(&pid, prog, pfacts, NULL, (char * const *)args, NULL));
51
52 if (pfacts)
53 posix_spawn_file_actions_destroy(pfacts);
54
55 int status;
56 assert(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
57
58 if (WIFEXITED(status))
59 return WEXITSTATUS(status);
60 else
61 return -1;
62 }