all repos — dwm @ 846128a498759bfcbf363fc014e50c1bf48bdf0c

fork of suckless dynamic window manager

util.c (view raw)

 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
11void *
12emallocz(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
20void
21eprint(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
30void
31spawn(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}