aewl

view util.c @ 373:44a55e6e46bf

added a comment to spawn
author Anselm R. Garbe <arg@10kloc.org>
date Mon, 28 Aug 2006 08:17:27 +0200
parents 06438e022f9a
children 9d23330a5268
line source
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>
12 /* extern */
14 void *
15 emallocz(unsigned int size)
16 {
17 void *res = calloc(1, size);
19 if(!res)
20 eprint("fatal: could not malloc() %u bytes\n", size);
21 return res;
22 }
24 void
25 eprint(const char *errstr, ...)
26 {
27 va_list ap;
29 va_start(ap, errstr);
30 vfprintf(stderr, errstr, ap);
31 va_end(ap);
32 exit(EXIT_FAILURE);
33 }
35 void *
36 erealloc(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 }
44 void
45 spawn(Arg *arg)
46 {
47 static char *shell = NULL;
49 if(!shell && !(shell = getenv("SHELL")))
50 shell = "/bin/sh";
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 }