[BACK]Return to uniq.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / uniq

Annotation of src/usr.bin/uniq/uniq.c, Revision 1.14

1.14    ! millert     1: /*     $OpenBSD: uniq.c,v 1.13 2003/04/05 16:17:26 deraadt Exp $       */
1.1       deraadt     2: /*     $NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Case Larsen.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.14    ! millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1989, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: #if 0
                     44: static char sccsid[] = "@(#)uniq.c     8.3 (Berkeley) 5/4/95";
                     45: #endif
1.14    ! millert    46: static char rcsid[] = "$OpenBSD: uniq.c,v 1.13 2003/04/05 16:17:26 deraadt Exp $";
1.1       deraadt    47: #endif /* not lint */
                     48:
                     49: #include <errno.h>
                     50: #include <stdio.h>
                     51: #include <ctype.h>
                     52: #include <stdlib.h>
                     53: #include <string.h>
                     54: #include <unistd.h>
1.5       mickey     55: #include <err.h>
1.1       deraadt    56:
                     57: #define        MAXLINELEN      (8 * 1024)
                     58:
                     59: int cflag, dflag, uflag;
                     60: int numchars, numfields, repeats;
                     61:
1.9       millert    62: FILE   *file(char *, char *);
                     63: void    show(FILE *, char *);
                     64: char   *skip(char *);
                     65: void    obsolete(char *[]);
                     66: void    usage(void);
1.1       deraadt    67:
                     68: int
1.10      deraadt    69: main(int argc, char *argv[])
1.1       deraadt    70: {
1.8       mpech      71:        char *t1, *t2;
1.10      deraadt    72:        FILE *ifp = NULL, *ofp = NULL;
1.1       deraadt    73:        int ch;
                     74:        char *prevline, *thisline, *p;
                     75:
                     76:        obsolete(argv);
1.11      millert    77:        while ((ch = getopt(argc, argv, "cdf:s:u")) != -1)
1.1       deraadt    78:                switch (ch) {
                     79:                case 'c':
                     80:                        cflag = 1;
                     81:                        break;
                     82:                case 'd':
                     83:                        dflag = 1;
                     84:                        break;
                     85:                case 'f':
                     86:                        numfields = strtol(optarg, &p, 10);
                     87:                        if (numfields < 0 || *p)
1.5       mickey     88:                                errx(1, "illegal field skip value: %s", optarg);
1.1       deraadt    89:                        break;
                     90:                case 's':
                     91:                        numchars = strtol(optarg, &p, 10);
                     92:                        if (numchars < 0 || *p)
1.5       mickey     93:                                errx(1, "illegal character skip value: %s", optarg);
1.1       deraadt    94:                        break;
                     95:                case 'u':
                     96:                        uflag = 1;
                     97:                        break;
                     98:                case '?':
                     99:                default:
                    100:                        usage();
                    101:        }
                    102:
1.11      millert   103:        argc -= optind;
1.1       deraadt   104:        argv +=optind;
                    105:
                    106:        /* If no flags are set, default is -d -u. */
                    107:        if (cflag) {
                    108:                if (dflag || uflag)
                    109:                        usage();
                    110:        } else if (!dflag && !uflag)
                    111:                dflag = uflag = 1;
                    112:
                    113:        switch(argc) {
                    114:        case 0:
                    115:                ifp = stdin;
                    116:                ofp = stdout;
                    117:                break;
                    118:        case 1:
                    119:                ifp = file(argv[0], "r");
                    120:                ofp = stdout;
                    121:                break;
                    122:        case 2:
                    123:                ifp = file(argv[0], "r");
                    124:                ofp = file(argv[1], "w");
                    125:                break;
                    126:        default:
                    127:                usage();
                    128:        }
                    129:
                    130:        prevline = malloc(MAXLINELEN);
                    131:        thisline = malloc(MAXLINELEN);
                    132:        if (prevline == NULL || thisline == NULL)
1.5       mickey    133:                err(1, "malloc");
1.1       deraadt   134:
                    135:        if (fgets(prevline, MAXLINELEN, ifp) == NULL)
                    136:                exit(0);
                    137:
                    138:        while (fgets(thisline, MAXLINELEN, ifp)) {
                    139:                /* If requested get the chosen fields + character offsets. */
                    140:                if (numfields || numchars) {
                    141:                        t1 = skip(thisline);
                    142:                        t2 = skip(prevline);
                    143:                } else {
                    144:                        t1 = thisline;
                    145:                        t2 = prevline;
                    146:                }
                    147:
                    148:                /* If different, print; set previous to new value. */
                    149:                if (strcmp(t1, t2)) {
                    150:                        show(ofp, prevline);
                    151:                        t1 = prevline;
                    152:                        prevline = thisline;
                    153:                        thisline = t1;
                    154:                        repeats = 0;
                    155:                } else
                    156:                        ++repeats;
                    157:        }
                    158:        show(ofp, prevline);
                    159:        exit(0);
                    160: }
                    161:
                    162: /*
                    163:  * show --
                    164:  *     Output a line depending on the flags and number of repetitions
                    165:  *     of the line.
                    166:  */
                    167: void
1.10      deraadt   168: show(FILE *ofp, char *str)
1.1       deraadt   169: {
                    170:
                    171:        if (cflag && *str)
                    172:                (void)fprintf(ofp, "%4d %s", repeats + 1, str);
                    173:        if (dflag && repeats || uflag && !repeats)
                    174:                (void)fprintf(ofp, "%s", str);
                    175: }
                    176:
                    177: char *
1.10      deraadt   178: skip(char *str)
1.1       deraadt   179: {
1.8       mpech     180:        int infield, nchars, nfields;
1.1       deraadt   181:
                    182:        for (nfields = numfields, infield = 0; nfields && *str; ++str)
                    183:                if (isspace(*str)) {
                    184:                        if (infield) {
                    185:                                infield = 0;
                    186:                                --nfields;
                    187:                        }
                    188:                } else if (!infield)
                    189:                        infield = 1;
                    190:        for (nchars = numchars; nchars-- && *str; ++str);
                    191:        return(str);
                    192: }
                    193:
                    194: FILE *
1.10      deraadt   195: file(char *name, char *mode)
1.1       deraadt   196: {
                    197:        FILE *fp;
                    198:
1.12      millert   199:        if (strcmp(name, "-") == 0)
                    200:                return(*mode == 'r' ? stdin : stdout);
1.1       deraadt   201:        if ((fp = fopen(name, mode)) == NULL)
1.6       millert   202:                err(1, "%s", name);
1.1       deraadt   203:        return(fp);
                    204: }
                    205:
                    206: void
1.10      deraadt   207: obsolete(char *argv[])
1.1       deraadt   208: {
                    209:        int len;
                    210:        char *ap, *p, *start;
                    211:
1.7       deraadt   212:        while ((ap = *++argv)) {
1.1       deraadt   213:                /* Return if "--" or not an option of any form. */
                    214:                if (ap[0] != '-') {
                    215:                        if (ap[0] != '+')
                    216:                                return;
                    217:                } else if (ap[1] == '-')
                    218:                        return;
                    219:                if (!isdigit(ap[1]))
                    220:                        continue;
                    221:                /*
                    222:                 * Digit signifies an old-style option.  Malloc space for dash,
                    223:                 * new option and argument.
                    224:                 */
1.13      deraadt   225:                len = strlen(ap) + 3;
                    226:                if ((start = p = malloc(len)) == NULL)
1.5       mickey    227:                        err(1, "malloc");
1.1       deraadt   228:                *p++ = '-';
                    229:                *p++ = ap[0] == '+' ? 's' : 'f';
1.13      deraadt   230:                (void)strlcpy(p, ap + 1, len - 2);
1.1       deraadt   231:                *argv = start;
                    232:        }
                    233: }
                    234:
                    235: void
1.10      deraadt   236: usage(void)
1.1       deraadt   237: {
                    238:        (void)fprintf(stderr,
                    239:            "usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n");
                    240:        exit(1);
                    241: }