1232633Smp/* $Header: /p/tcsh/cvsroot/tcsh/sh.sem.c,v 3.86 2011/02/25 23:24:19 christos Exp $ */
259243Sobrien/*
359243Sobrien * sh.sem.c: I/O redirections and job forking. A touchy issue!
459243Sobrien *	     Most stuff with builtins is incorrect
559243Sobrien */
659243Sobrien/*-
759243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
859243Sobrien * All rights reserved.
959243Sobrien *
1059243Sobrien * Redistribution and use in source and binary forms, with or without
1159243Sobrien * modification, are permitted provided that the following conditions
1259243Sobrien * are met:
1359243Sobrien * 1. Redistributions of source code must retain the above copyright
1459243Sobrien *    notice, this list of conditions and the following disclaimer.
1559243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1659243Sobrien *    notice, this list of conditions and the following disclaimer in the
1759243Sobrien *    documentation and/or other materials provided with the distribution.
18100616Smp * 3. Neither the name of the University nor the names of its contributors
1959243Sobrien *    may be used to endorse or promote products derived from this software
2059243Sobrien *    without specific prior written permission.
2159243Sobrien *
2259243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2359243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2459243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2559243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2659243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2759243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2859243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2959243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3059243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3159243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3259243Sobrien * SUCH DAMAGE.
3359243Sobrien */
3459243Sobrien#include "sh.h"
3559243Sobrien
36232633SmpRCSID("$tcsh: sh.sem.c,v 3.86 2011/02/25 23:24:19 christos Exp $")
3759243Sobrien
3859243Sobrien#include "tc.h"
3959243Sobrien#include "tw.h"
4069408Sache#ifdef WINNT_NATIVE
4159243Sobrien#include "nt.const.h"
4269408Sache#endif /*WINNT_NATIVE*/
4359243Sobrien
4459243Sobrien#ifdef CLOSE_ON_EXEC
4559243Sobrien# ifndef SUNOS4
4659243Sobrien#  ifndef CLEX_DUPS
4759243Sobrien#   define CLEX_DUPS
4859243Sobrien#  endif /* CLEX_DUPS */
4959243Sobrien# endif /* !SUNOS4 */
5059243Sobrien#endif /* CLOSE_ON_EXEC */
5159243Sobrien
5259243Sobrien#if defined(__sparc__) || defined(sparc)
53232633Smp# if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(__linux__) && !defined(__GNU__) && !defined(__GLIBC__)
5459243Sobrien#  include <vfork.h>
55145479Smp# endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !glibc */
5659243Sobrien#endif /* __sparc__ || sparc */
5759243Sobrien
5859243Sobrien#ifdef VFORK
59167465Smpstatic	void		vffree		(int);
6059243Sobrien#endif
61167465Smpstatic	Char		*splicepipe	(struct command *, Char *);
62167465Smpstatic	void		 doio		(struct command *, int *, int *);
63167465Smpstatic	void		 chkclob	(const char *);
6459243Sobrien
6559243Sobrien/*
6659243Sobrien * C shell
6759243Sobrien */
6859243Sobrien
6959243Sobrien/*
7059243Sobrien * For SVR4, there are problems with pipelines having the first process as
7159243Sobrien * the group leader.  The problem occurs when the first process exits before
7259243Sobrien * the others have a chance to setpgid().  This is because in SVR4 you can't
7359243Sobrien * have a zombie as a group leader.  The solution I have used is to reverse
7459243Sobrien * the order in which pipelines are started, making the last process the
7559243Sobrien * group leader.  (Note I am not using 'pipeline' in the generic sense -- I
7659243Sobrien * mean processes connected by '|'.)  I don't know yet if this causes other
7759243Sobrien * problems.
7859243Sobrien *
7959243Sobrien * All the changes for this are in execute(), and are enclosed in
8059243Sobrien * '#ifdef BACKPIPE'
8159243Sobrien *
8259243Sobrien * David Dawes (dawes@physics.su.oz.au) Oct 1991
8359243Sobrien */
8459243Sobrien
8559243Sobrien/*VARARGS 1*/
8659243Sobrienvoid
87167465Smpexecute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
88167465Smp    int do_glob)
8959243Sobrien{
90145479Smp    int    forked = 0;
91167465Smp    const struct biltins * volatile bifunc;
92167465Smp    pid_t pid = 0;
9359243Sobrien    int     pv[2];
94167465Smp    sigset_t set;
95167465Smp    static sigset_t csigset;
9659243Sobrien#ifdef VFORK
9759243Sobrien    static int onosigchld = 0;
9859243Sobrien#endif /* VFORK */
9959243Sobrien    static int nosigchld = 0;
10059243Sobrien
10159243Sobrien    (void) &wanttty;
10259243Sobrien    (void) &forked;
10359243Sobrien    (void) &bifunc;
10459243Sobrien
10559243Sobrien    if (t == 0)
10659243Sobrien	return;
10759243Sobrien
10869408Sache#ifdef WINNT_NATIVE
10959243Sobrien    {
11059243Sobrien        if ((varval(STRNTslowexec) == STRNULL) &&
11159243Sobrien            !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
11259243Sobrien            (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
11359243Sobrien	    !isbfunc(t)) {
11459243Sobrien	    if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
11559243Sobrien		(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
11659243Sobrien	    Dfix(t);
11759243Sobrien            if (nt_try_fast_exec(t) == 0)
11859243Sobrien                return;
11959243Sobrien        }
12059243Sobrien    }
12169408Sache#endif /* WINNT_NATIVE */
12259243Sobrien
12359243Sobrien    /*
12459243Sobrien     * Ed hutchins@sgi.com & Dominic dbg@sgi.com
12559243Sobrien     * Sat Feb 25 03:13:11 PST 1995
12659243Sobrien     * try implicit cd if we have a 1 word command
12759243Sobrien     */
12859243Sobrien    if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
12959243Sobrien	 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
130167465Smp	Char *sCName;
13159243Sobrien	struct stat stbuf;
13259243Sobrien	char *pathname;
13359243Sobrien
134167465Smp	sCName = dollar(t->t_dcom[0]);
135167465Smp	if (sCName != NULL && sCName[0] == '~') {
136167465Smp	    struct Strbuf buf = Strbuf_INIT;
137167465Smp	    const Char *name_end;
13859243Sobrien
139167465Smp	    for (name_end = sCName + 1; *name_end != '\0' && *name_end != '/';
140167465Smp		 name_end++)
141167465Smp		continue;
142167465Smp	    if (name_end != sCName + 1) {
143167465Smp		Char *name, *home;
144167465Smp
145167465Smp		name = Strnsave(sCName + 1, name_end - (sCName + 1));
146167465Smp		home = gethdir(name);
147167465Smp		if (home != NULL) {
148167465Smp		    Strbuf_append(&buf, home);
149167465Smp		    xfree(home);
150167465Smp		} else
151167465Smp		    Strbuf_append(&buf, name);
152167465Smp		xfree(name);
153167465Smp	    } else
154167465Smp		Strbuf_append(&buf, varval(STRhome));
155167465Smp	    Strbuf_append(&buf, name_end);
156167465Smp	    xfree(sCName);
157167465Smp	    sCName = Strbuf_finish(&buf);
15859243Sobrien	}
159167465Smp
16059243Sobrien	pathname = short2str(sCName);
161167465Smp	xfree(sCName);
16259243Sobrien	/* if this is a dir, tack a "cd" on as the first arg */
163167465Smp	if (pathname != NULL &&
164167465Smp	    ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
16569408Sache#ifdef WINNT_NATIVE
166167465Smp	     || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
16769408Sache#endif /* WINNT_NATIVE */
168167465Smp	     )) {
16959243Sobrien	    Char *vCD[2];
17059243Sobrien	    Char **ot_dcom = t->t_dcom;
17159243Sobrien
17259243Sobrien	    vCD[0] = Strsave(STRcd);
17359243Sobrien	    vCD[1] = NULL;
17459243Sobrien	    t->t_dcom = blkspl(vCD, ot_dcom);
175167465Smp	    xfree(ot_dcom);
17659243Sobrien	    if (implicit_cd > 1) {
17759243Sobrien		blkpr(t->t_dcom);
17859243Sobrien		xputchar( '\n' );
17959243Sobrien	    }
18059243Sobrien	}
18159243Sobrien    }
18259243Sobrien
18359243Sobrien    /*
18459243Sobrien     * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
18559243Sobrien     * Don't check for wantty > 0...
18659243Sobrien     */
18759243Sobrien    if (t->t_dflg & F_AMPERSAND)
18859243Sobrien	wanttty = 0;
18959243Sobrien    switch (t->t_dtyp) {
19059243Sobrien
19159243Sobrien    case NODE_COMMAND:
19259243Sobrien	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
193167465Smp	    memmove(t->t_dcom[0], t->t_dcom[0] + 1,
194167465Smp		    (Strlen(t->t_dcom[0] + 1) + 1) * sizeof (*t->t_dcom[0]));
19559243Sobrien	if ((t->t_dflg & F_REPEAT) == 0)
19659243Sobrien	    Dfix(t);		/* $ " ' \ */
19759243Sobrien	if (t->t_dcom[0] == 0) {
19859243Sobrien	    return;
19959243Sobrien	}
20059243Sobrien	/*FALLTHROUGH*/
20159243Sobrien
20259243Sobrien    case NODE_PAREN:
20359243Sobrien#ifdef BACKPIPE
20459243Sobrien	if (t->t_dflg & F_PIPEIN)
20559243Sobrien	    mypipe(pipein);
20659243Sobrien#else /* !BACKPIPE */
20759243Sobrien	if (t->t_dflg & F_PIPEOUT)
20859243Sobrien	    mypipe(pipeout);
20959243Sobrien#endif /* BACKPIPE */
21059243Sobrien	/*
21159243Sobrien	 * Must do << early so parent will know where input pointer should be.
21259243Sobrien	 * If noexec then this is all we do.
21359243Sobrien	 */
21459243Sobrien	if (t->t_dflg & F_READ) {
215167465Smp	    xclose(0);
21659243Sobrien	    heredoc(t->t_dlef);
21759243Sobrien	    if (noexec)
218167465Smp		xclose(0);
21959243Sobrien	}
22059243Sobrien
221167465Smp	setcopy(STRstatus, STR0, VAR_READWRITE);
22259243Sobrien
22359243Sobrien	/*
22459243Sobrien	 * This mess is the necessary kludge to handle the prefix builtins:
22559243Sobrien	 * nice, nohup, time.  These commands can also be used by themselves,
22659243Sobrien	 * and this is not handled here. This will also work when loops are
22759243Sobrien	 * parsed.
22859243Sobrien	 */
22959243Sobrien	while (t->t_dtyp == NODE_COMMAND)
23059243Sobrien	    if (eq(t->t_dcom[0], STRnice)) {
23159243Sobrien		if (t->t_dcom[1]) {
23259243Sobrien		    if (strchr("+-", t->t_dcom[1][0])) {
23359243Sobrien			if (t->t_dcom[2]) {
23459243Sobrien			    setname("nice");
235232633Smp			    t->t_nice = (unsigned char)getn(t->t_dcom[1]);
23659243Sobrien			    lshift(t->t_dcom, 2);
23759243Sobrien			    t->t_dflg |= F_NICE;
23859243Sobrien			}
23959243Sobrien			else
24059243Sobrien			    break;
24159243Sobrien		    }
24259243Sobrien		    else {
24359243Sobrien			t->t_nice = 4;
24459243Sobrien			lshift(t->t_dcom, 1);
24559243Sobrien			t->t_dflg |= F_NICE;
24659243Sobrien		    }
24759243Sobrien		}
24859243Sobrien		else
24959243Sobrien		    break;
25059243Sobrien	    }
25159243Sobrien	    else if (eq(t->t_dcom[0], STRnohup)) {
25259243Sobrien		if (t->t_dcom[1]) {
25359243Sobrien		    t->t_dflg |= F_NOHUP;
25459243Sobrien		    lshift(t->t_dcom, 1);
25559243Sobrien		}
25659243Sobrien		else
25759243Sobrien		    break;
25859243Sobrien	    }
25959243Sobrien	    else if (eq(t->t_dcom[0], STRhup)) {
26059243Sobrien		if (t->t_dcom[1]) {
26159243Sobrien		    t->t_dflg |= F_HUP;
26259243Sobrien		    lshift(t->t_dcom, 1);
26359243Sobrien		}
26459243Sobrien		else
26559243Sobrien		    break;
26659243Sobrien	    }
26759243Sobrien	    else if (eq(t->t_dcom[0], STRtime)) {
26859243Sobrien		if (t->t_dcom[1]) {
26959243Sobrien		    t->t_dflg |= F_TIME;
27059243Sobrien		    lshift(t->t_dcom, 1);
27159243Sobrien		}
27259243Sobrien		else
27359243Sobrien		    break;
27459243Sobrien	    }
27559243Sobrien#ifdef F_VER
27659243Sobrien	    else if (eq(t->t_dcom[0], STRver))
27759243Sobrien		if (t->t_dcom[1] && t->t_dcom[2]) {
27859243Sobrien		    setname("ver");
27959243Sobrien		    t->t_systype = getv(t->t_dcom[1]);
28059243Sobrien		    lshift(t->t_dcom, 2);
28159243Sobrien		    t->t_dflg |= F_VER;
28259243Sobrien		}
28359243Sobrien		else
28459243Sobrien		    break;
28559243Sobrien#endif  /* F_VER */
28659243Sobrien	    else
28759243Sobrien		break;
28859243Sobrien
28959243Sobrien	/* is it a command */
29059243Sobrien	if (t->t_dtyp == NODE_COMMAND) {
29159243Sobrien	    /*
29259243Sobrien	     * Check if we have a builtin function and remember which one.
29359243Sobrien	     */
29459243Sobrien	    bifunc = isbfunc(t);
295167465Smp 	    if (noexec) {
29659243Sobrien		/*
29759243Sobrien		 * Continue for builtins that are part of the scripting language
29859243Sobrien		 */
299167465Smp		if (bifunc == NULL)
300167465Smp		    break;
30159243Sobrien		if (bifunc->bfunct != (bfunc_t)dobreak	&&
30259243Sobrien		    bifunc->bfunct != (bfunc_t)docontin	&&
30359243Sobrien		    bifunc->bfunct != (bfunc_t)doelse	&&
30459243Sobrien		    bifunc->bfunct != (bfunc_t)doend	&&
30559243Sobrien		    bifunc->bfunct != (bfunc_t)doforeach&&
30659243Sobrien		    bifunc->bfunct != (bfunc_t)dogoto	&&
30759243Sobrien		    bifunc->bfunct != (bfunc_t)doif	&&
30859243Sobrien		    bifunc->bfunct != (bfunc_t)dorepeat	&&
30959243Sobrien		    bifunc->bfunct != (bfunc_t)doswbrk	&&
31059243Sobrien		    bifunc->bfunct != (bfunc_t)doswitch	&&
31159243Sobrien		    bifunc->bfunct != (bfunc_t)dowhile	&&
31259243Sobrien		    bifunc->bfunct != (bfunc_t)dozip)
31359243Sobrien		    break;
31459243Sobrien	    }
31559243Sobrien	}
31659243Sobrien	else {			/* not a command */
31759243Sobrien	    bifunc = NULL;
31859243Sobrien	    if (noexec)
31959243Sobrien		break;
32059243Sobrien	}
32159243Sobrien
32283098Smp	/*
32383098Smp	 * GrP Executing a command - run jobcmd hook
32483098Smp	 * Don't run for builtins
32583098Smp	 * Don't run if we're not in a tty
32683098Smp	 * Don't run if we're not really executing
32783098Smp	 */
328195609Smp	/*
329195609Smp	 * CR  -  Charles Ross Aug 2005
330195609Smp	 * added "isoutatty".
331195609Smp	 * The new behavior is that the jobcmd won't be executed
332195609Smp	 * if stdout (SHOUT) isnt attached to a tty.. IE when
333195609Smp	 * redirecting, or using backquotes etc..
334195609Smp	 */
335195609Smp	if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty && isoutatty) {
33683098Smp	    Char *cmd = unparse(t);
337167465Smp
338167465Smp	    cleanup_push(cmd, xfree);
33983098Smp	    job_cmd(cmd);
340167465Smp	    cleanup_until(cmd);
34183098Smp	}
34283098Smp
34359243Sobrien	/*
34459243Sobrien	 * We fork only if we are timed, or are not the end of a parenthesized
34559243Sobrien	 * list and not a simple builtin function. Simple meaning one that is
34659243Sobrien	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
34759243Sobrien	 * fork in some of these cases.
34859243Sobrien	 */
34959243Sobrien	/*
35059243Sobrien	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
35159243Sobrien	 * shell not to change dir!
35259243Sobrien	 */
35359243Sobrien#ifdef BACKPIPE
35459243Sobrien	/*
35559243Sobrien	 * Can't have NOFORK for the tail of a pipe - because it is not the
35659243Sobrien	 * last command spawned (even if it is at the end of a parenthesised
35759243Sobrien	 * list).
35859243Sobrien	 */
35959243Sobrien	if (t->t_dflg & F_PIPEIN)
36059243Sobrien	    t->t_dflg &= ~(F_NOFORK);
36159243Sobrien#endif /* BACKPIPE */
36259243Sobrien	if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
36359243Sobrien		       bifunc->bfunct == (bfunc_t)dopushd ||
36459243Sobrien		       bifunc->bfunct == (bfunc_t)dopopd))
36559243Sobrien	    t->t_dflg &= ~(F_NICE);
36659243Sobrien	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
36759243Sobrien	     (!bifunc || t->t_dflg &
36859243Sobrien	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
36959243Sobrien	/*
37059243Sobrien	 * We have to fork for eval too.
37159243Sobrien	 */
37259243Sobrien	    (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
37369408Sache	     bifunc->bfunct == (bfunc_t)doeval)) {
37459243Sobrien#ifdef VFORK
37559243Sobrien	    if (t->t_dtyp == NODE_PAREN ||
37659243Sobrien		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
37759243Sobrien#endif /* VFORK */
37859243Sobrien	    {
37959243Sobrien		forked++;
38059243Sobrien		/*
38159243Sobrien		 * We need to block SIGCHLD here, so that if the process does
38259243Sobrien		 * not die before we can set the process group
38359243Sobrien		 */
38459243Sobrien		if (wanttty >= 0 && !nosigchld) {
385167465Smp		    sigemptyset(&set);
386167465Smp		    sigaddset(&set, SIGCHLD);
387167465Smp		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
38859243Sobrien
38959243Sobrien		    nosigchld = 1;
39059243Sobrien		}
39159243Sobrien
39259243Sobrien		pid = pfork(t, wanttty);
39359243Sobrien		if (pid == 0 && nosigchld) {
394167465Smp		    sigprocmask(SIG_SETMASK, &csigset, NULL);
39559243Sobrien		    nosigchld = 0;
39659243Sobrien		}
39759243Sobrien		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
39859243Sobrien		    backpid = pid;
39959243Sobrien	    }
40059243Sobrien
40159243Sobrien#ifdef VFORK
40259243Sobrien	    else {
40359243Sobrien		int     ochild, osetintr, ohaderr, odidfds;
40459243Sobrien		int     oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
40559243Sobrien		int     oisoutatty, oisdiagatty;
406167465Smp		sigset_t oset, ocsigset;
40759243Sobrien# ifndef CLOSE_ON_EXEC
40859243Sobrien		int     odidcch;
40959243Sobrien# endif  /* !CLOSE_ON_EXEC */
41059243Sobrien
41159243Sobrien		/*
41259243Sobrien		 * Prepare for the vfork by saving everything that the child
41359243Sobrien		 * corrupts before it exec's. Note that in some signal
41459243Sobrien		 * implementations which keep the signal info in user space
41559243Sobrien		 * (e.g. Sun's) it will also be necessary to save and restore
41659243Sobrien		 * the current sigvec's for the signals the child touches
41759243Sobrien		 * before it exec's.
41859243Sobrien		 */
41959243Sobrien
42059243Sobrien		/*
42159243Sobrien		 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
42259243Sobrien		 * Gilbrech - 11/22/87)
42359243Sobrien		 */
42459243Sobrien# ifdef SAVESIGVEC
425167465Smp		struct sigaction savesv[NSIGSAVED];
426167465Smp		sigset_t savesm;
42759243Sobrien
42859243Sobrien# endif /* SAVESIGVEC */
42959243Sobrien		if (wanttty >= 0 && !nosigchld && !noexec) {
430167465Smp		    sigemptyset(&set);
431167465Smp		    sigaddset(&set, SIGCHLD);
432167465Smp		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
43359243Sobrien		    nosigchld = 1;
43459243Sobrien		}
435167465Smp		sigemptyset(&set);
436167465Smp		sigaddset(&set, SIGCHLD);
437167465Smp		sigaddset(&set, SIGINT);
438167465Smp		(void)sigprocmask(SIG_BLOCK, &set, &oset);
43959243Sobrien		ochild = child;
44059243Sobrien		osetintr = setintr;
44159243Sobrien		ohaderr = haderr;
44259243Sobrien		odidfds = didfds;
44359243Sobrien# ifndef CLOSE_ON_EXEC
44459243Sobrien		odidcch = didcch;
44559243Sobrien# endif /* !CLOSE_ON_EXEC */
44659243Sobrien		oSHIN = SHIN;
44759243Sobrien		oSHOUT = SHOUT;
44859243Sobrien		oSHDIAG = SHDIAG;
44959243Sobrien		oOLDSTD = OLDSTD;
45059243Sobrien		otpgrp = tpgrp;
45159243Sobrien		oisoutatty = isoutatty;
45259243Sobrien		oisdiagatty = isdiagatty;
453167465Smp		ocsigset = csigset;
45459243Sobrien		onosigchld = nosigchld;
45559243Sobrien		Vsav = Vdp = 0;
45659243Sobrien		Vexpath = 0;
45759243Sobrien		Vt = 0;
45859243Sobrien# ifdef SAVESIGVEC
459167465Smp		savesigvec(savesv, savesm);
46059243Sobrien# endif /* SAVESIGVEC */
46159243Sobrien		if (use_fork)
46259243Sobrien		    pid = fork();
46359243Sobrien		else
46459243Sobrien		    pid = vfork();
46559243Sobrien
46659243Sobrien		if (pid < 0) {
467167465Smp# ifdef SAVESIGVEC
46859243Sobrien		    restoresigvec(savesv, savesm);
469167465Smp# endif /* SAVESIGVEC */
470167465Smp		    sigprocmask(SIG_SETMASK, &oset, NULL);
47159243Sobrien		    stderror(ERR_NOPROC);
47259243Sobrien		}
47359243Sobrien		forked++;
47459243Sobrien		if (pid) {	/* parent */
47559243Sobrien# ifdef SAVESIGVEC
47659243Sobrien		    restoresigvec(savesv, savesm);
47759243Sobrien# endif /* SAVESIGVEC */
47859243Sobrien		    child = ochild;
47959243Sobrien		    setintr = osetintr;
48059243Sobrien		    haderr = ohaderr;
48159243Sobrien		    didfds = odidfds;
48259243Sobrien		    SHIN = oSHIN;
48359243Sobrien# ifndef CLOSE_ON_EXEC
48459243Sobrien		    didcch = odidcch;
48559243Sobrien# endif /* !CLOSE_ON_EXEC */
48659243Sobrien		    SHOUT = oSHOUT;
48759243Sobrien		    SHDIAG = oSHDIAG;
48859243Sobrien		    OLDSTD = oOLDSTD;
48959243Sobrien		    tpgrp = otpgrp;
49059243Sobrien		    isoutatty = oisoutatty;
49159243Sobrien		    isdiagatty = oisdiagatty;
492167465Smp		    csigset = ocsigset;
49359243Sobrien		    nosigchld = onosigchld;
49459243Sobrien
495167465Smp		    xfree(Vsav);
49659243Sobrien		    Vsav = 0;
497167465Smp		    xfree(Vdp);
49859243Sobrien		    Vdp = 0;
499167465Smp		    xfree(Vexpath);
50059243Sobrien		    Vexpath = 0;
501167465Smp		    blk_cleanup(Vt);
50259243Sobrien		    Vt = 0;
50359243Sobrien		    /* this is from pfork() */
50459243Sobrien		    palloc(pid, t);
505167465Smp		    sigprocmask(SIG_SETMASK, &oset, NULL);
50659243Sobrien		}
50759243Sobrien		else {		/* child */
50859243Sobrien		    /* this is from pfork() */
509167465Smp		    pid_t pgrp;
510145479Smp		    int    ignint = 0;
51159243Sobrien		    if (nosigchld) {
512167465Smp			sigprocmask(SIG_SETMASK, &csigset, NULL);
51359243Sobrien			nosigchld = 0;
51459243Sobrien		    }
51559243Sobrien
51659243Sobrien		    if (setintr)
51759243Sobrien			ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
51859243Sobrien				|| (gointr && eq(gointr, STRminus));
51959243Sobrien		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
52059243Sobrien		    child++;
52159243Sobrien		    if (setintr) {
52259243Sobrien			setintr = 0;
52359243Sobrien/*
52459243Sobrien * casts made right for SunOS 4.0 by Douglas C. Schmidt
52559243Sobrien * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
52659243Sobrien * (thanks! -- PWP)
52759243Sobrien *
52859243Sobrien * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
52959243Sobrien * (thanks again)
53059243Sobrien */
53159243Sobrien			if (ignint) {
53259243Sobrien			    (void) signal(SIGINT, SIG_IGN);
53359243Sobrien			    (void) signal(SIGQUIT, SIG_IGN);
53459243Sobrien			}
53559243Sobrien			else {
536167465Smp			    (void) signal(SIGINT, vffree);
53759243Sobrien			    (void) signal(SIGQUIT, SIG_DFL);
53859243Sobrien			}
53959243Sobrien# ifdef BSDJOBS
54059243Sobrien			if (wanttty >= 0) {
54159243Sobrien			    (void) signal(SIGTSTP, SIG_DFL);
54259243Sobrien			    (void) signal(SIGTTIN, SIG_DFL);
54359243Sobrien			    (void) signal(SIGTTOU, SIG_DFL);
54459243Sobrien			}
54559243Sobrien# endif /* BSDJOBS */
54659243Sobrien
547167465Smp			sigaction(SIGTERM, &parterm, NULL);
54859243Sobrien		    }
54959243Sobrien		    else if (tpgrp == -1 &&
55059243Sobrien			     (t->t_dflg & F_NOINTERRUPT)) {
55159243Sobrien			(void) signal(SIGINT, SIG_IGN);
55259243Sobrien			(void) signal(SIGQUIT, SIG_IGN);
55359243Sobrien		    }
55459243Sobrien
55559243Sobrien		    pgetty(wanttty, pgrp);
55659243Sobrien
55759243Sobrien		    if (t->t_dflg & F_NOHUP)
55859243Sobrien			(void) signal(SIGHUP, SIG_IGN);
55959243Sobrien		    if (t->t_dflg & F_HUP)
56059243Sobrien			(void) signal(SIGHUP, SIG_DFL);
56159243Sobrien		    if (t->t_dflg & F_NICE) {
56259243Sobrien			int nval = SIGN_EXTEND_CHAR(t->t_nice);
563145479Smp# ifdef HAVE_SETPRIORITY
56483098Smp			if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
56583098Smp				stderror(ERR_SYSTEM, "setpriority",
56683098Smp				    strerror(errno));
567145479Smp# else /* !HAVE_SETPRIORITY */
56859243Sobrien			(void) nice(nval);
569145479Smp# endif /* HAVE_SETPRIORITY */
57059243Sobrien		    }
57159243Sobrien# ifdef F_VER
57259243Sobrien		    if (t->t_dflg & F_VER) {
57359243Sobrien			tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
57459243Sobrien			dohash(NULL, NULL);
57559243Sobrien		    }
57659243Sobrien# endif /* F_VER */
57759243Sobrien		}
57859243Sobrien
57959243Sobrien	    }
58059243Sobrien#endif /* VFORK */
58169408Sache	}
58259243Sobrien	if (pid != 0) {
58359243Sobrien	    /*
58459243Sobrien	     * It would be better if we could wait for the whole job when we
58559243Sobrien	     * knew the last process had been started.  Pwait, in fact, does
58659243Sobrien	     * wait for the whole job anyway, but this test doesn't really
58759243Sobrien	     * express our intentions.
58859243Sobrien	     */
58959243Sobrien#ifdef BACKPIPE
59059243Sobrien	    if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
591167465Smp		xclose(pipeout[0]);
592167465Smp		xclose(pipeout[1]);
59359243Sobrien	    }
59459243Sobrien	    if ((t->t_dflg & F_PIPEIN) != 0)
59559243Sobrien		break;
59659243Sobrien#else /* !BACKPIPE */
59759243Sobrien	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
598167465Smp		xclose(pipein[0]);
599167465Smp		xclose(pipein[1]);
60059243Sobrien	    }
60159243Sobrien	    if ((t->t_dflg & F_PIPEOUT) != 0)
60259243Sobrien		break;
60359243Sobrien#endif /* BACKPIPE */
60459243Sobrien
60559243Sobrien	    if (nosigchld) {
606167465Smp		sigprocmask(SIG_SETMASK, &csigset, NULL);
60759243Sobrien		nosigchld = 0;
60859243Sobrien	    }
60959243Sobrien	    if ((t->t_dflg & F_AMPERSAND) == 0)
61059243Sobrien		pwait();
61159243Sobrien	    break;
61259243Sobrien	}
61359243Sobrien
61459243Sobrien	doio(t, pipein, pipeout);
61559243Sobrien#ifdef BACKPIPE
61659243Sobrien	if (t->t_dflg & F_PIPEIN) {
617167465Smp	    xclose(pipein[0]);
618167465Smp	    xclose(pipein[1]);
61959243Sobrien	}
62059243Sobrien#else /* !BACKPIPE */
62159243Sobrien	if (t->t_dflg & F_PIPEOUT) {
622167465Smp	    xclose(pipeout[0]);
623167465Smp	    xclose(pipeout[1]);
62459243Sobrien	}
62559243Sobrien#endif /* BACKPIPE */
62659243Sobrien	/*
62759243Sobrien	 * Perform a builtin function. If we are not forked, arrange for
62859243Sobrien	 * possible stopping
62959243Sobrien	 */
63059243Sobrien	if (bifunc) {
631232633Smp	    if (forked) {
632232633Smp		func(t, bifunc);
63359243Sobrien		exitstat();
634232633Smp	    } else {
635232633Smp		jmp_buf_t oldexit;
636232633Smp		int ohaderr = haderr;
637232633Smp
638232633Smp		getexit(oldexit);
639232633Smp		if (setexit() == 0)
640232633Smp		    func(t, bifunc);
641232633Smp		resexit(oldexit);
642232633Smp		haderr = ohaderr;
643232633Smp
644145479Smp		if (adrof(STRprintexitvalue)) {
645145479Smp		    int rv = getn(varval(STRstatus));
646145479Smp		    if (rv != 0)
647145479Smp			xprintf(CGETS(17, 2, "Exit %d\n"), rv);
648145479Smp		}
649145479Smp	    }
65059243Sobrien	    break;
65159243Sobrien	}
65259243Sobrien	if (t->t_dtyp != NODE_PAREN) {
653100616Smp	    doexec(t, do_glob);
65459243Sobrien	    /* NOTREACHED */
65559243Sobrien	}
65659243Sobrien	/*
65759243Sobrien	 * For () commands must put new 0,1,2 in FSH* and recurse
65859243Sobrien	 */
659232633Smp	if ((OLDSTD = dcopy(0, FOLDSTD)) >= 0)
660232633Smp	    (void)close_on_exec(OLDSTD, 1);
661232633Smp	if ((SHOUT = dcopy(1, FSHOUT)) >= 0) {
662232633Smp	    (void)close_on_exec(SHOUT, 1);
663232633Smp	    isoutatty = isatty(SHOUT);
664232633Smp	}
665232633Smp	if ((SHDIAG = dcopy(2, FSHDIAG)) >= 0) {
666232633Smp	    (void)close_on_exec(SHDIAG, 1);
667232633Smp	    isdiagatty = isatty(SHDIAG);
668232633Smp    	}
669167465Smp	xclose(SHIN);
67059243Sobrien	SHIN = -1;
67159243Sobrien#ifndef CLOSE_ON_EXEC
67259243Sobrien	didcch = 0;
673145479Smp#else
674145479Smp	(void) close_on_exec(FSHOUT, 1);
675145479Smp	(void) close_on_exec(FSHDIAG, 1);
676145479Smp	(void) close_on_exec(FOLDSTD, 1);
67759243Sobrien#endif /* !CLOSE_ON_EXEC */
67859243Sobrien	didfds = 0;
67959243Sobrien	wanttty = -1;
680232633Smp	t->t_dspr->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
681100616Smp	execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
68259243Sobrien	exitstat();
68359243Sobrien
68459243Sobrien    case NODE_PIPE:
68559243Sobrien#ifdef BACKPIPE
68659243Sobrien	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
687232633Smp	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
688100616Smp	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
689232633Smp	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
690232633Smp	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
691100616Smp	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
69259243Sobrien#else /* !BACKPIPE */
693232633Smp	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
694232633Smp	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
695100616Smp	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
69659243Sobrien	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
697232633Smp	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
698100616Smp	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
69959243Sobrien#endif /* BACKPIPE */
70059243Sobrien	break;
70159243Sobrien
70259243Sobrien    case NODE_LIST:
70359243Sobrien	if (t->t_dcar) {
704232633Smp	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
705100616Smp	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
70659243Sobrien	    /*
70759243Sobrien	     * In strange case of A&B make a new job after A
70859243Sobrien	     */
70959243Sobrien	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
71059243Sobrien		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
71159243Sobrien		pendjob();
71259243Sobrien	}
71359243Sobrien	if (t->t_dcdr) {
71459243Sobrien	    t->t_dcdr->t_dflg |= t->t_dflg &
715232633Smp		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
716100616Smp	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
71759243Sobrien	}
71859243Sobrien	break;
71959243Sobrien
72059243Sobrien    case NODE_OR:
72159243Sobrien    case NODE_AND:
72259243Sobrien	if (t->t_dcar) {
723232633Smp	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
724100616Smp	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
72559243Sobrien	    if ((getn(varval(STRstatus)) == 0) !=
72659243Sobrien		(t->t_dtyp == NODE_AND)) {
72759243Sobrien		return;
72859243Sobrien	    }
72959243Sobrien	}
73059243Sobrien	if (t->t_dcdr) {
73159243Sobrien	    t->t_dcdr->t_dflg |= t->t_dflg &
732232633Smp		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
733100616Smp	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
73459243Sobrien	}
73559243Sobrien	break;
73659243Sobrien
73759243Sobrien    default:
73859243Sobrien	break;
73959243Sobrien    }
74059243Sobrien    /*
74159243Sobrien     * Fall through for all breaks from switch
74259243Sobrien     *
74359243Sobrien     * If there will be no more executions of this command, flush all file
74459243Sobrien     * descriptors. Places that turn on the F_REPEAT bit are responsible for
74559243Sobrien     * doing donefds after the last re-execution
74659243Sobrien     */
74759243Sobrien    if (didfds && !(t->t_dflg & F_REPEAT))
74859243Sobrien	donefds();
74959243Sobrien}
75059243Sobrien
75159243Sobrien#ifdef VFORK
752167465Smpstatic void
75359243Sobrien/*ARGSUSED*/
754167465Smpvffree(int snum)
75559243Sobrien{
75659243Sobrien    USE(snum);
75759243Sobrien
75859243Sobrien    _exit(1);
75959243Sobrien}
76059243Sobrien#endif /* VFORK */
76159243Sobrien
76259243Sobrien/*
76359243Sobrien * Expand and glob the words after an i/o redirection.
76459243Sobrien * If more than one word is generated, then update the command vector.
76559243Sobrien *
76659243Sobrien * This is done differently in all the shells:
76759243Sobrien * 1. in the bourne shell and ksh globbing is not performed
76859243Sobrien * 2. Bash/csh say ambiguous
76959243Sobrien * 3. zsh does i/o to/from all the files
77059243Sobrien * 4. itcsh concatenates the words.
77159243Sobrien *
77259243Sobrien * I don't know what is best to do. I think that Ambiguous is better
77359243Sobrien * than restructuring the command vector, because the user can get
77459243Sobrien * unexpected results. In any case, the command vector restructuring
77559243Sobrien * code is present and the user can choose it by setting noambiguous
77659243Sobrien */
77759243Sobrienstatic Char *
778167465Smpsplicepipe(struct command *t, Char *cp)
77959243Sobrien{
78059243Sobrien    Char *blk[2];
78159243Sobrien
78259243Sobrien    if (adrof(STRnoambiguous)) {
78359243Sobrien	Char **pv;
784167465Smp	int gflag;
78559243Sobrien
78659243Sobrien	blk[0] = Dfix1(cp); /* expand $ */
78759243Sobrien	blk[1] = NULL;
78859243Sobrien
789167465Smp	gflag = tglob(blk);
79059243Sobrien	if (gflag) {
791167465Smp	    pv = globall(blk, gflag);
79259243Sobrien	    if (pv == NULL) {
79359243Sobrien		setname(short2str(blk[0]));
794167465Smp		xfree(blk[0]);
79559243Sobrien		stderror(ERR_NAME | ERR_NOMATCH);
79659243Sobrien	    }
79759243Sobrien	    if (pv[1] != NULL) { /* we need to fix the command vector */
79859243Sobrien		Char **av = blkspl(t->t_dcom, &pv[1]);
799167465Smp		xfree(t->t_dcom);
80059243Sobrien		t->t_dcom = av;
80159243Sobrien	    }
802167465Smp	    xfree(blk[0]);
80359243Sobrien	    blk[0] = pv[0];
804167465Smp	    xfree(pv);
80559243Sobrien	}
80659243Sobrien    }
80759243Sobrien    else {
808167465Smp	Char *buf;
80959243Sobrien
810167465Smp	buf = Dfix1(cp);
811167465Smp	cleanup_push(buf, xfree);
81259243Sobrien	blk[0] = globone(buf, G_ERROR);
813167465Smp	cleanup_until(buf);
81459243Sobrien    }
81559243Sobrien    return(blk[0]);
81659243Sobrien}
81759243Sobrien
81859243Sobrien/*
81959243Sobrien * Perform io redirection.
82059243Sobrien * We may or maynot be forked here.
82159243Sobrien */
82259243Sobrienstatic void
823167465Smpdoio(struct command *t, int *pipein, int *pipeout)
82459243Sobrien{
825145479Smp    int fd;
826145479Smp    Char *cp;
827145479Smp    unsigned long flags = t->t_dflg;
82859243Sobrien
82959243Sobrien    if (didfds || (flags & F_REPEAT))
83059243Sobrien	return;
83159243Sobrien    if ((flags & F_READ) == 0) {/* F_READ already done */
83259243Sobrien	if (t->t_dlef) {
833167465Smp	    char *tmp;
83459243Sobrien
83559243Sobrien	    /*
83659243Sobrien	     * so < /dev/std{in,out,err} work
83759243Sobrien	     */
83859243Sobrien	    (void) dcopy(SHIN, 0);
83959243Sobrien	    (void) dcopy(SHOUT, 1);
84059243Sobrien	    (void) dcopy(SHDIAG, 2);
84159243Sobrien	    cp = splicepipe(t, t->t_dlef);
842167465Smp	    tmp = strsave(short2str(cp));
843167465Smp	    xfree(cp);
844167465Smp	    cleanup_push(tmp, xfree);
845167465Smp	    if ((fd = xopen(tmp, O_RDONLY|O_LARGEFILE)) < 0)
84659243Sobrien		stderror(ERR_SYSTEM, tmp, strerror(errno));
847167465Smp	    cleanup_until(tmp);
84859243Sobrien	    /* allow input files larger than 2Gb  */
849131962Smp#ifndef WINNT_NATIVE
850145479Smp	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
851131962Smp#endif /*!WINNT_NATIVE*/
85259243Sobrien	    (void) dmove(fd, 0);
85359243Sobrien	}
85459243Sobrien	else if (flags & F_PIPEIN) {
855167465Smp	    xclose(0);
856232633Smp	    TCSH_IGNORE(dup(pipein[0]));
857167465Smp	    xclose(pipein[0]);
858167465Smp	    xclose(pipein[1]);
85959243Sobrien	}
86059243Sobrien	else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
861167465Smp	    xclose(0);
862167465Smp	    (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
86359243Sobrien	}
86459243Sobrien	else {
865167465Smp	    xclose(0);
866232633Smp	    TCSH_IGNORE(dup(OLDSTD));
86759243Sobrien#if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
86859243Sobrien	    /*
86959243Sobrien	     * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
87059243Sobrien	     * across dup()s, so we have to UNSET it here or else we get a
87159243Sobrien	     * command with NO stdin, stdout, or stderr at all (a bad thing
87259243Sobrien	     * indeed)
87359243Sobrien	     */
87459243Sobrien	    (void) close_on_exec(0, 0);
87559243Sobrien#endif /* CLOSE_ON_EXEC && CLEX_DUPS */
87659243Sobrien	}
87759243Sobrien    }
87859243Sobrien    if (t->t_drit) {
879167465Smp	char *tmp;
88059243Sobrien
88159243Sobrien	cp = splicepipe(t, t->t_drit);
882167465Smp	tmp = strsave(short2str(cp));
883167465Smp	xfree(cp);
884167465Smp	cleanup_push(tmp, xfree);
88559243Sobrien	/*
88659243Sobrien	 * so > /dev/std{out,err} work
88759243Sobrien	 */
88859243Sobrien	(void) dcopy(SHOUT, 1);
88959243Sobrien	(void) dcopy(SHDIAG, 2);
89059243Sobrien	if ((flags & F_APPEND) != 0) {
89159243Sobrien#ifdef O_APPEND
892167465Smp	    fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
89359243Sobrien#else /* !O_APPEND */
894167465Smp	    fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
89559243Sobrien	    (void) lseek(fd, (off_t) 0, L_XTND);
89659243Sobrien#endif /* O_APPEND */
89759243Sobrien	}
89859243Sobrien	else
89959243Sobrien	    fd = 0;
90059243Sobrien	if ((flags & F_APPEND) == 0 || fd == -1) {
90159243Sobrien	    if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
90259243Sobrien		if (flags & F_APPEND)
90359243Sobrien		    stderror(ERR_SYSTEM, tmp, strerror(errno));
90459243Sobrien		chkclob(tmp);
90559243Sobrien	    }
906167465Smp	    if ((fd = xcreat(tmp, 0666)) < 0)
90759243Sobrien		stderror(ERR_SYSTEM, tmp, strerror(errno));
90859243Sobrien	    /* allow input files larger than 2Gb  */
909131962Smp#ifndef WINNT_NATIVE
910145479Smp	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
911131962Smp#endif /*!WINNT_NATIVE*/
91259243Sobrien	}
913167465Smp	cleanup_until(tmp);
91459243Sobrien	(void) dmove(fd, 1);
91559243Sobrien	is1atty = isatty(1);
91659243Sobrien    }
91759243Sobrien    else if (flags & F_PIPEOUT) {
918167465Smp	xclose(1);
919232633Smp	TCSH_IGNORE(dup(pipeout[1]));
92059243Sobrien	is1atty = 0;
92159243Sobrien    }
92259243Sobrien    else {
923167465Smp	xclose(1);
924232633Smp	TCSH_IGNORE(dup(SHOUT));
92559243Sobrien	is1atty = isoutatty;
92659243Sobrien# if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
92759243Sobrien	(void) close_on_exec(1, 0);
92859243Sobrien# endif /* CLOSE_ON_EXEC && CLEX_DUPS */
92959243Sobrien    }
93059243Sobrien
931167465Smp    xclose(2);
93259243Sobrien    if (flags & F_STDERR) {
933232633Smp	TCSH_IGNORE(dup(1));
93459243Sobrien	is2atty = is1atty;
93559243Sobrien    }
93659243Sobrien    else {
937232633Smp	TCSH_IGNORE(dup(SHDIAG));
93859243Sobrien	is2atty = isdiagatty;
93959243Sobrien# if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
94059243Sobrien	(void) close_on_exec(2, 0);
94159243Sobrien# endif /* CLOSE_ON_EXEC && CLEX_DUPS */
94259243Sobrien    }
94359243Sobrien    didfds = 1;
94459243Sobrien}
94559243Sobrien
94659243Sobrienvoid
947167465Smpmypipe(int *pv)
94859243Sobrien{
94959243Sobrien
95059243Sobrien    if (pipe(pv) < 0)
95159243Sobrien	goto oops;
952145479Smp    (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
953145479Smp    (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
95459243Sobrien    if (pv[0] >= 0 && pv[1] >= 0)
95559243Sobrien	return;
956167465Smp    if (pv[0] >= 0)
957167465Smp	xclose(pv[0]);
958167465Smp    if (pv[1] >= 0)
959167465Smp	xclose(pv[1]);
96059243Sobrienoops:
96159243Sobrien    stderror(ERR_PIPE);
96259243Sobrien}
96359243Sobrien
96459243Sobrienstatic void
965167465Smpchkclob(const char *cp)
96659243Sobrien{
96759243Sobrien    struct stat stb;
96859243Sobrien
96959243Sobrien    if (stat(cp, &stb) < 0)
97059243Sobrien	return;
97159243Sobrien    if (S_ISCHR(stb.st_mode))
97259243Sobrien	return;
97359243Sobrien    stderror(ERR_EXISTS, cp);
97459243Sobrien}
975