sh.sem.c revision 167465
1167465Smp/* $Header: /p/tcsh/cvsroot/tcsh/sh.sem.c,v 3.78 2006/10/14 17:23:39 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
36167465SmpRCSID("$tcsh: sh.sem.c,v 3.78 2006/10/14 17:23:39 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)
53145479Smp# 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");
23559243Sobrien			    t->t_nice =
23659243Sobrien				getn(t->t_dcom[1]);
23759243Sobrien			    lshift(t->t_dcom, 2);
23859243Sobrien			    t->t_dflg |= F_NICE;
23959243Sobrien			}
24059243Sobrien			else
24159243Sobrien			    break;
24259243Sobrien		    }
24359243Sobrien		    else {
24459243Sobrien			t->t_nice = 4;
24559243Sobrien			lshift(t->t_dcom, 1);
24659243Sobrien			t->t_dflg |= F_NICE;
24759243Sobrien		    }
24859243Sobrien		}
24959243Sobrien		else
25059243Sobrien		    break;
25159243Sobrien	    }
25259243Sobrien	    else if (eq(t->t_dcom[0], STRnohup)) {
25359243Sobrien		if (t->t_dcom[1]) {
25459243Sobrien		    t->t_dflg |= F_NOHUP;
25559243Sobrien		    lshift(t->t_dcom, 1);
25659243Sobrien		}
25759243Sobrien		else
25859243Sobrien		    break;
25959243Sobrien	    }
26059243Sobrien	    else if (eq(t->t_dcom[0], STRhup)) {
26159243Sobrien		if (t->t_dcom[1]) {
26259243Sobrien		    t->t_dflg |= F_HUP;
26359243Sobrien		    lshift(t->t_dcom, 1);
26459243Sobrien		}
26559243Sobrien		else
26659243Sobrien		    break;
26759243Sobrien	    }
26859243Sobrien	    else if (eq(t->t_dcom[0], STRtime)) {
26959243Sobrien		if (t->t_dcom[1]) {
27059243Sobrien		    t->t_dflg |= F_TIME;
27159243Sobrien		    lshift(t->t_dcom, 1);
27259243Sobrien		}
27359243Sobrien		else
27459243Sobrien		    break;
27559243Sobrien	    }
27659243Sobrien#ifdef F_VER
27759243Sobrien	    else if (eq(t->t_dcom[0], STRver))
27859243Sobrien		if (t->t_dcom[1] && t->t_dcom[2]) {
27959243Sobrien		    setname("ver");
28059243Sobrien		    t->t_systype = getv(t->t_dcom[1]);
28159243Sobrien		    lshift(t->t_dcom, 2);
28259243Sobrien		    t->t_dflg |= F_VER;
28359243Sobrien		}
28459243Sobrien		else
28559243Sobrien		    break;
28659243Sobrien#endif  /* F_VER */
28759243Sobrien	    else
28859243Sobrien		break;
28959243Sobrien
29059243Sobrien	/* is it a command */
29159243Sobrien	if (t->t_dtyp == NODE_COMMAND) {
29259243Sobrien	    /*
29359243Sobrien	     * Check if we have a builtin function and remember which one.
29459243Sobrien	     */
29559243Sobrien	    bifunc = isbfunc(t);
296167465Smp 	    if (noexec) {
29759243Sobrien		/*
29859243Sobrien		 * Continue for builtins that are part of the scripting language
29959243Sobrien		 */
300167465Smp		if (bifunc == NULL)
301167465Smp		    break;
30259243Sobrien		if (bifunc->bfunct != (bfunc_t)dobreak	&&
30359243Sobrien		    bifunc->bfunct != (bfunc_t)docontin	&&
30459243Sobrien		    bifunc->bfunct != (bfunc_t)doelse	&&
30559243Sobrien		    bifunc->bfunct != (bfunc_t)doend	&&
30659243Sobrien		    bifunc->bfunct != (bfunc_t)doforeach&&
30759243Sobrien		    bifunc->bfunct != (bfunc_t)dogoto	&&
30859243Sobrien		    bifunc->bfunct != (bfunc_t)doif	&&
30959243Sobrien		    bifunc->bfunct != (bfunc_t)dorepeat	&&
31059243Sobrien		    bifunc->bfunct != (bfunc_t)doswbrk	&&
31159243Sobrien		    bifunc->bfunct != (bfunc_t)doswitch	&&
31259243Sobrien		    bifunc->bfunct != (bfunc_t)dowhile	&&
31359243Sobrien		    bifunc->bfunct != (bfunc_t)dozip)
31459243Sobrien		    break;
31559243Sobrien	    }
31659243Sobrien	}
31759243Sobrien	else {			/* not a command */
31859243Sobrien	    bifunc = NULL;
31959243Sobrien	    if (noexec)
32059243Sobrien		break;
32159243Sobrien	}
32259243Sobrien
32383098Smp	/*
32483098Smp	 * GrP Executing a command - run jobcmd hook
32583098Smp	 * Don't run for builtins
32683098Smp	 * Don't run if we're not in a tty
32783098Smp	 * Don't run if we're not really executing
32883098Smp	 */
32983098Smp	if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty) {
33083098Smp	    Char *cmd = unparse(t);
331167465Smp
332167465Smp	    cleanup_push(cmd, xfree);
33383098Smp	    job_cmd(cmd);
334167465Smp	    cleanup_until(cmd);
33583098Smp	}
33683098Smp
33759243Sobrien	/*
33859243Sobrien	 * We fork only if we are timed, or are not the end of a parenthesized
33959243Sobrien	 * list and not a simple builtin function. Simple meaning one that is
34059243Sobrien	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
34159243Sobrien	 * fork in some of these cases.
34259243Sobrien	 */
34359243Sobrien	/*
34459243Sobrien	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
34559243Sobrien	 * shell not to change dir!
34659243Sobrien	 */
34759243Sobrien#ifdef BACKPIPE
34859243Sobrien	/*
34959243Sobrien	 * Can't have NOFORK for the tail of a pipe - because it is not the
35059243Sobrien	 * last command spawned (even if it is at the end of a parenthesised
35159243Sobrien	 * list).
35259243Sobrien	 */
35359243Sobrien	if (t->t_dflg & F_PIPEIN)
35459243Sobrien	    t->t_dflg &= ~(F_NOFORK);
35559243Sobrien#endif /* BACKPIPE */
35659243Sobrien	if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
35759243Sobrien		       bifunc->bfunct == (bfunc_t)dopushd ||
35859243Sobrien		       bifunc->bfunct == (bfunc_t)dopopd))
35959243Sobrien	    t->t_dflg &= ~(F_NICE);
36059243Sobrien	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
36159243Sobrien	     (!bifunc || t->t_dflg &
36259243Sobrien	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
36359243Sobrien	/*
36459243Sobrien	 * We have to fork for eval too.
36559243Sobrien	 */
36659243Sobrien	    (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
36769408Sache	     bifunc->bfunct == (bfunc_t)doeval)) {
36859243Sobrien#ifdef VFORK
36959243Sobrien	    if (t->t_dtyp == NODE_PAREN ||
37059243Sobrien		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
37159243Sobrien#endif /* VFORK */
37259243Sobrien	    {
37359243Sobrien		forked++;
37459243Sobrien		/*
37559243Sobrien		 * We need to block SIGCHLD here, so that if the process does
37659243Sobrien		 * not die before we can set the process group
37759243Sobrien		 */
37859243Sobrien		if (wanttty >= 0 && !nosigchld) {
379167465Smp		    sigemptyset(&set);
380167465Smp		    sigaddset(&set, SIGCHLD);
381167465Smp		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
38259243Sobrien
38359243Sobrien		    nosigchld = 1;
38459243Sobrien		}
38559243Sobrien
38659243Sobrien		pid = pfork(t, wanttty);
38759243Sobrien		if (pid == 0 && nosigchld) {
388167465Smp		    sigprocmask(SIG_SETMASK, &csigset, NULL);
38959243Sobrien		    nosigchld = 0;
39059243Sobrien		}
39159243Sobrien		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
39259243Sobrien		    backpid = pid;
39359243Sobrien	    }
39459243Sobrien
39559243Sobrien#ifdef VFORK
39659243Sobrien	    else {
39759243Sobrien		int     ochild, osetintr, ohaderr, odidfds;
39859243Sobrien		int     oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
39959243Sobrien		int     oisoutatty, oisdiagatty;
400167465Smp		sigset_t oset, ocsigset;
40159243Sobrien# ifndef CLOSE_ON_EXEC
40259243Sobrien		int     odidcch;
40359243Sobrien# endif  /* !CLOSE_ON_EXEC */
40459243Sobrien
40559243Sobrien		/*
40659243Sobrien		 * Prepare for the vfork by saving everything that the child
40759243Sobrien		 * corrupts before it exec's. Note that in some signal
40859243Sobrien		 * implementations which keep the signal info in user space
40959243Sobrien		 * (e.g. Sun's) it will also be necessary to save and restore
41059243Sobrien		 * the current sigvec's for the signals the child touches
41159243Sobrien		 * before it exec's.
41259243Sobrien		 */
41359243Sobrien
41459243Sobrien		/*
41559243Sobrien		 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
41659243Sobrien		 * Gilbrech - 11/22/87)
41759243Sobrien		 */
41859243Sobrien# ifdef SAVESIGVEC
419167465Smp		struct sigaction savesv[NSIGSAVED];
420167465Smp		sigset_t savesm;
42159243Sobrien
42259243Sobrien# endif /* SAVESIGVEC */
42359243Sobrien		if (wanttty >= 0 && !nosigchld && !noexec) {
424167465Smp		    sigemptyset(&set);
425167465Smp		    sigaddset(&set, SIGCHLD);
426167465Smp		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
42759243Sobrien		    nosigchld = 1;
42859243Sobrien		}
429167465Smp		sigemptyset(&set);
430167465Smp		sigaddset(&set, SIGCHLD);
431167465Smp		sigaddset(&set, SIGINT);
432167465Smp		(void)sigprocmask(SIG_BLOCK, &set, &oset);
43359243Sobrien		ochild = child;
43459243Sobrien		osetintr = setintr;
43559243Sobrien		ohaderr = haderr;
43659243Sobrien		odidfds = didfds;
43759243Sobrien# ifndef CLOSE_ON_EXEC
43859243Sobrien		odidcch = didcch;
43959243Sobrien# endif /* !CLOSE_ON_EXEC */
44059243Sobrien		oSHIN = SHIN;
44159243Sobrien		oSHOUT = SHOUT;
44259243Sobrien		oSHDIAG = SHDIAG;
44359243Sobrien		oOLDSTD = OLDSTD;
44459243Sobrien		otpgrp = tpgrp;
44559243Sobrien		oisoutatty = isoutatty;
44659243Sobrien		oisdiagatty = isdiagatty;
447167465Smp		ocsigset = csigset;
44859243Sobrien		onosigchld = nosigchld;
44959243Sobrien		Vsav = Vdp = 0;
45059243Sobrien		Vexpath = 0;
45159243Sobrien		Vt = 0;
45259243Sobrien# ifdef SAVESIGVEC
453167465Smp		savesigvec(savesv, savesm);
45459243Sobrien# endif /* SAVESIGVEC */
45559243Sobrien		if (use_fork)
45659243Sobrien		    pid = fork();
45759243Sobrien		else
45859243Sobrien		    pid = vfork();
45959243Sobrien
46059243Sobrien		if (pid < 0) {
461167465Smp# ifdef SAVESIGVEC
46259243Sobrien		    restoresigvec(savesv, savesm);
463167465Smp# endif /* SAVESIGVEC */
464167465Smp		    sigprocmask(SIG_SETMASK, &oset, NULL);
46559243Sobrien		    stderror(ERR_NOPROC);
46659243Sobrien		}
46759243Sobrien		forked++;
46859243Sobrien		if (pid) {	/* parent */
46959243Sobrien# ifdef SAVESIGVEC
47059243Sobrien		    restoresigvec(savesv, savesm);
47159243Sobrien# endif /* SAVESIGVEC */
47259243Sobrien		    child = ochild;
47359243Sobrien		    setintr = osetintr;
47459243Sobrien		    haderr = ohaderr;
47559243Sobrien		    didfds = odidfds;
47659243Sobrien		    SHIN = oSHIN;
47759243Sobrien# ifndef CLOSE_ON_EXEC
47859243Sobrien		    didcch = odidcch;
47959243Sobrien# endif /* !CLOSE_ON_EXEC */
48059243Sobrien		    SHOUT = oSHOUT;
48159243Sobrien		    SHDIAG = oSHDIAG;
48259243Sobrien		    OLDSTD = oOLDSTD;
48359243Sobrien		    tpgrp = otpgrp;
48459243Sobrien		    isoutatty = oisoutatty;
48559243Sobrien		    isdiagatty = oisdiagatty;
486167465Smp		    csigset = ocsigset;
48759243Sobrien		    nosigchld = onosigchld;
48859243Sobrien
489167465Smp		    xfree(Vsav);
49059243Sobrien		    Vsav = 0;
491167465Smp		    xfree(Vdp);
49259243Sobrien		    Vdp = 0;
493167465Smp		    xfree(Vexpath);
49459243Sobrien		    Vexpath = 0;
495167465Smp		    blk_cleanup(Vt);
49659243Sobrien		    Vt = 0;
49759243Sobrien		    /* this is from pfork() */
49859243Sobrien		    palloc(pid, t);
499167465Smp		    sigprocmask(SIG_SETMASK, &oset, NULL);
50059243Sobrien		}
50159243Sobrien		else {		/* child */
50259243Sobrien		    /* this is from pfork() */
503167465Smp		    pid_t pgrp;
504145479Smp		    int    ignint = 0;
50559243Sobrien		    if (nosigchld) {
506167465Smp			sigprocmask(SIG_SETMASK, &csigset, NULL);
50759243Sobrien			nosigchld = 0;
50859243Sobrien		    }
50959243Sobrien
51059243Sobrien		    if (setintr)
51159243Sobrien			ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
51259243Sobrien				|| (gointr && eq(gointr, STRminus));
51359243Sobrien		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
51459243Sobrien		    child++;
51559243Sobrien		    if (setintr) {
51659243Sobrien			setintr = 0;
51759243Sobrien/*
51859243Sobrien * casts made right for SunOS 4.0 by Douglas C. Schmidt
51959243Sobrien * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
52059243Sobrien * (thanks! -- PWP)
52159243Sobrien *
52259243Sobrien * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
52359243Sobrien * (thanks again)
52459243Sobrien */
52559243Sobrien			if (ignint) {
52659243Sobrien			    (void) signal(SIGINT, SIG_IGN);
52759243Sobrien			    (void) signal(SIGQUIT, SIG_IGN);
52859243Sobrien			}
52959243Sobrien			else {
530167465Smp			    (void) signal(SIGINT, vffree);
53159243Sobrien			    (void) signal(SIGQUIT, SIG_DFL);
53259243Sobrien			}
53359243Sobrien# ifdef BSDJOBS
53459243Sobrien			if (wanttty >= 0) {
53559243Sobrien			    (void) signal(SIGTSTP, SIG_DFL);
53659243Sobrien			    (void) signal(SIGTTIN, SIG_DFL);
53759243Sobrien			    (void) signal(SIGTTOU, SIG_DFL);
53859243Sobrien			}
53959243Sobrien# endif /* BSDJOBS */
54059243Sobrien
541167465Smp			sigaction(SIGTERM, &parterm, NULL);
54259243Sobrien		    }
54359243Sobrien		    else if (tpgrp == -1 &&
54459243Sobrien			     (t->t_dflg & F_NOINTERRUPT)) {
54559243Sobrien			(void) signal(SIGINT, SIG_IGN);
54659243Sobrien			(void) signal(SIGQUIT, SIG_IGN);
54759243Sobrien		    }
54859243Sobrien
54959243Sobrien		    pgetty(wanttty, pgrp);
55059243Sobrien
55159243Sobrien		    if (t->t_dflg & F_NOHUP)
55259243Sobrien			(void) signal(SIGHUP, SIG_IGN);
55359243Sobrien		    if (t->t_dflg & F_HUP)
55459243Sobrien			(void) signal(SIGHUP, SIG_DFL);
55559243Sobrien		    if (t->t_dflg & F_NICE) {
55659243Sobrien			int nval = SIGN_EXTEND_CHAR(t->t_nice);
557145479Smp# ifdef HAVE_SETPRIORITY
55883098Smp			if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
55983098Smp				stderror(ERR_SYSTEM, "setpriority",
56083098Smp				    strerror(errno));
561145479Smp# else /* !HAVE_SETPRIORITY */
56259243Sobrien			(void) nice(nval);
563145479Smp# endif /* HAVE_SETPRIORITY */
56459243Sobrien		    }
56559243Sobrien# ifdef F_VER
56659243Sobrien		    if (t->t_dflg & F_VER) {
56759243Sobrien			tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
56859243Sobrien			dohash(NULL, NULL);
56959243Sobrien		    }
57059243Sobrien# endif /* F_VER */
57159243Sobrien		}
57259243Sobrien
57359243Sobrien	    }
57459243Sobrien#endif /* VFORK */
57569408Sache	}
57659243Sobrien	if (pid != 0) {
57759243Sobrien	    /*
57859243Sobrien	     * It would be better if we could wait for the whole job when we
57959243Sobrien	     * knew the last process had been started.  Pwait, in fact, does
58059243Sobrien	     * wait for the whole job anyway, but this test doesn't really
58159243Sobrien	     * express our intentions.
58259243Sobrien	     */
58359243Sobrien#ifdef BACKPIPE
58459243Sobrien	    if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
585167465Smp		xclose(pipeout[0]);
586167465Smp		xclose(pipeout[1]);
58759243Sobrien	    }
58859243Sobrien	    if ((t->t_dflg & F_PIPEIN) != 0)
58959243Sobrien		break;
59059243Sobrien#else /* !BACKPIPE */
59159243Sobrien	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
592167465Smp		xclose(pipein[0]);
593167465Smp		xclose(pipein[1]);
59459243Sobrien	    }
59559243Sobrien	    if ((t->t_dflg & F_PIPEOUT) != 0)
59659243Sobrien		break;
59759243Sobrien#endif /* BACKPIPE */
59859243Sobrien
59959243Sobrien	    if (nosigchld) {
600167465Smp		sigprocmask(SIG_SETMASK, &csigset, NULL);
60159243Sobrien		nosigchld = 0;
60259243Sobrien	    }
60359243Sobrien	    if ((t->t_dflg & F_AMPERSAND) == 0)
60459243Sobrien		pwait();
60559243Sobrien	    break;
60659243Sobrien	}
60759243Sobrien
60859243Sobrien	doio(t, pipein, pipeout);
60959243Sobrien#ifdef BACKPIPE
61059243Sobrien	if (t->t_dflg & F_PIPEIN) {
611167465Smp	    xclose(pipein[0]);
612167465Smp	    xclose(pipein[1]);
61359243Sobrien	}
61459243Sobrien#else /* !BACKPIPE */
61559243Sobrien	if (t->t_dflg & F_PIPEOUT) {
616167465Smp	    xclose(pipeout[0]);
617167465Smp	    xclose(pipeout[1]);
61859243Sobrien	}
61959243Sobrien#endif /* BACKPIPE */
62059243Sobrien	/*
62159243Sobrien	 * Perform a builtin function. If we are not forked, arrange for
62259243Sobrien	 * possible stopping
62359243Sobrien	 */
62459243Sobrien	if (bifunc) {
62559243Sobrien	    func(t, bifunc);
62659243Sobrien	    if (forked)
62759243Sobrien		exitstat();
628145479Smp	    else {
629145479Smp		if (adrof(STRprintexitvalue)) {
630145479Smp		    int rv = getn(varval(STRstatus));
631145479Smp		    if (rv != 0)
632145479Smp			xprintf(CGETS(17, 2, "Exit %d\n"), rv);
633145479Smp		}
634145479Smp	    }
63559243Sobrien	    break;
63659243Sobrien	}
63759243Sobrien	if (t->t_dtyp != NODE_PAREN) {
638100616Smp	    doexec(t, do_glob);
63959243Sobrien	    /* NOTREACHED */
64059243Sobrien	}
64159243Sobrien	/*
64259243Sobrien	 * For () commands must put new 0,1,2 in FSH* and recurse
64359243Sobrien	 */
644145479Smp	(void)close_on_exec(OLDSTD = dcopy(0, FOLDSTD), 1);
645145479Smp	(void)close_on_exec(SHOUT = dcopy(1, FSHOUT), 1);
64659243Sobrien	isoutatty = isatty(SHOUT);
647145479Smp	(void)close_on_exec(SHDIAG = dcopy(2, FSHDIAG), 1);
64859243Sobrien	isdiagatty = isatty(SHDIAG);
649167465Smp	xclose(SHIN);
65059243Sobrien	SHIN = -1;
65159243Sobrien#ifndef CLOSE_ON_EXEC
65259243Sobrien	didcch = 0;
653145479Smp#else
654145479Smp	(void) close_on_exec(FSHOUT, 1);
655145479Smp	(void) close_on_exec(FSHDIAG, 1);
656145479Smp	(void) close_on_exec(FOLDSTD, 1);
65759243Sobrien#endif /* !CLOSE_ON_EXEC */
65859243Sobrien	didfds = 0;
65959243Sobrien	wanttty = -1;
66059243Sobrien	t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
661100616Smp	execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
66259243Sobrien	exitstat();
66359243Sobrien
66459243Sobrien    case NODE_PIPE:
66559243Sobrien#ifdef BACKPIPE
66659243Sobrien	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
66759243Sobrien			(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
668100616Smp	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
66959243Sobrien	t->t_dcar->t_dflg |= F_PIPEOUT |
67059243Sobrien	    (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
671100616Smp	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
67259243Sobrien#else /* !BACKPIPE */
67359243Sobrien	t->t_dcar->t_dflg |= F_PIPEOUT |
67459243Sobrien	    (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
675100616Smp	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
67659243Sobrien	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
67759243Sobrien			(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
678100616Smp	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
67959243Sobrien#endif /* BACKPIPE */
68059243Sobrien	break;
68159243Sobrien
68259243Sobrien    case NODE_LIST:
68359243Sobrien	if (t->t_dcar) {
68459243Sobrien	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
685100616Smp	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
68659243Sobrien	    /*
68759243Sobrien	     * In strange case of A&B make a new job after A
68859243Sobrien	     */
68959243Sobrien	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
69059243Sobrien		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
69159243Sobrien		pendjob();
69259243Sobrien	}
69359243Sobrien	if (t->t_dcdr) {
69459243Sobrien	    t->t_dcdr->t_dflg |= t->t_dflg &
69559243Sobrien		(F_NOFORK | F_NOINTERRUPT);
696100616Smp	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
69759243Sobrien	}
69859243Sobrien	break;
69959243Sobrien
70059243Sobrien    case NODE_OR:
70159243Sobrien    case NODE_AND:
70259243Sobrien	if (t->t_dcar) {
70359243Sobrien	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
704100616Smp	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
70559243Sobrien	    if ((getn(varval(STRstatus)) == 0) !=
70659243Sobrien		(t->t_dtyp == NODE_AND)) {
70759243Sobrien		return;
70859243Sobrien	    }
70959243Sobrien	}
71059243Sobrien	if (t->t_dcdr) {
71159243Sobrien	    t->t_dcdr->t_dflg |= t->t_dflg &
71259243Sobrien		(F_NOFORK | F_NOINTERRUPT);
713100616Smp	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
71459243Sobrien	}
71559243Sobrien	break;
71659243Sobrien
71759243Sobrien    default:
71859243Sobrien	break;
71959243Sobrien    }
72059243Sobrien    /*
72159243Sobrien     * Fall through for all breaks from switch
72259243Sobrien     *
72359243Sobrien     * If there will be no more executions of this command, flush all file
72459243Sobrien     * descriptors. Places that turn on the F_REPEAT bit are responsible for
72559243Sobrien     * doing donefds after the last re-execution
72659243Sobrien     */
72759243Sobrien    if (didfds && !(t->t_dflg & F_REPEAT))
72859243Sobrien	donefds();
72959243Sobrien}
73059243Sobrien
73159243Sobrien#ifdef VFORK
732167465Smpstatic void
73359243Sobrien/*ARGSUSED*/
734167465Smpvffree(int snum)
73559243Sobrien{
73659243Sobrien    USE(snum);
73759243Sobrien
73859243Sobrien    _exit(1);
73959243Sobrien}
74059243Sobrien#endif /* VFORK */
74159243Sobrien
74259243Sobrien/*
74359243Sobrien * Expand and glob the words after an i/o redirection.
74459243Sobrien * If more than one word is generated, then update the command vector.
74559243Sobrien *
74659243Sobrien * This is done differently in all the shells:
74759243Sobrien * 1. in the bourne shell and ksh globbing is not performed
74859243Sobrien * 2. Bash/csh say ambiguous
74959243Sobrien * 3. zsh does i/o to/from all the files
75059243Sobrien * 4. itcsh concatenates the words.
75159243Sobrien *
75259243Sobrien * I don't know what is best to do. I think that Ambiguous is better
75359243Sobrien * than restructuring the command vector, because the user can get
75459243Sobrien * unexpected results. In any case, the command vector restructuring
75559243Sobrien * code is present and the user can choose it by setting noambiguous
75659243Sobrien */
75759243Sobrienstatic Char *
758167465Smpsplicepipe(struct command *t, Char *cp)
75959243Sobrien{
76059243Sobrien    Char *blk[2];
76159243Sobrien
76259243Sobrien    if (adrof(STRnoambiguous)) {
76359243Sobrien	Char **pv;
764167465Smp	int gflag;
76559243Sobrien
76659243Sobrien	blk[0] = Dfix1(cp); /* expand $ */
76759243Sobrien	blk[1] = NULL;
76859243Sobrien
769167465Smp	gflag = tglob(blk);
77059243Sobrien	if (gflag) {
771167465Smp	    pv = globall(blk, gflag);
77259243Sobrien	    if (pv == NULL) {
77359243Sobrien		setname(short2str(blk[0]));
774167465Smp		xfree(blk[0]);
77559243Sobrien		stderror(ERR_NAME | ERR_NOMATCH);
77659243Sobrien	    }
77759243Sobrien	    if (pv[1] != NULL) { /* we need to fix the command vector */
77859243Sobrien		Char **av = blkspl(t->t_dcom, &pv[1]);
779167465Smp		xfree(t->t_dcom);
78059243Sobrien		t->t_dcom = av;
78159243Sobrien	    }
782167465Smp	    xfree(blk[0]);
78359243Sobrien	    blk[0] = pv[0];
784167465Smp	    xfree(pv);
78559243Sobrien	}
78659243Sobrien    }
78759243Sobrien    else {
788167465Smp	Char *buf;
78959243Sobrien
790167465Smp	buf = Dfix1(cp);
791167465Smp	cleanup_push(buf, xfree);
79259243Sobrien	blk[0] = globone(buf, G_ERROR);
793167465Smp	cleanup_until(buf);
79459243Sobrien    }
79559243Sobrien    return(blk[0]);
79659243Sobrien}
79759243Sobrien
79859243Sobrien/*
79959243Sobrien * Perform io redirection.
80059243Sobrien * We may or maynot be forked here.
80159243Sobrien */
80259243Sobrienstatic void
803167465Smpdoio(struct command *t, int *pipein, int *pipeout)
80459243Sobrien{
805145479Smp    int fd;
806145479Smp    Char *cp;
807145479Smp    unsigned long flags = t->t_dflg;
80859243Sobrien
80959243Sobrien    if (didfds || (flags & F_REPEAT))
81059243Sobrien	return;
81159243Sobrien    if ((flags & F_READ) == 0) {/* F_READ already done */
81259243Sobrien	if (t->t_dlef) {
813167465Smp	    char *tmp;
81459243Sobrien
81559243Sobrien	    /*
81659243Sobrien	     * so < /dev/std{in,out,err} work
81759243Sobrien	     */
81859243Sobrien	    (void) dcopy(SHIN, 0);
81959243Sobrien	    (void) dcopy(SHOUT, 1);
82059243Sobrien	    (void) dcopy(SHDIAG, 2);
82159243Sobrien	    cp = splicepipe(t, t->t_dlef);
822167465Smp	    tmp = strsave(short2str(cp));
823167465Smp	    xfree(cp);
824167465Smp	    cleanup_push(tmp, xfree);
825167465Smp	    if ((fd = xopen(tmp, O_RDONLY|O_LARGEFILE)) < 0)
82659243Sobrien		stderror(ERR_SYSTEM, tmp, strerror(errno));
827167465Smp	    cleanup_until(tmp);
82859243Sobrien	    /* allow input files larger than 2Gb  */
829131962Smp#ifndef WINNT_NATIVE
830145479Smp	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
831131962Smp#endif /*!WINNT_NATIVE*/
83259243Sobrien	    (void) dmove(fd, 0);
83359243Sobrien	}
83459243Sobrien	else if (flags & F_PIPEIN) {
835167465Smp	    xclose(0);
83659243Sobrien	    (void) dup(pipein[0]);
837167465Smp	    xclose(pipein[0]);
838167465Smp	    xclose(pipein[1]);
83959243Sobrien	}
84059243Sobrien	else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
841167465Smp	    xclose(0);
842167465Smp	    (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
84359243Sobrien	}
84459243Sobrien	else {
845167465Smp	    xclose(0);
84659243Sobrien	    (void) dup(OLDSTD);
84759243Sobrien#if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
84859243Sobrien	    /*
84959243Sobrien	     * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
85059243Sobrien	     * across dup()s, so we have to UNSET it here or else we get a
85159243Sobrien	     * command with NO stdin, stdout, or stderr at all (a bad thing
85259243Sobrien	     * indeed)
85359243Sobrien	     */
85459243Sobrien	    (void) close_on_exec(0, 0);
85559243Sobrien#endif /* CLOSE_ON_EXEC && CLEX_DUPS */
85659243Sobrien	}
85759243Sobrien    }
85859243Sobrien    if (t->t_drit) {
859167465Smp	char *tmp;
86059243Sobrien
86159243Sobrien	cp = splicepipe(t, t->t_drit);
862167465Smp	tmp = strsave(short2str(cp));
863167465Smp	xfree(cp);
864167465Smp	cleanup_push(tmp, xfree);
86559243Sobrien	/*
86659243Sobrien	 * so > /dev/std{out,err} work
86759243Sobrien	 */
86859243Sobrien	(void) dcopy(SHOUT, 1);
86959243Sobrien	(void) dcopy(SHDIAG, 2);
87059243Sobrien	if ((flags & F_APPEND) != 0) {
87159243Sobrien#ifdef O_APPEND
872167465Smp	    fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
87359243Sobrien#else /* !O_APPEND */
874167465Smp	    fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
87559243Sobrien	    (void) lseek(fd, (off_t) 0, L_XTND);
87659243Sobrien#endif /* O_APPEND */
87759243Sobrien	}
87859243Sobrien	else
87959243Sobrien	    fd = 0;
88059243Sobrien	if ((flags & F_APPEND) == 0 || fd == -1) {
88159243Sobrien	    if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
88259243Sobrien		if (flags & F_APPEND)
88359243Sobrien		    stderror(ERR_SYSTEM, tmp, strerror(errno));
88459243Sobrien		chkclob(tmp);
88559243Sobrien	    }
886167465Smp	    if ((fd = xcreat(tmp, 0666)) < 0)
88759243Sobrien		stderror(ERR_SYSTEM, tmp, strerror(errno));
88859243Sobrien	    /* allow input files larger than 2Gb  */
889131962Smp#ifndef WINNT_NATIVE
890145479Smp	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
891131962Smp#endif /*!WINNT_NATIVE*/
89259243Sobrien	}
893167465Smp	cleanup_until(tmp);
89459243Sobrien	(void) dmove(fd, 1);
89559243Sobrien	is1atty = isatty(1);
89659243Sobrien    }
89759243Sobrien    else if (flags & F_PIPEOUT) {
898167465Smp	xclose(1);
89959243Sobrien	(void) dup(pipeout[1]);
90059243Sobrien	is1atty = 0;
90159243Sobrien    }
90259243Sobrien    else {
903167465Smp	xclose(1);
90459243Sobrien	(void) dup(SHOUT);
90559243Sobrien	is1atty = isoutatty;
90659243Sobrien# if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
90759243Sobrien	(void) close_on_exec(1, 0);
90859243Sobrien# endif /* CLOSE_ON_EXEC && CLEX_DUPS */
90959243Sobrien    }
91059243Sobrien
911167465Smp    xclose(2);
91259243Sobrien    if (flags & F_STDERR) {
91359243Sobrien	(void) dup(1);
91459243Sobrien	is2atty = is1atty;
91559243Sobrien    }
91659243Sobrien    else {
91759243Sobrien	(void) dup(SHDIAG);
91859243Sobrien	is2atty = isdiagatty;
91959243Sobrien# if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
92059243Sobrien	(void) close_on_exec(2, 0);
92159243Sobrien# endif /* CLOSE_ON_EXEC && CLEX_DUPS */
92259243Sobrien    }
92359243Sobrien    didfds = 1;
92459243Sobrien}
92559243Sobrien
92659243Sobrienvoid
927167465Smpmypipe(int *pv)
92859243Sobrien{
92959243Sobrien
93059243Sobrien    if (pipe(pv) < 0)
93159243Sobrien	goto oops;
932145479Smp    (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
933145479Smp    (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
93459243Sobrien    if (pv[0] >= 0 && pv[1] >= 0)
93559243Sobrien	return;
936167465Smp    if (pv[0] >= 0)
937167465Smp	xclose(pv[0]);
938167465Smp    if (pv[1] >= 0)
939167465Smp	xclose(pv[1]);
94059243Sobrienoops:
94159243Sobrien    stderror(ERR_PIPE);
94259243Sobrien}
94359243Sobrien
94459243Sobrienstatic void
945167465Smpchkclob(const char *cp)
94659243Sobrien{
94759243Sobrien    struct stat stb;
94859243Sobrien
94959243Sobrien    if (stat(cp, &stb) < 0)
95059243Sobrien	return;
95159243Sobrien    if (S_ISCHR(stb.st_mode))
95259243Sobrien	return;
95359243Sobrien    stderror(ERR_EXISTS, cp);
95459243Sobrien}
955