util.c
changeset 990 70f6fcd100b7
parent 989 5f7018237edb
child 991 277c0e5bd0df
equal deleted inserted replaced
989:5f7018237edb 990:70f6fcd100b7
     1 /* See LICENSE file for copyright and license details. */
       
     2 #include "dwm.h"
       
     3 #include <stdarg.h>
       
     4 #include <stdio.h>
       
     5 #include <stdlib.h>
       
     6 #include <sys/wait.h>
       
     7 #include <unistd.h>
       
     8 
       
     9 /* extern */
       
    10 
       
    11 void *
       
    12 emallocz(unsigned int size) {
       
    13 	void *res = calloc(1, size);
       
    14 
       
    15 	if(!res)
       
    16 		eprint("fatal: could not malloc() %u bytes\n", size);
       
    17 	return res;
       
    18 }
       
    19 
       
    20 void
       
    21 eprint(const char *errstr, ...) {
       
    22 	va_list ap;
       
    23 
       
    24 	va_start(ap, errstr);
       
    25 	vfprintf(stderr, errstr, ap);
       
    26 	va_end(ap);
       
    27 	exit(EXIT_FAILURE);
       
    28 }
       
    29 
       
    30 void
       
    31 spawn(const char *arg) {
       
    32 	static char *shell = NULL;
       
    33 
       
    34 	if(!shell && !(shell = getenv("SHELL")))
       
    35 		shell = "/bin/sh";
       
    36 	if(!arg)
       
    37 		return;
       
    38 	/* The double-fork construct avoids zombie processes and keeps the code
       
    39 	 * clean from stupid signal handlers. */
       
    40 	if(fork() == 0) {
       
    41 		if(fork() == 0) {
       
    42 			if(dpy)
       
    43 				close(ConnectionNumber(dpy));
       
    44 			setsid();
       
    45 			execl(shell, shell, "-c", arg, (char *)NULL);
       
    46 			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
       
    47 			perror(" failed");
       
    48 		}
       
    49 		exit(0);
       
    50 	}
       
    51 	wait(0);
       
    52 }