-
Notifications
You must be signed in to change notification settings - Fork 26
/
watch.c
249 lines (205 loc) · 4.12 KB
/
watch.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// watch.c
//
// Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/wait.h>
/*
* Command version.
*/
#define VERSION "0.1.0"
/*
* Default interval in milliseconds.
*/
#define DEFAULT_INTERVAL 1000
/*
* Max command args.
*/
#define ARGS_MAX 128
/*
* Quiet mode.
*/
static int quiet = 0;
/*
* Halt on failure.
*/
static int halt = 0;
/*
* Output command usage.
*/
void
usage() {
printf(
"\n"
" Usage: watch [options] <cmd>\n"
"\n"
" Options:\n"
"\n"
" -q, --quiet only output stderr\n"
" -x, --halt halt on failure\n"
" -i, --interval <n> interval in seconds or ms defaulting to 1\n"
" -v, --version output version number\n"
" -h, --help output this help information\n"
"\n"
);
exit(1);
}
/*
* Milliseconds string.
*/
int
milliseconds(const char *str) {
int len = strlen(str);
return 'm' == str[len-2] && 's' == str[len-1];
}
/*
* Sleep in `ms`.
*/
void
mssleep(int ms) {
struct timespec req = {0};
time_t sec = (int)(ms / 1000);
ms = ms -(sec * 1000);
req.tv_sec = sec;
req.tv_nsec = ms * 1000000L;
while(-1 == nanosleep(&req, &req)) ;
}
/*
* Redirect stdout to `path`.
*/
void
redirect_stdout(const char *path) {
int fd = open(path, O_WRONLY);
if (dup2(fd, 1) < 0) {
perror("dup2()");
exit(1);
}
}
/*
* Check if `arg` is the given short-opt or long-opt.
*/
int
option(char *small, char *large, const char *arg) {
if (!strcmp(small, arg) || !strcmp(large, arg)) return 1;
return 0;
}
/*
* Return the total string-length consumed by `strs`.
*/
int
length(char **strs) {
int n = 0;
char *str;
while ((str = *strs++)) n += strlen(str);
return n + 1;
}
/*
* Join the given `strs` with `val`.
*/
char *
join(char **strs, int len, char *val) {
--len;
char *buf = malloc(length(strs) + len * strlen(val));
char *str;
while ((str = *strs++)) {
strcat(buf, str);
if (*strs) strcat(buf, val);
}
return buf;
}
/*
* Parse argv.
*/
int
main(int argc, const char **argv){
if (1 == argc) usage();
int interval = DEFAULT_INTERVAL;
int len = 0;
int interpret = 1;
char *args[ARGS_MAX] = {0};
for (int i = 1; i < argc; ++i) {
const char *arg = argv[i];
if (!interpret) goto arg;
// -h, --help
if (option("-h", "--help", arg)) usage();
// -q, --quiet
if (option("-q", "--quiet", arg)) {
quiet = 1;
continue;
}
// -x, --halt
if (option("-x", "--halt", arg)) {
halt = 1;
continue;
}
// -v, --version
if (option("-v", "--version", arg)) {
printf("%s\n", VERSION);
exit(1);
}
// -i, --interval <n>
if (option("-i", "--interval", arg)) {
if (argc-1 == i) {
fprintf(stderr, "\n --interval requires an argument\n\n");
exit(1);
}
// seconds or milliseconds
arg = argv[++i];
interval = milliseconds(arg)
? atoi(arg)
: atoi(arg) * 1000;
continue;
}
// cmd args
if (len == ARGS_MAX) {
fprintf(stderr, "number of arguments exceeded %d\n", len);
exit(1);
}
arg:
args[len++] = (char *) arg;
interpret = 0;
}
// <cmd>
if (!len) {
fprintf(stderr, "\n <cmd> required\n\n");
exit(1);
}
// cmd
char *val = join(args, len, " ");
char *cmd[4] = { "sh", "-c", val, 0 };
// exec loop
loop: {
pid_t pid;
int status;
switch (pid = fork()) {
// error
case -1:
perror("fork()");
exit(1);
// child
case 0:
if (quiet) redirect_stdout("/dev/null");
execvp(cmd[0], cmd);
// parent
default:
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid()");
exit(1);
}
// exit > 0
if (WEXITSTATUS(status)) {
fprintf(stderr, "\033[90mexit: %d\33[0m\n\n", WEXITSTATUS(status));
if (halt) exit(WEXITSTATUS(status));
}
mssleep(interval);
goto loop;
}
}
return 0;
}