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
39 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/sh/redir.c,v 1.12.2.4 2002/08/27 01:36:28 tjr Exp $");
45 #include <sys/types.h>
55 * Code for dealing with input/output redirection.
69 #define EMPTY -2 /* marks an unused slot in redirtab */
70 #define PIPESIZE 4096 /* amount of buffering in a pipe */
75 struct redirtab *next;
80 MKINIT struct redirtab *redirlist;
83 * We keep track of whether or not fd0 has been redirected. This is for
84 * background commands, where we want to redirect fd0 to /dev/null only
85 * if it hasn't already been redirected.
87 int fd0_redirected = 0;
89 STATIC void openredirect(union node *, char[10 ]);
90 STATIC int openhere(union node *);
94 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
95 * old file descriptors are stashed away so that the redirection can be
96 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
97 * standard output, and the standard error if it becomes a duplicate of
98 * stdout, is saved in memory.
102 redirect(union node *redir, int flags)
105 struct redirtab *sv = NULL;
109 char memory[10]; /* file descriptors to write to memory */
111 for (i = 10 ; --i >= 0 ; )
113 memory[1] = flags & REDIR_BACKQ;
114 if (flags & REDIR_PUSH) {
115 sv = ckmalloc(sizeof (struct redirtab));
116 for (i = 0 ; i < 10 ; i++)
117 sv->renamed[i] = EMPTY;
118 sv->next = redirlist;
121 for (n = redir ; n ; n = n->nfile.next) {
124 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
126 continue; /* redirect from/to same file descriptor */
128 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
131 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
135 openredirect(n, memory);
142 error("%d: %s", fd, strerror(errno));
154 openredirect(n, memory);
164 openredirect(union node *redir, char memory[10])
167 int fd = redir->nfile.fd;
172 * We suppress interrupts so that we won't leave open file
173 * descriptors around. This may not be such a good idea because
174 * an open of a device or a fifo can block indefinitely.
178 switch (redir->nfile.type) {
180 fname = redir->nfile.expfname;
181 if ((f = open(fname, O_RDONLY)) < 0)
182 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
191 fname = redir->nfile.expfname;
193 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
194 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
196 if ((f = open(fname, O_RDWR, 0666)) < 0) {
198 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
199 else if ((f = creat(fname, 0666)) < 0)
200 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
203 if ((f = open(fname, O_RDWR)) < 0) {
204 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
212 fname = redir->nfile.expfname;
213 if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode))
214 error("cannot create %s: %s", fname,
215 errmsg(EEXIST, E_CREAT));
217 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
218 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
220 if ((f = creat(fname, 0666)) < 0)
221 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
225 fname = redir->nfile.expfname;
226 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
227 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
230 fname = redir->nfile.expfname;
232 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
233 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
235 if ((f = open(fname, O_WRONLY)) < 0
236 && (f = creat(fname, 0666)) < 0)
237 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
238 lseek(f, (off_t)0, 2);
243 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
244 if (memory[redir->ndup.dupfd])
248 copyfd(redir->ndup.dupfd, fd);
265 * Handle here documents. Normally we fork off a process to write the
266 * data to a pipe. If the document is short, we can stuff the data in
267 * the pipe without forking.
271 openhere(union node *redir)
277 error("Pipe call failed: %s", strerror(errno));
278 if (redir->type == NHERE) {
279 len = strlen(redir->nhere.doc->narg.text);
280 if (len <= PIPESIZE) {
281 xwrite(pip[1], redir->nhere.doc->narg.text, len);
285 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
287 signal(SIGINT, SIG_IGN);
288 signal(SIGQUIT, SIG_IGN);
289 signal(SIGHUP, SIG_IGN);
291 signal(SIGTSTP, SIG_IGN);
293 signal(SIGPIPE, SIG_DFL);
294 if (redir->type == NHERE)
295 xwrite(pip[1], redir->nhere.doc->narg.text, len);
297 expandhere(redir->nhere.doc, pip[1]);
308 * Undo the effects of the last redirection.
314 struct redirtab *rp = redirlist;
317 for (i = 0 ; i < 10 ; i++) {
318 if (rp->renamed[i] != EMPTY) {
322 if (rp->renamed[i] >= 0) {
323 copyfd(rp->renamed[i], i);
324 close(rp->renamed[i]);
329 redirlist = rp->next;
335 * Undo all redirections. Called on error or interrupt.
353 /* Return true if fd 0 has already been redirected at least once. */
355 fd0_redirected_p(void)
357 return fd0_redirected != 0;
361 * Discard all saved file descriptors.
370 for (rp = redirlist ; rp ; rp = rp->next) {
371 for (i = 0 ; i < 10 ; i++) {
372 if (rp->renamed[i] >= 0) {
373 close(rp->renamed[i]);
375 rp->renamed[i] = EMPTY;
383 * Copy a file descriptor to be >= to. Returns -1
384 * if the source file descriptor is closed, EMPTY if there are no unused
385 * file descriptors left.
389 copyfd(int from, int to)
393 newfd = fcntl(from, F_DUPFD, to);
398 error("%d: %s", from, strerror(errno));