JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
rearranged several stuff
[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
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12
13 #include "dwm.h"
14
15 void
16 eprint(const char *errstr, ...) {
17         va_list ap;
18         va_start(ap, errstr);
19         vfprintf(stderr, errstr, ap);
20         va_end(ap);
21         exit(1);
22 }
23
24 static void
25 bad_malloc(unsigned int size)
26 {
27         fprintf(stderr, "fatal: could not malloc() %d bytes\n",
28                         (int) size);
29         exit(1);
30 }
31
32 void *
33 emallocz(unsigned int size)
34 {
35         void *res = calloc(1, size);
36         if(!res)
37                 bad_malloc(size);
38         return res;
39 }
40
41 void
42 spawn(Arg *arg)
43 {
44         char **argv = (char **)arg->argv;
45         if(!argv || !argv[0])
46                 return;
47         if(fork() == 0) {
48                 if(fork() == 0) {
49                         if(dpy)
50                                 close(ConnectionNumber(dpy));
51                         setsid();
52                         execvp(argv[0], argv);
53                         fprintf(stderr, "dwm: execvp %s", argv[0]);
54                         perror(" failed");
55                 }
56                 exit (0);
57         }
58         wait(0);
59 }