plan9port

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

write.c (1166B)


      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: write [-z] [-h host] [-t type] <datablock\n");
     17 	threadexitsall("usage");
     18 }
     19 
     20 void
     21 threadmain(int argc, char *argv[])
     22 {
     23 	char *host;
     24 	int dotrunc, n, type;
     25 	uchar *p, score[VtScoreSize];
     26 	VtConn *z;
     27 
     28 	fmtinstall('F', vtfcallfmt);
     29 	fmtinstall('V', vtscorefmt);
     30 
     31 	host = nil;
     32 	dotrunc = 0;
     33 	type = VtDataType;
     34 	ARGBEGIN{
     35 	case 'z':
     36 		dotrunc = 1;
     37 		break;
     38 	case 'h':
     39 		host = EARGF(usage());
     40 		break;
     41 	case 't':
     42 		type = atoi(EARGF(usage()));
     43 		break;
     44 	default:
     45 		usage();
     46 		break;
     47 	}ARGEND
     48 
     49 	if(argc != 0)
     50 		usage();
     51 
     52 	p = vtmallocz(VtMaxLumpSize+1);
     53 	n = readn(0, p, VtMaxLumpSize+1);
     54 	if(n > VtMaxLumpSize)
     55 		sysfatal("input too big: max block size is %d", VtMaxLumpSize);
     56 	z = vtdial(host);
     57 	if(z == nil)
     58 		sysfatal("could not connect to server: %r");
     59 	if(vtconnect(z) < 0)
     60 		sysfatal("vtconnect: %r");
     61 	if(dotrunc)
     62 		n = vtzerotruncate(type, p, n);
     63 	if(vtwrite(z, score, type, p, n) < 0)
     64 		sysfatal("vtwrite: %r");
     65 	vthangup(z);
     66 	print("%V\n", score);
     67 	threadexitsall(0);
     68 }