]> git.ipfire.org Git - thirdparty/bash.git/blame - redir.c
commit bash-20070906 snapshot
[thirdparty/bash.git] / redir.c
CommitLineData
cce855bc
JA
1/* redir.c -- Functions to perform input and output redirection. */
2
b0c16657 3/* Copyright (C) 1997-2007 Free Software Foundation, Inc.
cce855bc
JA
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
bb70624e 9 the Free Software Foundation; either version 2, or (at your option)
cce855bc
JA
10 any later version.
11
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
bb70624e 19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
cce855bc
JA
20#include "config.h"
21
22#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
23 #pragma alloca
24#endif /* _AIX && RISC6000 && !__GNUC__ */
25
26#include <stdio.h>
27#include "bashtypes.h"
d3a24ed2 28#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
cce855bc
JA
29# include <sys/file.h>
30#endif
31#include "filecntl.h"
32#include "posixstat.h"
33
34#if defined (HAVE_UNISTD_H)
35# include <unistd.h>
36#endif
37
38#include <errno.h>
39
40#if !defined (errno)
41extern int errno;
42#endif
43
44#include "bashansi.h"
5e13499c 45#include "bashintl.h"
cce855bc
JA
46
47#include "memalloc.h"
48#include "shell.h"
49#include "flags.h"
50#include "execute_cmd.h"
51#include "redir.h"
52
53#if defined (BUFFERED_INPUT)
54# include "input.h"
55#endif
56
b0c16657
CR
57#define SHELL_FD_BASE 10
58
d3a24ed2
CR
59int expanding_redir;
60
cce855bc 61extern int posixly_correct;
cce855bc
JA
62extern REDIRECT *redirection_undo_list;
63extern REDIRECT *exec_redirection_undo_list;
64
65/* Static functions defined and used in this file. */
f73dda09
JA
66static void add_undo_close_redirect __P((int));
67static void add_exec_redirect __P((REDIRECT *));
d3ad40de 68static int add_undo_redirect __P((int, enum r_instruction));
f73dda09
JA
69static int expandable_redirection_filename __P((REDIRECT *));
70static int stdin_redirection __P((enum r_instruction, int));
d3a24ed2 71static int do_redirection_internal __P((REDIRECT *, int));
f73dda09
JA
72
73static int write_here_document __P((int, WORD_DESC *));
7117c2d2
JA
74static int write_here_string __P((int, WORD_DESC *));
75static int here_document_to_fd __P((WORD_DESC *, enum r_instruction));
f73dda09
JA
76
77static int redir_special_open __P((int, char *, int, int, enum r_instruction));
78static int noclobber_open __P((char *, int, int, enum r_instruction));
79static int redir_open __P((char *, int, int, enum r_instruction));
cce855bc 80
7117c2d2
JA
81/* Spare redirector used when translating [N]>&WORD[-] or [N]<&WORD[-] to
82 a new redirection and when creating the redirection undo list. */
cce855bc
JA
83static REDIRECTEE rd;
84
85/* Set to errno when a here document cannot be created for some reason.
86 Used to print a reasonable error message. */
87static int heredoc_errno;
88
89void
90redirection_error (temp, error)
91 REDIRECT *temp;
92 int error;
93{
f73dda09 94 char *filename, *allocname;
bb70624e 95 int oflags;
cce855bc 96
f73dda09
JA
97 allocname = 0;
98 if (temp->redirector < 0)
99 /* This can happen when read_token_word encounters overflow, like in
100 exec 4294967297>x */
5e13499c 101 filename = _("file descriptor out of range");
f73dda09
JA
102#ifdef EBADF
103 else if (temp->redirector >= 0 && errno == EBADF)
7117c2d2
JA
104 {
105 /* If we're dealing with two file descriptors, we have to guess about
106 which one is invalid; in the cases of r_{duplicating,move}_input and
107 r_{duplicating,move}_output we're here because dup2() failed. */
108 switch (temp->instruction)
109 {
110 case r_duplicating_input:
111 case r_duplicating_output:
112 case r_move_input:
113 case r_move_output:
114 filename = allocname = itos (temp->redirectee.dest);
115 break;
116 default:
117 filename = allocname = itos (temp->redirector);
118 break;
119 }
120 }
f73dda09
JA
121#endif
122 else if (expandable_redirection_filename (temp))
cce855bc 123 {
bb70624e
JA
124 if (posixly_correct && interactive_shell == 0)
125 {
126 oflags = temp->redirectee.filename->flags;
127 temp->redirectee.filename->flags |= W_NOGLOB;
128 }
f73dda09 129 filename = allocname = redirection_expand (temp->redirectee.filename);
bb70624e 130 if (posixly_correct && interactive_shell == 0)
28ef6c31 131 temp->redirectee.filename->flags = oflags;
cce855bc 132 if (filename == 0)
f73dda09 133 filename = temp->redirectee.filename->word;
cce855bc 134 }
f73dda09
JA
135 else if (temp->redirectee.dest < 0)
136 filename = "file descriptor out of range";
cce855bc 137 else
f73dda09 138 filename = allocname = itos (temp->redirectee.dest);
cce855bc
JA
139
140 switch (error)
141 {
142 case AMBIGUOUS_REDIRECT:
5e13499c 143 internal_error (_("%s: ambiguous redirect"), filename);
cce855bc
JA
144 break;
145
146 case NOCLOBBER_REDIRECT:
5e13499c 147 internal_error (_("%s: cannot overwrite existing file"), filename);
cce855bc
JA
148 break;
149
150#if defined (RESTRICTED_SHELL)
151 case RESTRICTED_REDIRECT:
5e13499c 152 internal_error (_("%s: restricted: cannot redirect output"), filename);
cce855bc
JA
153 break;
154#endif /* RESTRICTED_SHELL */
155
156 case HEREDOC_REDIRECT:
5e13499c 157 internal_error (_("cannot create temp file for here document: %s"), strerror (heredoc_errno));
cce855bc
JA
158 break;
159
160 default:
161 internal_error ("%s: %s", filename, strerror (error));
162 break;
163 }
164
f73dda09 165 FREE (allocname);
cce855bc
JA
166}
167
d3a24ed2
CR
168/* Perform the redirections on LIST. If flags & RX_ACTIVE, then actually
169 make input and output file descriptors, otherwise just do whatever is
170 neccessary for side effecting. flags & RX_UNDOABLE says to remember
171 how to undo the redirections later, if non-zero. If flags & RX_CLEXEC
172 is non-zero, file descriptors opened in do_redirection () have their
173 close-on-exec flag set. */
cce855bc 174int
d3a24ed2 175do_redirections (list, flags)
cce855bc 176 REDIRECT *list;
d3a24ed2 177 int flags;
cce855bc
JA
178{
179 int error;
180 REDIRECT *temp;
181
d3a24ed2 182 if (flags & RX_UNDOABLE)
cce855bc
JA
183 {
184 if (redirection_undo_list)
185 {
186 dispose_redirects (redirection_undo_list);
187 redirection_undo_list = (REDIRECT *)NULL;
188 }
189 if (exec_redirection_undo_list)
190 dispose_exec_redirects ();
191 }
192
193 for (temp = list; temp; temp = temp->next)
194 {
d3a24ed2 195 error = do_redirection_internal (temp, flags);
cce855bc
JA
196 if (error)
197 {
198 redirection_error (temp, error);
199 return (error);
200 }
201 }
202 return (0);
203}
204
205/* Return non-zero if the redirection pointed to by REDIRECT has a
206 redirectee.filename that can be expanded. */
207static int
208expandable_redirection_filename (redirect)
209 REDIRECT *redirect;
210{
211 switch (redirect->instruction)
212 {
213 case r_output_direction:
214 case r_appending_to:
215 case r_input_direction:
216 case r_inputa_direction:
217 case r_err_and_out:
218 case r_input_output:
219 case r_output_force:
220 case r_duplicating_input_word:
221 case r_duplicating_output_word:
7117c2d2
JA
222 case r_move_input_word:
223 case r_move_output_word:
cce855bc
JA
224 return 1;
225
226 default:
227 return 0;
228 }
229}
230
231/* Expand the word in WORD returning a string. If WORD expands to
232 multiple words (or no words), then return NULL. */
233char *
234redirection_expand (word)
235 WORD_DESC *word;
236{
237 char *result;
238 WORD_LIST *tlist1, *tlist2;
bb70624e
JA
239 WORD_DESC *w;
240
241 w = copy_word (word);
242 if (posixly_correct)
243 w->flags |= W_NOSPLIT;
cce855bc 244
bb70624e 245 tlist1 = make_word_list (w, (WORD_LIST *)NULL);
d3a24ed2 246 expanding_redir = 1;
cce855bc 247 tlist2 = expand_words_no_vars (tlist1);
d3a24ed2 248 expanding_redir = 0;
cce855bc
JA
249 dispose_words (tlist1);
250
251 if (!tlist2 || tlist2->next)
252 {
253 /* We expanded to no words, or to more than a single word.
254 Dispose of the word list and return NULL. */
255 if (tlist2)
256 dispose_words (tlist2);
257 return ((char *)NULL);
258 }
259 result = string_list (tlist2); /* XXX savestring (tlist2->word->word)? */
260 dispose_words (tlist2);
261 return (result);
262}
263
7117c2d2
JA
264static int
265write_here_string (fd, redirectee)
266 int fd;
267 WORD_DESC *redirectee;
268{
269 char *herestr;
270 int herelen, n, e;
271
272 herestr = expand_string_to_string (redirectee->word, 0);
d3a24ed2 273 herelen = STRLEN (herestr);
7117c2d2
JA
274
275 n = write (fd, herestr, herelen);
276 if (n == herelen)
277 {
278 n = write (fd, "\n", 1);
279 herelen = 1;
280 }
281 e = errno;
d3a24ed2 282 FREE (herestr);
7117c2d2
JA
283 if (n != herelen)
284 {
285 if (e == 0)
286 e = ENOSPC;
287 return e;
288 }
289 return 0;
290}
291
cce855bc
JA
292/* Write the text of the here document pointed to by REDIRECTEE to the file
293 descriptor FD, which is already open to a temp file. Return 0 if the
294 write is successful, otherwise return errno. */
295static int
296write_here_document (fd, redirectee)
297 int fd;
298 WORD_DESC *redirectee;
299{
300 char *document;
301 int document_len, fd2;
302 FILE *fp;
303 register WORD_LIST *t, *tlist;
304
305 /* Expand the text if the word that was specified had
306 no quoting. The text that we expand is treated
307 exactly as if it were surrounded by double quotes. */
308
309 if (redirectee->flags & W_QUOTED)
310 {
311 document = redirectee->word;
312 document_len = strlen (document);
313 /* Set errno to something reasonable if the write fails. */
314 if (write (fd, document, document_len) < document_len)
315 {
316 if (errno == 0)
317 errno = ENOSPC;
318 return (errno);
319 }
320 else
28ef6c31 321 return 0;
cce855bc
JA
322 }
323
324 tlist = expand_string (redirectee->word, Q_HERE_DOCUMENT);
325 if (tlist)
326 {
327 /* Try using buffered I/O (stdio) and writing a word
328 at a time, letting stdio do the work of buffering
329 for us rather than managing our own strings. Most
330 stdios are not particularly fast, however -- this
331 may need to be reconsidered later. */
332 if ((fd2 = dup (fd)) < 0 || (fp = fdopen (fd2, "w")) == NULL)
333 {
334 if (fd2 >= 0)
335 close (fd2);
336 return (errno);
337 }
338 errno = 0;
339 for (t = tlist; t; t = t->next)
340 {
341 /* This is essentially the body of
342 string_list_internal expanded inline. */
343 document = t->word->word;
344 document_len = strlen (document);
345 if (t != tlist)
346 putc (' ', fp); /* separator */
347 fwrite (document, document_len, 1, fp);
348 if (ferror (fp))
349 {
350 if (errno == 0)
351 errno = ENOSPC;
352 fd2 = errno;
353 fclose(fp);
354 dispose_words (tlist);
355 return (fd2);
356 }
357 }
cce855bc 358 dispose_words (tlist);
f73dda09
JA
359 if (fclose (fp) != 0)
360 {
361 if (errno == 0)
362 errno = ENOSPC;
363 return (errno);
364 }
cce855bc
JA
365 }
366 return 0;
367}
368
369/* Create a temporary file holding the text of the here document pointed to
370 by REDIRECTEE, and return a file descriptor open for reading to the temp
371 file. Return -1 on any error, and make sure errno is set appropriately. */
372static int
7117c2d2 373here_document_to_fd (redirectee, ri)
cce855bc 374 WORD_DESC *redirectee;
7117c2d2 375 enum r_instruction ri;
cce855bc 376{
28ef6c31 377 char *filename;
bb70624e 378 int r, fd, fd2;
cce855bc 379
d3ad40de 380 fd = sh_mktmpfd ("sh-thd", MT_USERANDOM|MT_USETMPDIR, &filename);
cce855bc
JA
381
382 /* If we failed for some reason other than the file existing, abort */
383 if (fd < 0)
28ef6c31
JA
384 {
385 FREE (filename);
386 return (fd);
387 }
cce855bc
JA
388
389 errno = r = 0; /* XXX */
390 /* write_here_document returns 0 on success, errno on failure. */
391 if (redirectee->word)
7117c2d2
JA
392 r = (ri != r_reading_string) ? write_here_document (fd, redirectee)
393 : write_here_string (fd, redirectee);
cce855bc 394
cce855bc
JA
395 if (r)
396 {
bb70624e 397 close (fd);
cce855bc 398 unlink (filename);
28ef6c31 399 free (filename);
cce855bc
JA
400 errno = r;
401 return (-1);
402 }
403
bb70624e
JA
404 /* In an attempt to avoid races, we close the first fd only after opening
405 the second. */
cce855bc 406 /* Make the document really temporary. Also make it the input. */
bb70624e 407 fd2 = open (filename, O_RDONLY, 0600);
cce855bc 408
bb70624e 409 if (fd2 < 0)
cce855bc
JA
410 {
411 r = errno;
412 unlink (filename);
28ef6c31 413 free (filename);
bb70624e 414 close (fd);
cce855bc
JA
415 errno = r;
416 return -1;
417 }
418
bb70624e 419 close (fd);
cce855bc
JA
420 if (unlink (filename) < 0)
421 {
422 r = errno;
28ef6c31
JA
423#if defined (__CYGWIN__)
424 /* Under CygWin 1.1.0, the unlink will fail if the file is
425 open. This hack will allow the previous action of silently
426 ignoring the error, but will still leave the file there. This
427 needs some kind of magic. */
428 if (r == EACCES)
429 return (fd2);
430#endif /* __CYGWIN__ */
bb70624e 431 close (fd2);
28ef6c31 432 free (filename);
cce855bc
JA
433 errno = r;
434 return (-1);
435 }
436
28ef6c31 437 free (filename);
bb70624e 438 return (fd2);
cce855bc
JA
439}
440
bb70624e
JA
441#define RF_DEVFD 1
442#define RF_DEVSTDERR 2
443#define RF_DEVSTDIN 3
444#define RF_DEVSTDOUT 4
445#define RF_DEVTCP 5
446#define RF_DEVUDP 6
447
448/* A list of pattern/value pairs for filenames that the redirection
449 code handles specially. */
450static STRING_INT_ALIST _redir_special_filenames[] = {
451#if !defined (HAVE_DEV_FD)
452 { "/dev/fd/[0-9]*", RF_DEVFD },
453#endif
454#if !defined (HAVE_DEV_STDIN)
455 { "/dev/stderr", RF_DEVSTDERR },
456 { "/dev/stdin", RF_DEVSTDIN },
457 { "/dev/stdout", RF_DEVSTDOUT },
458#endif
459#if defined (NETWORK_REDIRECTIONS)
460 { "/dev/tcp/*/*", RF_DEVTCP },
461 { "/dev/udp/*/*", RF_DEVUDP },
462#endif
463 { (char *)NULL, -1 }
464};
465
466static int
467redir_special_open (spec, filename, flags, mode, ri)
468 int spec;
469 char *filename;
470 int flags, mode;
471 enum r_instruction ri;
472{
473 int fd;
f73dda09 474#if !defined (HAVE_DEV_FD)
7117c2d2 475 intmax_t lfd;
f73dda09 476#endif
bb70624e
JA
477
478 fd = -1;
479 switch (spec)
480 {
481#if !defined (HAVE_DEV_FD)
482 case RF_DEVFD:
f73dda09
JA
483 if (all_digits (filename+8) && legal_number (filename+8, &lfd) && lfd == (int)lfd)
484 {
485 fd = lfd;
b0c16657 486 fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE);
f73dda09 487 }
bb70624e
JA
488 else
489 fd = AMBIGUOUS_REDIRECT;
490 break;
491#endif
492
493#if !defined (HAVE_DEV_STDIN)
494 case RF_DEVSTDIN:
b0c16657 495 fd = fcntl (0, F_DUPFD, SHELL_FD_BASE);
bb70624e
JA
496 break;
497 case RF_DEVSTDOUT:
b0c16657 498 fd = fcntl (1, F_DUPFD, SHELL_FD_BASE);
bb70624e
JA
499 break;
500 case RF_DEVSTDERR:
b0c16657 501 fd = fcntl (2, F_DUPFD, SHELL_FD_BASE);
bb70624e
JA
502 break;
503#endif
504
505#if defined (NETWORK_REDIRECTIONS)
506 case RF_DEVTCP:
507 case RF_DEVUDP:
508#if defined (HAVE_NETWORK)
509 fd = netopen (filename);
510#else
5e13499c 511 internal_warning (_("/dev/(tcp|udp)/host/port not supported without networking"));
bb70624e
JA
512 fd = open (filename, flags, mode);
513#endif
514 break;
515#endif /* NETWORK_REDIRECTIONS */
516 }
517
518 return fd;
519}
520
cce855bc
JA
521/* Open FILENAME with FLAGS in noclobber mode, hopefully avoiding most
522 race conditions and avoiding the problem where the file is replaced
523 between the stat(2) and open(2). */
524static int
bb70624e 525noclobber_open (filename, flags, mode, ri)
cce855bc 526 char *filename;
bb70624e 527 int flags, mode;
cce855bc
JA
528 enum r_instruction ri;
529{
530 int r, fd;
531 struct stat finfo, finfo2;
532
533 /* If the file exists and is a regular file, return an error
534 immediately. */
535 r = stat (filename, &finfo);
536 if (r == 0 && (S_ISREG (finfo.st_mode)))
537 return (NOCLOBBER_REDIRECT);
538
539 /* If the file was not present (r != 0), make sure we open it
540 exclusively so that if it is created before we open it, our open
541 will fail. Make sure that we do not truncate an existing file.
542 Note that we don't turn on O_EXCL unless the stat failed -- if
543 the file was not a regular file, we leave O_EXCL off. */
544 flags &= ~O_TRUNC;
545 if (r != 0)
546 {
bb70624e 547 fd = open (filename, flags|O_EXCL, mode);
cce855bc
JA
548 return ((fd < 0 && errno == EEXIST) ? NOCLOBBER_REDIRECT : fd);
549 }
bb70624e 550 fd = open (filename, flags, mode);
cce855bc
JA
551
552 /* If the open failed, return the file descriptor right away. */
553 if (fd < 0)
554 return (errno == EEXIST ? NOCLOBBER_REDIRECT : fd);
555
556 /* OK, the open succeeded, but the file may have been changed from a
557 non-regular file to a regular file between the stat and the open.
558 We are assuming that the O_EXCL open handles the case where FILENAME
559 did not exist and is symlinked to an existing file between the stat
560 and open. */
561
562 /* If we can open it and fstat the file descriptor, and neither check
563 revealed that it was a regular file, and the file has not been replaced,
564 return the file descriptor. */
565 if ((fstat (fd, &finfo2) == 0) && (S_ISREG (finfo2.st_mode) == 0) &&
566 r == 0 && (S_ISREG (finfo.st_mode) == 0) &&
567 same_file (filename, filename, &finfo, &finfo2))
568 return fd;
569
570 /* The file has been replaced. badness. */
571 close (fd);
572 errno = EEXIST;
573 return (NOCLOBBER_REDIRECT);
574}
575
bb70624e
JA
576static int
577redir_open (filename, flags, mode, ri)
578 char *filename;
579 int flags, mode;
580 enum r_instruction ri;
581{
582 int fd, r;
583
584 r = find_string_in_alist (filename, _redir_special_filenames, 1);
585 if (r >= 0)
586 return (redir_special_open (r, filename, flags, mode, ri));
587
588 /* If we are in noclobber mode, you are not allowed to overwrite
589 existing files. Check before opening. */
28ef6c31 590 if (noclobber && CLOBBERING_REDIRECT (ri))
bb70624e
JA
591 {
592 fd = noclobber_open (filename, flags, mode, ri);
593 if (fd == NOCLOBBER_REDIRECT)
594 return (NOCLOBBER_REDIRECT);
595 }
596 else
597 {
598 fd = open (filename, flags, mode);
599#if defined (AFS)
600 if ((fd < 0) && (errno == EACCES))
4f00fe31
CR
601 {
602 fd = open (filename, flags & ~O_CREAT, mode);
603 errno = EACCES; /* restore errno */
604 }
bb70624e
JA
605#endif /* AFS */
606 }
607
608 return fd;
609}
610
cce855bc
JA
611/* Do the specific redirection requested. Returns errno or one of the
612 special redirection errors (*_REDIRECT) in case of error, 0 on success.
d3a24ed2
CR
613 If flags & RX_ACTIVE is zero, then just do whatever is neccessary to
614 produce the appropriate side effects. flags & RX_UNDOABLE, if non-zero,
615 says to remember how to undo each redirection. If flags & RX_CLEXEC is
616 non-zero, then we set all file descriptors > 2 that we open to be
617 close-on-exec. */
cce855bc 618static int
d3a24ed2 619do_redirection_internal (redirect, flags)
cce855bc 620 REDIRECT *redirect;
d3a24ed2 621 int flags;
cce855bc
JA
622{
623 WORD_DESC *redirectee;
bb70624e 624 int redir_fd, fd, redirector, r, oflags;
7117c2d2 625 intmax_t lfd;
cce855bc
JA
626 char *redirectee_word;
627 enum r_instruction ri;
628 REDIRECT *new_redirect;
629
630 redirectee = redirect->redirectee.filename;
631 redir_fd = redirect->redirectee.dest;
632 redirector = redirect->redirector;
633 ri = redirect->instruction;
634
d3ad40de
CR
635 if (redirect->flags & RX_INTERNAL)
636 flags |= RX_INTERNAL;
e225d5a9 637
7117c2d2 638 if (TRANSLATE_REDIRECT (ri))
cce855bc 639 {
7117c2d2 640 /* We have [N]>&WORD[-] or [N]<&WORD[-]. Expand WORD, then translate
cce855bc
JA
641 the redirection into a new one and continue. */
642 redirectee_word = redirection_expand (redirectee);
643
7117c2d2
JA
644 /* XXX - what to do with [N]<&$w- where w is unset or null? ksh93
645 closes N. */
cce855bc
JA
646 if (redirectee_word == 0)
647 return (AMBIGUOUS_REDIRECT);
648 else if (redirectee_word[0] == '-' && redirectee_word[1] == '\0')
649 {
f73dda09 650 rd.dest = 0;
cce855bc
JA
651 new_redirect = make_redirection (redirector, r_close_this, rd);
652 }
653 else if (all_digits (redirectee_word))
654 {
f73dda09
JA
655 if (legal_number (redirectee_word, &lfd) && (int)lfd == lfd)
656 rd.dest = lfd;
cce855bc 657 else
f73dda09 658 rd.dest = -1; /* XXX */
7117c2d2
JA
659 switch (ri)
660 {
661 case r_duplicating_input_word:
662 new_redirect = make_redirection (redirector, r_duplicating_input, rd);
663 break;
664 case r_duplicating_output_word:
665 new_redirect = make_redirection (redirector, r_duplicating_output, rd);
666 break;
667 case r_move_input_word:
668 new_redirect = make_redirection (redirector, r_move_input, rd);
669 break;
670 case r_move_output_word:
671 new_redirect = make_redirection (redirector, r_move_output, rd);
672 break;
673 }
cce855bc
JA
674 }
675 else if (ri == r_duplicating_output_word && redirector == 1)
676 {
28ef6c31
JA
677 rd.filename = make_bare_word (redirectee_word);
678 new_redirect = make_redirection (1, r_err_and_out, rd);
cce855bc
JA
679 }
680 else
681 {
682 free (redirectee_word);
683 return (AMBIGUOUS_REDIRECT);
684 }
685
686 free (redirectee_word);
687
688 /* Set up the variables needed by the rest of the function from the
689 new redirection. */
690 if (new_redirect->instruction == r_err_and_out)
691 {
692 char *alloca_hack;
693
694 /* Copy the word without allocating any memory that must be
695 explicitly freed. */
696 redirectee = (WORD_DESC *)alloca (sizeof (WORD_DESC));
697 xbcopy ((char *)new_redirect->redirectee.filename,
698 (char *)redirectee, sizeof (WORD_DESC));
699
700 alloca_hack = (char *)
701 alloca (1 + strlen (new_redirect->redirectee.filename->word));
702 redirectee->word = alloca_hack;
703 strcpy (redirectee->word, new_redirect->redirectee.filename->word);
704 }
705 else
706 /* It's guaranteed to be an integer, and shouldn't be freed. */
707 redirectee = new_redirect->redirectee.filename;
708
709 redir_fd = new_redirect->redirectee.dest;
710 redirector = new_redirect->redirector;
711 ri = new_redirect->instruction;
712
713 /* Overwrite the flags element of the old redirect with the new value. */
714 redirect->flags = new_redirect->flags;
715 dispose_redirects (new_redirect);
716 }
717
718 switch (ri)
719 {
720 case r_output_direction:
721 case r_appending_to:
722 case r_input_direction:
723 case r_inputa_direction:
724 case r_err_and_out: /* command &>filename */
725 case r_input_output:
726 case r_output_force:
bb70624e 727 if (posixly_correct && interactive_shell == 0)
28ef6c31
JA
728 {
729 oflags = redirectee->flags;
730 redirectee->flags |= W_NOGLOB;
731 }
cce855bc 732 redirectee_word = redirection_expand (redirectee);
bb70624e
JA
733 if (posixly_correct && interactive_shell == 0)
734 redirectee->flags = oflags;
cce855bc
JA
735
736 if (redirectee_word == 0)
737 return (AMBIGUOUS_REDIRECT);
738
739#if defined (RESTRICTED_SHELL)
740 if (restricted && (WRITE_REDIRECT (ri)))
741 {
742 free (redirectee_word);
743 return (RESTRICTED_REDIRECT);
744 }
745#endif /* RESTRICTED_SHELL */
746
bb70624e 747 fd = redir_open (redirectee_word, redirect->flags, 0666, ri);
cce855bc
JA
748 free (redirectee_word);
749
bb70624e 750 if (fd == NOCLOBBER_REDIRECT)
28ef6c31 751 return (fd);
bb70624e 752
cce855bc
JA
753 if (fd < 0)
754 return (errno);
755
d3a24ed2 756 if (flags & RX_ACTIVE)
cce855bc 757 {
d3a24ed2 758 if (flags & RX_UNDOABLE)
f73dda09
JA
759 {
760 /* Only setup to undo it if the thing to undo is active. */
761 if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
d3ad40de 762 add_undo_redirect (redirector, ri);
f73dda09
JA
763 else
764 add_undo_close_redirect (redirector);
765 }
cce855bc
JA
766
767#if defined (BUFFERED_INPUT)
768 check_bash_input (redirector);
769#endif
770
b709b946
CR
771 /* Make sure there is no pending output before we change the state
772 of the underlying file descriptor, since the builtins use stdio
773 for output. */
774 if (redirector == 1 && fileno (stdout) == redirector)
775 fflush (stdout);
776
cce855bc
JA
777 if ((fd != redirector) && (dup2 (fd, redirector) < 0))
778 return (errno);
779
780#if defined (BUFFERED_INPUT)
781 /* Do not change the buffered stream for an implicit redirection
782 of /dev/null to fd 0 for asynchronous commands without job
783 control (r_inputa_direction). */
784 if (ri == r_input_direction || ri == r_input_output)
785 duplicate_buffered_stream (fd, redirector);
786#endif /* BUFFERED_INPUT */
787
788 /*
789 * If we're remembering, then this is the result of a while, for
790 * or until loop with a loop redirection, or a function/builtin
791 * executing in the parent shell with a redirection. In the
792 * function/builtin case, we want to set all file descriptors > 2
793 * to be close-on-exec to duplicate the effect of the old
794 * for i = 3 to NOFILE close(i) loop. In the case of the loops,
795 * both sh and ksh leave the file descriptors open across execs.
796 * The Posix standard mentions only the exec builtin.
797 */
d3a24ed2 798 if ((flags & RX_CLEXEC) && (redirector > 2))
cce855bc
JA
799 SET_CLOSE_ON_EXEC (redirector);
800 }
801
802 if (fd != redirector)
803 {
804#if defined (BUFFERED_INPUT)
805 if (INPUT_REDIRECT (ri))
806 close_buffered_fd (fd);
807 else
808#endif /* !BUFFERED_INPUT */
809 close (fd); /* Don't close what we just opened! */
810 }
811
812 /* If we are hacking both stdout and stderr, do the stderr
813 redirection here. */
814 if (ri == r_err_and_out)
815 {
d3a24ed2 816 if (flags & RX_ACTIVE)
cce855bc 817 {
d3a24ed2 818 if (flags & RX_UNDOABLE)
d3ad40de 819 add_undo_redirect (2, ri);
cce855bc
JA
820 if (dup2 (1, 2) < 0)
821 return (errno);
822 }
823 }
824 break;
825
826 case r_reading_until:
827 case r_deblank_reading_until:
7117c2d2 828 case r_reading_string:
cce855bc
JA
829 /* REDIRECTEE is a pointer to a WORD_DESC containing the text of
830 the new input. Place it in a temporary file. */
831 if (redirectee)
832 {
7117c2d2 833 fd = here_document_to_fd (redirectee, ri);
cce855bc
JA
834
835 if (fd < 0)
836 {
837 heredoc_errno = errno;
838 return (HEREDOC_REDIRECT);
839 }
840
d3a24ed2 841 if (flags & RX_ACTIVE)
cce855bc 842 {
d3a24ed2 843 if (flags & RX_UNDOABLE)
f73dda09
JA
844 {
845 /* Only setup to undo it if the thing to undo is active. */
846 if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
d3ad40de 847 add_undo_redirect (redirector, ri);
f73dda09
JA
848 else
849 add_undo_close_redirect (redirector);
850 }
cce855bc
JA
851
852#if defined (BUFFERED_INPUT)
853 check_bash_input (redirector);
854#endif
855 if (fd != redirector && dup2 (fd, redirector) < 0)
856 {
857 r = errno;
858 close (fd);
859 return (r);
860 }
861
862#if defined (BUFFERED_INPUT)
863 duplicate_buffered_stream (fd, redirector);
864#endif
865
d3a24ed2 866 if ((flags & RX_CLEXEC) && (redirector > 2))
cce855bc
JA
867 SET_CLOSE_ON_EXEC (redirector);
868 }
869
bb70624e 870 if (fd != redirector)
cce855bc 871#if defined (BUFFERED_INPUT)
bb70624e 872 close_buffered_fd (fd);
cce855bc 873#else
bb70624e 874 close (fd);
cce855bc
JA
875#endif
876 }
877 break;
878
879 case r_duplicating_input:
880 case r_duplicating_output:
7117c2d2
JA
881 case r_move_input:
882 case r_move_output:
d3a24ed2 883 if ((flags & RX_ACTIVE) && (redir_fd != redirector))
cce855bc 884 {
d3a24ed2 885 if (flags & RX_UNDOABLE)
f73dda09
JA
886 {
887 /* Only setup to undo it if the thing to undo is active. */
888 if (fcntl (redirector, F_GETFD, 0) != -1)
d3ad40de 889 add_undo_redirect (redirector, ri);
f73dda09
JA
890 else
891 add_undo_close_redirect (redirector);
892 }
cce855bc
JA
893#if defined (BUFFERED_INPUT)
894 check_bash_input (redirector);
895#endif
896 /* This is correct. 2>&1 means dup2 (1, 2); */
897 if (dup2 (redir_fd, redirector) < 0)
898 return (errno);
899
900#if defined (BUFFERED_INPUT)
7117c2d2 901 if (ri == r_duplicating_input || ri == r_move_input)
cce855bc
JA
902 duplicate_buffered_stream (redir_fd, redirector);
903#endif /* BUFFERED_INPUT */
904
905 /* First duplicate the close-on-exec state of redirectee. dup2
906 leaves the flag unset on the new descriptor, which means it
907 stays open. Only set the close-on-exec bit for file descriptors
908 greater than 2 in any case, since 0-2 should always be open
f75912ae
CR
909 unless closed by something like `exec 2<&-'. It should always
910 be safe to set fds > 2 to close-on-exec if they're being used to
911 save file descriptors < 2, since we don't need to preserve the
912 state of the close-on-exec flag for those fds -- they should
913 always be open. */
cce855bc
JA
914 /* if ((already_set || set_unconditionally) && (ok_to_set))
915 set_it () */
e225d5a9 916#if 0
f75912ae 917 if (((fcntl (redir_fd, F_GETFD, 0) == 1) || redir_fd < 2 || (flags & RX_CLEXEC)) &&
cce855bc 918 (redirector > 2))
e225d5a9
CR
919#else
920 if (((fcntl (redir_fd, F_GETFD, 0) == 1) || (redir_fd < 2 && (flags & RX_INTERNAL)) || (flags & RX_CLEXEC)) &&
921 (redirector > 2))
922#endif
cce855bc 923 SET_CLOSE_ON_EXEC (redirector);
7117c2d2
JA
924
925 /* dup-and-close redirection */
926 if (ri == r_move_input || ri == r_move_output)
927 close (redir_fd);
cce855bc
JA
928 }
929 break;
930
931 case r_close_this:
d3a24ed2 932 if (flags & RX_ACTIVE)
cce855bc 933 {
d3a24ed2 934 if ((flags & RX_UNDOABLE) && (fcntl (redirector, F_GETFD, 0) != -1))
d3ad40de 935 add_undo_redirect (redirector, ri);
cce855bc
JA
936
937#if defined (BUFFERED_INPUT)
938 check_bash_input (redirector);
939 close_buffered_fd (redirector);
940#else /* !BUFFERED_INPUT */
941 close (redirector);
942#endif /* !BUFFERED_INPUT */
943 }
944 break;
945
946 case r_duplicating_input_word:
947 case r_duplicating_output_word:
948 break;
949 }
950 return (0);
951}
952
cce855bc
JA
953/* Remember the file descriptor associated with the slot FD,
954 on REDIRECTION_UNDO_LIST. Note that the list will be reversed
955 before it is executed. Any redirections that need to be undone
956 even if REDIRECTION_UNDO_LIST is discarded by the exec builtin
957 are also saved on EXEC_REDIRECTION_UNDO_LIST. */
958static int
d3ad40de 959add_undo_redirect (fd, ri)
cce855bc 960 int fd;
d3ad40de 961 enum r_instruction ri;
cce855bc
JA
962{
963 int new_fd, clexec_flag;
964 REDIRECT *new_redirect, *closer, *dummy_redirect;
965
966 new_fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE);
967
968 if (new_fd < 0)
969 {
5e13499c 970 sys_error (_("redirection error: cannot duplicate fd"));
cce855bc
JA
971 return (-1);
972 }
973
974 clexec_flag = fcntl (fd, F_GETFD, 0);
975
f73dda09 976 rd.dest = 0;
cce855bc 977 closer = make_redirection (new_fd, r_close_this, rd);
e225d5a9 978 closer->flags |= RX_INTERNAL;
cce855bc
JA
979 dummy_redirect = copy_redirects (closer);
980
f73dda09 981 rd.dest = new_fd;
28ef6c31
JA
982 if (fd == 0)
983 new_redirect = make_redirection (fd, r_duplicating_input, rd);
984 else
985 new_redirect = make_redirection (fd, r_duplicating_output, rd);
e225d5a9 986 new_redirect->flags |= RX_INTERNAL;
cce855bc
JA
987 new_redirect->next = closer;
988
989 closer->next = redirection_undo_list;
990 redirection_undo_list = new_redirect;
991
992 /* Save redirections that need to be undone even if the undo list
993 is thrown away by the `exec' builtin. */
994 add_exec_redirect (dummy_redirect);
995
cac4cdbf
CR
996 /* experimental: if we're saving a redirection to undo for a file descriptor
997 above SHELL_FD_BASE, add a redirection to be undone if the exec builtin
b0c16657
CR
998 causes redirections to be discarded. There needs to be a difference
999 between fds that are used to save other fds and then are the target of
1000 user redirctions and fds that are just the target of user redirections.
1001 We use the close-on-exec flag to tell the difference; fds > SHELL_FD_BASE
1002 that have the close-on-exec flag set are assumed to be fds used internally
1003 to save others. */
1004 if (fd >= SHELL_FD_BASE && ri != r_close_this && clexec_flag)
cac4cdbf
CR
1005 {
1006 rd.dest = new_fd;
1007 new_redirect = make_redirection (fd, r_duplicating_output, rd);
b0c16657
CR
1008 new_redirect->flags |= RX_INTERNAL;
1009
cac4cdbf 1010 add_exec_redirect (new_redirect);
cac4cdbf
CR
1011 }
1012
cce855bc
JA
1013 /* File descriptors used only for saving others should always be
1014 marked close-on-exec. Unfortunately, we have to preserve the
1015 close-on-exec state of the file descriptor we are saving, since
1016 fcntl (F_DUPFD) sets the new file descriptor to remain open
1017 across execs. If, however, the file descriptor whose state we
1018 are saving is <= 2, we can just set the close-on-exec flag,
1019 because file descriptors 0-2 should always be open-on-exec,
1020 and the restore above in do_redirection() will take care of it. */
1021 if (clexec_flag || fd < 3)
1022 SET_CLOSE_ON_EXEC (new_fd);
1023
1024 return (0);
1025}
1026
1027/* Set up to close FD when we are finished with the current command
1028 and its redirections. */
1029static void
1030add_undo_close_redirect (fd)
1031 int fd;
1032{
1033 REDIRECT *closer;
1034
f73dda09 1035 rd.dest = 0;
cce855bc 1036 closer = make_redirection (fd, r_close_this, rd);
e225d5a9 1037 closer->flags |= RX_INTERNAL;
cce855bc
JA
1038 closer->next = redirection_undo_list;
1039 redirection_undo_list = closer;
1040}
1041
1042static void
1043add_exec_redirect (dummy_redirect)
1044 REDIRECT *dummy_redirect;
1045{
1046 dummy_redirect->next = exec_redirection_undo_list;
1047 exec_redirection_undo_list = dummy_redirect;
1048}
1049
bb70624e
JA
1050/* Return 1 if the redirection specified by RI and REDIRECTOR alters the
1051 standard input. */
1052static int
1053stdin_redirection (ri, redirector)
1054 enum r_instruction ri;
1055 int redirector;
1056{
1057 switch (ri)
1058 {
1059 case r_input_direction:
1060 case r_inputa_direction:
1061 case r_input_output:
1062 case r_reading_until:
1063 case r_deblank_reading_until:
7117c2d2 1064 case r_reading_string:
bb70624e
JA
1065 return (1);
1066 case r_duplicating_input:
1067 case r_duplicating_input_word:
1068 case r_close_this:
1069 return (redirector == 0);
1070 case r_output_direction:
1071 case r_appending_to:
1072 case r_duplicating_output:
1073 case r_err_and_out:
1074 case r_output_force:
1075 case r_duplicating_output_word:
1076 return (0);
1077 }
1078 return (0);
1079}
1080
cce855bc
JA
1081/* Return non-zero if any of the redirections in REDIRS alter the standard
1082 input. */
1083int
1084stdin_redirects (redirs)
1085 REDIRECT *redirs;
1086{
1087 REDIRECT *rp;
1088 int n;
1089
1090 for (n = 0, rp = redirs; rp; rp = rp->next)
bb70624e 1091 n += stdin_redirection (rp->instruction, rp->redirector);
cce855bc
JA
1092 return n;
1093}