libite 2.6.1
systemf.c
Go to the documentation of this file.
1/* Formatted system() which returns actual return status of command
2 *
3 * Copyright (c) 2021 Joachim Wiberg <troglobit@gmail.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
24
25#include <errno.h>
26#include <stdarg.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <sys/wait.h>
30
44int systemf(const char *fmt, ...)
45{
46 va_list ap;
47 char *cmd;
48 int len;
49 int rc;
50
51 va_start(ap, fmt);
52 len = vsnprintf(NULL, 0, fmt, ap);
53 va_end(ap);
54
55 cmd = alloca(++len);
56 if (!cmd) {
57 errno = ENOMEM;
58 return -1;
59 }
60
61 va_start(ap, fmt);
62 vsnprintf(cmd, len, fmt, ap);
63 va_end(ap);
64
65 rc = system(cmd);
66 if (rc == -1)
67 return -1;
68
69 if (WIFEXITED(rc)) {
70 errno = 0;
71 rc = WEXITSTATUS(rc);
72 } else if (WIFSIGNALED(rc)) {
73 errno = EINTR;
74 rc = -1;
75 }
76
77 return rc;
78}
79
int systemf(const char *fmt,...)
Definition systemf.c:44