Annotation of 43BSDReno/usr.bin/comm/comm.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * 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 are permitted provided
                      9:  * that: (1) source distributions retain this entire copyright notice and
                     10:  * comment, and (2) distributions including binaries display the following
                     11:  * acknowledgement:  ``This product includes software developed by the
                     12:  * University of California, Berkeley and its contributors'' in the
                     13:  * documentation or other materials provided with the distribution and in
                     14:  * all advertising materials mentioning features or use of this software.
                     15:  * Neither the name of the University nor the names of its contributors may
                     16:  * be used to endorse or promote products derived from this software without
                     17:  * specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: char copyright[] =
                     25: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
                     26:  All rights reserved.\n";
                     27: #endif /* not lint */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)comm.c     5.5 (Berkeley) 6/25/90";
                     31: #endif /* not lint */
                     32: 
                     33: #include <sys/file.h>
                     34: #include <limits.h>
                     35: #include <stdio.h>
                     36: 
                     37: #define        MAXLINELEN      (_BSD_LINE_MAX + 1)
                     38: 
                     39: char *tabs[] = { "", "\t", "\t\t" };
                     40: 
                     41: main(argc,argv)
                     42:        int argc;
                     43:        char *argv[];
                     44: {
                     45:        register int comp, file1done, file2done, read1, read2;
                     46:        register char *col1, *col2, *col3;
                     47:        int ch, flag1, flag2, flag3;
                     48:        FILE *fp1, *fp2, *file();
                     49:        char **p, line1[MAXLINELEN], line2[MAXLINELEN];
                     50:        extern int optind;
                     51: 
                     52:        flag1 = flag2 = flag3 = 1;
                     53:        while ((ch = getopt(argc, argv, "-123")) != EOF)
                     54:                switch(ch) {
                     55:                case '-':
                     56:                        --optind;
                     57:                        goto done;
                     58:                case '1':
                     59:                        flag1 = 0;
                     60:                        break;
                     61:                case '2':
                     62:                        flag2 = 0;
                     63:                        break;
                     64:                case '3':
                     65:                        flag3 = 0;
                     66:                        break;
                     67:                case '?':
                     68:                default:
                     69:                        usage();
                     70:                }
                     71: done:  argc -= optind;
                     72:        argv += optind;
                     73: 
                     74:        if (argc != 2)
                     75:                usage();
                     76: 
                     77:        fp1 = file(argv[0]);
                     78:        fp2 = file(argv[1]);
                     79: 
                     80:        /* for each column printed, add another tab offset */
                     81:        p = tabs;
                     82:        if (flag1)
                     83:                col1 = *p++;
                     84:        if (flag2)
                     85:                col2 = *p++;
                     86:        if (flag3)
                     87:                col3 = *p++;
                     88: 
                     89:        for (read1 = read2 = 1;;) {
                     90:                /* read next line, check for EOF */
                     91:                if (read1)
                     92:                        file1done = !fgets(line1, MAXLINELEN, fp1);
                     93:                if (read2)
                     94:                        file2done = !fgets(line2, MAXLINELEN, fp2);
                     95: 
                     96:                /* if one file done, display the rest of the other file */
                     97:                if (file1done) {
                     98:                        if (!file2done && col2)
                     99:                                show(fp2, col2, line2);
                    100:                        break;
                    101:                }
                    102:                if (file2done) {
                    103:                        if (!file1done && col1)
                    104:                                show(fp1, col1, line1);
                    105:                        break;
                    106:                }
                    107: 
                    108:                /* lines are the same */
                    109:                if (!(comp = strcmp(line1, line2))) {
                    110:                        read1 = read2 = 1;
                    111:                        if (col3)
                    112:                                (void)printf("%s%s", col3, line1);
                    113:                        continue;
                    114:                }
                    115: 
                    116:                /* lines are different */
                    117:                if (comp < 0) {
                    118:                        read1 = 1;
                    119:                        read2 = 0;
                    120:                        if (col1)
                    121:                                (void)printf("%s%s", col1, line1);
                    122:                } else {
                    123:                        read1 = 0;
                    124:                        read2 = 1;
                    125:                        if (col2)
                    126:                                (void)printf("%s%s", col2, line2);
                    127:                }
                    128:        }
                    129:        exit(0);
                    130: }
                    131: 
                    132: show(fp, offset, buf)
                    133:        FILE *fp;
                    134:        char *offset, *buf;
                    135: {
                    136:        do {
                    137:                (void)printf("%s%s", offset, buf);
                    138:        } while (fgets(buf, MAXLINELEN, fp));
                    139: }
                    140: 
                    141: FILE *
                    142: file(name)
                    143:        char *name;
                    144: {
                    145:        FILE *fp;
                    146: 
                    147:        if (!strcmp(name, "-"))
                    148:                return(stdin);
                    149:        if (!(fp = fopen(name, "r"))) {
                    150:                (void)fprintf(stderr, "comm: can't read %s.\n", name);
                    151:                exit(1);
                    152:        }
                    153:        return(fp);
                    154: }
                    155: 
                    156: usage()
                    157: {
                    158:        (void)fprintf(stderr, "usage: comm [-123] [ - ] file1 file2\n");
                    159:        exit(1);
                    160: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.