1 /* redir.c -- Functions to perform input and output redirection. */
3 /* Copyright (C) 1997 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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
9 the Free Software Foundation; either version 2, or (at your option)
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.
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
19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
24 #endif /* _AIX && RISC6000 && !__GNUC__ */
27 #include "bashtypes.h"
29 # include <sys/file.h>
32 #include "posixstat.h"
34 #if defined (HAVE_UNISTD_H)
49 #include "execute_cmd.h"
52 #if defined (BUFFERED_INPUT)
56 extern int posixly_correct;
57 extern int interactive, interactive_shell;
58 extern REDIRECT *redirection_undo_list;
59 extern REDIRECT *exec_redirection_undo_list;
61 /* Static functions defined and used in this file. */
62 static void add_undo_close_redirect ();
63 static void add_exec_redirect ();
64 static int add_undo_redirect ();
65 static int do_redirection_internal ();
66 static int expandable_redirection_filename ();
67 static int stdin_redirection ();
69 /* Spare redirector used when translating [N]>&WORD or [N]<&WORD to a new
70 redirection and when creating the redirection undo list. */
73 /* Set to errno when a here document cannot be created for some reason.
74 Used to print a reasonable error message. */
75 static int heredoc_errno;
78 redirection_error (temp, error)
85 if (expandable_redirection_filename (temp))
87 if (posixly_correct && interactive_shell == 0)
89 oflags = temp->redirectee.filename->flags;
90 temp->redirectee.filename->flags |= W_NOGLOB;
92 filename = redirection_expand (temp->redirectee.filename);
93 if (posixly_correct && interactive_shell == 0)
94 temp->redirectee.filename->flags = oflags;
96 filename = savestring (temp->redirectee.filename->word);
99 filename = xmalloc (1);
104 filename = itos (temp->redirectee.dest);
108 case AMBIGUOUS_REDIRECT:
109 internal_error ("%s: ambiguous redirect", filename);
112 case NOCLOBBER_REDIRECT:
113 internal_error ("%s: cannot overwrite existing file", filename);
116 #if defined (RESTRICTED_SHELL)
117 case RESTRICTED_REDIRECT:
118 internal_error ("%s: restricted: cannot redirect output", filename);
120 #endif /* RESTRICTED_SHELL */
122 case HEREDOC_REDIRECT:
123 internal_error ("cannot create temp file for here document: %s", strerror (heredoc_errno));
127 internal_error ("%s: %s", filename, strerror (error));
134 /* Perform the redirections on LIST. If FOR_REAL, then actually make
135 input and output file descriptors, otherwise just do whatever is
136 neccessary for side effecting. INTERNAL says to remember how to
137 undo the redirections later, if non-zero. If SET_CLEXEC is non-zero,
138 file descriptors opened in do_redirection () have their close-on-exec
141 do_redirections (list, for_real, internal, set_clexec)
143 int for_real, internal, set_clexec;
150 if (redirection_undo_list)
152 dispose_redirects (redirection_undo_list);
153 redirection_undo_list = (REDIRECT *)NULL;
155 if (exec_redirection_undo_list)
156 dispose_exec_redirects ();
159 for (temp = list; temp; temp = temp->next)
161 error = do_redirection_internal (temp, for_real, internal, set_clexec);
164 redirection_error (temp, error);
171 /* Return non-zero if the redirection pointed to by REDIRECT has a
172 redirectee.filename that can be expanded. */
174 expandable_redirection_filename (redirect)
177 switch (redirect->instruction)
179 case r_output_direction:
181 case r_input_direction:
182 case r_inputa_direction:
186 case r_duplicating_input_word:
187 case r_duplicating_output_word:
195 /* Expand the word in WORD returning a string. If WORD expands to
196 multiple words (or no words), then return NULL. */
198 redirection_expand (word)
202 WORD_LIST *tlist1, *tlist2;
205 w = copy_word (word);
207 w->flags |= W_NOSPLIT;
209 tlist1 = make_word_list (w, (WORD_LIST *)NULL);
210 tlist2 = expand_words_no_vars (tlist1);
211 dispose_words (tlist1);
213 if (!tlist2 || tlist2->next)
215 /* We expanded to no words, or to more than a single word.
216 Dispose of the word list and return NULL. */
218 dispose_words (tlist2);
219 return ((char *)NULL);
221 result = string_list (tlist2); /* XXX savestring (tlist2->word->word)? */
222 dispose_words (tlist2);
226 /* Write the text of the here document pointed to by REDIRECTEE to the file
227 descriptor FD, which is already open to a temp file. Return 0 if the
228 write is successful, otherwise return errno. */
230 write_here_document (fd, redirectee)
232 WORD_DESC *redirectee;
235 int document_len, fd2;
237 register WORD_LIST *t, *tlist;
239 /* Expand the text if the word that was specified had
240 no quoting. The text that we expand is treated
241 exactly as if it were surrounded by double quotes. */
243 if (redirectee->flags & W_QUOTED)
245 document = redirectee->word;
246 document_len = strlen (document);
247 /* Set errno to something reasonable if the write fails. */
248 if (write (fd, document, document_len) < document_len)
258 tlist = expand_string (redirectee->word, Q_HERE_DOCUMENT);
261 /* Try using buffered I/O (stdio) and writing a word
262 at a time, letting stdio do the work of buffering
263 for us rather than managing our own strings. Most
264 stdios are not particularly fast, however -- this
265 may need to be reconsidered later. */
266 if ((fd2 = dup (fd)) < 0 || (fp = fdopen (fd2, "w")) == NULL)
273 for (t = tlist; t; t = t->next)
275 /* This is essentially the body of
276 string_list_internal expanded inline. */
277 document = t->word->word;
278 document_len = strlen (document);
280 putc (' ', fp); /* separator */
281 fwrite (document, document_len, 1, fp);
288 dispose_words (tlist);
293 dispose_words (tlist);
298 /* Create a temporary file holding the text of the here document pointed to
299 by REDIRECTEE, and return a file descriptor open for reading to the temp
300 file. Return -1 on any error, and make sure errno is set appropriately. */
302 here_document_to_fd (redirectee)
303 WORD_DESC *redirectee;
309 fd = sh_mktmpfd ("sh-thd", MT_USERANDOM, &filename);
311 /* If we failed for some reason other than the file existing, abort */
318 errno = r = 0; /* XXX */
319 /* write_here_document returns 0 on success, errno on failure. */
320 if (redirectee->word)
321 r = write_here_document (fd, redirectee);
332 /* In an attempt to avoid races, we close the first fd only after opening
334 /* Make the document really temporary. Also make it the input. */
335 fd2 = open (filename, O_RDONLY, 0600);
348 if (unlink (filename) < 0)
351 #if defined (__CYGWIN__)
352 /* Under CygWin 1.1.0, the unlink will fail if the file is
353 open. This hack will allow the previous action of silently
354 ignoring the error, but will still leave the file there. This
355 needs some kind of magic. */
358 #endif /* __CYGWIN__ */
370 #define RF_DEVSTDERR 2
371 #define RF_DEVSTDIN 3
372 #define RF_DEVSTDOUT 4
376 /* A list of pattern/value pairs for filenames that the redirection
377 code handles specially. */
378 static STRING_INT_ALIST _redir_special_filenames[] = {
379 #if !defined (HAVE_DEV_FD)
380 { "/dev/fd/[0-9]*", RF_DEVFD },
382 #if !defined (HAVE_DEV_STDIN)
383 { "/dev/stderr", RF_DEVSTDERR },
384 { "/dev/stdin", RF_DEVSTDIN },
385 { "/dev/stdout", RF_DEVSTDOUT },
387 #if defined (NETWORK_REDIRECTIONS)
388 { "/dev/tcp/*/*", RF_DEVTCP },
389 { "/dev/udp/*/*", RF_DEVUDP },
395 redir_special_open (spec, filename, flags, mode, ri)
399 enum r_instruction ri;
407 #if !defined (HAVE_DEV_FD)
409 if (legal_number, filename+8, &lfd)
410 fd = fcntl ((int)lfd, F_DUPFD, 10);
412 fd = AMBIGUOUS_REDIRECT;
416 #if !defined (HAVE_DEV_STDIN)
418 fd = fcntl (0, F_DUPFD, 10);
421 fd = fcntl (1, F_DUPFD, 10);
424 fd = fcntl (2, F_DUPFD, 10);
428 #if defined (NETWORK_REDIRECTIONS)
431 #if defined (HAVE_NETWORK)
432 fd = netopen (filename);
434 internal_warning ("/dev/(tcp|udp)/host/port not supported without networking");
435 fd = open (filename, flags, mode);
438 #endif /* NETWORK_REDIRECTIONS */
444 /* Open FILENAME with FLAGS in noclobber mode, hopefully avoiding most
445 race conditions and avoiding the problem where the file is replaced
446 between the stat(2) and open(2). */
448 noclobber_open (filename, flags, mode, ri)
451 enum r_instruction ri;
454 struct stat finfo, finfo2;
456 /* If the file exists and is a regular file, return an error
458 r = stat (filename, &finfo);
459 if (r == 0 && (S_ISREG (finfo.st_mode)))
460 return (NOCLOBBER_REDIRECT);
462 /* If the file was not present (r != 0), make sure we open it
463 exclusively so that if it is created before we open it, our open
464 will fail. Make sure that we do not truncate an existing file.
465 Note that we don't turn on O_EXCL unless the stat failed -- if
466 the file was not a regular file, we leave O_EXCL off. */
470 fd = open (filename, flags|O_EXCL, mode);
471 return ((fd < 0 && errno == EEXIST) ? NOCLOBBER_REDIRECT : fd);
473 fd = open (filename, flags, mode);
475 /* If the open failed, return the file descriptor right away. */
477 return (errno == EEXIST ? NOCLOBBER_REDIRECT : fd);
479 /* OK, the open succeeded, but the file may have been changed from a
480 non-regular file to a regular file between the stat and the open.
481 We are assuming that the O_EXCL open handles the case where FILENAME
482 did not exist and is symlinked to an existing file between the stat
485 /* If we can open it and fstat the file descriptor, and neither check
486 revealed that it was a regular file, and the file has not been replaced,
487 return the file descriptor. */
488 if ((fstat (fd, &finfo2) == 0) && (S_ISREG (finfo2.st_mode) == 0) &&
489 r == 0 && (S_ISREG (finfo.st_mode) == 0) &&
490 same_file (filename, filename, &finfo, &finfo2))
493 /* The file has been replaced. badness. */
496 return (NOCLOBBER_REDIRECT);
500 redir_open (filename, flags, mode, ri)
503 enum r_instruction ri;
507 r = find_string_in_alist (filename, _redir_special_filenames, 1);
509 return (redir_special_open (r, filename, flags, mode, ri));
511 /* If we are in noclobber mode, you are not allowed to overwrite
512 existing files. Check before opening. */
513 if (noclobber && CLOBBERING_REDIRECT (ri))
515 fd = noclobber_open (filename, flags, mode, ri);
516 if (fd == NOCLOBBER_REDIRECT)
517 return (NOCLOBBER_REDIRECT);
521 fd = open (filename, flags, mode);
523 if ((fd < 0) && (errno == EACCES))
524 fd = open (filename, flags & ~O_CREAT, mode);
531 /* Do the specific redirection requested. Returns errno or one of the
532 special redirection errors (*_REDIRECT) in case of error, 0 on success.
533 If FOR_REAL is zero, then just do whatever is neccessary to produce the
534 appropriate side effects. REMEMBERING, if non-zero, says to remember
535 how to undo each redirection. If SET_CLEXEC is non-zero, then
536 we set all file descriptors > 2 that we open to be close-on-exec. */
538 do_redirection_internal (redirect, for_real, remembering, set_clexec)
540 int for_real, remembering, set_clexec;
542 WORD_DESC *redirectee;
543 int redir_fd, fd, redirector, r, oflags;
544 char *redirectee_word;
545 enum r_instruction ri;
546 REDIRECT *new_redirect;
548 redirectee = redirect->redirectee.filename;
549 redir_fd = redirect->redirectee.dest;
550 redirector = redirect->redirector;
551 ri = redirect->instruction;
553 if (ri == r_duplicating_input_word || ri == r_duplicating_output_word)
555 /* We have [N]>&WORD or [N]<&WORD. Expand WORD, then translate
556 the redirection into a new one and continue. */
557 redirectee_word = redirection_expand (redirectee);
559 if (redirectee_word == 0)
560 return (AMBIGUOUS_REDIRECT);
561 else if (redirectee_word[0] == '-' && redirectee_word[1] == '\0')
564 new_redirect = make_redirection (redirector, r_close_this, rd);
566 else if (all_digits (redirectee_word))
568 if (ri == r_duplicating_input_word)
570 rd.dest = atol (redirectee_word);
571 new_redirect = make_redirection (redirector, r_duplicating_input, rd);
575 rd.dest = atol (redirectee_word);
576 new_redirect = make_redirection (redirector, r_duplicating_output, rd);
579 else if (ri == r_duplicating_output_word && redirector == 1)
581 rd.filename = make_bare_word (redirectee_word);
582 new_redirect = make_redirection (1, r_err_and_out, rd);
586 free (redirectee_word);
587 return (AMBIGUOUS_REDIRECT);
590 free (redirectee_word);
592 /* Set up the variables needed by the rest of the function from the
594 if (new_redirect->instruction == r_err_and_out)
598 /* Copy the word without allocating any memory that must be
600 redirectee = (WORD_DESC *)alloca (sizeof (WORD_DESC));
601 xbcopy ((char *)new_redirect->redirectee.filename,
602 (char *)redirectee, sizeof (WORD_DESC));
604 alloca_hack = (char *)
605 alloca (1 + strlen (new_redirect->redirectee.filename->word));
606 redirectee->word = alloca_hack;
607 strcpy (redirectee->word, new_redirect->redirectee.filename->word);
610 /* It's guaranteed to be an integer, and shouldn't be freed. */
611 redirectee = new_redirect->redirectee.filename;
613 redir_fd = new_redirect->redirectee.dest;
614 redirector = new_redirect->redirector;
615 ri = new_redirect->instruction;
617 /* Overwrite the flags element of the old redirect with the new value. */
618 redirect->flags = new_redirect->flags;
619 dispose_redirects (new_redirect);
624 case r_output_direction:
626 case r_input_direction:
627 case r_inputa_direction:
628 case r_err_and_out: /* command &>filename */
631 if (posixly_correct && interactive_shell == 0)
633 oflags = redirectee->flags;
634 redirectee->flags |= W_NOGLOB;
636 redirectee_word = redirection_expand (redirectee);
637 if (posixly_correct && interactive_shell == 0)
638 redirectee->flags = oflags;
640 if (redirectee_word == 0)
641 return (AMBIGUOUS_REDIRECT);
643 #if defined (RESTRICTED_SHELL)
644 if (restricted && (WRITE_REDIRECT (ri)))
646 free (redirectee_word);
647 return (RESTRICTED_REDIRECT);
649 #endif /* RESTRICTED_SHELL */
651 fd = redir_open (redirectee_word, redirect->flags, 0666, ri);
652 free (redirectee_word);
654 if (fd == NOCLOBBER_REDIRECT)
663 /* Only setup to undo it if the thing to undo is active. */
664 if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
665 add_undo_redirect (redirector);
667 add_undo_close_redirect (redirector);
669 #if defined (BUFFERED_INPUT)
670 check_bash_input (redirector);
673 if ((fd != redirector) && (dup2 (fd, redirector) < 0))
676 #if defined (BUFFERED_INPUT)
677 /* Do not change the buffered stream for an implicit redirection
678 of /dev/null to fd 0 for asynchronous commands without job
679 control (r_inputa_direction). */
680 if (ri == r_input_direction || ri == r_input_output)
681 duplicate_buffered_stream (fd, redirector);
682 #endif /* BUFFERED_INPUT */
685 * If we're remembering, then this is the result of a while, for
686 * or until loop with a loop redirection, or a function/builtin
687 * executing in the parent shell with a redirection. In the
688 * function/builtin case, we want to set all file descriptors > 2
689 * to be close-on-exec to duplicate the effect of the old
690 * for i = 3 to NOFILE close(i) loop. In the case of the loops,
691 * both sh and ksh leave the file descriptors open across execs.
692 * The Posix standard mentions only the exec builtin.
694 if (set_clexec && (redirector > 2))
695 SET_CLOSE_ON_EXEC (redirector);
698 if (fd != redirector)
700 #if defined (BUFFERED_INPUT)
701 if (INPUT_REDIRECT (ri))
702 close_buffered_fd (fd);
704 #endif /* !BUFFERED_INPUT */
705 close (fd); /* Don't close what we just opened! */
708 /* If we are hacking both stdout and stderr, do the stderr
710 if (ri == r_err_and_out)
715 add_undo_redirect (2);
722 case r_reading_until:
723 case r_deblank_reading_until:
724 /* REDIRECTEE is a pointer to a WORD_DESC containing the text of
725 the new input. Place it in a temporary file. */
728 fd = here_document_to_fd (redirectee);
732 heredoc_errno = errno;
733 return (HEREDOC_REDIRECT);
739 /* Only setup to undo it if the thing to undo is active. */
740 if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
741 add_undo_redirect (redirector);
743 add_undo_close_redirect (redirector);
745 #if defined (BUFFERED_INPUT)
746 check_bash_input (redirector);
748 if (fd != redirector && dup2 (fd, redirector) < 0)
755 #if defined (BUFFERED_INPUT)
756 duplicate_buffered_stream (fd, redirector);
759 if (set_clexec && (redirector > 2))
760 SET_CLOSE_ON_EXEC (redirector);
763 if (fd != redirector)
764 #if defined (BUFFERED_INPUT)
765 close_buffered_fd (fd);
772 case r_duplicating_input:
773 case r_duplicating_output:
774 if (for_real && (redir_fd != redirector))
777 /* Only setup to undo it if the thing to undo is active. */
778 if (fcntl (redirector, F_GETFD, 0) != -1)
779 add_undo_redirect (redirector);
781 add_undo_close_redirect (redirector);
783 #if defined (BUFFERED_INPUT)
784 check_bash_input (redirector);
786 /* This is correct. 2>&1 means dup2 (1, 2); */
787 if (dup2 (redir_fd, redirector) < 0)
790 #if defined (BUFFERED_INPUT)
791 if (ri == r_duplicating_input)
792 duplicate_buffered_stream (redir_fd, redirector);
793 #endif /* BUFFERED_INPUT */
795 /* First duplicate the close-on-exec state of redirectee. dup2
796 leaves the flag unset on the new descriptor, which means it
797 stays open. Only set the close-on-exec bit for file descriptors
798 greater than 2 in any case, since 0-2 should always be open
799 unless closed by something like `exec 2<&-'. */
800 /* if ((already_set || set_unconditionally) && (ok_to_set))
802 if (((fcntl (redir_fd, F_GETFD, 0) == 1) || set_clexec) &&
804 SET_CLOSE_ON_EXEC (redirector);
811 if (remembering && (fcntl (redirector, F_GETFD, 0) != -1))
812 add_undo_redirect (redirector);
814 #if defined (BUFFERED_INPUT)
815 check_bash_input (redirector);
816 close_buffered_fd (redirector);
817 #else /* !BUFFERED_INPUT */
819 #endif /* !BUFFERED_INPUT */
823 case r_duplicating_input_word:
824 case r_duplicating_output_word:
830 #define SHELL_FD_BASE 10
832 /* Remember the file descriptor associated with the slot FD,
833 on REDIRECTION_UNDO_LIST. Note that the list will be reversed
834 before it is executed. Any redirections that need to be undone
835 even if REDIRECTION_UNDO_LIST is discarded by the exec builtin
836 are also saved on EXEC_REDIRECTION_UNDO_LIST. */
838 add_undo_redirect (fd)
841 int new_fd, clexec_flag;
842 REDIRECT *new_redirect, *closer, *dummy_redirect;
844 new_fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE);
848 sys_error ("redirection error: cannot duplicate fd");
852 clexec_flag = fcntl (fd, F_GETFD, 0);
855 closer = make_redirection (new_fd, r_close_this, rd);
856 dummy_redirect = copy_redirects (closer);
858 rd.dest = (long)new_fd;
860 new_redirect = make_redirection (fd, r_duplicating_input, rd);
862 new_redirect = make_redirection (fd, r_duplicating_output, rd);
863 new_redirect->next = closer;
865 closer->next = redirection_undo_list;
866 redirection_undo_list = new_redirect;
868 /* Save redirections that need to be undone even if the undo list
869 is thrown away by the `exec' builtin. */
870 add_exec_redirect (dummy_redirect);
872 /* File descriptors used only for saving others should always be
873 marked close-on-exec. Unfortunately, we have to preserve the
874 close-on-exec state of the file descriptor we are saving, since
875 fcntl (F_DUPFD) sets the new file descriptor to remain open
876 across execs. If, however, the file descriptor whose state we
877 are saving is <= 2, we can just set the close-on-exec flag,
878 because file descriptors 0-2 should always be open-on-exec,
879 and the restore above in do_redirection() will take care of it. */
880 if (clexec_flag || fd < 3)
881 SET_CLOSE_ON_EXEC (new_fd);
886 /* Set up to close FD when we are finished with the current command
887 and its redirections. */
889 add_undo_close_redirect (fd)
895 closer = make_redirection (fd, r_close_this, rd);
896 closer->next = redirection_undo_list;
897 redirection_undo_list = closer;
901 add_exec_redirect (dummy_redirect)
902 REDIRECT *dummy_redirect;
904 dummy_redirect->next = exec_redirection_undo_list;
905 exec_redirection_undo_list = dummy_redirect;
908 /* Return 1 if the redirection specified by RI and REDIRECTOR alters the
911 stdin_redirection (ri, redirector)
912 enum r_instruction ri;
917 case r_input_direction:
918 case r_inputa_direction:
920 case r_reading_until:
921 case r_deblank_reading_until:
923 case r_duplicating_input:
924 case r_duplicating_input_word:
926 return (redirector == 0);
927 case r_output_direction:
929 case r_duplicating_output:
932 case r_duplicating_output_word:
938 /* Return non-zero if any of the redirections in REDIRS alter the standard
941 stdin_redirects (redirs)
947 for (n = 0, rp = redirs; rp; rp = rp->next)
948 n += stdin_redirection (rp->instruction, rp->redirector);