write.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. char *host;
  5. void
  6. usage(void)
  7. {
  8. fprint(2, "usage: write [-z] [-h host] [-t type] <datablock\n");
  9. exits("usage");
  10. }
  11. int
  12. main(int argc, char *argv[])
  13. {
  14. uchar *p, score[VtScoreSize];
  15. int type, n, dotrunc;
  16. VtSession *z;
  17. dotrunc = 0;
  18. type = VtDataType;
  19. ARGBEGIN{
  20. case 'z':
  21. dotrunc = 1;
  22. break;
  23. case 'h':
  24. host = EARGF(usage());
  25. break;
  26. case 't':
  27. type = atoi(EARGF(usage()));
  28. break;
  29. default:
  30. usage();
  31. break;
  32. }ARGEND
  33. if(argc != 0)
  34. usage();
  35. vtAttach();
  36. fmtinstall('V', vtScoreFmt);
  37. fmtinstall('R', vtErrFmt);
  38. p = vtMemAllocZ(VtMaxLumpSize+1);
  39. n = readn(0, p, VtMaxLumpSize+1);
  40. if(n > VtMaxLumpSize)
  41. vtFatal("data too big");
  42. z = vtDial(host, 0);
  43. if(z == nil)
  44. vtFatal("could not connect to server: %R");
  45. if(!vtConnect(z, 0))
  46. vtFatal("vtConnect: %r");
  47. if(dotrunc)
  48. n = vtZeroTruncate(type, p, n);
  49. if(!vtWrite(z, score, type, p, n))
  50. vtFatal("vtWrite: %R");
  51. vtClose(z);
  52. print("%V\n", score);
  53. vtDetach();
  54. exits(0);
  55. return 0; /* shut up compiler */
  56. }