util.c (view raw)
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#include <stdarg.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <sys/wait.h>
10#include <unistd.h>
11
12/* extern */
13
14void *
15emallocz(unsigned int size)
16{
17 void *res = calloc(1, size);
18
19 if(!res)
20 eprint("fatal: could not malloc() %u bytes\n", size);
21 return res;
22}
23
24void
25eprint(const char *errstr, ...)
26{
27 va_list ap;
28
29 va_start(ap, errstr);
30 vfprintf(stderr, errstr, ap);
31 va_end(ap);
32 exit(EXIT_FAILURE);
33}
34
35void *
36erealloc(void *ptr, unsigned int size)
37{
38 void *res = realloc(ptr, size);
39 if(!res)
40 eprint("fatal: could not malloc() %u bytes\n", size);
41 return res;
42}
43
44void
45spawn(Arg *arg)
46{
47 static char *shell = NULL;
48
49 if(!shell && !(shell = getenv("SHELL")))
50 shell = "/bin/sh";
51
52 if(!arg->cmd)
53 return;
54 /* the double-fork construct avoids zombie processes */
55 if(fork() == 0) {
56 if(fork() == 0) {
57 if(dpy)
58 close(ConnectionNumber(dpy));
59 setsid();
60 execl(shell, shell, "-c", arg->cmd, (char *)NULL);
61 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
62 perror(" failed");
63 }
64 exit(0);
65 }
66 wait(0);
67}