diff util.c @ 0:7024076fa948

initial add
author paulo@localhost
date Sun, 22 Mar 2009 23:26:35 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/util.c	Sun Mar 22 23:26:35 2009 -0700
     1.3 @@ -0,0 +1,54 @@
     1.4 +/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
     1.5 + * See LICENSE file for license details.
     1.6 + */
     1.7 +#include "dwm.h"
     1.8 +#include <stdarg.h>
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include <sys/wait.h>
    1.12 +#include <unistd.h>
    1.13 +
    1.14 +/* extern */
    1.15 +
    1.16 +void *
    1.17 +emallocz(unsigned int size) {
    1.18 +	void *res = calloc(1, size);
    1.19 +
    1.20 +	if(!res)
    1.21 +		eprint("fatal: could not malloc() %u bytes\n", size);
    1.22 +	return res;
    1.23 +}
    1.24 +
    1.25 +void
    1.26 +eprint(const char *errstr, ...) {
    1.27 +	va_list ap;
    1.28 +
    1.29 +	va_start(ap, errstr);
    1.30 +	vfprintf(stderr, errstr, ap);
    1.31 +	va_end(ap);
    1.32 +	exit(EXIT_FAILURE);
    1.33 +}
    1.34 +
    1.35 +void
    1.36 +spawn(Arg *arg) {
    1.37 +	static char *shell = NULL;
    1.38 +
    1.39 +	if(!shell && !(shell = getenv("SHELL")))
    1.40 +		shell = "/bin/sh";
    1.41 +	if(!arg->cmd)
    1.42 +		return;
    1.43 +	/* The double-fork construct avoids zombie processes and keeps the code
    1.44 +	 * clean from stupid signal handlers. */
    1.45 +	if(fork() == 0) {
    1.46 +		if(fork() == 0) {
    1.47 +			if(dpy)
    1.48 +				close(ConnectionNumber(dpy));
    1.49 +			setsid();
    1.50 +			execl(shell, shell, "-c", arg->cmd, (char *)NULL);
    1.51 +			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
    1.52 +			perror(" failed");
    1.53 +		}
    1.54 +		exit(0);
    1.55 +	}
    1.56 +	wait(0);
    1.57 +}