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

1.31    ! cheloha     1: /*     $OpenBSD: uniq.c,v 1.30 2021/12/24 17:59:28 cheloha 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.24      schwarze   40: #include <locale.h>
1.1       deraadt    41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
                     44: #include <unistd.h>
1.24      schwarze   45: #include <wchar.h>
                     46: #include <wctype.h>
1.1       deraadt    47:
1.25      tb         48: int cflag, dflag, iflag, uflag;
1.1       deraadt    49: int numchars, numfields, repeats;
                     50:
1.31    ! cheloha    51: void    show(const char *);
1.9       millert    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.30      cheloha    59:        char *p, *prevline, *t, *thisline, *tmp;
1.28      cheloha    60:        size_t prevsize, thissize, tmpsize;
1.29      cheloha    61:        ssize_t len;
1.1       deraadt    62:        int ch;
                     63:
1.24      schwarze   64:        setlocale(LC_CTYPE, "");
                     65:
1.22      deraadt    66:        if (pledge("stdio rpath wpath cpath", NULL) == -1)
                     67:                err(1, "pledge");
1.20      deraadt    68:
1.1       deraadt    69:        obsolete(argv);
1.25      tb         70:        while ((ch = getopt(argc, argv, "cdf:is:u")) != -1) {
1.15      ray        71:                const char *errstr;
                     72:
1.1       deraadt    73:                switch (ch) {
                     74:                case 'c':
                     75:                        cflag = 1;
                     76:                        break;
                     77:                case 'd':
                     78:                        dflag = 1;
                     79:                        break;
                     80:                case 'f':
1.15      ray        81:                        numfields = (int)strtonum(optarg, 0, INT_MAX,
                     82:                            &errstr);
                     83:                        if (errstr)
                     84:                                errx(1, "field skip value is %s: %s",
                     85:                                    errstr, optarg);
1.1       deraadt    86:                        break;
1.25      tb         87:                case 'i':
                     88:                        iflag = 1;
                     89:                        break;
1.1       deraadt    90:                case 's':
1.15      ray        91:                        numchars = (int)strtonum(optarg, 0, INT_MAX,
                     92:                            &errstr);
                     93:                        if (errstr)
                     94:                                errx(1,
                     95:                                    "character skip value is %s: %s",
                     96:                                    errstr, optarg);
1.1       deraadt    97:                        break;
                     98:                case 'u':
                     99:                        uflag = 1;
                    100:                        break;
                    101:                default:
                    102:                        usage();
1.15      ray       103:                }
1.1       deraadt   104:        }
                    105:
1.11      millert   106:        argc -= optind;
1.15      ray       107:        argv += optind;
1.1       deraadt   108:
1.16      kili      109:        /* If neither -d nor -u are set, default is -d -u. */
                    110:        if (!dflag && !uflag)
1.1       deraadt   111:                dflag = uflag = 1;
                    112:
1.31    ! cheloha   113:        if (argc > 2)
1.1       deraadt   114:                usage();
1.31    ! cheloha   115:        if (argc >= 1 && strcmp(argv[0], "-") != 0) {
        !           116:                if (freopen(argv[0], "r", stdin) == NULL)
        !           117:                        err(1, "%s", argv[0]);
        !           118:        }
        !           119:        if (argc == 2 && strcmp(argv[1], "-") != 0) {
        !           120:                if (freopen(argv[1], "w", stdout) == NULL)
        !           121:                        err(1, "%s", argv[1]);
1.1       deraadt   122:        }
1.20      deraadt   123:
1.22      deraadt   124:        if (pledge("stdio", NULL) == -1)
                    125:                err(1, "pledge");
1.1       deraadt   126:
1.28      cheloha   127:        prevsize = 0;
                    128:        prevline = NULL;
1.31    ! cheloha   129:        if ((len = getline(&prevline, &prevsize, stdin)) == -1) {
1.28      cheloha   130:                free(prevline);
1.31    ! cheloha   131:                if (ferror(stdin))
1.28      cheloha   132:                        err(1, "getline");
1.1       deraadt   133:                exit(0);
1.28      cheloha   134:        }
1.29      cheloha   135:        if (prevline[len - 1] == '\n')
                    136:                prevline[len - 1] = '\0';
1.30      cheloha   137:        if (numfields || numchars)
                    138:                p = skip(prevline);
                    139:        else
                    140:                p = prevline;
1.28      cheloha   141:
                    142:        thissize = 0;
                    143:        thisline = NULL;
1.31    ! cheloha   144:        while ((len = getline(&thisline, &thissize, stdin)) != -1) {
1.29      cheloha   145:                if (thisline[len - 1] == '\n')
                    146:                        thisline[len - 1] = '\0';
                    147:
1.1       deraadt   148:                /* If requested get the chosen fields + character offsets. */
1.30      cheloha   149:                if (numfields || numchars)
                    150:                        t = skip(thisline);
                    151:                else
                    152:                        t = thisline;
1.1       deraadt   153:
                    154:                /* If different, print; set previous to new value. */
1.30      cheloha   155:                if ((iflag ? strcasecmp : strcmp)(p, t)) {
1.31    ! cheloha   156:                        show(prevline);
1.30      cheloha   157:                        tmp = prevline;
1.1       deraadt   158:                        prevline = thisline;
1.30      cheloha   159:                        thisline = tmp;
                    160:                        tmp = p;
                    161:                        p = t;
                    162:                        t = tmp;
1.28      cheloha   163:                        tmpsize = prevsize;
                    164:                        prevsize = thissize;
                    165:                        thissize = tmpsize;
1.1       deraadt   166:                        repeats = 0;
                    167:                } else
                    168:                        ++repeats;
                    169:        }
1.28      cheloha   170:        free(thisline);
1.31    ! cheloha   171:        if (ferror(stdin))
1.28      cheloha   172:                err(1, "getline");
                    173:
1.31    ! cheloha   174:        show(prevline);
1.28      cheloha   175:        free(prevline);
                    176:
1.1       deraadt   177:        exit(0);
                    178: }
                    179:
                    180: /*
                    181:  * show --
                    182:  *     Output a line depending on the flags and number of repetitions
                    183:  *     of the line.
                    184:  */
                    185: void
1.31    ! cheloha   186: show(const char *str)
1.1       deraadt   187: {
1.16      kili      188:        if ((dflag && repeats) || (uflag && !repeats)) {
                    189:                if (cflag)
1.31    ! cheloha   190:                        printf("%4d %s\n", repeats + 1, str);
1.16      kili      191:                else
1.31    ! cheloha   192:                        printf("%s\n", str);
1.16      kili      193:        }
1.1       deraadt   194: }
                    195:
                    196: char *
1.10      deraadt   197: skip(char *str)
1.1       deraadt   198: {
1.24      schwarze  199:        wchar_t wc;
1.16      kili      200:        int nchars, nfields;
1.24      schwarze  201:        int len;
                    202:        int field_started;
1.1       deraadt   203:
1.16      kili      204:        for (nfields = numfields; nfields && *str; nfields--) {
1.24      schwarze  205:                /* Skip one field, including preceding blanks. */
                    206:                for (field_started = 0; *str != '\0'; str += len) {
                    207:                        if ((len = mbtowc(&wc, str, MB_CUR_MAX)) == -1) {
                    208:                                (void)mbtowc(NULL, NULL, MB_CUR_MAX);
                    209:                                wc = L'?';
                    210:                                len = 1;
                    211:                        }
                    212:                        if (iswblank(wc)) {
                    213:                                if (field_started)
                    214:                                        break;
                    215:                        } else
                    216:                                field_started = 1;
                    217:                }
1.16      kili      218:        }
1.24      schwarze  219:
                    220:        /* Skip some additional characters. */
                    221:        for (nchars = numchars; nchars-- && *str != '\0'; str += len)
                    222:                if ((len = mblen(str, MB_CUR_MAX)) == -1)
                    223:                        len = 1;
                    224:
1.15      ray       225:        return (str);
1.1       deraadt   226: }
                    227:
                    228: void
1.10      deraadt   229: obsolete(char *argv[])
1.1       deraadt   230: {
1.15      ray       231:        size_t len;
1.1       deraadt   232:        char *ap, *p, *start;
                    233:
1.7       deraadt   234:        while ((ap = *++argv)) {
1.1       deraadt   235:                /* Return if "--" or not an option of any form. */
                    236:                if (ap[0] != '-') {
                    237:                        if (ap[0] != '+')
                    238:                                return;
                    239:                } else if (ap[1] == '-')
                    240:                        return;
1.23      mmcc      241:                if (!isdigit((unsigned char)ap[1]))
1.1       deraadt   242:                        continue;
                    243:                /*
                    244:                 * Digit signifies an old-style option.  Malloc space for dash,
                    245:                 * new option and argument.
                    246:                 */
1.13      deraadt   247:                len = strlen(ap) + 3;
                    248:                if ((start = p = malloc(len)) == NULL)
1.5       mickey    249:                        err(1, "malloc");
1.1       deraadt   250:                *p++ = '-';
                    251:                *p++ = ap[0] == '+' ? 's' : 'f';
1.13      deraadt   252:                (void)strlcpy(p, ap + 1, len - 2);
1.1       deraadt   253:                *argv = start;
                    254:        }
                    255: }
                    256:
1.15      ray       257: __dead void
1.10      deraadt   258: usage(void)
1.1       deraadt   259: {
1.15      ray       260:        extern char *__progname;
1.16      kili      261:
1.1       deraadt   262:        (void)fprintf(stderr,
1.25      tb        263:            "usage: %s [-ci] [-d | -u] [-f fields] [-s chars] [input_file [output_file]]\n",
1.15      ray       264:            __progname);
1.1       deraadt   265:        exit(1);
                    266: }