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