all repos — dwm @ 6646468125eb4822d8f96b2b587c66c9b1d12eec

fork of suckless dynamic window manager

util.c (view raw)

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