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