aewl

annotate util.c @ 480:680aca428830

small change to achieve Jukka's last proposal
author arg@mmvi
date Fri, 22 Sep 2006 13:53:28 +0200
parents 9d23330a5268
children 651f2c868b31
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 *
arg@461 15 emallocz(unsigned int size) {
garbeam@3 16 void *res = calloc(1, size);
arg@123 17
garbeam@3 18 if(!res)
arg@325 19 eprint("fatal: could not malloc() %u bytes\n", size);
garbeam@3 20 return res;
garbeam@3 21 }
garbeam@3 22
garbeam@3 23 void
arg@461 24 eprint(const char *errstr, ...) {
garbeam@76 25 va_list ap;
arg@123 26
garbeam@76 27 va_start(ap, errstr);
garbeam@76 28 vfprintf(stderr, errstr, ap);
garbeam@76 29 va_end(ap);
garbeam@92 30 exit(EXIT_FAILURE);
garbeam@76 31 }
garbeam@76 32
arg@270 33 void *
arg@461 34 erealloc(void *ptr, unsigned int size) {
arg@270 35 void *res = realloc(ptr, size);
arg@270 36 if(!res)
arg@325 37 eprint("fatal: could not malloc() %u bytes\n", size);
arg@270 38 return res;
arg@270 39 }
arg@270 40
garbeam@76 41 void
arg@461 42 spawn(Arg *arg) {
arg@189 43 static char *shell = NULL;
arg@123 44
arg@189 45 if(!shell && !(shell = getenv("SHELL")))
arg@189 46 shell = "/bin/sh";
arg@189 47
arg@189 48 if(!arg->cmd)
garbeam@5 49 return;
arg@471 50 /* The double-fork construct avoids zombie processes and keeps the code
arg@471 51 * clean from stupid signal handlers. */
garbeam@5 52 if(fork() == 0) {
garbeam@5 53 if(fork() == 0) {
garbeam@5 54 if(dpy)
garbeam@5 55 close(ConnectionNumber(dpy));
garbeam@9 56 setsid();
arg@338 57 execl(shell, shell, "-c", arg->cmd, (char *)NULL);
arg@217 58 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
garbeam@5 59 perror(" failed");
garbeam@5 60 }
arg@138 61 exit(0);
garbeam@5 62 }
garbeam@5 63 wait(0);
garbeam@5 64 }