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

1.21    ! deraadt     1: /*     $OpenBSD: uniq.c,v 1.20 2015/10/03 02:18:20 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:
1.15      ray        36: #include <ctype.h>
                     37: #include <err.h>
1.1       deraadt    38: #include <errno.h>
1.15      ray        39: #include <limits.h>
1.1       deraadt    40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
                     43: #include <unistd.h>
                     44:
                     45: #define        MAXLINELEN      (8 * 1024)
                     46:
                     47: int cflag, dflag, uflag;
                     48: int numchars, numfields, repeats;
                     49:
1.9       millert    50: FILE   *file(char *, char *);
                     51: void    show(FILE *, char *);
                     52: char   *skip(char *);
                     53: void    obsolete(char *[]);
1.15      ray        54: __dead void    usage(void);
1.1       deraadt    55:
                     56: int
1.10      deraadt    57: main(int argc, char *argv[])
1.1       deraadt    58: {
1.8       mpech      59:        char *t1, *t2;
1.10      deraadt    60:        FILE *ifp = NULL, *ofp = NULL;
1.1       deraadt    61:        int ch;
1.15      ray        62:        char *prevline, *thisline;
1.1       deraadt    63:
1.20      deraadt    64:        if (tame("stdio rpath wpath cpath", NULL) == -1)
                     65:                err(1, "tame");
                     66:
1.1       deraadt    67:        obsolete(argv);
1.15      ray        68:        while ((ch = getopt(argc, argv, "cdf:s:u")) != -1) {
                     69:                const char *errstr;
                     70:
1.1       deraadt    71:                switch (ch) {
                     72:                case 'c':
                     73:                        cflag = 1;
                     74:                        break;
                     75:                case 'd':
                     76:                        dflag = 1;
                     77:                        break;
                     78:                case 'f':
1.15      ray        79:                        numfields = (int)strtonum(optarg, 0, INT_MAX,
                     80:                            &errstr);
                     81:                        if (errstr)
                     82:                                errx(1, "field skip value is %s: %s",
                     83:                                    errstr, optarg);
1.1       deraadt    84:                        break;
                     85:                case 's':
1.15      ray        86:                        numchars = (int)strtonum(optarg, 0, INT_MAX,
                     87:                            &errstr);
                     88:                        if (errstr)
                     89:                                errx(1,
                     90:                                    "character skip value is %s: %s",
                     91:                                    errstr, optarg);
1.1       deraadt    92:                        break;
                     93:                case 'u':
                     94:                        uflag = 1;
                     95:                        break;
                     96:                default:
                     97:                        usage();
1.15      ray        98:                }
1.1       deraadt    99:        }
                    100:
1.11      millert   101:        argc -= optind;
1.15      ray       102:        argv += optind;
1.1       deraadt   103:
1.16      kili      104:        /* If neither -d nor -u are set, default is -d -u. */
                    105:        if (!dflag && !uflag)
1.1       deraadt   106:                dflag = uflag = 1;
                    107:
                    108:        switch(argc) {
                    109:        case 0:
                    110:                ifp = stdin;
                    111:                ofp = stdout;
                    112:                break;
                    113:        case 1:
                    114:                ifp = file(argv[0], "r");
                    115:                ofp = stdout;
                    116:                break;
                    117:        case 2:
                    118:                ifp = file(argv[0], "r");
                    119:                ofp = file(argv[1], "w");
                    120:                break;
                    121:        default:
                    122:                usage();
                    123:        }
1.20      deraadt   124:
1.21    ! deraadt   125:        if (tame("stdio", NULL) == -1)
1.20      deraadt   126:                err(1, "tame");
1.1       deraadt   127:
                    128:        prevline = malloc(MAXLINELEN);
                    129:        thisline = malloc(MAXLINELEN);
                    130:        if (prevline == NULL || thisline == NULL)
1.5       mickey    131:                err(1, "malloc");
1.1       deraadt   132:
                    133:        if (fgets(prevline, MAXLINELEN, ifp) == NULL)
                    134:                exit(0);
                    135:
                    136:        while (fgets(thisline, MAXLINELEN, ifp)) {
                    137:                /* If requested get the chosen fields + character offsets. */
                    138:                if (numfields || numchars) {
                    139:                        t1 = skip(thisline);
                    140:                        t2 = skip(prevline);
                    141:                } else {
                    142:                        t1 = thisline;
                    143:                        t2 = prevline;
                    144:                }
                    145:
                    146:                /* If different, print; set previous to new value. */
                    147:                if (strcmp(t1, t2)) {
                    148:                        show(ofp, prevline);
                    149:                        t1 = prevline;
                    150:                        prevline = thisline;
                    151:                        thisline = t1;
                    152:                        repeats = 0;
                    153:                } else
                    154:                        ++repeats;
                    155:        }
                    156:        show(ofp, prevline);
                    157:        exit(0);
                    158: }
                    159:
                    160: /*
                    161:  * show --
                    162:  *     Output a line depending on the flags and number of repetitions
                    163:  *     of the line.
                    164:  */
                    165: void
1.10      deraadt   166: show(FILE *ofp, char *str)
1.1       deraadt   167: {
1.16      kili      168:        if ((dflag && repeats) || (uflag && !repeats)) {
                    169:                if (cflag)
                    170:                        (void)fprintf(ofp, "%4d %s", repeats + 1, str);
                    171:                else
                    172:                        (void)fprintf(ofp, "%s", str);
                    173:        }
1.1       deraadt   174: }
                    175:
                    176: char *
1.10      deraadt   177: skip(char *str)
1.1       deraadt   178: {
1.16      kili      179:        int nchars, nfields;
1.1       deraadt   180:
1.16      kili      181:        for (nfields = numfields; nfields && *str; nfields--) {
1.19      deraadt   182:                while (isblank((unsigned char)*str))
1.16      kili      183:                        str++;
1.19      deraadt   184:                while (*str && !isblank((unsigned char)*str))
1.16      kili      185:                        str++;
                    186:        }
                    187:        for (nchars = numchars; nchars-- && *str && *str != '\n'; ++str)
1.15      ray       188:                ;
                    189:        return (str);
1.1       deraadt   190: }
                    191:
                    192: FILE *
1.10      deraadt   193: file(char *name, char *mode)
1.1       deraadt   194: {
                    195:        FILE *fp;
                    196:
1.12      millert   197:        if (strcmp(name, "-") == 0)
                    198:                return(*mode == 'r' ? stdin : stdout);
1.1       deraadt   199:        if ((fp = fopen(name, mode)) == NULL)
1.6       millert   200:                err(1, "%s", name);
1.15      ray       201:        return (fp);
1.1       deraadt   202: }
                    203:
                    204: void
1.10      deraadt   205: obsolete(char *argv[])
1.1       deraadt   206: {
1.15      ray       207:        size_t len;
1.1       deraadt   208:        char *ap, *p, *start;
                    209:
1.7       deraadt   210:        while ((ap = *++argv)) {
1.1       deraadt   211:                /* Return if "--" or not an option of any form. */
                    212:                if (ap[0] != '-') {
                    213:                        if (ap[0] != '+')
                    214:                                return;
                    215:                } else if (ap[1] == '-')
                    216:                        return;
                    217:                if (!isdigit(ap[1]))
                    218:                        continue;
                    219:                /*
                    220:                 * Digit signifies an old-style option.  Malloc space for dash,
                    221:                 * new option and argument.
                    222:                 */
1.13      deraadt   223:                len = strlen(ap) + 3;
                    224:                if ((start = p = malloc(len)) == NULL)
1.5       mickey    225:                        err(1, "malloc");
1.1       deraadt   226:                *p++ = '-';
                    227:                *p++ = ap[0] == '+' ? 's' : 'f';
1.13      deraadt   228:                (void)strlcpy(p, ap + 1, len - 2);
1.1       deraadt   229:                *argv = start;
                    230:        }
                    231: }
                    232:
1.15      ray       233: __dead void
1.10      deraadt   234: usage(void)
1.1       deraadt   235: {
1.15      ray       236:        extern char *__progname;
1.16      kili      237:
1.1       deraadt   238:        (void)fprintf(stderr,
1.17      kili      239:            "usage: %s [-c] [-d | -u] [-f fields] [-s chars] [input_file [output_file]]\n",
1.15      ray       240:            __progname);
1.1       deraadt   241:        exit(1);
                    242: }