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

1.17    ! deraadt     1: /*     $OpenBSD: paste.c,v 1.16 2007/08/06 19:16:06 sobrado 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 *);
                     49: void   usage(void);
                     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;
                     59:
                     60:        seq = 0;
1.3       millert    61:        while ((ch = getopt(argc, argv, "d:s")) != -1)
1.1       deraadt    62:                switch(ch) {
                     63:                case 'd':
                     64:                        delimcnt = tr(delim = optarg);
                     65:                        break;
                     66:                case 's':
                     67:                        seq = 1;
                     68:                        break;
                     69:                case '?':
                     70:                default:
                     71:                        usage();
                     72:                }
                     73:        argc -= optind;
                     74:        argv += optind;
                     75:
                     76:        if (!delim) {
                     77:                delimcnt = 1;
                     78:                delim = "\t";
                     79:        }
                     80:
                     81:        if (seq)
                     82:                sequential(argv);
                     83:        else
                     84:                parallel(argv);
                     85:        exit(0);
                     86: }
                     87:
1.15      ray        88: struct list {
                     89:        SIMPLEQ_ENTRY(list) entries;
1.1       deraadt    90:        FILE *fp;
                     91:        int cnt;
                     92:        char *name;
1.15      ray        93: };
1.1       deraadt    94:
1.5       deraadt    95: void
1.12      deraadt    96: parallel(char **argv)
1.1       deraadt    97: {
1.15      ray        98:        SIMPLEQ_HEAD(, list) head = SIMPLEQ_HEAD_INITIALIZER(head);
                     99:        struct list *lp;
1.9       mpech     100:        int cnt;
                    101:        char ch, *p;
1.1       deraadt   102:        int opencnt, output;
1.7       aaron     103:        char *buf, *lbuf;
1.6       aaron     104:        size_t len;
1.1       deraadt   105:
1.15      ray       106:        for (cnt = 0; (p = *argv); ++argv, ++cnt) {
                    107:                if (!(lp = malloc(sizeof(struct list))))
1.14      mickey    108:                        err(1, "malloc");
                    109:
1.1       deraadt   110:                if (p[0] == '-' && !p[1])
                    111:                        lp->fp = stdin;
1.14      mickey    112:                else if (!(lp->fp = fopen(p, "r")))
                    113:                        err(1, "%s", p);
1.1       deraadt   114:                lp->cnt = cnt;
                    115:                lp->name = p;
1.15      ray       116:                SIMPLEQ_INSERT_TAIL(&head, lp, entries);
1.1       deraadt   117:        }
                    118:
                    119:        for (opencnt = cnt; opencnt;) {
1.15      ray       120:                output = 0;
                    121:                SIMPLEQ_FOREACH(lp, &head, entries) {
1.8       aaron     122:                        lbuf = NULL;
1.1       deraadt   123:                        if (!lp->fp) {
                    124:                                if (output && lp->cnt &&
                    125:                                    (ch = delim[(lp->cnt - 1) % delimcnt]))
                    126:                                        putchar(ch);
                    127:                                continue;
                    128:                        }
1.7       aaron     129:                        if (!(buf = fgetln(lp->fp, &len))) {
1.1       deraadt   130:                                if (!--opencnt)
                    131:                                        break;
                    132:                                lp->fp = NULL;
                    133:                                if (output && lp->cnt &&
                    134:                                    (ch = delim[(lp->cnt - 1) % delimcnt]))
                    135:                                        putchar(ch);
                    136:                                continue;
                    137:                        }
1.7       aaron     138:                        if (*(buf + len - 1) == '\n')
                    139:                                *(buf + len - 1) = '\0';
1.6       aaron     140:                        else {
1.8       aaron     141:                                if ((lbuf = malloc(len + 1)) == NULL)
1.14      mickey    142:                                        err(1, "malloc");
1.7       aaron     143:                                memcpy(lbuf, buf, len);
                    144:                                lbuf[len] = '\0';
                    145:                                buf = lbuf;
1.1       deraadt   146:                        }
                    147:                        /*
                    148:                         * make sure that we don't print any delimiters
                    149:                         * unless there's a non-empty file.
                    150:                         */
                    151:                        if (!output) {
                    152:                                output = 1;
                    153:                                for (cnt = 0; cnt < lp->cnt; ++cnt)
1.5       deraadt   154:                                        if ((ch = delim[cnt % delimcnt]))
1.1       deraadt   155:                                                putchar(ch);
1.5       deraadt   156:                        } else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
1.1       deraadt   157:                                putchar(ch);
1.7       aaron     158:                        (void)printf("%s", buf);
1.8       aaron     159:                        if (lbuf)
1.7       aaron     160:                                free(lbuf);
1.1       deraadt   161:                }
                    162:                if (output)
                    163:                        putchar('\n');
                    164:        }
                    165: }
                    166:
1.5       deraadt   167: void
1.12      deraadt   168: sequential(char **argv)
1.1       deraadt   169: {
1.9       mpech     170:        FILE *fp;
                    171:        int cnt;
                    172:        char ch, *p, *dp;
1.7       aaron     173:        char *buf, *lbuf;
                    174:        size_t len;
1.1       deraadt   175:
1.5       deraadt   176:        for (; (p = *argv); ++argv) {
1.8       aaron     177:                lbuf = NULL;
1.1       deraadt   178:                if (p[0] == '-' && !p[1])
                    179:                        fp = stdin;
                    180:                else if (!(fp = fopen(p, "r"))) {
1.14      mickey    181:                        warn("%s", p);
1.1       deraadt   182:                        continue;
                    183:                }
1.7       aaron     184:                if ((buf = fgetln(fp, &len))) {
1.1       deraadt   185:                        for (cnt = 0, dp = delim;;) {
1.7       aaron     186:                                if (*(buf + len - 1) == '\n')
                    187:                                        *(buf + len - 1) = '\0';
                    188:                                else {
1.8       aaron     189:                                        if ((lbuf = malloc(len + 1)) == NULL)
1.14      mickey    190:                                                err(1, "malloc");
1.7       aaron     191:                                        memcpy(lbuf, buf, len);
                    192:                                        lbuf[len] = '\0';
                    193:                                        buf = lbuf;
1.1       deraadt   194:                                }
                    195:                                (void)printf("%s", buf);
1.7       aaron     196:                                if (!(buf = fgetln(fp, &len)))
1.1       deraadt   197:                                        break;
1.5       deraadt   198:                                if ((ch = *dp++))
1.1       deraadt   199:                                        putchar(ch);
                    200:                                if (++cnt == delimcnt) {
                    201:                                        dp = delim;
                    202:                                        cnt = 0;
                    203:                                }
                    204:                        }
                    205:                        putchar('\n');
                    206:                }
                    207:                if (fp != stdin)
                    208:                        (void)fclose(fp);
1.8       aaron     209:                if (lbuf)
1.7       aaron     210:                        free(lbuf);
1.1       deraadt   211:        }
                    212: }
                    213:
1.5       deraadt   214: int
1.12      deraadt   215: tr(char *arg)
1.1       deraadt   216: {
1.9       mpech     217:        int cnt;
                    218:        char ch, *p;
1.1       deraadt   219:
                    220:        for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
                    221:                if (ch == '\\')
                    222:                        switch(ch = *p++) {
                    223:                        case 'n':
                    224:                                *arg = '\n';
                    225:                                break;
                    226:                        case 't':
                    227:                                *arg = '\t';
                    228:                                break;
                    229:                        case '0':
                    230:                                *arg = '\0';
                    231:                                break;
                    232:                        default:
                    233:                                *arg = ch;
                    234:                                break;
                    235:                } else
                    236:                        *arg = ch;
                    237:
1.14      mickey    238:        if (!cnt)
                    239:                errx(1, "no delimiters specified");
1.1       deraadt   240:        return(cnt);
                    241: }
                    242:
1.5       deraadt   243: void
1.12      deraadt   244: usage(void)
1.1       deraadt   245: {
1.14      mickey    246:        extern char *__progname;
1.16      sobrado   247:        (void)fprintf(stderr, "usage: %s [-s] [-d list] file ...\n",
                    248:            __progname);
1.1       deraadt   249:        exit(1);
                    250: }