[BACK]Return to cut.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cut

Annotation of src/usr.bin/cut/cut.c, Revision 1.17

1.17    ! sobrado     1: /*     $OpenBSD: cut.c,v 1.16 2013/11/23 17:30:29 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: cut.c,v 1.9 1995/09/02 05:59:23 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:  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
                     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.10      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:
                     36: #include <ctype.h>
                     37: #include <err.h>
                     38: #include <errno.h>
                     39: #include <limits.h>
                     40: #include <locale.h>
                     41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
                     44: #include <unistd.h>
                     45:
                     46: int    cflag;
                     47: char   dchar;
                     48: int    dflag;
                     49: int    fflag;
                     50: int    sflag;
                     51:
1.9       millert    52: void   c_cut(FILE *, char *);
                     53: void   f_cut(FILE *, char *);
                     54: void   get_list(char *);
                     55: void   usage(void);
1.1       deraadt    56:
                     57: int
1.11      deraadt    58: main(int argc, char *argv[])
1.1       deraadt    59: {
                     60:        FILE *fp;
1.9       millert    61:        void (*fcn)(FILE *, char *);
1.17    ! sobrado    62:        int ch, eval = 0;
1.1       deraadt    63:
                     64:        setlocale (LC_ALL, "");
                     65:
                     66:        dchar = '\t';                   /* default delimiter is \t */
                     67:
                     68:        /* Since we don't support multi-byte characters, the -c and -b
                     69:           options are equivalent, and the -n option is meaningless. */
1.3       millert    70:        while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
1.1       deraadt    71:                switch(ch) {
                     72:                case 'b':
                     73:                case 'c':
                     74:                        fcn = c_cut;
                     75:                        get_list(optarg);
                     76:                        cflag = 1;
                     77:                        break;
                     78:                case 'd':
                     79:                        dchar = *optarg;
                     80:                        dflag = 1;
                     81:                        break;
                     82:                case 'f':
                     83:                        get_list(optarg);
                     84:                        fcn = f_cut;
                     85:                        fflag = 1;
                     86:                        break;
                     87:                case 's':
                     88:                        sflag = 1;
                     89:                        break;
                     90:                case 'n':
                     91:                        break;
                     92:                case '?':
                     93:                default:
                     94:                        usage();
                     95:                }
                     96:        argc -= optind;
                     97:        argv += optind;
                     98:
                     99:        if (fflag) {
                    100:                if (cflag)
                    101:                        usage();
                    102:        } else if (!cflag || dflag || sflag)
                    103:                usage();
                    104:
                    105:        if (*argv)
                    106:                for (; *argv; ++argv) {
1.17    ! sobrado   107:                        if (strcmp(*argv, "-") == 0)
        !           108:                                fcn(stdin, "stdin");
        !           109:                        else {
        !           110:                                if ((fp = fopen(*argv, "r"))) {
        !           111:                                        fcn(fp, *argv);
        !           112:                                        (void)fclose(fp);
        !           113:                                } else {
        !           114:                                        eval = 1;
        !           115:                                        warn("%s", *argv);
        !           116:                                }
        !           117:                        }
1.1       deraadt   118:                }
                    119:        else
                    120:                fcn(stdin, "stdin");
1.17    ! sobrado   121:        exit(eval);
1.1       deraadt   122: }
                    123:
                    124: int autostart, autostop, maxval;
                    125:
                    126: char positions[_POSIX2_LINE_MAX + 1];
                    127:
                    128: void
1.11      deraadt   129: get_list(char *list)
1.1       deraadt   130: {
1.8       mpech     131:        int setautostart, start, stop;
                    132:        char *pos;
1.1       deraadt   133:        char *p;
                    134:
                    135:        /*
                    136:         * set a byte in the positions array to indicate if a field or
                    137:         * column is to be selected; use +1, it's 1-based, not 0-based.
                    138:         * This parser is less restrictive than the Draft 9 POSIX spec.
                    139:         * POSIX doesn't allow lists that aren't in increasing order or
                    140:         * overlapping lists.  We also handle "-3-5" although there's no
                    141:         * real reason too.
                    142:         */
1.7       aaron     143:        while ((p = strsep(&list, ", \t"))) {
1.1       deraadt   144:                setautostart = start = stop = 0;
                    145:                if (*p == '-') {
                    146:                        ++p;
                    147:                        setautostart = 1;
                    148:                }
1.16      deraadt   149:                if (isdigit((unsigned char)*p)) {
1.1       deraadt   150:                        start = stop = strtol(p, &p, 10);
                    151:                        if (setautostart && start > autostart)
                    152:                                autostart = start;
                    153:                }
                    154:                if (*p == '-') {
1.16      deraadt   155:                        if (isdigit((unsigned char)p[1]))
1.1       deraadt   156:                                stop = strtol(p + 1, &p, 10);
                    157:                        if (*p == '-') {
                    158:                                ++p;
                    159:                                if (!autostop || autostop > stop)
                    160:                                        autostop = stop;
                    161:                        }
                    162:                }
                    163:                if (*p)
1.15      schwarze  164:                        errx(1, "[-bcf] list: illegal list value");
1.1       deraadt   165:                if (!stop || !start)
1.15      schwarze  166:                        errx(1, "[-bcf] list: values may not include zero");
1.1       deraadt   167:                if (stop > _POSIX2_LINE_MAX)
1.15      schwarze  168:                        errx(1, "[-bcf] list: %d too large (max %d)",
1.1       deraadt   169:                            stop, _POSIX2_LINE_MAX);
                    170:                if (maxval < stop)
                    171:                        maxval = stop;
1.7       aaron     172:                for (pos = positions + start; start++ <= stop; *pos++ = 1)
                    173:                        ;
1.1       deraadt   174:        }
                    175:
                    176:        /* overlapping ranges */
                    177:        if (autostop && maxval > autostop)
                    178:                maxval = autostop;
                    179:
                    180:        /* set autostart */
                    181:        if (autostart)
                    182:                memset(positions + 1, '1', autostart);
                    183: }
                    184:
                    185: /* ARGSUSED */
                    186: void
1.11      deraadt   187: c_cut(FILE *fp, char *fname)
1.1       deraadt   188: {
1.8       mpech     189:        int ch, col;
                    190:        char *pos;
1.1       deraadt   191:
                    192:        for (;;) {
                    193:                pos = positions + 1;
                    194:                for (col = maxval; col; --col) {
                    195:                        if ((ch = getc(fp)) == EOF)
                    196:                                return;
                    197:                        if (ch == '\n')
                    198:                                break;
                    199:                        if (*pos++)
                    200:                                (void)putchar(ch);
                    201:                }
1.7       aaron     202:                if (ch != '\n') {
1.1       deraadt   203:                        if (autostop)
                    204:                                while ((ch = getc(fp)) != EOF && ch != '\n')
                    205:                                        (void)putchar(ch);
                    206:                        else
1.7       aaron     207:                                while ((ch = getc(fp)) != EOF && ch != '\n')
                    208:                                        ;
                    209:                }
1.1       deraadt   210:                (void)putchar('\n');
                    211:        }
                    212: }
                    213:
                    214: void
1.11      deraadt   215: f_cut(FILE *fp, char *fname)
1.1       deraadt   216: {
1.8       mpech     217:        int ch, field, isdelim;
                    218:        char *pos, *p, sep;
1.1       deraadt   219:        int output;
1.7       aaron     220:        size_t len;
                    221:        char *lbuf, *tbuf;
1.1       deraadt   222:
1.7       aaron     223:        for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len));) {
1.1       deraadt   224:                output = 0;
1.7       aaron     225:                if (lbuf[len - 1] != '\n') {
                    226:                        /* no newline at the end of the last line so add one */
                    227:                        if ((tbuf = (char *)malloc(len + 1)) == NULL)
                    228:                                err(1, NULL);
                    229:                        memcpy(tbuf, lbuf, len);
                    230:                        tbuf[len] = '\n';
                    231:                        lbuf = tbuf;
                    232:                }
1.1       deraadt   233:                for (isdelim = 0, p = lbuf;; ++p) {
1.7       aaron     234:                        ch = *p;
1.1       deraadt   235:                        /* this should work if newline is delimiter */
                    236:                        if (ch == sep)
                    237:                                isdelim = 1;
                    238:                        if (ch == '\n') {
                    239:                                if (!isdelim && !sflag)
1.7       aaron     240:                                        (void)fwrite(lbuf, len, 1, stdout);
1.1       deraadt   241:                                break;
                    242:                        }
                    243:                }
                    244:                if (!isdelim)
                    245:                        continue;
                    246:
                    247:                pos = positions + 1;
                    248:                for (field = maxval, p = lbuf; field; --field, ++pos) {
                    249:                        if (*pos) {
                    250:                                if (output++)
                    251:                                        (void)putchar(sep);
                    252:                                while ((ch = *p++) != '\n' && ch != sep)
                    253:                                        (void)putchar(ch);
                    254:                        } else
1.7       aaron     255:                                while ((ch = *p++) != '\n' && ch != sep)
                    256:                                        ;
1.1       deraadt   257:                        if (ch == '\n')
                    258:                                break;
                    259:                }
1.7       aaron     260:                if (ch != '\n') {
1.1       deraadt   261:                        if (autostop) {
                    262:                                if (output)
                    263:                                        (void)putchar(sep);
                    264:                                for (; (ch = *p) != '\n'; ++p)
                    265:                                        (void)putchar(ch);
                    266:                        } else
1.7       aaron     267:                                for (; (ch = *p) != '\n'; ++p)
                    268:                                        ;
                    269:                }
1.1       deraadt   270:                (void)putchar('\n');
                    271:        }
1.7       aaron     272:        if (tbuf)
                    273:                free(tbuf);
1.1       deraadt   274: }
                    275:
                    276: void
1.11      deraadt   277: usage(void)
1.1       deraadt   278: {
                    279:        (void)fprintf(stderr,
1.13      sobrado   280:            "usage: cut -b list [-n] [file ...]\n"
                    281:            "       cut -c list [file ...]\n"
                    282:            "       cut -f list [-s] [-d delim] [file ...]\n");
1.1       deraadt   283:        exit(1);
                    284: }