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

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