1 /* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Case Larsen. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1989, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 static char sccsid[] = "@(#)comm.c 8.3 (Berkeley) 4/2/94"; 45 #endif /* not lint */ 46 47 #include <fcntl.h> 48 #include <limits.h> 49 #include <errno.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 #define MAXLINELEN (LINE_MAX + 1) 55 56 char *tabs[] = { "", "\t", "\t\t" }; 57 58 FILE *file __P((char *)); 59 void show __P((FILE *, char *, char *)); 60 void usage __P((void)); 61 62 int 63 main(argc, argv) 64 int argc; 65 char *argv[]; 66 { 67 int comp, file1done, file2done, read1, read2; 68 int ch, flag1, flag2, flag3; 69 FILE *fp1, *fp2; 70 char *col1, *col2, *col3; 71 char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 72 73 flag1 = flag2 = flag3 = 1; 74 while ((ch = getopt(argc, argv, "-123")) != EOF) 75 switch(ch) { 76 case '-': 77 --optind; 78 goto done; 79 case '1': 80 flag1 = 0; 81 break; 82 case '2': 83 flag2 = 0; 84 break; 85 case '3': 86 flag3 = 0; 87 break; 88 case '?': 89 default: 90 usage(); 91 } 92 done: argc -= optind; 93 argv += optind; 94 95 if (argc != 2) 96 usage(); 97 98 fp1 = file(argv[0]); 99 fp2 = file(argv[1]); 100 101 /* for each column printed, add another tab offset */ 102 p = tabs; 103 col1 = col2 = col3 = NULL; 104 if (flag1) 105 col1 = *p++; 106 if (flag2) 107 col2 = *p++; 108 if (flag3) 109 col3 = *p; 110 111 for (read1 = read2 = 1;;) { 112 /* read next line, check for EOF */ 113 if (read1) 114 file1done = !fgets(line1, MAXLINELEN, fp1); 115 if (read2) 116 file2done = !fgets(line2, MAXLINELEN, fp2); 117 118 /* if one file done, display the rest of the other file */ 119 if (file1done) { 120 if (!file2done && col2) 121 show(fp2, col2, line2); 122 break; 123 } 124 if (file2done) { 125 if (!file1done && col1) 126 show(fp1, col1, line1); 127 break; 128 } 129 130 /* lines are the same */ 131 if (!(comp = strcmp(line1, line2))) { 132 read1 = read2 = 1; 133 if (col3) 134 (void)printf("%s%s", col3, line1); 135 continue; 136 } 137 138 /* lines are different */ 139 if (comp < 0) { 140 read1 = 1; 141 read2 = 0; 142 if (col1) 143 (void)printf("%s%s", col1, line1); 144 } else { 145 read1 = 0; 146 read2 = 1; 147 if (col2) 148 (void)printf("%s%s", col2, line2); 149 } 150 } 151 exit(0); 152 } 153 154 void 155 show(fp, offset, buf) 156 FILE *fp; 157 char *offset, *buf; 158 { 159 160 do { 161 (void)printf("%s%s", offset, buf); 162 } while (fgets(buf, MAXLINELEN, fp)); 163 } 164 165 FILE * 166 file(name) 167 char *name; 168 { 169 FILE *fp; 170 171 if (!strcmp(name, "-")) 172 return (stdin); 173 if ((fp = fopen(name, "r")) == NULL) { 174 (void)fprintf(stderr, "comm: %s: %s\n", name, strerror(errno)); 175 exit(1); 176 } 177 return (fp); 178 } 179 180 void 181 usage() 182 { 183 184 (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); 185 exit(1); 186 } 187