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
6#include <stdarg.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <sys/types.h>
10#include <sys/wait.h>
11#include <unistd.h>
12
13#include "dwm.h"
14
15void
16error(const char *errstr, ...) {
17 va_list ap;
18 va_start(ap, errstr);
19 vfprintf(stderr, errstr, ap);
20 va_end(ap);
21 exit(1);
22}
23
24static void
25bad_malloc(unsigned int size)
26{
27 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
28 (int) size);
29 exit(1);
30}
31
32void *
33emallocz(unsigned int size)
34{
35 void *res = calloc(1, size);
36 if(!res)
37 bad_malloc(size);
38 return res;
39}
40
41void
42spawn(Arg *arg)
43{
44 char **argv = (char **)arg->argv;
45 if(!argv || !argv[0])
46 return;
47 if(fork() == 0) {
48 if(fork() == 0) {
49 if(dpy)
50 close(ConnectionNumber(dpy));
51 setsid();
52 execvp(argv[0], argv);
53 fprintf(stderr, "dwm: execvp %s", argv[0]);
54 perror(" failed");
55 }
56 exit (0);
57 }
58 wait(0);
59}