comm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <bio.h>
  12. #define LB 2048
  13. int one;
  14. int two;
  15. int three;
  16. char *ldr[3];
  17. Biobuf *ib1;
  18. Biobuf *ib2;
  19. Biobuf *openfil(char*);
  20. int rd(Biobuf*, char*);
  21. void wr(char*, int);
  22. void copy(Biobuf*, char*, int);
  23. int compare(char*, char*);
  24. void
  25. main(int argc, char *argv[])
  26. {
  27. int l;
  28. char lb1[LB],lb2[LB];
  29. ldr[0] = "";
  30. ldr[1] = "\t";
  31. ldr[2] = "\t\t";
  32. l = 1;
  33. ARGBEGIN{
  34. case '1':
  35. if(!one) {
  36. one = 1;
  37. ldr[1][0] =
  38. ldr[2][l--] = '\0';
  39. }
  40. break;
  41. case '2':
  42. if(!two) {
  43. two = 1;
  44. ldr[2][l--] = '\0';
  45. }
  46. break;
  47. case '3':
  48. three = 1;
  49. break;
  50. default:
  51. goto Usage;
  52. }ARGEND
  53. if(argc < 2) {
  54. Usage:
  55. fprint(2, "usage: comm [-123] file1 file2\n");
  56. exits("usage");
  57. }
  58. ib1 = openfil(argv[0]);
  59. ib2 = openfil(argv[1]);
  60. if(rd(ib1,lb1) < 0){
  61. if(rd(ib2,lb2) < 0)
  62. exits(0);
  63. copy(ib2,lb2,2);
  64. }
  65. if(rd(ib2,lb2) < 0)
  66. copy(ib1,lb1,1);
  67. for(;;){
  68. switch(compare(lb1,lb2)) {
  69. case 0:
  70. wr(lb1,3);
  71. if(rd(ib1,lb1) < 0) {
  72. if(rd(ib2,lb2) < 0)
  73. exits(0);
  74. copy(ib2,lb2,2);
  75. }
  76. if(rd(ib2,lb2) < 0)
  77. copy(ib1,lb1,1);
  78. continue;
  79. case 1:
  80. wr(lb1,1);
  81. if(rd(ib1,lb1) < 0)
  82. copy(ib2,lb2,2);
  83. continue;
  84. case 2:
  85. wr(lb2,2);
  86. if(rd(ib2,lb2) < 0)
  87. copy(ib1,lb1,1);
  88. continue;
  89. }
  90. }
  91. }
  92. int
  93. rd(Biobuf *file, char *buf)
  94. {
  95. int i, c;
  96. i = 0;
  97. while((c = Bgetc(file)) != Beof) {
  98. *buf = c;
  99. if(c == '\n' || i > LB-2) {
  100. *buf = '\0';
  101. return 0;
  102. }
  103. i++;
  104. buf++;
  105. }
  106. return -1;
  107. }
  108. void
  109. wr(char *str, int n)
  110. {
  111. switch(n){
  112. case 1:
  113. if(one)
  114. return;
  115. break;
  116. case 2:
  117. if(two)
  118. return;
  119. break;
  120. case 3:
  121. if(three)
  122. return;
  123. }
  124. print("%s%s\n", ldr[n-1],str);
  125. }
  126. void
  127. copy(Biobuf *ibuf, char *lbuf, int n)
  128. {
  129. do
  130. wr(lbuf,n);
  131. while(rd(ibuf,lbuf) >= 0);
  132. exits(0);
  133. }
  134. int
  135. compare(char *a, char *b)
  136. {
  137. while(*a == *b){
  138. if(*a == '\0')
  139. return 0;
  140. a++;
  141. b++;
  142. }
  143. if(*a < *b)
  144. return 1;
  145. return 2;
  146. }
  147. Biobuf*
  148. openfil(char *s)
  149. {
  150. Biobuf *b;
  151. if(s[0]=='-' && s[1]==0)
  152. s = "/fd/0";
  153. b = Bopen(s, OREAD);
  154. if(b)
  155. return b;
  156. fprint(2,"comm: cannot open %s: %r\n",s);
  157. exits("open");
  158. return 0; /* shut up ken */
  159. }