write.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. #include <libsec.h>
  5. #include <thread.h>
  6. void
  7. usage(void)
  8. {
  9. fprint(2, "usage: write [-z] [-h host] [-t type] <datablock\n");
  10. threadexitsall("usage");
  11. }
  12. void
  13. threadmain(int argc, char *argv[])
  14. {
  15. char *host;
  16. int dotrunc, n, type;
  17. uchar *p, score[VtScoreSize];
  18. VtConn *z;
  19. fmtinstall('F', vtfcallfmt);
  20. fmtinstall('V', vtscorefmt);
  21. host = nil;
  22. dotrunc = 0;
  23. type = VtDataType;
  24. ARGBEGIN{
  25. case 'z':
  26. dotrunc = 1;
  27. break;
  28. case 'h':
  29. host = EARGF(usage());
  30. break;
  31. case 't':
  32. type = atoi(EARGF(usage()));
  33. break;
  34. default:
  35. usage();
  36. break;
  37. }ARGEND
  38. if(argc != 0)
  39. usage();
  40. p = vtmallocz(VtMaxLumpSize+1);
  41. n = readn(0, p, VtMaxLumpSize+1);
  42. if(n > VtMaxLumpSize)
  43. sysfatal("input too big: max block size is %d", VtMaxLumpSize);
  44. z = vtdial(host);
  45. if(z == nil)
  46. sysfatal("could not connect to server: %r");
  47. if(vtconnect(z) < 0)
  48. sysfatal("vtconnect: %r");
  49. if(dotrunc)
  50. n = vtzerotruncate(type, p, n);
  51. if(vtwrite(z, score, type, p, n) < 0)
  52. sysfatal("vtwrite: %r");
  53. vthangup(z);
  54. print("%V\n", score);
  55. threadexitsall(0);
  56. }