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
42swap(void **p1, void **p2)
43{
44 void *tmp = *p1;
45 *p1 = *p2;
46 *p2 = tmp;
47}
48
49void
50spawn(Arg *arg)
51{
52 char **argv = (char **)arg->argv;
53 if(!argv || !argv[0])
54 return;
55 if(fork() == 0) {
56 if(fork() == 0) {
57 if(dpy)
58 close(ConnectionNumber(dpy));
59 setsid();
60 execvp(argv[0], argv);
61 fprintf(stderr, "dwm: execvp %s", argv[0]);
62 perror(" failed");
63 }
64 exit (0);
65 }
66 wait(0);
67}