Blob


1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
5 char *host;
7 void
8 usage(void)
9 {
10 fprint(2, "usage: read [-h host] score [type]\n");
11 threadexitsall("usage");
12 }
14 int
15 parsescore(uchar *score, char *buf, int n)
16 {
17 int i, c;
19 memset(score, 0, VtScoreSize);
21 if(n != VtScoreSize*2){
22 werrstr("score wrong length %d", n);
23 return -1;
24 }
25 for(i=0; i<VtScoreSize*2; i++) {
26 if(buf[i] >= '0' && buf[i] <= '9')
27 c = buf[i] - '0';
28 else if(buf[i] >= 'a' && buf[i] <= 'f')
29 c = buf[i] - 'a' + 10;
30 else if(buf[i] >= 'A' && buf[i] <= 'F')
31 c = buf[i] - 'A' + 10;
32 else {
33 c = buf[i];
34 werrstr("bad score char %d '%c'", c, c);
35 return -1;
36 }
38 if((i & 1) == 0)
39 c <<= 4;
41 score[i>>1] |= c;
42 }
43 return 0;
44 }
46 void
47 threadmain(int argc, char *argv[])
48 {
49 int type, n;
50 uchar score[VtScoreSize];
51 uchar *buf;
52 VtConn *z;
54 ARGBEGIN{
55 case 'h':
56 host = EARGF(usage());
57 break;
58 default:
59 usage();
60 break;
61 }ARGEND
63 if(argc != 1 && argc != 2)
64 usage();
67 fmtinstall('V', vtscorefmt);
69 if(parsescore(score, argv[0], strlen(argv[0])) < 0)
70 sysfatal("could not parse score '%s': %r", argv[0]);
72 buf = vtmallocz(VtMaxLumpSize);
74 z = vtdial(host);
75 if(z == nil)
76 sysfatal("could not connect to server: %r");
78 if(vtconnect(z) < 0)
79 sysfatal("vtconnect: %r");
81 if(argc == 1){
82 n = -1;
83 for(type=0; type<VtMaxType; type++){
84 n = vtread(z, score, type, buf, VtMaxLumpSize);
85 if(n >= 0){
86 fprint(2, "venti/read%s%s %V %d\n", host ? " -h" : "", host ? host : "",
87 score, type);
88 break;
89 }
90 }
91 }else{
92 type = atoi(argv[1]);
93 n = vtread(z, score, type, buf, VtMaxLumpSize);
94 }
95 vthangup(z);
96 if(n < 0)
97 sysfatal("could not read block: %r");
98 if(write(1, buf, n) != n)
99 sysfatal("write: %r");
101 threadexitsall(0);