aewl

view util.c @ 292:4aa632b6ba66

changed main event loop
author Anselm R.Garbe <arg@10ksloc.org>
date Tue, 15 Aug 2006 07:31:42 +0200
parents 7b63c375d28c
children 45af6a8a0cbf
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 /* static */
14 static void
15 bad_malloc(unsigned int size)
16 {
17 eprint("fatal: could not malloc() %u bytes\n", size);
18 }
20 /* extern */
22 void *
23 emallocz(unsigned int size)
24 {
25 void *res = calloc(1, size);
27 if(!res)
28 bad_malloc(size);
29 return res;
30 }
32 void
33 eprint(const char *errstr, ...)
34 {
35 va_list ap;
37 va_start(ap, errstr);
38 vfprintf(stderr, errstr, ap);
39 va_end(ap);
40 exit(EXIT_FAILURE);
41 }
43 void *
44 erealloc(void *ptr, unsigned int size)
45 {
46 void *res = realloc(ptr, size);
47 if(!res)
48 bad_malloc(size);
49 return res;
50 }
52 void
53 spawn(Arg *arg)
54 {
55 static char *shell = NULL;
57 if(!shell && !(shell = getenv("SHELL")))
58 shell = "/bin/sh";
60 if(!arg->cmd)
61 return;
62 if(fork() == 0) {
63 if(fork() == 0) {
64 if(dpy)
65 close(ConnectionNumber(dpy));
66 setsid();
67 execl(shell, shell, "-c", arg->cmd, NULL);
68 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
69 perror(" failed");
70 }
71 exit(0);
72 }
73 wait(0);
74 }