all repos — dwm @ 4ad20ffc2c23d29329bc7349985d889f2cb45612

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/* static */
13
14static void
15bad_malloc(unsigned int size)
16{
17	eprint("fatal: could not malloc() %u bytes\n", size);
18}
19
20/* extern */
21
22void *
23emallocz(unsigned int size)
24{
25	void *res = calloc(1, size);
26
27	if(!res)
28		bad_malloc(size);
29	return res;
30}
31
32void
33eprint(const char *errstr, ...)
34{
35	va_list ap;
36
37	va_start(ap, errstr);
38	vfprintf(stderr, errstr, ap);
39	va_end(ap);
40	exit(EXIT_FAILURE);
41}
42
43void *
44erealloc(void *ptr, unsigned int size)
45{
46	void *res = realloc(ptr, size);
47	if(!res)
48		bad_malloc(size);
49	return res;
50}
51
52void
53spawn(Arg *arg)
54{
55	static char *shell = NULL;
56
57	if(!shell && !(shell = getenv("SHELL")))
58		shell = "/bin/sh";
59
60	if(!arg->cmd)
61		return;
62	if(fork() == 0) {
63		if(fork() == 0) {
64			if(dpy)
65				close(ConnectionNumber(dpy));
66			setsid();
67			execl(shell, shell, "-c", arg->cmd, NULL);
68			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
69			perror(" failed");
70		}
71		exit(0);
72	}
73	wait(0);
74}