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