annotate util.c @ 712:b288b57d81f8
this version should also work with cornercases (like unmanage during !issel, etc.)
author |
Anselm R. Garbe <arg@suckless.org> |
date |
Tue, 23 Jan 2007 12:00:49 +0100 |
parents |
1ed8c40dde36 |
children |
743061dbe7f2 |
rev |
line source |
arg@644
|
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@2
|
2 * See LICENSE file for license details.
|
garbeam@2
|
3 */
|
garbeam@76
|
4 #include "dwm.h"
|
garbeam@2
|
5 #include <stdarg.h>
|
garbeam@2
|
6 #include <stdio.h>
|
garbeam@2
|
7 #include <stdlib.h>
|
garbeam@5
|
8 #include <sys/wait.h>
|
garbeam@5
|
9 #include <unistd.h>
|
garbeam@5
|
10
|
garbeam@84
|
11 /* extern */
|
garbeam@76
|
12
|
garbeam@3
|
13 void *
|
arg@461
|
14 emallocz(unsigned int size) {
|
garbeam@3
|
15 void *res = calloc(1, size);
|
arg@123
|
16
|
garbeam@3
|
17 if(!res)
|
arg@325
|
18 eprint("fatal: could not malloc() %u bytes\n", size);
|
garbeam@3
|
19 return res;
|
garbeam@3
|
20 }
|
garbeam@3
|
21
|
garbeam@3
|
22 void
|
arg@461
|
23 eprint(const char *errstr, ...) {
|
garbeam@76
|
24 va_list ap;
|
arg@123
|
25
|
garbeam@76
|
26 va_start(ap, errstr);
|
garbeam@76
|
27 vfprintf(stderr, errstr, ap);
|
garbeam@76
|
28 va_end(ap);
|
garbeam@92
|
29 exit(EXIT_FAILURE);
|
garbeam@76
|
30 }
|
garbeam@76
|
31
|
garbeam@76
|
32 void
|
arg@461
|
33 spawn(Arg *arg) {
|
arg@189
|
34 static char *shell = NULL;
|
arg@123
|
35
|
arg@189
|
36 if(!shell && !(shell = getenv("SHELL")))
|
arg@189
|
37 shell = "/bin/sh";
|
arg@189
|
38 if(!arg->cmd)
|
garbeam@5
|
39 return;
|
arg@471
|
40 /* The double-fork construct avoids zombie processes and keeps the code
|
arg@471
|
41 * clean from stupid signal handlers. */
|
garbeam@5
|
42 if(fork() == 0) {
|
garbeam@5
|
43 if(fork() == 0) {
|
garbeam@5
|
44 if(dpy)
|
garbeam@5
|
45 close(ConnectionNumber(dpy));
|
garbeam@9
|
46 setsid();
|
arg@338
|
47 execl(shell, shell, "-c", arg->cmd, (char *)NULL);
|
arg@217
|
48 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
|
garbeam@5
|
49 perror(" failed");
|
garbeam@5
|
50 }
|
arg@138
|
51 exit(0);
|
garbeam@5
|
52 }
|
garbeam@5
|
53 wait(0);
|
garbeam@5
|
54 }
|