-
Notifications
You must be signed in to change notification settings - Fork 0
/
kfscmd.c
58 lines (53 loc) · 1.13 KB
/
kfscmd.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
#include <u.h>
#include <libc.h>
void
main(int argc, char *argv[])
{
char *name, buf[4*1024], *ns;
int fd, n, i, errs;
name = 0;
ARGBEGIN{
case 'n':
name = ARGF();
break;
default:
fprint(2, "usage: kfscmd [-n server] commands\n");
exits("usage");
}ARGEND
ns = getns();
if(name)
snprint(buf, sizeof buf, "%s/kfs.%s.cmd", ns, name);
else
snprint(buf, sizeof buf, "%s/kfs.cmd", ns);
fd = open(buf, ORDWR);
if(fd < 0){
fprint(2, "kfscmd: can't open commands file: %s\n", buf);
exits("commands file");
}
errs = 0;
for(i = 0; i < argc; i++){
if(write(fd, argv[i], strlen(argv[i])) != strlen(argv[i])){
fprint(2, "%s: error writing %s: %r", argv0, argv[i]);
errs++;
continue;
}
for(;;){
n = read(fd, buf, sizeof buf - 1);
if(n < 0){
fprint(2, "%s: error executing %s: %r", argv0, argv[i]);
errs++;
break;
}
buf[n] = '\0';
if(strcmp(buf, "done") == 0 || strcmp(buf, "success") == 0)
break;
if(strcmp(buf, "unknown command") == 0){
errs++;
print("kfscmd: command %s not recognized\n", argv[i]);
break;
}
write(1, buf, n);
}
}
exits(errs ? "errors" : 0);
}