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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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
35 static char sccsid
[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/redir.c,v 1.26 2004/04/06 20:06:51 markm Exp $");
43 #include <sys/types.h>
53 * Code for dealing with input/output redirection.
67 #define EMPTY -2 /* marks an unused slot in redirtab */
68 #define PIPESIZE 4096 /* amount of buffering in a pipe */
73 struct redirtab
*next
;
78 MKINIT
struct redirtab
*redirlist
;
81 * We keep track of whether or not fd0 has been redirected. This is for
82 * background commands, where we want to redirect fd0 to /dev/null only
83 * if it hasn't already been redirected.
85 STATIC
int fd0_redirected
= 0;
87 STATIC
void openredirect(union node
*, char[10 ]);
88 STATIC
int openhere(union node
*);
92 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
93 * old file descriptors are stashed away so that the redirection can be
94 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
95 * standard output, and the standard error if it becomes a duplicate of
96 * stdout, is saved in memory.
100 redirect(union node
*redir
, int flags
)
103 struct redirtab
*sv
= NULL
;
107 char memory
[10]; /* file descriptors to write to memory */
109 for (i
= 10 ; --i
>= 0 ; )
111 memory
[1] = flags
& REDIR_BACKQ
;
112 if (flags
& REDIR_PUSH
) {
113 sv
= ckmalloc(sizeof (struct redirtab
));
114 for (i
= 0 ; i
< 10 ; i
++)
115 sv
->renamed
[i
] = EMPTY
;
116 sv
->next
= redirlist
;
119 for (n
= redir
; n
; n
= n
->nfile
.next
) {
122 if ((n
->nfile
.type
== NTOFD
|| n
->nfile
.type
== NFROMFD
) &&
124 continue; /* redirect from/to same file descriptor */
126 if ((flags
& REDIR_PUSH
) && sv
->renamed
[fd
] == EMPTY
) {
129 if ((i
= fcntl(fd
, F_DUPFD
, 10)) == -1) {
133 openredirect(n
, memory
);
140 error("%d: %s", fd
, strerror(errno
));
152 openredirect(n
, memory
);
162 openredirect(union node
*redir
, char memory
[10])
165 int fd
= redir
->nfile
.fd
;
169 /* Assume redirection succeeds. */
170 { extern int exitstatus
; exitstatus
= 0; }
173 * We suppress interrupts so that we won't leave open file
174 * descriptors around. This may not be such a good idea because
175 * an open of a device or a fifo can block indefinitely.
179 switch (redir
->nfile
.type
) {
181 fname
= redir
->nfile
.expfname
;
182 if ((f
= open(fname
, O_RDONLY
)) < 0)
183 error("cannot open %s: %s", fname
, strerror(errno
));
191 fname
= redir
->nfile
.expfname
;
192 if ((f
= open(fname
, O_RDWR
|O_CREAT
, 0666)) < 0)
193 error("cannot create %s: %s", fname
, strerror(errno
));
196 fname
= redir
->nfile
.expfname
;
197 if (Cflag
&& stat(fname
, &sb
) != -1 && S_ISREG(sb
.st_mode
))
198 error("cannot create %s: %s", fname
,
200 if ((f
= open(fname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0)
201 error("cannot create %s: %s", fname
, strerror(errno
));
204 fname
= redir
->nfile
.expfname
;
205 if ((f
= open(fname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0)
206 error("cannot create %s: %s", fname
, strerror(errno
));
209 fname
= redir
->nfile
.expfname
;
210 if ((f
= open(fname
, O_WRONLY
|O_CREAT
|O_APPEND
, 0666)) < 0)
211 error("cannot create %s: %s", fname
, strerror(errno
));
215 if (redir
->ndup
.dupfd
>= 0) { /* if not ">&-" */
216 if (memory
[redir
->ndup
.dupfd
])
219 dup2(redir
->ndup
.dupfd
, fd
);
236 * Handle here documents. Normally we fork off a process to write the
237 * data to a pipe. If the document is short, we can stuff the data in
238 * the pipe without forking.
242 openhere(union node
*redir
)
248 error("Pipe call failed: %s", strerror(errno
));
249 if (redir
->type
== NHERE
) {
250 len
= strlen(redir
->nhere
.doc
->narg
.text
);
251 if (len
<= PIPESIZE
) {
252 xwrite(pip
[1], redir
->nhere
.doc
->narg
.text
, len
);
256 if (forkshell((struct job
*)NULL
, (union node
*)NULL
, FORK_NOJOB
) == 0) {
258 signal(SIGINT
, SIG_IGN
);
259 signal(SIGQUIT
, SIG_IGN
);
260 signal(SIGHUP
, SIG_IGN
);
261 signal(SIGTSTP
, SIG_IGN
);
262 signal(SIGPIPE
, SIG_DFL
);
263 if (redir
->type
== NHERE
)
264 xwrite(pip
[1], redir
->nhere
.doc
->narg
.text
, len
);
266 expandhere(redir
->nhere
.doc
, pip
[1]);
277 * Undo the effects of the last redirection.
283 struct redirtab
*rp
= redirlist
;
286 for (i
= 0 ; i
< 10 ; i
++) {
287 if (rp
->renamed
[i
] != EMPTY
) {
290 if (rp
->renamed
[i
] >= 0) {
291 dup2(rp
->renamed
[i
], i
);
292 close(rp
->renamed
[i
]);
299 redirlist
= rp
->next
;
305 * Undo all redirections. Called on error or interrupt.
323 /* Return true if fd 0 has already been redirected at least once. */
325 fd0_redirected_p(void)
327 return fd0_redirected
!= 0;
331 * Discard all saved file descriptors.
340 for (rp
= redirlist
; rp
; rp
= rp
->next
) {
341 for (i
= 0 ; i
< 10 ; i
++) {
342 if (rp
->renamed
[i
] >= 0) {
343 close(rp
->renamed
[i
]);
345 rp
->renamed
[i
] = EMPTY
;
351 * $PchId: redir.c,v 1.5 2006/05/22 12:27:37 philip Exp $