Annotation of coherent/d/bin/uniq.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * uniq [-udc] [-n] [+n] [input [output]]
                      3:  *
                      4:  * Report repeated lines in a file.
                      5:  */
                      6: 
                      7: #include <stdio.h>
                      8: 
                      9: #define        MAXLINE 200             /* Longest line allowed */
                     10: 
                     11: char   lines[2][MAXLINE];
                     12: char   *prevline = lines[0];
                     13: char   *currline = lines[1];
                     14: 
                     15: int    cflag;          /* Counts */
                     16: int    uflag;          /* non-repeated lines */
                     17: int    dflag;          /* print one copy of repeated lines */
                     18: int    nfield;         /* Fields to skip */
                     19: int    nchar;          /* Chars to skip */
                     20: int    count;          /* Repeat count */
                     21: 
                     22: FILE   *ifp, *ofp;
                     23: 
                     24: char   *input();
                     25: 
                     26: main(argc, argv)
                     27: char *argv[];
                     28: {
                     29:        register char *ap;
                     30: 
                     31: #ifdef MSDOS
                     32:        msdoscvt("uniq", &argc, &argv);
                     33: #endif
                     34:        if (argc > 1)
                     35:                while (*argv[1]=='-' || *argv[1]=='+') {
                     36:                        if (*argv[1] == '-') {
                     37:                                ap = &argv[1][1];
                     38:                                if (*ap>='0' && *ap<='9')
                     39:                                        nfield = atoi(ap);
                     40:                                else
                     41:                                        for (; *ap; ap++)
                     42:                                        switch (*ap) {
                     43:                                        case 'c':
                     44:                                                cflag = 1;
                     45:                                                break;
                     46:        
                     47:                                        case 'd':
                     48:                                                dflag = 1;
                     49:                                                break;
                     50:        
                     51:                                        case 'u':
                     52:                                                uflag = 1;
                     53:                                                break;
                     54:        
                     55:                                        default:
                     56:                                                usage();
                     57:                                        }
                     58:                        } else
                     59:                                nchar = atoi(&argv[1][1]);
                     60:                        argc--;
                     61:                        argv++;
                     62:                } /* end while */
                     63:        if (cflag == 0 && dflag==0 && uflag==0)
                     64:                dflag = uflag = 1;
                     65:        ofp = stdout;
                     66:        ifp = stdin;
                     67:        if (argc > 3)
                     68:                usage();
                     69:        if (argc == 3)
                     70:                if ((ofp = fopen(argv[2], "w")) == NULL) {
                     71:                        fprintf(stderr, "Cannot open output %s\n", argv[2]);
                     72:                        exit(1);
                     73:                }
                     74:        if (argc > 1)
                     75:                if ((ifp = fopen(argv[1], "r")) == NULL) {
                     76:                        fprintf(stderr, "Cannot open input %s\n", argv[1]);
                     77:                        exit(1);
                     78:                }
                     79:        uniq();
                     80:        exit(0);
                     81: }
                     82: 
                     83: /*
                     84:  * Actually do the work of checking for uniqueness.
                     85:  */
                     86: uniq()
                     87: {
                     88:        register indx;
                     89:        register char *pl = prevline;
                     90:        register char *cl = currline;
                     91: 
                     92:        indx = 1;
                     93:        if ((pl = input(prevline)) == NULL)
                     94:                return;
                     95:        for (count = 1; (cl = input(currline)) != NULL; count++) {
                     96:                if (strcmp(pl, cl) != 0) {
                     97:                        output(prevline);
                     98:                        prevline = currline;
                     99:                        pl = cl;
                    100:                        indx ^= 1;
                    101:                        currline = lines[indx];
                    102:                        count = 0;
                    103:                }
                    104:        }
                    105:        output(prevline);
                    106: }
                    107: 
                    108: /*
                    109:  * Output line according to the various formats
                    110:  */
                    111: output(line)
                    112: char *line;
                    113: {
                    114:        if (cflag)
                    115:                fprintf(ofp, "%d: %s", count, line);
                    116:        else if ((uflag && count==1) || (dflag && count>1))
                    117:                fprintf(ofp, "%s", line);
                    118: }
                    119: 
                    120: /*
                    121:  * Input lines, doing field and character skipping.
                    122:  */
                    123: char *
                    124: input(lp)
                    125: register char *lp;
                    126: {
                    127:        register nf, nc;
                    128: 
                    129:        if (fgets(lp, MAXLINE, ifp) == NULL)
                    130:                return (NULL);
                    131:        nf = nfield;
                    132:        nc = nchar;
                    133:        while (nf--) {
                    134:                while (*lp==' ' || *lp=='\t')
                    135:                        lp++;
                    136:                while (*lp!=' ' && *lp!='\t' && *lp!='\n' && *lp!='\0')
                    137:                        lp++;
                    138:        }
                    139:        if (nfield)
                    140:                while (*lp==' ' || *lp=='\t')
                    141:                        lp++;
                    142:        for (; nc--; lp++)
                    143:                if (*lp=='\n' || *lp=='\0')
                    144:                        break;
                    145:        return (lp);
                    146: }
                    147: 
                    148: usage()
                    149: {
                    150:        fprintf(stderr, "Usage: uniq [-udc] [+n] [-n] [ input [ output ] ]\n");
                    151:        exit(1);
                    152: }

unix.superglobalmegacorp.com

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