Annotation of researchv10dc/cmd/uniq.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Deal with duplicated lines in a file
                      3:  */
                      4: #include <stdio.h>
                      5: #include <ctype.h>
                      6: #define        SIZE    1000
                      7: int    fields  = 0;
                      8: int    letters = 0;
                      9: int    linec   = 0;
                     10: char   mode;
                     11: int    uniq;
                     12: char   *skip();
                     13: 
                     14: main(argc, argv)
                     15: int argc;
                     16: char *argv[];
                     17: {
                     18:        static char b1[SIZE], b2[SIZE];
                     19: 
                     20:        while(argc > 1) {
                     21:                if(*argv[1] == '-') {
                     22:                        if (isdigit(argv[1][1]))
                     23:                                fields = atoi(&argv[1][1]);
                     24:                        else mode = argv[1][1];
                     25:                        argc--;
                     26:                        argv++;
                     27:                        continue;
                     28:                }
                     29:                if(*argv[1] == '+') {
                     30:                        letters = atoi(&argv[1][1]);
                     31:                        argc--;
                     32:                        argv++;
                     33:                        continue;
                     34:                }
                     35:                if (freopen(argv[1], "r", stdin) == NULL) {
                     36:                        fprintf(stderr, "cannot open %s\n", argv[1]);
                     37:                        exit(1);
                     38:                }
                     39:                break;
                     40:        }
                     41:        if(argc > 2) {
                     42:                fprintf(stderr, "unexpected argument %s\n", argv[2]);
                     43:                exit(1);
                     44:        }
                     45: 
                     46:        if(gline(b1))
                     47:                exit(0);
                     48:        for(;;) {
                     49:                linec++;
                     50:                if(gline(b2)) {
                     51:                        pline(b1);
                     52:                        exit(0);
                     53:                }
                     54:                if(!equal(b1, b2)) {
                     55:                        pline(b1);
                     56:                        linec = 0;
                     57:                        do {
                     58:                                linec++;
                     59:                                if(gline(b1)) {
                     60:                                        pline(b2);
                     61:                                        exit(0);
                     62:                                }
                     63:                        } while(equal(b1, b2));
                     64:                        pline(b2);
                     65:                        linec = 0;
                     66:                }
                     67:        }
                     68: }
                     69: 
                     70: gline(buf)
                     71: register char buf[];
                     72: {
                     73:        register c, n;
                     74: 
                     75:        n = 0;
                     76:        while((c = getchar()) != '\n') {
                     77:                if(c == 0)
                     78:                        continue;
                     79:                if(c == EOF)
                     80:                        return(1);
                     81:                *buf++ = c;
                     82:                n++;
                     83:                if(n >= SIZE-1) {
                     84:                        fprintf(stderr, "line too long\n");
                     85:                        exit(1);
                     86:                }
                     87:        }
                     88:        *buf = 0;
                     89:        return(0);
                     90: }
                     91: 
                     92: pline(buf)
                     93: register char buf[];
                     94: {
                     95: 
                     96:        switch(mode) {
                     97: 
                     98:        case 'u':
                     99:                if(uniq) {
                    100:                        uniq = 0;
                    101:                        return;
                    102:                }
                    103:                break;
                    104: 
                    105:        case 'd':
                    106:                if(uniq) break;
                    107:                return;
                    108: 
                    109:        case 'c':
                    110:                printf("%4d ", linec);
                    111:        }
                    112:        uniq = 0;
                    113:        fputs(buf, stdout);
                    114:        putchar('\n');
                    115: }
                    116: 
                    117: equal(b1, b2)
                    118: register char b1[], b2[];
                    119: {
                    120:        register char c;
                    121: 
                    122:        b1 = skip(b1);
                    123:        b2 = skip(b2);
                    124:        while((c = *b1++) != 0)
                    125:                if(c != *b2++) return(0);
                    126:        if(*b2 != 0)
                    127:                return(0);
                    128:        uniq++;
                    129:        return(1);
                    130: }
                    131: 
                    132: char *
                    133: skip(s)
                    134: register char *s;
                    135: {
                    136:        register nf, nl;
                    137: 
                    138:        nf = nl = 0;
                    139:        while(nf++ < fields) {
                    140:                while(*s == ' ' || *s == '\t')
                    141:                        s++;
                    142:                while( !(*s == ' ' || *s == '\t' || *s == 0) ) 
                    143:                        s++;
                    144:        }
                    145:        while(nl++ < letters && *s != 0) 
                    146:                        s++;
                    147:        return(s);
                    148: }

unix.superglobalmegacorp.com

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