/* redir.c: code for opening files and piping heredocs after fork but before exec. */
Walk the redirection queue, and open files and dup2 to them. Also,
here-documents are treated here by dumping them down a pipe. (this
should make here-documents fast on systems with lots of memory which
do pipes right. Under sh, a file is copied to /tmp, and then read
out of /tmp again. I'm interested in knowing how much faster, say,
shar runs when unpacking when invoked with rc instead of sh. On my
sun4/280, it runs in about 60-75% of the time of sh for unpacking
the rc source distribution.)
for (r
= redirq
; r
!= NULL
; r
= r
->n
) {
panic("unexpected node in doredirs");
if (r
->r
->u
[0].i
== rHerestring
) {
fname
= flatten(glom(r
->r
->u
[2].p
)); /* fname is really a string */
if (rc_fork() == 0) { /* child writes to pipe */
writeall(p
[1], fname
->w
, strlen(fname
->w
));
if (mvfd(p
[0], r
->r
->u
[1].i
) < 0)
fname
= glob(glom(r
->r
->u
[2].p
));
rc_error("null filename in redirection");
rc_error("multi-word filename in redirection");
panic("unexpected node in doredirs");
case rCreate
: case rAppend
: case rFrom
:
fd
= rc_open(fname
->w
, r
->r
->u
[0].i
);
if (mvfd(fd
, r
->r
->u
[1].i
) < 0)
else if (r
->r
->u
[2].i
!= r
->r
->u
[1].i
) {
if (dup2(r
->r
->u
[2].i
, r
->r
->u
[1].i
) < 0) {