forked from ashish-yadav11/dsblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
121 lines (111 loc) · 3.18 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include "util.h"
void
cspawn(char *const *arg)
{
setsid();
execvp(arg[0], arg);
perror("cspawn - execvp");
_exit(127);
}
void
csigself(int sig, int sigval)
{
union sigval sv;
sig += SIGRTMIN;
sv.sival_int = sigval;
if (sigqueue(pid, sig, sv) == -1) {
perror("csigself - sigqueue");
exit(1);
}
}
/* getcmdout doesn't null terminate cmdout
* make sure that terminal newline character is handled if the command spits one */
size_t
getcmdout(char *const *arg, char *cmdout, size_t cmdoutlen)
{
int fd[2];
size_t trd;
ssize_t rd;
if (pipe(fd) == -1) {
perror("getcmdout - pipe");
cleanup();
exit(1);
}
switch (fork()) {
case -1:
perror("getcmdout - fork");
cleanup();
exit(1);
case 0:
close(ConnectionNumber(dpy));
close(fd[0]);
if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
perror("getcmdout - child - dup2");
exit(1);
}
close(fd[1]);
}
execvp(arg[0], arg);
perror("getcmdout - child - execvp");
_exit(127);
default:
close(fd[1]);
trd = 0;
do
rd = read(fd[0], cmdout + trd, cmdoutlen - trd);
while (rd > 0 && (trd += rd) < cmdoutlen);
if (rd == -1) {
perror("getcmdout - read");
cleanup();
exit(1);
}
close(fd[0]);
}
return trd;
}
int
readint(const char *path, int *var) {
FILE *fp;
if (!(fp = fopen(path, "r")))
return 0;
if (fscanf(fp, "%d", var) != 1) {
fclose(fp);
return 0;
}
fclose(fp);
return 1;
}
int
readint32(const char *path, uint32_t *var) {
FILE *fp;
if (!(fp = fopen(path, "r")))
return 0;
if (fscanf(fp, "%u", var) != 1) {
fclose(fp);
return 0;
}
fclose(fp);
return 1;
}
void
uspawn(char *const *arg)
{
switch (fork()) {
case -1:
perror("uspawn - fork");
cleanup();
exit(1);
case 0:
close(ConnectionNumber(dpy));
setsid();
execvp(arg[0], arg);
perror("uspawn - child - execvp");
_exit(127);
}
}