JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
94590f9233e397ccccb0f971b3376befe8ddfab7
[dwm.git] / util.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5 #include "dwm.h"
6
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12
13 /* static */
14
15 static void
16 bad_malloc(unsigned int size)
17 {
18         fprintf(stderr, "fatal: could not malloc() %d bytes\n",
19                         (int) size);
20         exit(EXIT_FAILURE);
21 }
22
23 /* extern */
24
25 void *
26 emallocz(unsigned int size)
27 {
28         void *res = calloc(1, size);
29
30         if(!res)
31                 bad_malloc(size);
32         return res;
33 }
34
35 void
36 eprint(const char *errstr, ...) {
37         va_list ap;
38
39         va_start(ap, errstr);
40         vfprintf(stderr, errstr, ap);
41         va_end(ap);
42         exit(EXIT_FAILURE);
43 }
44
45 void
46 spawn(Arg *arg)
47 {
48         char **argv = (char **)arg->argv;
49
50         if(!argv || !argv[0])
51                 return;
52         if(fork() == 0) {
53                 if(fork() == 0) {
54                         if(dpy)
55                                 close(ConnectionNumber(dpy));
56                         setsid();
57                         execvp(argv[0], argv);
58                         fprintf(stderr, "dwm: execvp %s", argv[0]);
59                         perror(" failed");
60                 }
61                 exit(EXIT_FAILURE);
62         }
63         wait(0);
64 }