cmp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <ctype.h>
  12. #define BUF 65536
  13. int sflag = 0;
  14. int lflag = 0;
  15. int Lflag = 0;
  16. static void usage(void);
  17. char **
  18. seekoff(int fd, char *name, char **argv)
  19. {
  20. int64_t o;
  21. if(*argv){
  22. if (!isascii(**argv) || !isdigit(**argv))
  23. usage();
  24. o = strtoll(*argv++, 0, 0);
  25. if(seek(fd, o, 0) < 0){
  26. if(!sflag) fprint(2, "cmp: %s: seek by %lld: %r\n",
  27. name, o);
  28. exits("seek");
  29. }
  30. }
  31. return argv;
  32. }
  33. void
  34. main(int argc, char *argv[])
  35. {
  36. int n, i;
  37. unsigned char *p, *q;
  38. unsigned char buf1[BUF], buf2[BUF];
  39. int f1, f2;
  40. int64_t nc = 1, l = 1;
  41. char *name1, *name2;
  42. unsigned char *b1s, *b1e, *b2s, *b2e;
  43. ARGBEGIN{
  44. case 's': sflag = 1; break;
  45. case 'l': lflag = 1; break;
  46. case 'L': Lflag = 1; break;
  47. default: usage();
  48. }ARGEND
  49. if(argc < 2 || argc > 4)
  50. usage();
  51. if((f1 = open(name1 = *argv++, OREAD)) == -1){
  52. if(!sflag) perror(name1);
  53. exits("open");
  54. }
  55. if((f2 = open(name2 = *argv++, OREAD)) == -1){
  56. if(!sflag) perror(name2);
  57. exits("open");
  58. }
  59. argv = seekoff(f1, name1, argv);
  60. argv = seekoff(f2, name2, argv);
  61. if(*argv)
  62. usage();
  63. b1s = b1e = buf1;
  64. b2s = b2e = buf2;
  65. for(;;){
  66. if(b1s >= b1e){
  67. if(b1s >= &buf1[BUF])
  68. b1s = buf1;
  69. n = read(f1, b1s, &buf1[BUF] - b1s);
  70. b1e = b1s + n;
  71. }
  72. if(b2s >= b2e){
  73. if(b2s >= &buf2[BUF])
  74. b2s = buf2;
  75. n = read(f2, b2s, &buf2[BUF] - b2s);
  76. b2e = b2s + n;
  77. }
  78. n = b2e - b2s;
  79. if(n > b1e - b1s)
  80. n = b1e - b1s;
  81. if(n <= 0)
  82. break;
  83. if(memcmp((void *)b1s, (void *)b2s, n) != 0){
  84. if(sflag)
  85. exits("differ");
  86. for(p = b1s, q = b2s, i = 0; i < n; p++, q++, i++) {
  87. if(*p == '\n')
  88. l++;
  89. if(*p != *q){
  90. if(!lflag){
  91. print("%s %s differ: char %lld",
  92. name1, name2, nc+i);
  93. print(Lflag?" line %lld\n":"\n", l);
  94. exits("differ");
  95. }
  96. print("%6lld 0x%.2x 0x%.2x\n", nc+i, *p, *q);
  97. }
  98. }
  99. }
  100. if(Lflag)
  101. for(p = b1s; p < b1e;)
  102. if(*p++ == '\n')
  103. l++;
  104. nc += n;
  105. b1s += n;
  106. b2s += n;
  107. }
  108. if (b1e - b1s < 0 || b2e - b2s < 0) {
  109. if (!sflag) {
  110. if (b1e - b1s < 0)
  111. print("error on %s after %lld bytes\n",
  112. name1, nc-1);
  113. if (b2e - b2s < 0)
  114. print("error on %s after %lld bytes\n",
  115. name2, nc-1);
  116. }
  117. exits("read error");
  118. }
  119. if(b1e - b1s == b2e - b2s)
  120. exits((char *)0);
  121. if(!sflag)
  122. print("EOF on %s after %lld bytes\n",
  123. (b1e - b1s > b2e - b2s)? name2 : name1, nc-1);
  124. exits("EOF");
  125. }
  126. static void
  127. usage(void)
  128. {
  129. print("usage: cmp [-lLs] file1 file2 [offset1 [offset2] ]\n");
  130. exits("usage");
  131. }