all repos — dwm @ 9fdd2cd1a9dbe9b533fab9a8a98b9849dc4c7b59

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