Refactor GUI code, add grid layout by Dave Ball
[qi-bootmenu.git] / util.c
blobaf969c5377764ac20064ba264b4621a891a22793
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <unistd.h>
4 #include <ctype.h>
5 #include <signal.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include "util.h"
10 void eprint(const char *errstr, ...) {
11 va_list ap;
12 va_start(ap, errstr);
13 vfprintf(stderr, errstr, ap);
14 va_end(ap);
17 void skip_until(char** str, char c) {
18 while (**str && **str != c)
19 (*str)++;
22 void skip_spaces(char **str) {
23 while (isspace(**str))
24 (*str)++;
27 int fexecw(const char *path, char *const argv[], char *const envp[]) {
28 pid_t pid;
29 struct sigaction ignore, old_int, old_quit;
30 sigset_t masked, oldmask;
31 int status;
33 /* Block SIGCHLD and ignore SIGINT and SIGQUIT before forking
34 * restore the original signal handlers afterwards. */
36 ignore.sa_handler = SIG_IGN;
37 sigemptyset(&ignore.sa_mask);
38 ignore.sa_flags = 0;
39 sigaction(SIGINT, &ignore, &old_int);
40 sigaction(SIGQUIT, &ignore, &old_quit);
42 sigemptyset(&masked);
43 sigaddset(&masked, SIGCHLD);
44 sigprocmask(SIG_BLOCK, &masked, &oldmask);
46 pid = fork();
48 if (pid < 0)
49 return -1; /* can't fork */
50 else if (pid == 0) { /* child process */
51 sigaction(SIGINT, &old_int, NULL);
52 sigaction(SIGQUIT, &old_quit, NULL);
53 sigprocmask(SIG_SETMASK, &oldmask, NULL);
54 execve(path, (char *const *)argv, (char *const *)envp);
55 _exit(127);
58 /* wait for our child and store it's exit status */
59 waitpid(pid, &status, 0);
61 /* restore signal handlers */
62 sigaction(SIGINT, &old_int, NULL);
63 sigaction(SIGQUIT, &old_quit, NULL);
64 sigprocmask(SIG_SETMASK, &oldmask, NULL);
66 return status;