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

Annotation of src/usr.bin/paste/paste.c, Revision 1.26

1.26    ! schwarze    1: /*     $OpenBSD: paste.c,v 1.25 2018/08/04 16:47:05 schwarze Exp $     */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1989 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Adam S. Moskowitz of Menlo Consulting.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.11      millert    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
1.15      ray        35: #include <sys/queue.h>
1.1       deraadt    36: #include <sys/types.h>
1.13      david      37: #include <err.h>
1.1       deraadt    38: #include <errno.h>
                     39: #include <limits.h>
                     40: #include <stdio.h>
1.6       aaron      41: #include <stdlib.h>
1.1       deraadt    42: #include <string.h>
1.6       aaron      43: #include <unistd.h>
1.1       deraadt    44:
                     45: char *delim;
                     46: int delimcnt;
                     47:
1.10      millert    48: int    tr(char *);
1.25      schwarze   49: __dead void usage(void);
1.10      millert    50: void   parallel(char **);
                     51: void   sequential(char **);
1.5       deraadt    52:
                     53: int
1.12      deraadt    54: main(int argc, char *argv[])
1.1       deraadt    55: {
                     56:        extern char *optarg;
                     57:        extern int optind;
                     58:        int ch, seq;
1.20      deraadt    59:
1.21      deraadt    60:        if (pledge("stdio rpath", NULL) == -1)
                     61:                err(1, "pledge");
1.1       deraadt    62:
                     63:        seq = 0;
1.18      tedu       64:        while ((ch = getopt(argc, argv, "d:s")) != -1) {
                     65:                switch (ch) {
1.1       deraadt    66:                case 'd':
                     67:                        delimcnt = tr(delim = optarg);
                     68:                        break;
                     69:                case 's':
                     70:                        seq = 1;
                     71:                        break;
                     72:                case '?':
                     73:                default:
                     74:                        usage();
                     75:                }
1.18      tedu       76:        }
1.1       deraadt    77:        argc -= optind;
                     78:        argv += optind;
1.23      guenther   79:
                     80:        if (argc == 0)
                     81:                usage();
1.1       deraadt    82:
1.25      schwarze   83:        if (delim == NULL) {
1.1       deraadt    84:                delimcnt = 1;
                     85:                delim = "\t";
                     86:        }
                     87:
                     88:        if (seq)
                     89:                sequential(argv);
                     90:        else
                     91:                parallel(argv);
1.25      schwarze   92:        return 0;
1.1       deraadt    93: }
                     94:
1.15      ray        95: struct list {
                     96:        SIMPLEQ_ENTRY(list) entries;
1.1       deraadt    97:        FILE *fp;
                     98:        int cnt;
                     99:        char *name;
1.15      ray       100: };
1.1       deraadt   101:
1.5       deraadt   102: void
1.12      deraadt   103: parallel(char **argv)
1.1       deraadt   104: {
1.15      ray       105:        SIMPLEQ_HEAD(, list) head = SIMPLEQ_HEAD_INITIALIZER(head);
                    106:        struct list *lp;
1.24      schwarze  107:        char *line, *p;
1.26    ! schwarze  108:        size_t linesize;
        !           109:        ssize_t len;
1.9       mpech     110:        int cnt;
1.1       deraadt   111:        int opencnt, output;
1.24      schwarze  112:        char ch;
1.1       deraadt   113:
1.25      schwarze  114:        for (cnt = 0; (p = *argv) != NULL; ++argv, ++cnt) {
                    115:                if ((lp = malloc(sizeof(*lp))) == NULL)
                    116:                        err(1, NULL);
1.14      mickey    117:
1.25      schwarze  118:                if (p[0] == '-' && p[1] == '\0')
1.1       deraadt   119:                        lp->fp = stdin;
1.25      schwarze  120:                else if ((lp->fp = fopen(p, "r")) == NULL)
1.14      mickey    121:                        err(1, "%s", p);
1.1       deraadt   122:                lp->cnt = cnt;
                    123:                lp->name = p;
1.15      ray       124:                SIMPLEQ_INSERT_TAIL(&head, lp, entries);
1.1       deraadt   125:        }
                    126:
1.24      schwarze  127:        line = NULL;
                    128:        linesize = 0;
                    129:
1.1       deraadt   130:        for (opencnt = cnt; opencnt;) {
1.15      ray       131:                output = 0;
                    132:                SIMPLEQ_FOREACH(lp, &head, entries) {
1.25      schwarze  133:                        if (lp->fp == NULL) {
1.1       deraadt   134:                                if (output && lp->cnt &&
                    135:                                    (ch = delim[(lp->cnt - 1) % delimcnt]))
                    136:                                        putchar(ch);
                    137:                                continue;
                    138:                        }
1.24      schwarze  139:                        if ((len = getline(&line, &linesize, lp->fp)) == -1) {
                    140:                                if (ferror(lp->fp))
                    141:                                        err(1, "%s", lp->fp == stdin ?
                    142:                                            "getline" : lp->name);
1.25      schwarze  143:                                if (--opencnt == 0)
1.1       deraadt   144:                                        break;
1.19      tobias    145:                                if (lp->fp != stdin)
1.25      schwarze  146:                                        fclose(lp->fp);
1.1       deraadt   147:                                lp->fp = NULL;
                    148:                                if (output && lp->cnt &&
                    149:                                    (ch = delim[(lp->cnt - 1) % delimcnt]))
                    150:                                        putchar(ch);
                    151:                                continue;
                    152:                        }
1.24      schwarze  153:                        if (line[len - 1] == '\n')
                    154:                                line[len - 1] = '\0';
1.1       deraadt   155:                        /*
                    156:                         * make sure that we don't print any delimiters
                    157:                         * unless there's a non-empty file.
                    158:                         */
                    159:                        if (!output) {
                    160:                                output = 1;
                    161:                                for (cnt = 0; cnt < lp->cnt; ++cnt)
1.5       deraadt   162:                                        if ((ch = delim[cnt % delimcnt]))
1.1       deraadt   163:                                                putchar(ch);
1.5       deraadt   164:                        } else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
1.1       deraadt   165:                                putchar(ch);
1.24      schwarze  166:                        fputs(line, stdout);
1.1       deraadt   167:                }
                    168:                if (output)
                    169:                        putchar('\n');
                    170:        }
1.24      schwarze  171:        free(line);
1.1       deraadt   172: }
                    173:
1.5       deraadt   174: void
1.12      deraadt   175: sequential(char **argv)
1.1       deraadt   176: {
1.9       mpech     177:        FILE *fp;
1.24      schwarze  178:        char *line, *p;
1.26    ! schwarze  179:        size_t linesize;
        !           180:        ssize_t len;
1.9       mpech     181:        int cnt;
1.1       deraadt   182:
1.24      schwarze  183:        line = NULL;
                    184:        linesize = 0;
1.25      schwarze  185:        for (; (p = *argv) != NULL; ++argv) {
                    186:                if (p[0] == '-' && p[1] == '\0')
1.1       deraadt   187:                        fp = stdin;
1.25      schwarze  188:                else if ((fp = fopen(p, "r")) == NULL) {
1.14      mickey    189:                        warn("%s", p);
1.1       deraadt   190:                        continue;
                    191:                }
1.24      schwarze  192:                cnt = -1;
                    193:                while ((len = getline(&line, &linesize, fp)) != -1) {
                    194:                        if (line[len - 1] == '\n')
                    195:                                line[len - 1] = '\0';
                    196:                        if (cnt >= 0)
                    197:                                putchar(delim[cnt]);
                    198:                        if (++cnt == delimcnt)
                    199:                                cnt = 0;
                    200:                        fputs(line, stdout);
                    201:                }
                    202:                if (ferror(fp))
                    203:                        err(1, "%s", fp == stdin ? "getline" : p);
                    204:                if (cnt >= 0)
1.1       deraadt   205:                        putchar('\n');
                    206:                if (fp != stdin)
1.25      schwarze  207:                        fclose(fp);
1.1       deraadt   208:        }
1.24      schwarze  209:        free(line);
1.1       deraadt   210: }
                    211:
1.5       deraadt   212: int
1.12      deraadt   213: tr(char *arg)
1.1       deraadt   214: {
1.9       mpech     215:        int cnt;
                    216:        char ch, *p;
1.1       deraadt   217:
1.25      schwarze  218:        for (p = arg, cnt = 0; (ch = *p++) != '\0'; ++arg, ++cnt) {
1.18      tedu      219:                if (ch == '\\') {
                    220:                        switch (ch = *p++) {
1.1       deraadt   221:                        case 'n':
                    222:                                *arg = '\n';
                    223:                                break;
                    224:                        case 't':
                    225:                                *arg = '\t';
                    226:                                break;
                    227:                        case '0':
                    228:                                *arg = '\0';
                    229:                                break;
                    230:                        default:
                    231:                                *arg = ch;
                    232:                                break;
1.18      tedu      233:                        }
1.1       deraadt   234:                } else
                    235:                        *arg = ch;
1.18      tedu      236:        }
1.1       deraadt   237:
1.25      schwarze  238:        if (cnt == 0)
1.14      mickey    239:                errx(1, "no delimiters specified");
1.25      schwarze  240:        return cnt;
1.1       deraadt   241: }
                    242:
1.25      schwarze  243: __dead void
1.12      deraadt   244: usage(void)
1.1       deraadt   245: {
1.14      mickey    246:        extern char *__progname;
1.25      schwarze  247:        fprintf(stderr, "usage: %s [-s] [-d list] file ...\n", __progname);
1.1       deraadt   248:        exit(1);
                    249: }