aewl

annotate util.c @ 576:8d7dbd11dc05

Added tag 2.3 for changeset 719b37b37b0df829d7cf017ac70e353088fe5849
author arg@mig29
date Fri, 24 Nov 2006 15:46:32 +0100
parents 740c4bfc3124
children e90bf387bf6f
rev   line source
arg@532 1 /* (C)opyright MMVI 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
arg@270 32 void *
arg@461 33 erealloc(void *ptr, unsigned int size) {
arg@270 34 void *res = realloc(ptr, size);
arg@532 35
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 if(!arg->cmd)
garbeam@5 48 return;
arg@471 49 /* The double-fork construct avoids zombie processes and keeps the code
arg@471 50 * clean from stupid signal handlers. */
garbeam@5 51 if(fork() == 0) {
garbeam@5 52 if(fork() == 0) {
garbeam@5 53 if(dpy)
garbeam@5 54 close(ConnectionNumber(dpy));
garbeam@9 55 setsid();
arg@338 56 execl(shell, shell, "-c", arg->cmd, (char *)NULL);
arg@217 57 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
garbeam@5 58 perror(" failed");
garbeam@5 59 }
arg@138 60 exit(0);
garbeam@5 61 }
garbeam@5 62 wait(0);
garbeam@5 63 }