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

Annotation of src/usr.bin/tee/tee.c, Revision 1.12

1.12    ! bluhm       1: /*     $OpenBSD: tee.c,v 1.11 2016/10/28 07:22:59 schwarze Exp $       */
1.1       deraadt     2: /*     $NetBSD: tee.c,v 1.5 1994/12/09 01:43:39 jtc Exp $      */
                      3:
                      4: /*
                      5:  * Copyright (c) 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.5       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/types.h>
                     34: #include <sys/stat.h>
1.12    ! bluhm      35: #include <sys/queue.h>
1.11      schwarze   36:
                     37: #include <err.h>
1.1       deraadt    38: #include <errno.h>
                     39: #include <fcntl.h>
1.11      schwarze   40: #include <signal.h>
1.1       deraadt    41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
1.11      schwarze   44: #include <unistd.h>
1.1       deraadt    45:
1.8       tedu       46: struct list {
1.12    ! bluhm      47:        SLIST_ENTRY(list) next;
1.1       deraadt    48:        int fd;
                     49:        char *name;
1.8       tedu       50: };
1.12    ! bluhm      51: SLIST_HEAD(, list) head;
1.1       deraadt    52:
1.8       tedu       53: static void
                     54: add(int fd, char *name)
                     55: {
                     56:        struct list *p;
                     57:
                     58:        if ((p = malloc(sizeof(*p))) == NULL)
                     59:                err(1, NULL);
                     60:        p->fd = fd;
                     61:        p->name = name;
1.12    ! bluhm      62:        SLIST_INSERT_HEAD(&head, p, next);
1.8       tedu       63: }
1.1       deraadt    64:
                     65: int
1.6       deraadt    66: main(int argc, char *argv[])
1.1       deraadt    67: {
1.8       tedu       68:        struct list *p;
                     69:        int fd;
                     70:        ssize_t n, rval, wval;
1.3       mpech      71:        char *bp;
1.1       deraadt    72:        int append, ch, exitval;
1.8       tedu       73:        char buf[8192];
1.1       deraadt    74:
1.10      deraadt    75:        if (pledge("stdio wpath cpath", NULL) == -1)
                     76:                err(1, "pledge");
1.9       deraadt    77:
1.12    ! bluhm      78:        SLIST_INIT(&head);
        !            79:
1.1       deraadt    80:        append = 0;
1.8       tedu       81:        while ((ch = getopt(argc, argv, "ai")) != -1) {
                     82:                switch(ch) {
1.1       deraadt    83:                case 'a':
                     84:                        append = 1;
                     85:                        break;
                     86:                case 'i':
                     87:                        (void)signal(SIGINT, SIG_IGN);
                     88:                        break;
                     89:                default:
                     90:                        (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
1.11      schwarze   91:                        return 1;
1.1       deraadt    92:                }
1.8       tedu       93:        }
1.1       deraadt    94:        argv += optind;
                     95:        argc -= optind;
                     96:
                     97:        add(STDOUT_FILENO, "stdout");
                     98:
1.8       tedu       99:        exitval = 0;
                    100:        while (*argv) {
                    101:                if ((fd = open(*argv, O_WRONLY | O_CREAT |
                    102:                    (append ? O_APPEND : O_TRUNC), DEFFILEMODE)) == -1) {
1.1       deraadt   103:                        warn("%s", *argv);
                    104:                        exitval = 1;
                    105:                } else
                    106:                        add(fd, *argv);
1.8       tedu      107:                argv++;
                    108:        }
1.9       deraadt   109:
1.10      deraadt   110:        if (pledge("stdio", NULL) == -1)
                    111:                err(1, "pledge");
1.1       deraadt   112:
1.8       tedu      113:        while ((rval = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
1.12    ! bluhm     114:                SLIST_FOREACH(p, &head, next) {
1.1       deraadt   115:                        n = rval;
                    116:                        bp = buf;
                    117:                        do {
                    118:                                if ((wval = write(p->fd, bp, n)) == -1) {
                    119:                                        warn("%s", p->name);
                    120:                                        exitval = 1;
                    121:                                        break;
                    122:                                }
                    123:                                bp += wval;
                    124:                        } while (n -= wval);
                    125:                }
1.8       tedu      126:        }
                    127:        if (rval == -1) {
1.1       deraadt   128:                warn("read");
                    129:                exitval = 1;
                    130:        }
                    131:
1.12    ! bluhm     132:        SLIST_FOREACH(p, &head, next) {
1.1       deraadt   133:                if (close(p->fd) == -1) {
                    134:                        warn("%s", p->name);
                    135:                        exitval = 1;
                    136:                }
                    137:        }
                    138:
1.11      schwarze  139:        return exitval;
1.1       deraadt   140: }