rev |
line source |
garbeam@2
|
1 /*
|
garbeam@2
|
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@2
|
3 * See LICENSE file for license details.
|
garbeam@2
|
4 */
|
garbeam@2
|
5
|
garbeam@2
|
6 #include <stdarg.h>
|
garbeam@2
|
7 #include <stdio.h>
|
garbeam@2
|
8 #include <stdlib.h>
|
garbeam@3
|
9 #include <string.h>
|
garbeam@5
|
10 #include <sys/types.h>
|
garbeam@5
|
11 #include <sys/wait.h>
|
garbeam@5
|
12 #include <unistd.h>
|
garbeam@5
|
13
|
garbeam@5
|
14 #include "util.h"
|
garbeam@2
|
15
|
garbeam@2
|
16 void
|
garbeam@2
|
17 error(char *errstr, ...) {
|
garbeam@2
|
18 va_list ap;
|
garbeam@2
|
19 va_start(ap, errstr);
|
garbeam@2
|
20 vfprintf(stderr, errstr, ap);
|
garbeam@2
|
21 va_end(ap);
|
garbeam@2
|
22 exit(1);
|
garbeam@2
|
23 }
|
garbeam@2
|
24
|
garbeam@3
|
25 static void
|
garbeam@3
|
26 bad_malloc(unsigned int size)
|
garbeam@3
|
27 {
|
garbeam@3
|
28 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
|
garbeam@3
|
29 (int) size);
|
garbeam@3
|
30 exit(1);
|
garbeam@3
|
31 }
|
garbeam@3
|
32
|
garbeam@3
|
33 void *
|
garbeam@3
|
34 emallocz(unsigned int size)
|
garbeam@3
|
35 {
|
garbeam@3
|
36 void *res = calloc(1, size);
|
garbeam@3
|
37 if(!res)
|
garbeam@3
|
38 bad_malloc(size);
|
garbeam@3
|
39 return res;
|
garbeam@3
|
40 }
|
garbeam@3
|
41
|
garbeam@3
|
42 void *
|
garbeam@3
|
43 emalloc(unsigned int size)
|
garbeam@3
|
44 {
|
garbeam@3
|
45 void *res = malloc(size);
|
garbeam@3
|
46 if(!res)
|
garbeam@3
|
47 bad_malloc(size);
|
garbeam@3
|
48 return res;
|
garbeam@3
|
49 }
|
garbeam@3
|
50
|
garbeam@3
|
51 void *
|
garbeam@3
|
52 erealloc(void *ptr, unsigned int size)
|
garbeam@3
|
53 {
|
garbeam@3
|
54 void *res = realloc(ptr, size);
|
garbeam@3
|
55 if(!res)
|
garbeam@3
|
56 bad_malloc(size);
|
garbeam@3
|
57 return res;
|
garbeam@3
|
58 }
|
garbeam@3
|
59
|
garbeam@3
|
60 char *
|
garbeam@3
|
61 estrdup(const char *str)
|
garbeam@3
|
62 {
|
garbeam@3
|
63 void *res = strdup(str);
|
garbeam@3
|
64 if(!res)
|
garbeam@3
|
65 bad_malloc(strlen(str));
|
garbeam@3
|
66 return res;
|
garbeam@3
|
67 }
|
garbeam@3
|
68
|
garbeam@3
|
69 void
|
garbeam@3
|
70 failed_assert(char *a, char *file, int line)
|
garbeam@3
|
71 {
|
garbeam@3
|
72 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
|
garbeam@3
|
73 abort();
|
garbeam@3
|
74 }
|
garbeam@3
|
75
|
garbeam@3
|
76 void
|
garbeam@3
|
77 swap(void **p1, void **p2)
|
garbeam@3
|
78 {
|
garbeam@3
|
79 void *tmp = *p1;
|
garbeam@3
|
80 *p1 = *p2;
|
garbeam@3
|
81 *p2 = tmp;
|
garbeam@3
|
82 }
|
garbeam@5
|
83
|
garbeam@5
|
84 void
|
garbeam@14
|
85 spawn(Display *dpy, char *argv[])
|
garbeam@5
|
86 {
|
garbeam@14
|
87 if(!argv || !argv[0])
|
garbeam@5
|
88 return;
|
garbeam@5
|
89 if(fork() == 0) {
|
garbeam@5
|
90 if(fork() == 0) {
|
garbeam@5
|
91 if(dpy)
|
garbeam@5
|
92 close(ConnectionNumber(dpy));
|
garbeam@9
|
93 setsid();
|
garbeam@14
|
94 execvp(argv[0], argv);
|
garbeam@14
|
95 fprintf(stderr, "gridwm: execvp %s", argv[0]);
|
garbeam@5
|
96 perror(" failed");
|
garbeam@5
|
97 }
|
garbeam@5
|
98 exit (0);
|
garbeam@5
|
99 }
|
garbeam@5
|
100 wait(0);
|
garbeam@5
|
101 }
|
garbeam@6
|
102
|
garbeam@6
|
103 void
|
garbeam@14
|
104 pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[])
|
garbeam@6
|
105 {
|
garbeam@6
|
106 unsigned int l, n;
|
garbeam@6
|
107 int pfd[2];
|
garbeam@6
|
108
|
garbeam@14
|
109 if(!argv || !argv[0])
|
garbeam@6
|
110 return;
|
garbeam@6
|
111
|
garbeam@6
|
112 if(pipe(pfd) == -1) {
|
garbeam@6
|
113 perror("pipe");
|
garbeam@6
|
114 exit(1);
|
garbeam@6
|
115 }
|
garbeam@6
|
116
|
garbeam@6
|
117 if(fork() == 0) {
|
garbeam@6
|
118 if(dpy)
|
garbeam@6
|
119 close(ConnectionNumber(dpy));
|
garbeam@9
|
120 setsid();
|
garbeam@6
|
121 dup2(pfd[1], STDOUT_FILENO);
|
garbeam@6
|
122 close(pfd[0]);
|
garbeam@6
|
123 close(pfd[1]);
|
garbeam@14
|
124 execvp(argv[0], argv);
|
garbeam@14
|
125 fprintf(stderr, "gridwm: execvp %s", argv[0]);
|
garbeam@6
|
126 perror(" failed");
|
garbeam@6
|
127 }
|
garbeam@6
|
128 else {
|
garbeam@6
|
129 n = 0;
|
garbeam@6
|
130 close(pfd[1]);
|
garbeam@6
|
131 while(l > n) {
|
garbeam@6
|
132 if((l = read(pfd[0], buf + n, len - n)) < 1)
|
garbeam@6
|
133 break;
|
garbeam@6
|
134 n += l;
|
garbeam@6
|
135 }
|
garbeam@6
|
136 close(pfd[0]);
|
garbeam@6
|
137 buf[n - 1] = 0;
|
garbeam@6
|
138 }
|
garbeam@6
|
139 wait(0);
|
garbeam@6
|
140 }
|