[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.13

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