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

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