1 /* 2 * Copyright (c) 1989, 1993 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 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char copyright[] = 13 "@(#) Copyright (c) 1989, 1993\n\ 14 The Regents of the University of California. All rights reserved.\n"; 15 #endif /* not lint */ 16 17 #ifndef lint 18 static char sccsid[] = "@(#)comm.c 8.1 (Berkeley) 06/06/93"; 19 #endif /* not lint */ 20 21 #include <fcntl.h> 22 #include <limits.h> 23 #include <errno.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #define MAXLINELEN (_POSIX2_LINE_MAX + 1) 29 30 char *tabs[] = { "", "\t", "\t\t" }; 31 32 main(argc,argv) 33 int argc; 34 char *argv[]; 35 { 36 register int comp, file1done, file2done, read1, read2; 37 register char *col1, *col2, *col3; 38 int ch, flag1, flag2, flag3; 39 FILE *fp1, *fp2, *file(); 40 char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 41 extern int optind; 42 43 flag1 = flag2 = flag3 = 1; 44 while ((ch = getopt(argc, argv, "-123")) != EOF) 45 switch(ch) { 46 case '-': 47 --optind; 48 goto done; 49 case '1': 50 flag1 = 0; 51 break; 52 case '2': 53 flag2 = 0; 54 break; 55 case '3': 56 flag3 = 0; 57 break; 58 case '?': 59 default: 60 usage(); 61 } 62 done: argc -= optind; 63 argv += optind; 64 65 if (argc != 2) 66 usage(); 67 68 fp1 = file(argv[0]); 69 fp2 = file(argv[1]); 70 71 /* for each column printed, add another tab offset */ 72 p = tabs; 73 col1 = col2 = col3 = NULL; 74 if (flag1) 75 col1 = *p++; 76 if (flag2) 77 col2 = *p++; 78 if (flag3) 79 col3 = *p; 80 81 for (read1 = read2 = 1;;) { 82 /* read next line, check for EOF */ 83 if (read1) 84 file1done = !fgets(line1, MAXLINELEN, fp1); 85 if (read2) 86 file2done = !fgets(line2, MAXLINELEN, fp2); 87 88 /* if one file done, display the rest of the other file */ 89 if (file1done) { 90 if (!file2done && col2) 91 show(fp2, col2, line2); 92 break; 93 } 94 if (file2done) { 95 if (!file1done && col1) 96 show(fp1, col1, line1); 97 break; 98 } 99 100 /* lines are the same */ 101 if (!(comp = strcmp(line1, line2))) { 102 read1 = read2 = 1; 103 if (col3) 104 (void)printf("%s%s", col3, line1); 105 continue; 106 } 107 108 /* lines are different */ 109 if (comp < 0) { 110 read1 = 1; 111 read2 = 0; 112 if (col1) 113 (void)printf("%s%s", col1, line1); 114 } else { 115 read1 = 0; 116 read2 = 1; 117 if (col2) 118 (void)printf("%s%s", col2, line2); 119 } 120 } 121 exit(0); 122 } 123 124 show(fp, offset, buf) 125 FILE *fp; 126 char *offset, *buf; 127 { 128 do { 129 (void)printf("%s%s", offset, buf); 130 } while (fgets(buf, MAXLINELEN, fp)); 131 } 132 133 FILE * 134 file(name) 135 char *name; 136 { 137 FILE *fp; 138 139 if (!strcmp(name, "-")) 140 return(stdin); 141 if ((fp = fopen(name, "r")) == NULL) { 142 (void)fprintf(stderr, "comm: %s: %s\n", name, strerror(errno)); 143 exit(1); 144 } 145 return(fp); 146 } 147 148 usage() 149 { 150 (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); 151 exit(1); 152 } 153