plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

read.c (1349B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <venti.h>
      4 #include <libsec.h>
      5 #include <thread.h>
      6 
      7 enum
      8 {
      9 	// XXX What to do here?
     10 	VtMaxLumpSize = 65535,
     11 };
     12 
     13 void
     14 usage(void)
     15 {
     16 	fprint(2, "usage: read [-h host] [-t type] score\n");
     17 	threadexitsall("usage");
     18 }
     19 
     20 void
     21 threadmain(int argc, char *argv[])
     22 {
     23 	int type, n;
     24 	uchar score[VtScoreSize];
     25 	uchar *buf;
     26 	VtConn *z;
     27 	char *host;
     28 
     29 	fmtinstall('F', vtfcallfmt);
     30 	fmtinstall('V', vtscorefmt);
     31 
     32 	host = nil;
     33 	type = -1;
     34 	ARGBEGIN{
     35 	case 'h':
     36 		host = EARGF(usage());
     37 		break;
     38 	case 't':
     39 		type = atoi(EARGF(usage()));
     40 		break;
     41 	default:
     42 		usage();
     43 		break;
     44 	}ARGEND
     45 
     46 	if(argc != 1)
     47 		usage();
     48 
     49 	if(vtparsescore(argv[0], nil, score) < 0)
     50 		sysfatal("could not parse score '%s': %r", argv[0]);
     51 
     52 	buf = vtmallocz(VtMaxLumpSize);
     53 
     54 	z = vtdial(host);
     55 	if(z == nil)
     56 		sysfatal("could not connect to server: %r");
     57 
     58 	if(vtconnect(z) < 0)
     59 		sysfatal("vtconnect: %r");
     60 
     61 	if(type == -1){
     62 		n = -1;
     63 		for(type=0; type<VtMaxType; type++){
     64 			n = vtread(z, score, type, buf, VtMaxLumpSize);
     65 			if(n >= 0){
     66 				fprint(2, "venti/read%s%s %V %d\n", host ? " -h" : "", host ? host : "",
     67 					score, type);
     68 				break;
     69 			}
     70 		}
     71 	}else
     72 		n = vtread(z, score, type, buf, VtMaxLumpSize);
     73 
     74 	vthangup(z);
     75 	if(n < 0)
     76 		sysfatal("could not read block: %r");
     77 	if(write(1, buf, n) != n)
     78 		sysfatal("write: %r");
     79 	threadexitsall(0);
     80 }