Xinqi Bao's Git

cae9ba3508dddd753685c24a2053cdd49ef19e85
[dwm.git] / util.c
1 /* © 2004-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details. */
3 #include "dwm.h"
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9
10 /* extern */
11
12 void *
13 emallocz(unsigned int size) {
14 void *res = calloc(1, size);
15
16 if(!res)
17 eprint("fatal: could not malloc() %u bytes\n", size);
18 return res;
19 }
20
21 void
22 eprint(const char *errstr, ...) {
23 va_list ap;
24
25 va_start(ap, errstr);
26 vfprintf(stderr, errstr, ap);
27 va_end(ap);
28 exit(EXIT_FAILURE);
29 }
30
31 void
32 spawn(const char *arg) {
33 static char *shell = NULL;
34
35 if(!shell && !(shell = getenv("SHELL")))
36 shell = "/bin/sh";
37 if(!arg)
38 return;
39 /* The double-fork construct avoids zombie processes and keeps the code
40 * clean from stupid signal handlers. */
41 if(fork() == 0) {
42 if(fork() == 0) {
43 if(dpy)
44 close(ConnectionNumber(dpy));
45 setsid();
46 execl(shell, shell, "-c", arg, (char *)NULL);
47 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
48 perror(" failed");
49 }
50 exit(0);
51 }
52 wait(0);
53 }