util.c (view raw)
1/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * © 2007 Premysl Hruby <dfenze at gmail dot com>
4 * See LICENSE file for license details. */
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 void *res = calloc(1, size);
17
18 if(!res)
19 eprint("fatal: could not malloc() %u bytes\n", size);
20 return res;
21}
22
23void
24eprint(const char *errstr, ...) {
25 va_list ap;
26
27 va_start(ap, errstr);
28 vfprintf(stderr, errstr, ap);
29 va_end(ap);
30 exit(EXIT_FAILURE);
31}
32
33void
34spawn(const char *arg) {
35 static char *shell = NULL;
36
37 if(!shell && !(shell = getenv("SHELL")))
38 shell = "/bin/sh";
39 if(!arg)
40 return;
41 /* The double-fork construct avoids zombie processes and keeps the code
42 * clean from stupid signal handlers. */
43 if(fork() == 0) {
44 if(fork() == 0) {
45 if(dpy)
46 close(ConnectionNumber(dpy));
47 setsid();
48 execl(shell, shell, "-c", arg, (char *)NULL);
49 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
50 perror(" failed");
51 }
52 exit(0);
53 }
54 wait(0);
55}