1 /* $Header: /src/pub/tcsh/sh.sem.c,v 3.56 2002/03/08 17:36:46 christos Exp $ */
3 * sh.sem.c: I/O redirections and job forking. A touchy issue!
4 * Most stuff with builtins is incorrect
7 * Copyright (c) 1980, 1991 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: sh.sem.c,v 3.56 2002/03/08 17:36:46 christos Exp $")
42 #endif /*WINNT_NATIVE*/
48 # endif /* CLEX_DUPS */
50 #endif /* CLOSE_ON_EXEC */
52 #if defined(__sparc__) || defined(sparc)
53 # if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(linux)
55 # endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !linux */
56 #endif /* __sparc__ || sparc */
59 static sigret_t vffree
__P((int));
61 static Char
*splicepipe
__P((struct command
*, Char
*));
62 static void doio
__P((struct command
*, int *, int *));
63 static void chkclob
__P((char *));
70 * For SVR4, there are problems with pipelines having the first process as
71 * the group leader. The problem occurs when the first process exits before
72 * the others have a chance to setpgid(). This is because in SVR4 you can't
73 * have a zombie as a group leader. The solution I have used is to reverse
74 * the order in which pipelines are started, making the last process the
75 * group leader. (Note I am not using 'pipeline' in the generic sense -- I
76 * mean processes connected by '|'.) I don't know yet if this causes other
79 * All the changes for this are in execute(), and are enclosed in
82 * David Dawes (dawes@physics.su.oz.au) Oct 1991
87 execute(t
, wanttty
, pipein
, pipeout
, do_glob
)
88 register struct command
*t
;
90 int *pipein
, *pipeout
;
94 extern bool use_fork
; /* use fork() instead of vfork()? */
98 struct biltins
*bifunc
;
102 static sigmask_t csigmask
;
105 static int onosigchld
= 0;
107 static int nosigchld
= 0;
118 if ((varval(STRNTslowexec
) == STRNULL
) &&
119 !t
->t_dcdr
&& !t
->t_dcar
&& !t
->t_dflg
&& !didfds
&&
120 (intty
|| intact
) && (t
->t_dtyp
== NODE_COMMAND
) &&
122 if ((t
->t_dcom
[0][0] & (QUOTE
| TRIM
)) == QUOTE
)
123 (void) Strcpy(t
->t_dcom
[0], t
->t_dcom
[0] + 1);
125 if (nt_try_fast_exec(t
) == 0)
129 #endif /* WINNT_NATIVE */
132 * Ed hutchins@sgi.com & Dominic dbg@sgi.com
133 * Sat Feb 25 03:13:11 PST 1995
134 * try implicit cd if we have a 1 word command
136 if (implicit_cd
&& (intty
|| intact
) && t
->t_dcom
&& t
->t_dcom
[0] &&
137 t
->t_dcom
[0][0] && (blklen(t
->t_dcom
) == 1) && !noexec
) {
138 Char sCName
[MAXPATHLEN
];
143 dollar(sCName
, t
->t_dcom
[0]);
146 Char sCPath
[MAXPATHLEN
];
150 while (*pCN
&& *pCN
!= '/')
156 (void) Strcpy(sCPath
, varval(STRhome
));
157 catn(sCPath
, pCN
, MAXPATHLEN
);
158 (void) Strcpy(sCName
, sCPath
);
161 pathname
= short2str(sCName
);
162 /* if this is a dir, tack a "cd" on as the first arg */
163 if ((stat(pathname
, &stbuf
) != -1 && S_ISDIR(stbuf
.st_mode
))
165 || (pathname
[0] && pathname
[1] == ':' && pathname
[2] == '\0')
166 #endif /* WINNT_NATIVE */
169 Char
**ot_dcom
= t
->t_dcom
;
171 vCD
[0] = Strsave(STRcd
);
173 t
->t_dcom
= blkspl(vCD
, ot_dcom
);
174 if (implicit_cd
> 1) {
178 xfree((ptr_t
) ot_dcom
);
183 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
184 * Don't check for wantty > 0...
186 if (t
->t_dflg
& F_AMPERSAND
)
191 if ((t
->t_dcom
[0][0] & (QUOTE
| TRIM
)) == QUOTE
)
192 (void) Strcpy(t
->t_dcom
[0], t
->t_dcom
[0] + 1);
193 if ((t
->t_dflg
& F_REPEAT
) == 0)
194 Dfix(t
); /* $ " ' \ */
195 if (t
->t_dcom
[0] == 0) {
202 if (t
->t_dflg
& F_PIPEIN
)
204 #else /* !BACKPIPE */
205 if (t
->t_dflg
& F_PIPEOUT
)
207 #endif /* BACKPIPE */
209 * Must do << early so parent will know where input pointer should be.
210 * If noexec then this is all we do.
212 if (t
->t_dflg
& F_READ
) {
219 set(STRstatus
, Strsave(STR0
), VAR_READWRITE
);
222 * This mess is the necessary kludge to handle the prefix builtins:
223 * nice, nohup, time. These commands can also be used by themselves,
224 * and this is not handled here. This will also work when loops are
227 while (t
->t_dtyp
== NODE_COMMAND
)
228 if (eq(t
->t_dcom
[0], STRnice
)) {
230 if (strchr("+-", t
->t_dcom
[1][0])) {
235 lshift(t
->t_dcom
, 2);
243 lshift(t
->t_dcom
, 1);
250 else if (eq(t
->t_dcom
[0], STRnohup
)) {
252 t
->t_dflg
|= F_NOHUP
;
253 lshift(t
->t_dcom
, 1);
258 else if (eq(t
->t_dcom
[0], STRhup
)) {
261 lshift(t
->t_dcom
, 1);
266 else if (eq(t
->t_dcom
[0], STRtime
)) {
269 lshift(t
->t_dcom
, 1);
275 else if (eq(t
->t_dcom
[0], STRver
))
276 if (t
->t_dcom
[1] && t
->t_dcom
[2]) {
278 t
->t_systype
= getv(t
->t_dcom
[1]);
279 lshift(t
->t_dcom
, 2);
288 /* is it a command */
289 if (t
->t_dtyp
== NODE_COMMAND
) {
291 * Check if we have a builtin function and remember which one.
294 if (noexec
&& bifunc
) {
296 * Continue for builtins that are part of the scripting language
298 if (bifunc
->bfunct
!= (bfunc_t
)dobreak
&&
299 bifunc
->bfunct
!= (bfunc_t
)docontin
&&
300 bifunc
->bfunct
!= (bfunc_t
)doelse
&&
301 bifunc
->bfunct
!= (bfunc_t
)doend
&&
302 bifunc
->bfunct
!= (bfunc_t
)doforeach
&&
303 bifunc
->bfunct
!= (bfunc_t
)dogoto
&&
304 bifunc
->bfunct
!= (bfunc_t
)doif
&&
305 bifunc
->bfunct
!= (bfunc_t
)dorepeat
&&
306 bifunc
->bfunct
!= (bfunc_t
)doswbrk
&&
307 bifunc
->bfunct
!= (bfunc_t
)doswitch
&&
308 bifunc
->bfunct
!= (bfunc_t
)dowhile
&&
309 bifunc
->bfunct
!= (bfunc_t
)dozip
)
313 else { /* not a command */
320 * GrP Executing a command - run jobcmd hook
321 * Don't run for builtins
322 * Don't run if we're not in a tty
323 * Don't run if we're not really executing
325 if (t
->t_dtyp
== NODE_COMMAND
&& !bifunc
&& !noexec
&& intty
) {
326 Char
*cmd
= unparse(t
);
332 * We fork only if we are timed, or are not the end of a parenthesized
333 * list and not a simple builtin function. Simple meaning one that is
334 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
335 * fork in some of these cases.
338 * Prevent forking cd, pushd, popd, chdir cause this will cause the
339 * shell not to change dir!
343 * Can't have NOFORK for the tail of a pipe - because it is not the
344 * last command spawned (even if it is at the end of a parenthesised
347 if (t
->t_dflg
& F_PIPEIN
)
348 t
->t_dflg
&= ~(F_NOFORK
);
349 #endif /* BACKPIPE */
350 if (bifunc
&& (bifunc
->bfunct
== (bfunc_t
)dochngd
||
351 bifunc
->bfunct
== (bfunc_t
)dopushd
||
352 bifunc
->bfunct
== (bfunc_t
)dopopd
))
353 t
->t_dflg
&= ~(F_NICE
);
354 if (((t
->t_dflg
& F_TIME
) || ((t
->t_dflg
& F_NOFORK
) == 0 &&
355 (!bifunc
|| t
->t_dflg
&
356 (F_PIPEOUT
| F_AMPERSAND
| F_NICE
| F_NOHUP
| F_HUP
)))) ||
358 * We have to fork for eval too.
360 (bifunc
&& (t
->t_dflg
& F_PIPEIN
) != 0 &&
361 bifunc
->bfunct
== (bfunc_t
)doeval
)) {
363 if (t
->t_dtyp
== NODE_PAREN
||
364 t
->t_dflg
& (F_REPEAT
| F_AMPERSAND
) || bifunc
)
369 * We need to block SIGCHLD here, so that if the process does
370 * not die before we can set the process group
372 if (wanttty
>= 0 && !nosigchld
) {
374 csigmask
= sigblock(sigmask(SIGCHLD
));
376 (void) sighold(SIGCHLD
);
382 pid
= pfork(t
, wanttty
);
383 if (pid
== 0 && nosigchld
) {
385 (void) sigsetmask(csigmask
);
387 (void) sigrelse(SIGCHLD
);
391 else if (pid
!= 0 && (t
->t_dflg
& F_AMPERSAND
))
397 int ochild
, osetintr
, ohaderr
, odidfds
;
398 int oSHIN
, oSHOUT
, oSHDIAG
, oOLDSTD
, otpgrp
;
399 int oisoutatty
, oisdiagatty
;
401 # ifndef CLOSE_ON_EXEC
403 # endif /* !CLOSE_ON_EXEC */
405 sigmask_t omask
, ocsigmask
;
406 # endif /* BSDSIGS */
409 * Prepare for the vfork by saving everything that the child
410 * corrupts before it exec's. Note that in some signal
411 * implementations which keep the signal info in user space
412 * (e.g. Sun's) it will also be necessary to save and restore
413 * the current sigvec's for the signals the child touches
418 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
419 * Gilbrech - 11/22/87)
422 sigvec_t savesv
[NSIGSAVED
];
425 # endif /* SAVESIGVEC */
426 if (wanttty
>= 0 && !nosigchld
&& !noexec
) {
428 csigmask
= sigblock(sigmask(SIGCHLD
));
429 # else /* !BSDSIGS */
430 (void) sighold(SIGCHLD
);
431 # endif /* BSDSIGS */
435 omask
= sigblock(sigmask(SIGCHLD
)|sigmask(SIGINT
));
436 # else /* !BSDSIGS */
437 (void) sighold(SIGCHLD
);
438 (void) sighold(SIGINT
);
439 # endif /* BSDSIGS */
444 # ifndef CLOSE_ON_EXEC
446 # endif /* !CLOSE_ON_EXEC */
452 oisoutatty
= isoutatty
;
453 oisdiagatty
= isdiagatty
;
455 ocsigmask
= csigmask
;
456 # endif /* BSDSIGS */
457 onosigchld
= nosigchld
;
462 savesm
= savesigvec(savesv
);
463 # endif /* SAVESIGVEC */
472 restoresigvec(savesv
, savesm
);
473 # endif /* SAVESIGVEC */
474 (void) sigsetmask(omask
);
475 # else /* !BSDSIGS */
476 (void) sigrelse(SIGCHLD
);
477 (void) sigrelse(SIGINT
);
478 # endif /* BSDSIGS */
479 stderror(ERR_NOPROC
);
482 if (pid
) { /* parent */
484 restoresigvec(savesv
, savesm
);
485 # endif /* SAVESIGVEC */
491 # ifndef CLOSE_ON_EXEC
493 # endif /* !CLOSE_ON_EXEC */
498 isoutatty
= oisoutatty
;
499 isdiagatty
= oisdiagatty
;
501 csigmask
= ocsigmask
;
502 # endif /* BSDSIGS */
503 nosigchld
= onosigchld
;
509 xfree((ptr_t
) Vexpath
);
511 blkfree((Char
**) Vt
);
513 /* this is from pfork() */
516 (void) sigsetmask(omask
);
517 # else /* !BSDSIGS */
518 (void) sigrelse(SIGCHLD
);
519 (void) sigrelse(SIGINT
);
520 # endif /* BSDSIGS */
523 /* this is from pfork() */
528 (void) sigsetmask(csigmask
);
529 # else /* !BSDSIGS */
530 (void) sigrelse(SIGCHLD
);
531 # endif /* BSDSIGS */
536 ignint
= (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
))
537 || (gointr
&& eq(gointr
, STRminus
));
538 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: getpid();
543 * casts made right for SunOS 4.0 by Douglas C. Schmidt
544 * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
547 * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
551 (void) signal(SIGINT
, SIG_IGN
);
552 (void) signal(SIGQUIT
, SIG_IGN
);
555 (void) signal(SIGINT
, vffree
);
556 (void) signal(SIGQUIT
, SIG_DFL
);
560 (void) signal(SIGTSTP
, SIG_DFL
);
561 (void) signal(SIGTTIN
, SIG_DFL
);
562 (void) signal(SIGTTOU
, SIG_DFL
);
564 # endif /* BSDJOBS */
566 (void) signal(SIGTERM
, parterm
);
568 else if (tpgrp
== -1 &&
569 (t
->t_dflg
& F_NOINTERRUPT
)) {
570 (void) signal(SIGINT
, SIG_IGN
);
571 (void) signal(SIGQUIT
, SIG_IGN
);
574 pgetty(wanttty
, pgrp
);
576 if (t
->t_dflg
& F_NOHUP
)
577 (void) signal(SIGHUP
, SIG_IGN
);
578 if (t
->t_dflg
& F_HUP
)
579 (void) signal(SIGHUP
, SIG_DFL
);
580 if (t
->t_dflg
& F_NICE
) {
581 int nval
= SIGN_EXTEND_CHAR(t
->t_nice
);
583 if (setpriority(PRIO_PROCESS
, 0, nval
) == -1 && errno
)
584 stderror(ERR_SYSTEM
, "setpriority",
586 # else /* !BSDNICE */
588 # endif /* BSDNICE */
591 if (t
->t_dflg
& F_VER
) {
592 tsetenv(STRSYSTYPE
, t
->t_systype
? STRbsd43
: STRsys53
);
603 * It would be better if we could wait for the whole job when we
604 * knew the last process had been started. Pwait, in fact, does
605 * wait for the whole job anyway, but this test doesn't really
606 * express our intentions.
609 if (didfds
== 0 && t
->t_dflg
& F_PIPEOUT
) {
610 (void) close(pipeout
[0]);
611 (void) close(pipeout
[1]);
613 if ((t
->t_dflg
& F_PIPEIN
) != 0)
615 #else /* !BACKPIPE */
616 if (didfds
== 0 && t
->t_dflg
& F_PIPEIN
) {
617 (void) close(pipein
[0]);
618 (void) close(pipein
[1]);
620 if ((t
->t_dflg
& F_PIPEOUT
) != 0)
622 #endif /* BACKPIPE */
626 (void) sigsetmask(csigmask
);
628 (void) sigrelse(SIGCHLD
);
632 if ((t
->t_dflg
& F_AMPERSAND
) == 0)
637 doio(t
, pipein
, pipeout
);
639 if (t
->t_dflg
& F_PIPEIN
) {
640 (void) close(pipein
[0]);
641 (void) close(pipein
[1]);
643 #else /* !BACKPIPE */
644 if (t
->t_dflg
& F_PIPEOUT
) {
645 (void) close(pipeout
[0]);
646 (void) close(pipeout
[1]);
648 #endif /* BACKPIPE */
650 * Perform a builtin function. If we are not forked, arrange for
659 if (t
->t_dtyp
!= NODE_PAREN
) {
664 * For () commands must put new 0,1,2 in FSH* and recurse
666 OLDSTD
= dcopy(0, FOLDSTD
);
667 SHOUT
= dcopy(1, FSHOUT
);
668 isoutatty
= isatty(SHOUT
);
669 SHDIAG
= dcopy(2, FSHDIAG
);
670 isdiagatty
= isatty(SHDIAG
);
673 #ifndef CLOSE_ON_EXEC
675 #endif /* !CLOSE_ON_EXEC */
678 t
->t_dspr
->t_dflg
|= t
->t_dflg
& F_NOINTERRUPT
;
679 execute(t
->t_dspr
, wanttty
, NULL
, NULL
, do_glob
);
684 t
->t_dcdr
->t_dflg
|= F_PIPEIN
| (t
->t_dflg
&
685 (F_PIPEOUT
| F_AMPERSAND
| F_NOFORK
| F_NOINTERRUPT
));
686 execute(t
->t_dcdr
, wanttty
, pv
, pipeout
, do_glob
);
687 t
->t_dcar
->t_dflg
|= F_PIPEOUT
|
688 (t
->t_dflg
& (F_PIPEIN
| F_AMPERSAND
| F_STDERR
| F_NOINTERRUPT
));
689 execute(t
->t_dcar
, wanttty
, pipein
, pv
, do_glob
);
690 #else /* !BACKPIPE */
691 t
->t_dcar
->t_dflg
|= F_PIPEOUT
|
692 (t
->t_dflg
& (F_PIPEIN
| F_AMPERSAND
| F_STDERR
| F_NOINTERRUPT
));
693 execute(t
->t_dcar
, wanttty
, pipein
, pv
, do_glob
);
694 t
->t_dcdr
->t_dflg
|= F_PIPEIN
| (t
->t_dflg
&
695 (F_PIPEOUT
| F_AMPERSAND
| F_NOFORK
| F_NOINTERRUPT
));
696 execute(t
->t_dcdr
, wanttty
, pv
, pipeout
, do_glob
);
697 #endif /* BACKPIPE */
702 t
->t_dcar
->t_dflg
|= t
->t_dflg
& F_NOINTERRUPT
;
703 execute(t
->t_dcar
, wanttty
, NULL
, NULL
, do_glob
);
705 * In strange case of A&B make a new job after A
707 if (t
->t_dcar
->t_dflg
& F_AMPERSAND
&& t
->t_dcdr
&&
708 (t
->t_dcdr
->t_dflg
& F_AMPERSAND
) == 0)
712 t
->t_dcdr
->t_dflg
|= t
->t_dflg
&
713 (F_NOFORK
| F_NOINTERRUPT
);
714 execute(t
->t_dcdr
, wanttty
, NULL
, NULL
, do_glob
);
721 t
->t_dcar
->t_dflg
|= t
->t_dflg
& F_NOINTERRUPT
;
722 execute(t
->t_dcar
, wanttty
, NULL
, NULL
, do_glob
);
723 if ((getn(varval(STRstatus
)) == 0) !=
724 (t
->t_dtyp
== NODE_AND
)) {
729 t
->t_dcdr
->t_dflg
|= t
->t_dflg
&
730 (F_NOFORK
| F_NOINTERRUPT
);
731 execute(t
->t_dcdr
, wanttty
, NULL
, NULL
, do_glob
);
739 * Fall through for all breaks from switch
741 * If there will be no more executions of this command, flush all file
742 * descriptors. Places that turn on the F_REPEAT bit are responsible for
743 * doing donefds after the last re-execution
745 if (didfds
&& !(t
->t_dflg
& F_REPEAT
))
758 if ((v
= gargv
) != 0) {
763 if ((v
= pargv
) != 0) {
777 * Expand and glob the words after an i/o redirection.
778 * If more than one word is generated, then update the command vector.
780 * This is done differently in all the shells:
781 * 1. in the bourne shell and ksh globbing is not performed
782 * 2. Bash/csh say ambiguous
783 * 3. zsh does i/o to/from all the files
784 * 4. itcsh concatenates the words.
786 * I don't know what is best to do. I think that Ambiguous is better
787 * than restructuring the command vector, because the user can get
788 * unexpected results. In any case, the command vector restructuring
789 * code is present and the user can choose it by setting noambiguous
793 register struct command
*t
;
794 Char
*cp
; /* word after < or > */
798 if (adrof(STRnoambiguous
)) {
801 blk
[0] = Dfix1(cp
); /* expand $ */
804 gflag
= 0, tglob(blk
);
808 setname(short2str(blk
[0]));
809 xfree((ptr_t
) blk
[0]);
810 stderror(ERR_NAME
| ERR_NOMATCH
);
813 if (pv
[1] != NULL
) { /* we need to fix the command vector */
814 Char
**av
= blkspl(t
->t_dcom
, &pv
[1]);
815 xfree((ptr_t
) t
->t_dcom
);
818 xfree((ptr_t
) blk
[0]);
826 (void) Strcpy(buf
, blk
[1] = Dfix1(cp
));
827 xfree((ptr_t
) blk
[1]);
828 blk
[0] = globone(buf
, G_ERROR
);
834 * Perform io redirection.
835 * We may or maynot be forked here.
838 doio(t
, pipein
, pipeout
)
839 register struct command
*t
;
840 int *pipein
, *pipeout
;
844 register unsigned long flags
= t
->t_dflg
;
846 if (didfds
|| (flags
& F_REPEAT
))
848 if ((flags
& F_READ
) == 0) {/* F_READ already done */
850 char tmp
[MAXPATHLEN
+1];
853 * so < /dev/std{in,out,err} work
855 (void) dcopy(SHIN
, 0);
856 (void) dcopy(SHOUT
, 1);
857 (void) dcopy(SHDIAG
, 2);
858 cp
= splicepipe(t
, t
->t_dlef
);
859 (void) strncpy(tmp
, short2str(cp
), MAXPATHLEN
);
860 tmp
[MAXPATHLEN
] = '\0';
862 if ((fd
= open(tmp
, O_RDONLY
)) < 0)
863 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
865 /* allow input files larger than 2Gb */
866 (void) fcntl(fd
, O_LARGEFILE
, 0);
867 #endif /* O_LARGEFILE */
870 else if (flags
& F_PIPEIN
) {
872 (void) dup(pipein
[0]);
873 (void) close(pipein
[0]);
874 (void) close(pipein
[1]);
876 else if ((flags
& F_NOINTERRUPT
) && tpgrp
== -1) {
878 (void) open(_PATH_DEVNULL
, O_RDONLY
);
883 #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
885 * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
886 * across dup()s, so we have to UNSET it here or else we get a
887 * command with NO stdin, stdout, or stderr at all (a bad thing
890 (void) close_on_exec(0, 0);
891 #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
895 char tmp
[MAXPATHLEN
+1];
897 cp
= splicepipe(t
, t
->t_drit
);
898 (void) strncpy(tmp
, short2str(cp
), MAXPATHLEN
);
899 tmp
[MAXPATHLEN
] = '\0';
902 * so > /dev/std{out,err} work
904 (void) dcopy(SHOUT
, 1);
905 (void) dcopy(SHDIAG
, 2);
906 if ((flags
& F_APPEND
) != 0) {
908 fd
= open(tmp
, O_WRONLY
| O_APPEND
);
909 #else /* !O_APPEND */
910 fd
= open(tmp
, O_WRONLY
);
911 (void) lseek(fd
, (off_t
) 0, L_XTND
);
912 #endif /* O_APPEND */
916 if ((flags
& F_APPEND
) == 0 || fd
== -1) {
917 if (!(flags
& F_OVERWRITE
) && adrof(STRnoclobber
)) {
918 if (flags
& F_APPEND
)
919 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
922 if ((fd
= creat(tmp
, 0666)) < 0)
923 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
925 /* allow input files larger than 2Gb */
926 (void) fcntl(fd
, O_LARGEFILE
, 0);
927 #endif /* O_LARGEFILE */
932 else if (flags
& F_PIPEOUT
) {
934 (void) dup(pipeout
[1]);
941 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
942 (void) close_on_exec(1, 0);
943 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
947 if (flags
& F_STDERR
) {
953 is2atty
= isdiagatty
;
954 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
955 (void) close_on_exec(2, 0);
956 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
968 pv
[0] = dmove(pv
[0], -1);
969 pv
[1] = dmove(pv
[1], -1);
970 if (pv
[0] >= 0 && pv
[1] >= 0)
982 if (stat(cp
, &stb
) < 0)
984 if (S_ISCHR(stb
.st_mode
))
986 stderror(ERR_EXISTS
, cp
);