read.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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: read [-h host] [-t type] score\n");
  10. threadexitsall("usage");
  11. }
  12. void
  13. threadmain(int argc, char *argv[])
  14. {
  15. int type, n;
  16. uchar score[VtScoreSize];
  17. uchar *buf;
  18. VtConn *z;
  19. char *host;
  20. fmtinstall('F', vtfcallfmt);
  21. fmtinstall('V', vtscorefmt);
  22. host = nil;
  23. type = -1;
  24. ARGBEGIN{
  25. case 'h':
  26. host = EARGF(usage());
  27. break;
  28. case 't':
  29. type = atoi(EARGF(usage()));
  30. break;
  31. default:
  32. usage();
  33. break;
  34. }ARGEND
  35. if(argc != 1)
  36. usage();
  37. if(vtparsescore(argv[0], nil, score) < 0)
  38. sysfatal("could not parse score '%s': %r", argv[0]);
  39. buf = vtmallocz(VtMaxLumpSize);
  40. z = vtdial(host);
  41. if(z == nil)
  42. sysfatal("could not connect to server: %r");
  43. if(vtconnect(z) < 0)
  44. sysfatal("vtconnect: %r");
  45. if(type == -1){
  46. n = -1;
  47. for(type=0; type<VtMaxType; type++){
  48. n = vtread(z, score, type, buf, VtMaxLumpSize);
  49. if(n >= 0){
  50. fprint(2, "venti/read%s%s %V %d\n", host ? " -h" : "", host ? host : "",
  51. score, type);
  52. break;
  53. }
  54. }
  55. }else
  56. n = vtread(z, score, type, buf, VtMaxLumpSize);
  57. vthangup(z);
  58. if(n < 0)
  59. sysfatal("could not read block: %r");
  60. if(write(1, buf, n) != n)
  61. sysfatal("write: %r");
  62. threadexitsall(0);
  63. }