addhash.c 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <regexp.h>
  5. #include "hash.h"
  6. Hash hash;
  7. void
  8. usage(void)
  9. {
  10. fprint(2, "addhash [-o out] file scale [file scale]...\n");
  11. exits("usage");
  12. }
  13. void
  14. main(int argc, char **argv)
  15. {
  16. int i, fd, n;
  17. char err[ERRMAX], *out;
  18. Biobuf *b, bout;
  19. out = nil;
  20. ARGBEGIN{
  21. case 'o':
  22. out = EARGF(usage());
  23. break;
  24. default:
  25. usage();
  26. }ARGEND;
  27. if(argc==0 || argc%2)
  28. usage();
  29. while(argc > 0){
  30. if((b = Bopenlock(argv[0], OREAD)) == nil)
  31. sysfatal("open %s: %r", argv[0]);
  32. n = atoi(argv[1]);
  33. if(n == 0)
  34. sysfatal("0 scale given");
  35. Breadhash(b, &hash, n);
  36. Bterm(b);
  37. argv += 2;
  38. argc -= 2;
  39. }
  40. fd = 1;
  41. if(out){
  42. for(i=0; i<120; i++){
  43. if((fd = create(out, OWRITE, 0666|DMEXCL)) >= 0)
  44. break;
  45. rerrstr(err, sizeof err);
  46. if(strstr(err, "file is locked") == nil)
  47. break;
  48. sleep(1000);
  49. }
  50. if(fd < 0)
  51. sysfatal("could not open %s: %r\n", out);
  52. }
  53. Binit(&bout, fd, OWRITE);
  54. Bwritehash(&bout, &hash);
  55. Bterm(&bout);
  56. exits(0);
  57. }