write.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <venti.h>
  12. #include <libsec.h>
  13. #include <thread.h>
  14. void
  15. usage(void)
  16. {
  17. fprint(2, "usage: write [-z] [-h host] [-t type] <datablock\n");
  18. threadexitsall("usage");
  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. fmtinstall('F', vtfcallfmt);
  28. fmtinstall('V', vtscorefmt);
  29. host = nil;
  30. dotrunc = 0;
  31. type = VtDataType;
  32. ARGBEGIN{
  33. case 'z':
  34. dotrunc = 1;
  35. break;
  36. case 'h':
  37. host = EARGF(usage());
  38. break;
  39. case 't':
  40. type = atoi(EARGF(usage()));
  41. break;
  42. default:
  43. usage();
  44. break;
  45. }ARGEND
  46. if(argc != 0)
  47. usage();
  48. p = vtmallocz(VtMaxLumpSize+1);
  49. n = readn(0, p, VtMaxLumpSize+1);
  50. if(n > VtMaxLumpSize)
  51. sysfatal("input too big: max block size is %d", VtMaxLumpSize);
  52. z = vtdial(host);
  53. if(z == nil)
  54. sysfatal("could not connect to server: %r");
  55. if(vtconnect(z) < 0)
  56. sysfatal("vtconnect: %r");
  57. if(dotrunc)
  58. n = vtzerotruncate(type, p, n);
  59. if(vtwrite(z, score, type, p, n) < 0)
  60. sysfatal("vtwrite: %r");
  61. vthangup(z);
  62. print("%V\n", score);
  63. threadexitsall(0);
  64. }