2 * sh.sem.c: I/O redirections and job forking. A touchy issue!
3 * Most stuff with builtins is incorrect
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #endif /*WINNT_NATIVE*/
44 # endif /* CLEX_DUPS */
46 #endif /* CLOSE_ON_EXEC */
48 #if defined(__sparc__) || defined(sparc)
49 # if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(__linux__) && !defined(__GNU__) && !defined(__GLIBC__)
51 # endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !glibc */
52 #endif /* __sparc__ || sparc */
55 static void vffree (int);
57 static Char
*splicepipe (struct command
*, Char
*);
58 static void doio (struct command
*, int *, int *);
59 static void chkclob (const char *);
66 * For SVR4, there are problems with pipelines having the first process as
67 * the group leader. The problem occurs when the first process exits before
68 * the others have a chance to setpgid(). This is because in SVR4 you can't
69 * have a zombie as a group leader. The solution I have used is to reverse
70 * the order in which pipelines are started, making the last process the
71 * group leader. (Note I am not using 'pipeline' in the generic sense -- I
72 * mean processes connected by '|'.) I don't know yet if this causes other
75 * All the changes for this are in execute(), and are enclosed in
78 * David Dawes (dawes@physics.su.oz.au) Oct 1991
83 execute(struct command
*t
, volatile int wanttty
, int *pipein
, int *pipeout
,
87 const struct biltins
* volatile bifunc
;
91 static sigset_t csigset
;
93 static int onosigchld
= 0;
95 static int nosigchld
= 0;
106 if ((varval(STRNTslowexec
) == STRNULL
) &&
107 !t
->t_dcdr
&& !t
->t_dcar
&& !t
->t_dflg
&& !didfds
&&
108 (intty
|| intact
) && (t
->t_dtyp
== NODE_COMMAND
) &&
110 if ((t
->t_dcom
[0][0] & (QUOTE
| TRIM
)) == QUOTE
)
111 (void) Strcpy(t
->t_dcom
[0], t
->t_dcom
[0] + 1);
113 if (nt_try_fast_exec(t
) == 0)
117 #endif /* WINNT_NATIVE */
120 * Ed hutchins@sgi.com & Dominic dbg@sgi.com
121 * Sat Feb 25 03:13:11 PST 1995
122 * try implicit cd if we have a 1 word command
124 if (implicit_cd
&& (intty
|| intact
) && t
->t_dcom
&& t
->t_dcom
[0] &&
125 t
->t_dcom
[0][0] && (blklen(t
->t_dcom
) == 1) && !noexec
) {
130 sCName
= dollar(t
->t_dcom
[0]);
131 if (sCName
!= NULL
&& sCName
[0] == '~') {
132 struct Strbuf buf
= Strbuf_INIT
;
133 const Char
*name_end
;
135 for (name_end
= sCName
+ 1; *name_end
!= '\0' && *name_end
!= '/';
138 if (name_end
!= sCName
+ 1) {
141 name
= Strnsave(sCName
+ 1, name_end
- (sCName
+ 1));
142 home
= gethdir(name
);
144 Strbuf_append(&buf
, home
);
147 Strbuf_append(&buf
, name
);
150 Strbuf_append(&buf
, varval(STRhome
));
151 Strbuf_append(&buf
, name_end
);
153 sCName
= Strbuf_finish(&buf
);
156 pathname
= short2str(sCName
);
158 /* if this is a dir, tack a "cd" on as the first arg */
159 if (pathname
!= NULL
&&
160 ((stat(pathname
, &stbuf
) != -1 && S_ISDIR(stbuf
.st_mode
))
162 || (pathname
[0] && pathname
[1] == ':' && pathname
[2] == '\0')
163 #endif /* WINNT_NATIVE */
166 Char
**ot_dcom
= t
->t_dcom
;
168 vCD
[0] = Strsave(STRcd
);
170 t
->t_dcom
= blkspl(vCD
, ot_dcom
);
172 if (implicit_cd
> 1) {
180 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
181 * Don't check for wantty > 0...
183 if (t
->t_dflg
& F_AMPERSAND
)
188 if ((t
->t_dcom
[0][0] & (QUOTE
| TRIM
)) == QUOTE
)
189 memmove(t
->t_dcom
[0], t
->t_dcom
[0] + 1,
190 (Strlen(t
->t_dcom
[0] + 1) + 1) * sizeof (*t
->t_dcom
[0]));
191 if ((t
->t_dflg
& F_REPEAT
) == 0)
192 Dfix(t
); /* $ " ' \ */
193 if (t
->t_dcom
[0] == 0) {
200 if (t
->t_dflg
& F_PIPEIN
)
202 #else /* !BACKPIPE */
203 if (t
->t_dflg
& F_PIPEOUT
)
205 #endif /* BACKPIPE */
207 * Must do << early so parent will know where input pointer should be.
208 * If noexec then this is all we do.
210 if (t
->t_dflg
& F_READ
) {
211 int old_pintr_disabled
;
215 pintr_push_enable(&old_pintr_disabled
);
218 cleanup_until(&old_pintr_disabled
);
226 * This mess is the necessary kludge to handle the prefix builtins:
227 * nice, nohup, time. These commands can also be used by themselves,
228 * and this is not handled here. This will also work when loops are
231 while (t
->t_dtyp
== NODE_COMMAND
)
232 if (eq(t
->t_dcom
[0], STRnice
)) {
234 if (strchr("+-", t
->t_dcom
[1][0])) {
237 t
->t_nice
= (unsigned char)getn(t
->t_dcom
[1]);
238 lshift(t
->t_dcom
, 2);
246 lshift(t
->t_dcom
, 1);
253 else if (eq(t
->t_dcom
[0], STRnohup
)) {
255 t
->t_dflg
|= F_NOHUP
;
256 lshift(t
->t_dcom
, 1);
261 else if (eq(t
->t_dcom
[0], STRhup
)) {
264 lshift(t
->t_dcom
, 1);
269 else if (eq(t
->t_dcom
[0], STRtime
)) {
272 lshift(t
->t_dcom
, 1);
278 else if (eq(t
->t_dcom
[0], STRver
))
279 if (t
->t_dcom
[1] && t
->t_dcom
[2]) {
281 t
->t_systype
= getv(t
->t_dcom
[1]);
282 lshift(t
->t_dcom
, 2);
291 /* is it a command */
292 if (t
->t_dtyp
== NODE_COMMAND
) {
294 * Check if we have a builtin function and remember which one.
299 * Continue for builtins that are part of the scripting language
303 if (bifunc
->bfunct
!= (bfunc_t
)dobreak
&&
304 bifunc
->bfunct
!= (bfunc_t
)docontin
&&
305 bifunc
->bfunct
!= (bfunc_t
)doelse
&&
306 bifunc
->bfunct
!= (bfunc_t
)doend
&&
307 bifunc
->bfunct
!= (bfunc_t
)doforeach
&&
308 bifunc
->bfunct
!= (bfunc_t
)dogoto
&&
309 bifunc
->bfunct
!= (bfunc_t
)doif
&&
310 bifunc
->bfunct
!= (bfunc_t
)dorepeat
&&
311 bifunc
->bfunct
!= (bfunc_t
)doswbrk
&&
312 bifunc
->bfunct
!= (bfunc_t
)doswitch
&&
313 bifunc
->bfunct
!= (bfunc_t
)dowhile
&&
314 bifunc
->bfunct
!= (bfunc_t
)dozip
)
318 else { /* not a command */
325 * GrP Executing a command - run jobcmd hook
326 * Don't run for builtins
327 * Don't run if we're not in a tty
328 * Don't run if we're not really executing
331 * CR - Charles Ross Aug 2005
333 * The new behavior is that the jobcmd won't be executed
334 * if stdout (SHOUT) isnt attached to a tty.. IE when
335 * redirecting, or using backquotes etc..
337 if (t
->t_dtyp
== NODE_COMMAND
&& !bifunc
&& !noexec
&& intty
&& isoutatty
) {
338 Char
*cmd
= unparse(t
);
340 cleanup_push(cmd
, xfree
);
346 * We fork only if we are timed, or are not the end of a parenthesized
347 * list and not a simple builtin function. Simple meaning one that is
348 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
349 * fork in some of these cases.
353 * Can't have NOFORK for the tail of a pipe - because it is not the
354 * last command spawned (even if it is at the end of a parenthesised
357 if (t
->t_dflg
& F_PIPEIN
)
358 t
->t_dflg
&= ~(F_NOFORK
);
361 * "command | builtin" may cause major misbehaviour as noted in
362 * in the BUGS file entry
363 * Subject: Redirected input to built-in functions misbehaves badly
364 * forking when the builtin is the end of the pipe corrects the
367 if (bifunc
&& (t
->t_dflg
& F_PIPEIN
))
368 t
->t_dflg
&= ~(F_NOFORK
);
369 #endif /* BACKPIPE */
371 * Prevent forking cd, pushd, popd, chdir cause this will cause the
372 * shell not to change dir! (XXX: but only for nice?)
374 if (bifunc
&& (bifunc
->bfunct
== (bfunc_t
)dochngd
||
375 bifunc
->bfunct
== (bfunc_t
)dopushd
||
376 bifunc
->bfunct
== (bfunc_t
)dopopd
))
377 t
->t_dflg
&= ~(F_NICE
);
379 if (((t
->t_dflg
& F_TIME
) || ((t
->t_dflg
& F_NOFORK
) == 0 &&
380 (!bifunc
|| t
->t_dflg
&
381 (F_PIPEOUT
| F_AMPERSAND
| F_NICE
| F_NOHUP
| F_HUP
)))) ||
383 * We have to fork for eval too.
385 (bifunc
&& (t
->t_dflg
& F_PIPEIN
) != 0 &&
386 bifunc
->bfunct
== (bfunc_t
)doeval
)) {
388 if (t
->t_dtyp
== NODE_PAREN
||
389 t
->t_dflg
& (F_REPEAT
| F_AMPERSAND
) || bifunc
)
394 * We need to block SIGCHLD here, so that if the process does
395 * not die before we can set the process group
397 if (wanttty
>= 0 && !nosigchld
) {
399 sigaddset(&set
, SIGCHLD
);
400 (void)sigprocmask(SIG_BLOCK
, &set
, &csigset
);
405 pid
= pfork(t
, wanttty
);
406 if (pid
== 0 && nosigchld
) {
407 sigprocmask(SIG_SETMASK
, &csigset
, NULL
);
410 else if (pid
!= 0 && (t
->t_dflg
& F_AMPERSAND
))
416 int ochild
, osetintr
, ohaderr
, odidfds
;
417 int oSHIN
, oSHOUT
, oSHDIAG
, oOLDSTD
, otpgrp
;
418 int oisoutatty
, oisdiagatty
;
419 sigset_t oset
, ocsigset
;
420 # ifndef CLOSE_ON_EXEC
422 # endif /* !CLOSE_ON_EXEC */
425 * Prepare for the vfork by saving everything that the child
426 * corrupts before it exec's. Note that in some signal
427 * implementations which keep the signal info in user space
428 * (e.g. Sun's) it will also be necessary to save and restore
429 * the current sigvec's for the signals the child touches
434 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
435 * Gilbrech - 11/22/87)
438 struct sigaction savesv
[NSIGSAVED
];
441 # endif /* SAVESIGVEC */
442 if (wanttty
>= 0 && !nosigchld
&& !noexec
) {
444 sigaddset(&set
, SIGCHLD
);
445 (void)sigprocmask(SIG_BLOCK
, &set
, &csigset
);
449 sigaddset(&set
, SIGCHLD
);
450 sigaddset(&set
, SIGINT
);
451 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
456 # ifndef CLOSE_ON_EXEC
458 # endif /* !CLOSE_ON_EXEC */
464 oisoutatty
= isoutatty
;
465 oisdiagatty
= isdiagatty
;
467 onosigchld
= nosigchld
;
472 savesigvec(savesv
, savesm
);
473 # endif /* SAVESIGVEC */
481 restoresigvec(savesv
, savesm
);
482 # endif /* SAVESIGVEC */
483 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
484 stderror(ERR_NOPROC
);
487 if (pid
) { /* parent */
489 restoresigvec(savesv
, savesm
);
490 # endif /* SAVESIGVEC */
496 # ifndef CLOSE_ON_EXEC
498 # endif /* !CLOSE_ON_EXEC */
503 isoutatty
= oisoutatty
;
504 isdiagatty
= oisdiagatty
;
506 nosigchld
= onosigchld
;
516 /* this is from pfork() */
518 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
521 /* this is from pfork() */
525 sigprocmask(SIG_SETMASK
, &csigset
, NULL
);
530 ignint
= (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
))
531 || (gointr
&& eq(gointr
, STRminus
));
532 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: getpid();
537 * casts made right for SunOS 4.0 by Douglas C. Schmidt
538 * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
541 * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
545 (void) signal(SIGINT
, SIG_IGN
);
546 (void) signal(SIGQUIT
, SIG_IGN
);
549 (void) signal(SIGINT
, vffree
);
550 (void) signal(SIGQUIT
, SIG_DFL
);
554 (void) signal(SIGTSTP
, SIG_DFL
);
555 (void) signal(SIGTTIN
, SIG_DFL
);
556 (void) signal(SIGTTOU
, SIG_DFL
);
558 # endif /* BSDJOBS */
560 sigaction(SIGTERM
, &parterm
, NULL
);
562 else if (tpgrp
== -1 &&
563 (t
->t_dflg
& F_NOINTERRUPT
)) {
564 (void) signal(SIGINT
, SIG_IGN
);
565 (void) signal(SIGQUIT
, SIG_IGN
);
568 pgetty(wanttty
, pgrp
);
570 if (t
->t_dflg
& F_NOHUP
)
571 (void) signal(SIGHUP
, SIG_IGN
);
572 if (t
->t_dflg
& F_HUP
)
573 (void) signal(SIGHUP
, SIG_DFL
);
574 if (t
->t_dflg
& F_NICE
) {
575 int nval
= SIGN_EXTEND_CHAR(t
->t_nice
);
576 # if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
577 if (setpriority(PRIO_PROCESS
, 0, nval
) == -1 && errno
)
578 stderror(ERR_SYSTEM
, "setpriority",
580 # else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
582 # endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
585 if (t
->t_dflg
& F_VER
) {
586 tsetenv(STRSYSTYPE
, t
->t_systype
? STRbsd43
: STRsys53
);
597 * It would be better if we could wait for the whole job when we
598 * knew the last process had been started. Pwait, in fact, does
599 * wait for the whole job anyway, but this test doesn't really
600 * express our intentions.
603 if (didfds
== 0 && t
->t_dflg
& F_PIPEOUT
) {
607 if ((t
->t_dflg
& F_PIPEIN
) != 0)
609 #else /* !BACKPIPE */
610 if (didfds
== 0 && t
->t_dflg
& F_PIPEIN
) {
614 if ((t
->t_dflg
& F_PIPEOUT
) != 0)
616 #endif /* BACKPIPE */
619 sigprocmask(SIG_SETMASK
, &csigset
, NULL
);
622 if ((t
->t_dflg
& F_AMPERSAND
) == 0)
627 doio(t
, pipein
, pipeout
);
629 if (t
->t_dflg
& F_PIPEIN
) {
633 #else /* !BACKPIPE */
634 if (t
->t_dflg
& F_PIPEOUT
) {
638 #endif /* BACKPIPE */
640 * Perform a builtin function. If we are not forked, arrange for
649 int ohaderr
= haderr
;
657 if (adrof(STRprintexitvalue
)) {
658 int rv
= getstatus();
660 xprintf(CGETS(17, 2, "Exit %d\n"), rv
);
665 if (t
->t_dtyp
!= NODE_PAREN
) {
670 * For () commands must put new 0,1,2 in FSH* and recurse
672 if ((OLDSTD
= dcopy(0, FOLDSTD
)) >= 0)
673 (void)close_on_exec(OLDSTD
, 1);
674 if ((SHOUT
= dcopy(1, FSHOUT
)) >= 0) {
675 (void)close_on_exec(SHOUT
, 1);
676 isoutatty
= isatty(SHOUT
);
678 if ((SHDIAG
= dcopy(2, FSHDIAG
)) >= 0) {
679 (void)close_on_exec(SHDIAG
, 1);
680 isdiagatty
= isatty(SHDIAG
);
684 #ifndef CLOSE_ON_EXEC
687 (void) close_on_exec(FSHOUT
, 1);
688 (void) close_on_exec(FSHDIAG
, 1);
689 (void) close_on_exec(FOLDSTD
, 1);
690 #endif /* !CLOSE_ON_EXEC */
693 t
->t_dspr
->t_dflg
|= t
->t_dflg
& (F_NOINTERRUPT
| F_BACKQ
);
694 execute(t
->t_dspr
, wanttty
, NULL
, NULL
, do_glob
);
699 t
->t_dcdr
->t_dflg
|= F_PIPEIN
| (t
->t_dflg
&
700 (F_PIPEOUT
| F_AMPERSAND
| F_NOFORK
| F_NOINTERRUPT
| F_BACKQ
));
701 execute(t
->t_dcdr
, wanttty
, pv
, pipeout
, do_glob
);
702 t
->t_dcar
->t_dflg
|= F_PIPEOUT
| (t
->t_dflg
&
703 (F_PIPEIN
| F_AMPERSAND
| F_STDERR
| F_NOINTERRUPT
| F_BACKQ
));
704 execute(t
->t_dcar
, wanttty
, pipein
, pv
, do_glob
);
705 #else /* !BACKPIPE */
706 t
->t_dcar
->t_dflg
|= F_PIPEOUT
| (t
->t_dflg
&
707 (F_PIPEIN
| F_AMPERSAND
| F_STDERR
| F_NOINTERRUPT
| F_BACKQ
));
708 execute(t
->t_dcar
, wanttty
, pipein
, pv
, do_glob
);
709 t
->t_dcdr
->t_dflg
|= F_PIPEIN
| (t
->t_dflg
&
710 (F_PIPEOUT
| F_AMPERSAND
| F_NOFORK
| F_NOINTERRUPT
| F_BACKQ
));
711 execute(t
->t_dcdr
, wanttty
, pv
, pipeout
, do_glob
);
712 #endif /* BACKPIPE */
717 t
->t_dcar
->t_dflg
|= t
->t_dflg
& (F_NOINTERRUPT
| F_BACKQ
);
718 execute(t
->t_dcar
, wanttty
, NULL
, NULL
, do_glob
);
720 * In strange case of A&B make a new job after A
722 if (t
->t_dcar
->t_dflg
& F_AMPERSAND
&& t
->t_dcdr
&&
723 (t
->t_dcdr
->t_dflg
& F_AMPERSAND
) == 0)
727 t
->t_dcdr
->t_dflg
|= t
->t_dflg
&
728 (F_NOFORK
| F_NOINTERRUPT
| F_BACKQ
);
729 execute(t
->t_dcdr
, wanttty
, NULL
, NULL
, do_glob
);
736 t
->t_dcar
->t_dflg
|= t
->t_dflg
& (F_NOINTERRUPT
| F_BACKQ
);
737 execute(t
->t_dcar
, wanttty
, NULL
, NULL
, do_glob
);
738 if ((getstatus() == 0) != (t
->t_dtyp
== NODE_AND
)) {
743 t
->t_dcdr
->t_dflg
|= t
->t_dflg
&
744 (F_NOFORK
| F_NOINTERRUPT
| F_BACKQ
);
745 execute(t
->t_dcdr
, wanttty
, NULL
, NULL
, do_glob
);
753 * Fall through for all breaks from switch
755 * If there will be no more executions of this command, flush all file
756 * descriptors. Places that turn on the F_REPEAT bit are responsible for
757 * doing donefds after the last re-execution
759 if (didfds
&& !(t
->t_dflg
& F_REPEAT
))
775 * Expand and glob the words after an i/o redirection.
776 * If more than one word is generated, then update the command vector.
778 * This is done differently in all the shells:
779 * 1. in the bourne shell and ksh globbing is not performed
780 * 2. Bash/csh say ambiguous
781 * 3. zsh does i/o to/from all the files
782 * 4. itcsh concatenates the words.
784 * I don't know what is best to do. I think that Ambiguous is better
785 * than restructuring the command vector, because the user can get
786 * unexpected results. In any case, the command vector restructuring
787 * code is present and the user can choose it by setting noambiguous
790 splicepipe(struct command
*t
, Char
*cp
)
794 if (adrof(STRnoambiguous
)) {
798 blk
[0] = Dfix1(cp
); /* expand $ */
803 pv
= globall(blk
, gflag
);
805 setname(short2str(blk
[0]));
807 stderror(ERR_NAME
| ERR_NOMATCH
);
809 if (pv
[1] != NULL
) { /* we need to fix the command vector */
810 Char
**av
= blkspl(t
->t_dcom
, &pv
[1]);
823 cleanup_push(buf
, xfree
);
824 blk
[0] = globone(buf
, G_ERROR
);
831 * Perform io redirection.
832 * We may or maynot be forked here.
835 doio(struct command
*t
, int *pipein
, int *pipeout
)
839 unsigned long flags
= t
->t_dflg
;
841 if (didfds
|| (flags
& F_REPEAT
))
843 if ((flags
& F_READ
) == 0) {/* F_READ already done */
848 * so < /dev/std{in,out,err} work
850 (void) dcopy(SHIN
, 0);
851 (void) dcopy(SHOUT
, 1);
852 (void) dcopy(SHDIAG
, 2);
853 cp
= splicepipe(t
, t
->t_dlef
);
854 tmp
= strsave(short2str(cp
));
856 cleanup_push(tmp
, xfree
);
857 if ((fd
= xopen(tmp
, O_RDONLY
|O_LARGEFILE
)) < 0)
858 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
860 /* allow input files larger than 2Gb */
862 (void) fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_LARGEFILE
);
863 #endif /*!WINNT_NATIVE*/
866 else if (flags
& F_PIPEIN
) {
868 TCSH_IGNORE(dup(pipein
[0]));
872 else if ((flags
& F_NOINTERRUPT
) && tpgrp
== -1) {
874 (void) xopen(_PATH_DEVNULL
, O_RDONLY
|O_LARGEFILE
);
878 TCSH_IGNORE(dup(OLDSTD
));
879 #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
881 * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
882 * across dup()s, so we have to UNSET it here or else we get a
883 * command with NO stdin, stdout, or stderr at all (a bad thing
886 (void) close_on_exec(0, 0);
887 #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
893 cp
= splicepipe(t
, t
->t_drit
);
894 tmp
= strsave(short2str(cp
));
896 cleanup_push(tmp
, xfree
);
898 * so > /dev/std{out,err} work
900 (void) dcopy(SHOUT
, 1);
901 (void) dcopy(SHDIAG
, 2);
902 if ((flags
& F_APPEND
) != 0) {
904 fd
= xopen(tmp
, O_WRONLY
|O_APPEND
|O_LARGEFILE
);
905 #else /* !O_APPEND */
906 fd
= xopen(tmp
, O_WRONLY
|O_LARGEFILE
);
907 (void) lseek(fd
, (off_t
) 0, L_XTND
);
908 #endif /* O_APPEND */
912 if ((flags
& F_APPEND
) == 0 || fd
== -1) {
913 if (!(flags
& F_OVERWRITE
) && no_clobber
) {
914 if (flags
& F_APPEND
)
915 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
918 if ((fd
= xcreat(tmp
, 0666)) < 0)
919 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
920 /* allow input files larger than 2Gb */
922 (void) fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_LARGEFILE
);
923 #endif /*!WINNT_NATIVE*/
929 else if (flags
& F_PIPEOUT
) {
931 TCSH_IGNORE(dup(pipeout
[1]));
936 TCSH_IGNORE(dup(SHOUT
));
938 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
939 (void) close_on_exec(1, 0);
940 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
944 if (flags
& F_STDERR
) {
949 TCSH_IGNORE(dup(SHDIAG
));
950 is2atty
= isdiagatty
;
951 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
952 (void) close_on_exec(2, 0);
953 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
964 (void)close_on_exec(pv
[0] = dmove(pv
[0], -1), 1);
965 (void)close_on_exec(pv
[1] = dmove(pv
[1], -1), 1);
966 if (pv
[0] >= 0 && pv
[1] >= 0)
977 chkclob(const char *cp
)
981 if (stat(cp
, &stb
) < 0)
983 if (S_ISCHR(stb
.st_mode
))
985 if (no_clobber
& NOCLOBBER_NOTEMPTY
&& stb
.st_size
== 0)
987 if (no_clobber
& NOCLOBBER_ASK
) {
988 if (getYN(CGETS(22, 15,
989 "Do you really want to overwrite an existing file? [N/y] ")))
993 stderror(ERR_EXISTS
, cp
);