annotate util.c @ 456:d11d739ad9df
some other simplifications
author |
Anselm R. Garbe <arg@10kloc.org> |
date |
Mon, 11 Sep 2006 17:31:21 +0200 |
parents |
06438e022f9a |
children |
9d23330a5268 |
rev |
line source |
garbeam@2
|
1 /*
|
garbeam@2
|
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@2
|
3 * See LICENSE file for license details.
|
garbeam@2
|
4 */
|
garbeam@76
|
5 #include "dwm.h"
|
garbeam@2
|
6 #include <stdarg.h>
|
garbeam@2
|
7 #include <stdio.h>
|
garbeam@2
|
8 #include <stdlib.h>
|
garbeam@5
|
9 #include <sys/wait.h>
|
garbeam@5
|
10 #include <unistd.h>
|
garbeam@5
|
11
|
garbeam@84
|
12 /* extern */
|
garbeam@76
|
13
|
garbeam@3
|
14 void *
|
garbeam@3
|
15 emallocz(unsigned int size)
|
garbeam@3
|
16 {
|
garbeam@3
|
17 void *res = calloc(1, size);
|
arg@123
|
18
|
garbeam@3
|
19 if(!res)
|
arg@325
|
20 eprint("fatal: could not malloc() %u bytes\n", size);
|
garbeam@3
|
21 return res;
|
garbeam@3
|
22 }
|
garbeam@3
|
23
|
garbeam@3
|
24 void
|
arg@187
|
25 eprint(const char *errstr, ...)
|
arg@187
|
26 {
|
garbeam@76
|
27 va_list ap;
|
arg@123
|
28
|
garbeam@76
|
29 va_start(ap, errstr);
|
garbeam@76
|
30 vfprintf(stderr, errstr, ap);
|
garbeam@76
|
31 va_end(ap);
|
garbeam@92
|
32 exit(EXIT_FAILURE);
|
garbeam@76
|
33 }
|
garbeam@76
|
34
|
arg@270
|
35 void *
|
arg@270
|
36 erealloc(void *ptr, unsigned int size)
|
arg@270
|
37 {
|
arg@270
|
38 void *res = realloc(ptr, size);
|
arg@270
|
39 if(!res)
|
arg@325
|
40 eprint("fatal: could not malloc() %u bytes\n", size);
|
arg@270
|
41 return res;
|
arg@270
|
42 }
|
arg@270
|
43
|
garbeam@76
|
44 void
|
garbeam@49
|
45 spawn(Arg *arg)
|
garbeam@5
|
46 {
|
arg@189
|
47 static char *shell = NULL;
|
arg@123
|
48
|
arg@189
|
49 if(!shell && !(shell = getenv("SHELL")))
|
arg@189
|
50 shell = "/bin/sh";
|
arg@189
|
51
|
arg@189
|
52 if(!arg->cmd)
|
garbeam@5
|
53 return;
|
arg@373
|
54 /* the double-fork construct avoids zombie processes */
|
garbeam@5
|
55 if(fork() == 0) {
|
garbeam@5
|
56 if(fork() == 0) {
|
garbeam@5
|
57 if(dpy)
|
garbeam@5
|
58 close(ConnectionNumber(dpy));
|
garbeam@9
|
59 setsid();
|
arg@338
|
60 execl(shell, shell, "-c", arg->cmd, (char *)NULL);
|
arg@217
|
61 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
|
garbeam@5
|
62 perror(" failed");
|
garbeam@5
|
63 }
|
arg@138
|
64 exit(0);
|
garbeam@5
|
65 }
|
garbeam@5
|
66 wait(0);
|
garbeam@5
|
67 }
|