all repos — dwm @ d934296476be7345842fec1a2630d1752c704078

fork of suckless dynamic window manager

util.c (view raw)

 1/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
 2 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
 3 * © 2006-2007 Jukka Salmi <jukka at salmi dot ch>
 4 * © 2007 Premysl Hruby <dfenze at gmail dot com>
 5 * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
 6 * See LICENSE file for license details. */
 7#include "dwm.h"
 8#include <stdarg.h>
 9#include <stdio.h>
10#include <stdlib.h>
11#include <sys/wait.h>
12#include <unistd.h>
13
14/* extern */
15
16void *
17emallocz(unsigned int size) {
18	void *res = calloc(1, size);
19
20	if(!res)
21		eprint("fatal: could not malloc() %u bytes\n", size);
22	return res;
23}
24
25void
26eprint(const char *errstr, ...) {
27	va_list ap;
28
29	va_start(ap, errstr);
30	vfprintf(stderr, errstr, ap);
31	va_end(ap);
32	exit(EXIT_FAILURE);
33}
34
35void
36spawn(const char *arg) {
37	static char *shell = NULL;
38
39	if(!shell && !(shell = getenv("SHELL")))
40		shell = "/bin/sh";
41	if(!arg)
42		return;
43	/* The double-fork construct avoids zombie processes and keeps the code
44	 * clean from stupid signal handlers. */
45	if(fork() == 0) {
46		if(fork() == 0) {
47			if(dpy)
48				close(ConnectionNumber(dpy));
49			setsid();
50			execl(shell, shell, "-c", arg, (char *)NULL);
51			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
52			perror(" failed");
53		}
54		exit(0);
55	}
56	wait(0);
57}