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