read.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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: read [-h host] score [type]\n");
  9. exits("usage");
  10. }
  11. int
  12. parseScore(uchar *score, char *buf, int n)
  13. {
  14. int i, c;
  15. memset(score, 0, VtScoreSize);
  16. if(n < VtScoreSize*2)
  17. return 0;
  18. for(i=0; i<VtScoreSize*2; i++) {
  19. if(buf[i] >= '0' && buf[i] <= '9')
  20. c = buf[i] - '0';
  21. else if(buf[i] >= 'a' && buf[i] <= 'f')
  22. c = buf[i] - 'a' + 10;
  23. else if(buf[i] >= 'A' && buf[i] <= 'F')
  24. c = buf[i] - 'A' + 10;
  25. else {
  26. return 0;
  27. }
  28. if((i & 1) == 0)
  29. c <<= 4;
  30. score[i>>1] |= c;
  31. }
  32. return 1;
  33. }
  34. int
  35. main(int argc, char *argv[])
  36. {
  37. int type, n;
  38. uchar score[VtScoreSize];
  39. uchar *buf;
  40. VtSession *z;
  41. ARGBEGIN{
  42. case 'h':
  43. host = EARGF(usage());
  44. break;
  45. default:
  46. usage();
  47. break;
  48. }ARGEND
  49. if(argc != 1 && argc != 2)
  50. usage();
  51. vtAttach();
  52. fmtinstall('V', vtScoreFmt);
  53. fmtinstall('R', vtErrFmt);
  54. if(!parseScore(score, argv[0], strlen(argv[0])))
  55. vtFatal("could not parse score: %s", vtGetError());
  56. buf = vtMemAllocZ(VtMaxLumpSize);
  57. z = vtDial(host, 0);
  58. if(z == nil)
  59. vtFatal("could not connect to server: %R");
  60. if(!vtConnect(z, 0))
  61. sysfatal("vtConnect: %r");
  62. if(argc == 1){
  63. n = -1;
  64. for(type=0; type<VtMaxType; type++){
  65. n = vtRead(z, score, type, buf, VtMaxLumpSize);
  66. if(n >= 0){
  67. fprint(2, "venti/read%s%s %V %d\n", host ? " -h" : "", host ? host : "",
  68. score, type);
  69. break;
  70. }
  71. }
  72. }else{
  73. type = atoi(argv[1]);
  74. n = vtRead(z, score, type, buf, VtMaxLumpSize);
  75. }
  76. vtClose(z);
  77. if(n < 0)
  78. vtFatal("could not read block: %s", vtGetError());
  79. if(write(1, buf, n) != n)
  80. vtFatal("write: %r");
  81. vtDetach();
  82. exits(0);
  83. return 0; /* shut up compiler */
  84. }