2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)redir.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/redir.c,v 1.12.2.4 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/redir.c,v 1.3 2003/08/24 16:26:00 drhodus Exp $
41 #include <sys/types.h>
51 * Code for dealing with input/output redirection.
65 #define EMPTY -2 /* marks an unused slot in redirtab */
66 #define PIPESIZE 4096 /* amount of buffering in a pipe */
71 struct redirtab *next;
76 MKINIT struct redirtab *redirlist;
79 * We keep track of whether or not fd0 has been redirected. This is for
80 * background commands, where we want to redirect fd0 to /dev/null only
81 * if it hasn't already been redirected.
83 STATIC int fd0_redirected = 0;
85 STATIC void openredirect(union node *, char[10 ]);
86 STATIC int openhere(union node *);
90 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
91 * old file descriptors are stashed away so that the redirection can be
92 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
93 * standard output, and the standard error if it becomes a duplicate of
94 * stdout, is saved in memory.
98 redirect(union node *redir, int flags)
101 struct redirtab *sv = NULL;
105 char memory[10]; /* file descriptors to write to memory */
107 for (i = 10 ; --i >= 0 ; )
109 memory[1] = flags & REDIR_BACKQ;
110 if (flags & REDIR_PUSH) {
111 sv = ckmalloc(sizeof (struct redirtab));
112 for (i = 0 ; i < 10 ; i++)
113 sv->renamed[i] = EMPTY;
114 sv->next = redirlist;
117 for (n = redir ; n ; n = n->nfile.next) {
120 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
122 continue; /* redirect from/to same file descriptor */
124 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
127 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
131 openredirect(n, memory);
138 error("%d: %s", fd, strerror(errno));
150 openredirect(n, memory);
160 openredirect(union node *redir, char memory[10])
163 int fd = redir->nfile.fd;
168 * We suppress interrupts so that we won't leave open file
169 * descriptors around. This may not be such a good idea because
170 * an open of a device or a fifo can block indefinitely.
174 switch (redir->nfile.type) {
176 fname = redir->nfile.expfname;
177 if ((f = open(fname, O_RDONLY)) < 0)
178 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
187 fname = redir->nfile.expfname;
189 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
190 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
192 if ((f = open(fname, O_RDWR, 0666)) < 0) {
194 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
195 else if ((f = creat(fname, 0666)) < 0)
196 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
199 if ((f = open(fname, O_RDWR)) < 0) {
200 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
208 fname = redir->nfile.expfname;
209 if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode))
210 error("cannot create %s: %s", fname,
211 errmsg(EEXIST, E_CREAT));
213 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
214 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
216 if ((f = creat(fname, 0666)) < 0)
217 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
221 fname = redir->nfile.expfname;
222 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
223 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
226 fname = redir->nfile.expfname;
228 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
229 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
231 if ((f = open(fname, O_WRONLY)) < 0
232 && (f = creat(fname, 0666)) < 0)
233 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
234 lseek(f, (off_t)0, 2);
239 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
240 if (memory[redir->ndup.dupfd])
244 copyfd(redir->ndup.dupfd, fd);
261 * Handle here documents. Normally we fork off a process to write the
262 * data to a pipe. If the document is short, we can stuff the data in
263 * the pipe without forking.
267 openhere(union node *redir)
273 error("Pipe call failed: %s", strerror(errno));
274 if (redir->type == NHERE) {
275 len = strlen(redir->nhere.doc->narg.text);
276 if (len <= PIPESIZE) {
277 xwrite(pip[1], redir->nhere.doc->narg.text, len);
281 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
283 signal(SIGINT, SIG_IGN);
284 signal(SIGQUIT, SIG_IGN);
285 signal(SIGHUP, SIG_IGN);
287 signal(SIGTSTP, SIG_IGN);
289 signal(SIGPIPE, SIG_DFL);
290 if (redir->type == NHERE)
291 xwrite(pip[1], redir->nhere.doc->narg.text, len);
293 expandhere(redir->nhere.doc, pip[1]);
304 * Undo the effects of the last redirection.
310 struct redirtab *rp = redirlist;
313 for (i = 0 ; i < 10 ; i++) {
314 if (rp->renamed[i] != EMPTY) {
318 if (rp->renamed[i] >= 0) {
319 copyfd(rp->renamed[i], i);
320 close(rp->renamed[i]);
325 redirlist = rp->next;
331 * Undo all redirections. Called on error or interrupt.
349 /* Return true if fd 0 has already been redirected at least once. */
351 fd0_redirected_p(void)
353 return fd0_redirected != 0;
357 * Discard all saved file descriptors.
366 for (rp = redirlist ; rp ; rp = rp->next) {
367 for (i = 0 ; i < 10 ; i++) {
368 if (rp->renamed[i] >= 0) {
369 close(rp->renamed[i]);
371 rp->renamed[i] = EMPTY;
379 * Copy a file descriptor to be >= to. Returns -1
380 * if the source file descriptor is closed, EMPTY if there are no unused
381 * file descriptors left.
385 copyfd(int from, int to)
389 newfd = fcntl(from, F_DUPFD, to);
394 error("%d: %s", from, strerror(errno));