aewl

view util.c @ 60:24f9c674d03f

made stdin reader more robust
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 12:08:32 +0200
parents 989178822938
children f14858218641
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <unistd.h>
14 #include "dwm.h"
16 void
17 error(const char *errstr, ...) {
18 va_list ap;
19 va_start(ap, errstr);
20 vfprintf(stderr, errstr, ap);
21 va_end(ap);
22 exit(1);
23 }
25 static void
26 bad_malloc(unsigned int size)
27 {
28 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
29 (int) size);
30 exit(1);
31 }
33 void *
34 emallocz(unsigned int size)
35 {
36 void *res = calloc(1, size);
37 if(!res)
38 bad_malloc(size);
39 return res;
40 }
42 void *
43 emalloc(unsigned int size)
44 {
45 void *res = malloc(size);
46 if(!res)
47 bad_malloc(size);
48 return res;
49 }
51 void *
52 erealloc(void *ptr, unsigned int size)
53 {
54 void *res = realloc(ptr, size);
55 if(!res)
56 bad_malloc(size);
57 return res;
58 }
60 char *
61 estrdup(const char *str)
62 {
63 char *res = strdup(str);
64 if(!res)
65 bad_malloc(strlen(str));
66 return res;
67 }
69 void
70 swap(void **p1, void **p2)
71 {
72 void *tmp = *p1;
73 *p1 = *p2;
74 *p2 = tmp;
75 }
77 void
78 spawn(Arg *arg)
79 {
80 char **argv = (char **)arg->argv;
81 if(!argv || !argv[0])
82 return;
83 if(fork() == 0) {
84 if(fork() == 0) {
85 if(dpy)
86 close(ConnectionNumber(dpy));
87 setsid();
88 execvp(argv[0], argv);
89 fprintf(stderr, "dwm: execvp %s", argv[0]);
90 perror(" failed");
91 }
92 exit (0);
93 }
94 wait(0);
95 }