xref: /freebsd/contrib/tcsh/sh.sem.c (revision 6560ac57ce879857203bc456cdc3849808dc0700)
1c80476e4SDavid E. O'Brien /*
2c80476e4SDavid E. O'Brien  * sh.sem.c: I/O redirections and job forking. A touchy issue!
3c80476e4SDavid E. O'Brien  *	     Most stuff with builtins is incorrect
4c80476e4SDavid E. O'Brien  */
5c80476e4SDavid E. O'Brien /*-
6c80476e4SDavid E. O'Brien  * Copyright (c) 1980, 1991 The Regents of the University of California.
7c80476e4SDavid E. O'Brien  * All rights reserved.
8c80476e4SDavid E. O'Brien  *
9c80476e4SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
10c80476e4SDavid E. O'Brien  * modification, are permitted provided that the following conditions
11c80476e4SDavid E. O'Brien  * are met:
12c80476e4SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
13c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
14c80476e4SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
15c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
16c80476e4SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
1729301572SMark Peek  * 3. Neither the name of the University nor the names of its contributors
18c80476e4SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
19c80476e4SDavid E. O'Brien  *    without specific prior written permission.
20c80476e4SDavid E. O'Brien  *
21c80476e4SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22c80476e4SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c80476e4SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24c80476e4SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25c80476e4SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26c80476e4SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27c80476e4SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28c80476e4SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29c80476e4SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30c80476e4SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c80476e4SDavid E. O'Brien  * SUCH DAMAGE.
32c80476e4SDavid E. O'Brien  */
33c80476e4SDavid E. O'Brien #include "sh.h"
34c80476e4SDavid E. O'Brien #include "tc.h"
35c80476e4SDavid E. O'Brien #include "tw.h"
363b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
37c80476e4SDavid E. O'Brien #include "nt.const.h"
383b6eaa7bSAndrey A. Chernov #endif /*WINNT_NATIVE*/
39c80476e4SDavid E. O'Brien 
40c80476e4SDavid E. O'Brien #ifdef CLOSE_ON_EXEC
41c80476e4SDavid E. O'Brien # ifndef SUNOS4
42c80476e4SDavid E. O'Brien #  ifndef CLEX_DUPS
43c80476e4SDavid E. O'Brien #   define CLEX_DUPS
44c80476e4SDavid E. O'Brien #  endif /* CLEX_DUPS */
45c80476e4SDavid E. O'Brien # endif /* !SUNOS4 */
46c80476e4SDavid E. O'Brien #endif /* CLOSE_ON_EXEC */
47c80476e4SDavid E. O'Brien 
48c80476e4SDavid E. O'Brien #if defined(__sparc__) || defined(sparc)
499ccc37e3SMark Peek # if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(__linux__) && !defined(__GNU__) && !defined(__GLIBC__)
50c80476e4SDavid E. O'Brien #  include <vfork.h>
5123338178SMark Peek # endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !glibc */
52c80476e4SDavid E. O'Brien #endif /* __sparc__ || sparc */
53c80476e4SDavid E. O'Brien 
54c80476e4SDavid E. O'Brien #ifdef VFORK
5545e5710bSMark Peek static	void		vffree		(int);
56c80476e4SDavid E. O'Brien #endif
5745e5710bSMark Peek static	Char		*splicepipe	(struct command *, Char *);
5845e5710bSMark Peek static	void		 doio		(struct command *, int *, int *);
5945e5710bSMark Peek static	void		 chkclob	(const char *);
60c80476e4SDavid E. O'Brien 
61c80476e4SDavid E. O'Brien /*
62c80476e4SDavid E. O'Brien  * C shell
63c80476e4SDavid E. O'Brien  */
64c80476e4SDavid E. O'Brien 
65c80476e4SDavid E. O'Brien /*
66c80476e4SDavid E. O'Brien  * For SVR4, there are problems with pipelines having the first process as
67c80476e4SDavid E. O'Brien  * the group leader.  The problem occurs when the first process exits before
68c80476e4SDavid E. O'Brien  * the others have a chance to setpgid().  This is because in SVR4 you can't
69c80476e4SDavid E. O'Brien  * have a zombie as a group leader.  The solution I have used is to reverse
70c80476e4SDavid E. O'Brien  * the order in which pipelines are started, making the last process the
71c80476e4SDavid E. O'Brien  * group leader.  (Note I am not using 'pipeline' in the generic sense -- I
72c80476e4SDavid E. O'Brien  * mean processes connected by '|'.)  I don't know yet if this causes other
73c80476e4SDavid E. O'Brien  * problems.
74c80476e4SDavid E. O'Brien  *
75c80476e4SDavid E. O'Brien  * All the changes for this are in execute(), and are enclosed in
76c80476e4SDavid E. O'Brien  * '#ifdef BACKPIPE'
77c80476e4SDavid E. O'Brien  *
78c80476e4SDavid E. O'Brien  * David Dawes (dawes@physics.su.oz.au) Oct 1991
79c80476e4SDavid E. O'Brien  */
80c80476e4SDavid E. O'Brien 
81c80476e4SDavid E. O'Brien /*VARARGS 1*/
82c80476e4SDavid E. O'Brien void
execute(struct command * t,volatile int wanttty,int * pipein,int * pipeout,int do_glob)8345e5710bSMark Peek execute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
8445e5710bSMark Peek     int do_glob)
85c80476e4SDavid E. O'Brien {
8623338178SMark Peek     int    forked = 0;
8745e5710bSMark Peek     const struct biltins * volatile bifunc;
8845e5710bSMark Peek     pid_t pid = 0;
89c80476e4SDavid E. O'Brien     int     pv[2];
9045e5710bSMark Peek     sigset_t set;
9145e5710bSMark Peek     static sigset_t csigset;
92c80476e4SDavid E. O'Brien #ifdef VFORK
93c80476e4SDavid E. O'Brien     static int onosigchld = 0;
94c80476e4SDavid E. O'Brien #endif /* VFORK */
95c80476e4SDavid E. O'Brien     static int nosigchld = 0;
96c80476e4SDavid E. O'Brien 
97c80476e4SDavid E. O'Brien     (void) &wanttty;
98c80476e4SDavid E. O'Brien     (void) &forked;
99c80476e4SDavid E. O'Brien     (void) &bifunc;
100c80476e4SDavid E. O'Brien 
101c80476e4SDavid E. O'Brien     if (t == 0)
102c80476e4SDavid E. O'Brien 	return;
103c80476e4SDavid E. O'Brien 
1043b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
105c80476e4SDavid E. O'Brien     {
106c80476e4SDavid E. O'Brien         if ((varval(STRNTslowexec) == STRNULL) &&
107c80476e4SDavid E. O'Brien             !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
108c80476e4SDavid E. O'Brien             (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
109c80476e4SDavid E. O'Brien 	    !isbfunc(t)) {
110c80476e4SDavid E. O'Brien 	    if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
111c80476e4SDavid E. O'Brien 		(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
112c80476e4SDavid E. O'Brien 	    Dfix(t);
113c80476e4SDavid E. O'Brien             if (nt_try_fast_exec(t) == 0)
114c80476e4SDavid E. O'Brien                 return;
115c80476e4SDavid E. O'Brien         }
116c80476e4SDavid E. O'Brien     }
1173b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
118c80476e4SDavid E. O'Brien 
119c80476e4SDavid E. O'Brien     /*
120c80476e4SDavid E. O'Brien      * Ed hutchins@sgi.com & Dominic dbg@sgi.com
121c80476e4SDavid E. O'Brien      * Sat Feb 25 03:13:11 PST 1995
122c80476e4SDavid E. O'Brien      * try implicit cd if we have a 1 word command
123c80476e4SDavid E. O'Brien      */
124c80476e4SDavid E. O'Brien     if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
125c80476e4SDavid E. O'Brien 	 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
12645e5710bSMark Peek 	Char *sCName;
127c80476e4SDavid E. O'Brien 	struct stat stbuf;
128c80476e4SDavid E. O'Brien 	char *pathname;
129c80476e4SDavid E. O'Brien 
13045e5710bSMark Peek 	sCName = dollar(t->t_dcom[0]);
13145e5710bSMark Peek 	if (sCName != NULL && sCName[0] == '~') {
13245e5710bSMark Peek 	    struct Strbuf buf = Strbuf_INIT;
13345e5710bSMark Peek 	    const Char *name_end;
134c80476e4SDavid E. O'Brien 
13545e5710bSMark Peek 	    for (name_end = sCName + 1; *name_end != '\0' && *name_end != '/';
13645e5710bSMark Peek 		 name_end++)
13745e5710bSMark Peek 		continue;
13845e5710bSMark Peek 	    if (name_end != sCName + 1) {
13945e5710bSMark Peek 		Char *name, *home;
14045e5710bSMark Peek 
14145e5710bSMark Peek 		name = Strnsave(sCName + 1, name_end - (sCName + 1));
14245e5710bSMark Peek 		home = gethdir(name);
14345e5710bSMark Peek 		if (home != NULL) {
14445e5710bSMark Peek 		    Strbuf_append(&buf, home);
14545e5710bSMark Peek 		    xfree(home);
14645e5710bSMark Peek 		} else
14745e5710bSMark Peek 		    Strbuf_append(&buf, name);
14845e5710bSMark Peek 		xfree(name);
14945e5710bSMark Peek 	    } else
15045e5710bSMark Peek 		Strbuf_append(&buf, varval(STRhome));
15145e5710bSMark Peek 	    Strbuf_append(&buf, name_end);
15245e5710bSMark Peek 	    xfree(sCName);
15345e5710bSMark Peek 	    sCName = Strbuf_finish(&buf);
154c80476e4SDavid E. O'Brien 	}
155c80476e4SDavid E. O'Brien 
156c80476e4SDavid E. O'Brien 	pathname = short2str(sCName);
15745e5710bSMark Peek 	xfree(sCName);
158c80476e4SDavid E. O'Brien 	/* if this is a dir, tack a "cd" on as the first arg */
15945e5710bSMark Peek 	if (pathname != NULL &&
16045e5710bSMark Peek 	    ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
1613b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
162c80476e4SDavid E. O'Brien 	     || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
1633b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
16445e5710bSMark Peek 	     )) {
165c80476e4SDavid E. O'Brien 	    Char *vCD[2];
166c80476e4SDavid E. O'Brien 	    Char **ot_dcom = t->t_dcom;
167c80476e4SDavid E. O'Brien 
168c80476e4SDavid E. O'Brien 	    vCD[0] = Strsave(STRcd);
169c80476e4SDavid E. O'Brien 	    vCD[1] = NULL;
170c80476e4SDavid E. O'Brien 	    t->t_dcom = blkspl(vCD, ot_dcom);
17145e5710bSMark Peek 	    xfree(ot_dcom);
172c80476e4SDavid E. O'Brien 	    if (implicit_cd > 1) {
173c80476e4SDavid E. O'Brien 		blkpr(t->t_dcom);
174c80476e4SDavid E. O'Brien 		xputchar( '\n' );
175c80476e4SDavid E. O'Brien 	    }
176c80476e4SDavid E. O'Brien 	}
177c80476e4SDavid E. O'Brien     }
178c80476e4SDavid E. O'Brien 
179c80476e4SDavid E. O'Brien     /*
180c80476e4SDavid E. O'Brien      * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
181c80476e4SDavid E. O'Brien      * Don't check for wantty > 0...
182c80476e4SDavid E. O'Brien      */
183c80476e4SDavid E. O'Brien     if (t->t_dflg & F_AMPERSAND)
184c80476e4SDavid E. O'Brien 	wanttty = 0;
185c80476e4SDavid E. O'Brien     switch (t->t_dtyp) {
186c80476e4SDavid E. O'Brien 
187c80476e4SDavid E. O'Brien     case NODE_COMMAND:
188c80476e4SDavid E. O'Brien 	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
18945e5710bSMark Peek 	    memmove(t->t_dcom[0], t->t_dcom[0] + 1,
19045e5710bSMark Peek 		    (Strlen(t->t_dcom[0] + 1) + 1) * sizeof (*t->t_dcom[0]));
191c80476e4SDavid E. O'Brien 	if ((t->t_dflg & F_REPEAT) == 0)
192c80476e4SDavid E. O'Brien 	    Dfix(t);		/* $ " ' \ */
193c80476e4SDavid E. O'Brien 	if (t->t_dcom[0] == 0) {
194c80476e4SDavid E. O'Brien 	    return;
195c80476e4SDavid E. O'Brien 	}
196c80476e4SDavid E. O'Brien 	/*FALLTHROUGH*/
197c80476e4SDavid E. O'Brien 
198c80476e4SDavid E. O'Brien     case NODE_PAREN:
199c80476e4SDavid E. O'Brien #ifdef BACKPIPE
200c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_PIPEIN)
201c80476e4SDavid E. O'Brien 	    mypipe(pipein);
202c80476e4SDavid E. O'Brien #else /* !BACKPIPE */
203c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_PIPEOUT)
204c80476e4SDavid E. O'Brien 	    mypipe(pipeout);
205c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
206c80476e4SDavid E. O'Brien 	/*
207c80476e4SDavid E. O'Brien 	 * Must do << early so parent will know where input pointer should be.
208c80476e4SDavid E. O'Brien 	 * If noexec then this is all we do.
209c80476e4SDavid E. O'Brien 	 */
210c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_READ) {
211*19d2e3deSDmitry Chagin 	    int old_pintr_disabled;
212*19d2e3deSDmitry Chagin 
21345e5710bSMark Peek 	    xclose(0);
214*19d2e3deSDmitry Chagin 	    if (setintr)
215*19d2e3deSDmitry Chagin 		pintr_push_enable(&old_pintr_disabled);
216c80476e4SDavid E. O'Brien 	    heredoc(t->t_dlef);
217*19d2e3deSDmitry Chagin 	    if (setintr)
218*19d2e3deSDmitry Chagin 		cleanup_until(&old_pintr_disabled);
219c80476e4SDavid E. O'Brien 	    if (noexec)
22045e5710bSMark Peek 		xclose(0);
221c80476e4SDavid E. O'Brien 	}
222c80476e4SDavid E. O'Brien 
22345e5710bSMark Peek 	setcopy(STRstatus, STR0, VAR_READWRITE);
224c80476e4SDavid E. O'Brien 
225c80476e4SDavid E. O'Brien 	/*
226c80476e4SDavid E. O'Brien 	 * This mess is the necessary kludge to handle the prefix builtins:
227c80476e4SDavid E. O'Brien 	 * nice, nohup, time.  These commands can also be used by themselves,
228c80476e4SDavid E. O'Brien 	 * and this is not handled here. This will also work when loops are
229c80476e4SDavid E. O'Brien 	 * parsed.
230c80476e4SDavid E. O'Brien 	 */
231c80476e4SDavid E. O'Brien 	while (t->t_dtyp == NODE_COMMAND)
232c80476e4SDavid E. O'Brien 	    if (eq(t->t_dcom[0], STRnice)) {
233c80476e4SDavid E. O'Brien 		if (t->t_dcom[1]) {
234c80476e4SDavid E. O'Brien 		    if (strchr("+-", t->t_dcom[1][0])) {
235c80476e4SDavid E. O'Brien 			if (t->t_dcom[2]) {
236c80476e4SDavid E. O'Brien 			    setname("nice");
2379ccc37e3SMark Peek 			    t->t_nice = (unsigned char)getn(t->t_dcom[1]);
238c80476e4SDavid E. O'Brien 			    lshift(t->t_dcom, 2);
239c80476e4SDavid E. O'Brien 			    t->t_dflg |= F_NICE;
240c80476e4SDavid E. O'Brien 			}
241c80476e4SDavid E. O'Brien 			else
242c80476e4SDavid E. O'Brien 			    break;
243c80476e4SDavid E. O'Brien 		    }
244c80476e4SDavid E. O'Brien 		    else {
245c80476e4SDavid E. O'Brien 			t->t_nice = 4;
246c80476e4SDavid E. O'Brien 			lshift(t->t_dcom, 1);
247c80476e4SDavid E. O'Brien 			t->t_dflg |= F_NICE;
248c80476e4SDavid E. O'Brien 		    }
249c80476e4SDavid E. O'Brien 		}
250c80476e4SDavid E. O'Brien 		else
251c80476e4SDavid E. O'Brien 		    break;
252c80476e4SDavid E. O'Brien 	    }
253c80476e4SDavid E. O'Brien 	    else if (eq(t->t_dcom[0], STRnohup)) {
254c80476e4SDavid E. O'Brien 		if (t->t_dcom[1]) {
255c80476e4SDavid E. O'Brien 		    t->t_dflg |= F_NOHUP;
256c80476e4SDavid E. O'Brien 		    lshift(t->t_dcom, 1);
257c80476e4SDavid E. O'Brien 		}
258c80476e4SDavid E. O'Brien 		else
259c80476e4SDavid E. O'Brien 		    break;
260c80476e4SDavid E. O'Brien 	    }
261c80476e4SDavid E. O'Brien 	    else if (eq(t->t_dcom[0], STRhup)) {
262c80476e4SDavid E. O'Brien 		if (t->t_dcom[1]) {
263c80476e4SDavid E. O'Brien 		    t->t_dflg |= F_HUP;
264c80476e4SDavid E. O'Brien 		    lshift(t->t_dcom, 1);
265c80476e4SDavid E. O'Brien 		}
266c80476e4SDavid E. O'Brien 		else
267c80476e4SDavid E. O'Brien 		    break;
268c80476e4SDavid E. O'Brien 	    }
269c80476e4SDavid E. O'Brien 	    else if (eq(t->t_dcom[0], STRtime)) {
270c80476e4SDavid E. O'Brien 		if (t->t_dcom[1]) {
271c80476e4SDavid E. O'Brien 		    t->t_dflg |= F_TIME;
272c80476e4SDavid E. O'Brien 		    lshift(t->t_dcom, 1);
273c80476e4SDavid E. O'Brien 		}
274c80476e4SDavid E. O'Brien 		else
275c80476e4SDavid E. O'Brien 		    break;
276c80476e4SDavid E. O'Brien 	    }
277c80476e4SDavid E. O'Brien #ifdef F_VER
278c80476e4SDavid E. O'Brien 	    else if (eq(t->t_dcom[0], STRver))
279c80476e4SDavid E. O'Brien 		if (t->t_dcom[1] && t->t_dcom[2]) {
280c80476e4SDavid E. O'Brien 		    setname("ver");
281c80476e4SDavid E. O'Brien 		    t->t_systype = getv(t->t_dcom[1]);
282c80476e4SDavid E. O'Brien 		    lshift(t->t_dcom, 2);
283c80476e4SDavid E. O'Brien 		    t->t_dflg |= F_VER;
284c80476e4SDavid E. O'Brien 		}
285c80476e4SDavid E. O'Brien 		else
286c80476e4SDavid E. O'Brien 		    break;
287c80476e4SDavid E. O'Brien #endif  /* F_VER */
288c80476e4SDavid E. O'Brien 	    else
289c80476e4SDavid E. O'Brien 		break;
290c80476e4SDavid E. O'Brien 
291c80476e4SDavid E. O'Brien 	/* is it a command */
292c80476e4SDavid E. O'Brien 	if (t->t_dtyp == NODE_COMMAND) {
293c80476e4SDavid E. O'Brien 	    /*
294c80476e4SDavid E. O'Brien 	     * Check if we have a builtin function and remember which one.
295c80476e4SDavid E. O'Brien 	     */
296c80476e4SDavid E. O'Brien 	    bifunc = isbfunc(t);
29745e5710bSMark Peek  	    if (noexec) {
298c80476e4SDavid E. O'Brien 		/*
299c80476e4SDavid E. O'Brien 		 * Continue for builtins that are part of the scripting language
300c80476e4SDavid E. O'Brien 		 */
30145e5710bSMark Peek 		if (bifunc == NULL)
30245e5710bSMark Peek 		    break;
303c80476e4SDavid E. O'Brien 		if (bifunc->bfunct != (bfunc_t)dobreak	&&
304c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)docontin	&&
305c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)doelse	&&
306c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)doend	&&
307c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)doforeach&&
308c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)dogoto	&&
309c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)doif	&&
310c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)dorepeat	&&
311c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)doswbrk	&&
312c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)doswitch	&&
313c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)dowhile	&&
314c80476e4SDavid E. O'Brien 		    bifunc->bfunct != (bfunc_t)dozip)
315c80476e4SDavid E. O'Brien 		    break;
316c80476e4SDavid E. O'Brien 	    }
317c80476e4SDavid E. O'Brien 	}
318c80476e4SDavid E. O'Brien 	else {			/* not a command */
319c80476e4SDavid E. O'Brien 	    bifunc = NULL;
320c80476e4SDavid E. O'Brien 	    if (noexec)
321c80476e4SDavid E. O'Brien 		break;
322c80476e4SDavid E. O'Brien 	}
323c80476e4SDavid E. O'Brien 
324c80476e4SDavid E. O'Brien 	/*
3256767bd61SMark Peek 	 * GrP Executing a command - run jobcmd hook
3266767bd61SMark Peek 	 * Don't run for builtins
3276767bd61SMark Peek 	 * Don't run if we're not in a tty
3286767bd61SMark Peek 	 * Don't run if we're not really executing
3296767bd61SMark Peek 	 */
330a15e6f9aSMark Peek 	/*
331a15e6f9aSMark Peek 	 * CR  -  Charles Ross Aug 2005
332a15e6f9aSMark Peek 	 * added "isoutatty".
333a15e6f9aSMark Peek 	 * The new behavior is that the jobcmd won't be executed
334a15e6f9aSMark Peek 	 * if stdout (SHOUT) isnt attached to a tty.. IE when
335a15e6f9aSMark Peek 	 * redirecting, or using backquotes etc..
336a15e6f9aSMark Peek 	 */
337a15e6f9aSMark Peek 	if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty && isoutatty) {
3386767bd61SMark Peek 	    Char *cmd = unparse(t);
33945e5710bSMark Peek 
34045e5710bSMark Peek 	    cleanup_push(cmd, xfree);
3416767bd61SMark Peek 	    job_cmd(cmd);
34245e5710bSMark Peek 	    cleanup_until(cmd);
3436767bd61SMark Peek 	}
3446767bd61SMark Peek 
3456767bd61SMark Peek 	/*
346c80476e4SDavid E. O'Brien 	 * We fork only if we are timed, or are not the end of a parenthesized
347c80476e4SDavid E. O'Brien 	 * list and not a simple builtin function. Simple meaning one that is
348c80476e4SDavid E. O'Brien 	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
349c80476e4SDavid E. O'Brien 	 * fork in some of these cases.
350c80476e4SDavid E. O'Brien 	 */
351c80476e4SDavid E. O'Brien #ifdef BACKPIPE
352c80476e4SDavid E. O'Brien 	/*
353c80476e4SDavid E. O'Brien 	 * Can't have NOFORK for the tail of a pipe - because it is not the
354c80476e4SDavid E. O'Brien 	 * last command spawned (even if it is at the end of a parenthesised
355c80476e4SDavid E. O'Brien 	 * list).
356c80476e4SDavid E. O'Brien 	 */
357c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_PIPEIN)
358c80476e4SDavid E. O'Brien 	    t->t_dflg &= ~(F_NOFORK);
359*19d2e3deSDmitry Chagin #else
360*19d2e3deSDmitry Chagin 	/*
361*19d2e3deSDmitry Chagin 	 * "command | builtin" may cause major misbehaviour as noted in
362*19d2e3deSDmitry Chagin 	 * in the BUGS file entry
363*19d2e3deSDmitry Chagin 	 * Subject: Redirected input to built-in functions misbehaves badly
364*19d2e3deSDmitry Chagin 	 * forking when the builtin is the end of the pipe corrects the
365*19d2e3deSDmitry Chagin 	 * problem.
366*19d2e3deSDmitry Chagin 	 */
367*19d2e3deSDmitry Chagin 	if (bifunc && (t->t_dflg & F_PIPEIN))
368*19d2e3deSDmitry Chagin 	    t->t_dflg &= ~(F_NOFORK);
369c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
370*19d2e3deSDmitry Chagin 	/*
371*19d2e3deSDmitry Chagin 	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
372*19d2e3deSDmitry Chagin 	 * shell not to change dir! (XXX: but only for nice?)
373*19d2e3deSDmitry Chagin 	 */
374c80476e4SDavid E. O'Brien 	if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
375c80476e4SDavid E. O'Brien 		       bifunc->bfunct == (bfunc_t)dopushd ||
376c80476e4SDavid E. O'Brien 		       bifunc->bfunct == (bfunc_t)dopopd))
377c80476e4SDavid E. O'Brien 	    t->t_dflg &= ~(F_NICE);
378*19d2e3deSDmitry Chagin 
379c80476e4SDavid E. O'Brien 	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
380c80476e4SDavid E. O'Brien 	     (!bifunc || t->t_dflg &
381c80476e4SDavid E. O'Brien 	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
382c80476e4SDavid E. O'Brien 	/*
383c80476e4SDavid E. O'Brien 	 * We have to fork for eval too.
384c80476e4SDavid E. O'Brien 	 */
385c80476e4SDavid E. O'Brien 	    (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
3863b6eaa7bSAndrey A. Chernov 	     bifunc->bfunct == (bfunc_t)doeval)) {
387c80476e4SDavid E. O'Brien #ifdef VFORK
388c80476e4SDavid E. O'Brien 	    if (t->t_dtyp == NODE_PAREN ||
389c80476e4SDavid E. O'Brien 		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
390c80476e4SDavid E. O'Brien #endif /* VFORK */
391c80476e4SDavid E. O'Brien 	    {
392c80476e4SDavid E. O'Brien 		forked++;
393c80476e4SDavid E. O'Brien 		/*
394c80476e4SDavid E. O'Brien 		 * We need to block SIGCHLD here, so that if the process does
395c80476e4SDavid E. O'Brien 		 * not die before we can set the process group
396c80476e4SDavid E. O'Brien 		 */
397c80476e4SDavid E. O'Brien 		if (wanttty >= 0 && !nosigchld) {
39845e5710bSMark Peek 		    sigemptyset(&set);
39945e5710bSMark Peek 		    sigaddset(&set, SIGCHLD);
40045e5710bSMark Peek 		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
401c80476e4SDavid E. O'Brien 
402c80476e4SDavid E. O'Brien 		    nosigchld = 1;
403c80476e4SDavid E. O'Brien 		}
404c80476e4SDavid E. O'Brien 
405c80476e4SDavid E. O'Brien 		pid = pfork(t, wanttty);
406c80476e4SDavid E. O'Brien 		if (pid == 0 && nosigchld) {
40745e5710bSMark Peek 		    sigprocmask(SIG_SETMASK, &csigset, NULL);
408c80476e4SDavid E. O'Brien 		    nosigchld = 0;
409c80476e4SDavid E. O'Brien 		}
410c80476e4SDavid E. O'Brien 		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
411c80476e4SDavid E. O'Brien 		    backpid = pid;
412c80476e4SDavid E. O'Brien 	    }
413c80476e4SDavid E. O'Brien 
414c80476e4SDavid E. O'Brien #ifdef VFORK
415c80476e4SDavid E. O'Brien 	    else {
416c80476e4SDavid E. O'Brien 		int     ochild, osetintr, ohaderr, odidfds;
417c80476e4SDavid E. O'Brien 		int     oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
418c80476e4SDavid E. O'Brien 		int     oisoutatty, oisdiagatty;
41945e5710bSMark Peek 		sigset_t oset, ocsigset;
420c80476e4SDavid E. O'Brien # ifndef CLOSE_ON_EXEC
421c80476e4SDavid E. O'Brien 		int     odidcch;
422c80476e4SDavid E. O'Brien # endif  /* !CLOSE_ON_EXEC */
423c80476e4SDavid E. O'Brien 
424c80476e4SDavid E. O'Brien 		/*
425c80476e4SDavid E. O'Brien 		 * Prepare for the vfork by saving everything that the child
426c80476e4SDavid E. O'Brien 		 * corrupts before it exec's. Note that in some signal
427c80476e4SDavid E. O'Brien 		 * implementations which keep the signal info in user space
428c80476e4SDavid E. O'Brien 		 * (e.g. Sun's) it will also be necessary to save and restore
429c80476e4SDavid E. O'Brien 		 * the current sigvec's for the signals the child touches
430c80476e4SDavid E. O'Brien 		 * before it exec's.
431c80476e4SDavid E. O'Brien 		 */
432c80476e4SDavid E. O'Brien 
433c80476e4SDavid E. O'Brien 		/*
434c80476e4SDavid E. O'Brien 		 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
435c80476e4SDavid E. O'Brien 		 * Gilbrech - 11/22/87)
436c80476e4SDavid E. O'Brien 		 */
437c80476e4SDavid E. O'Brien # ifdef SAVESIGVEC
43845e5710bSMark Peek 		struct sigaction savesv[NSIGSAVED];
43945e5710bSMark Peek 		sigset_t savesm;
440c80476e4SDavid E. O'Brien 
441c80476e4SDavid E. O'Brien # endif /* SAVESIGVEC */
442c80476e4SDavid E. O'Brien 		if (wanttty >= 0 && !nosigchld && !noexec) {
44345e5710bSMark Peek 		    sigemptyset(&set);
44445e5710bSMark Peek 		    sigaddset(&set, SIGCHLD);
44545e5710bSMark Peek 		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
446c80476e4SDavid E. O'Brien 		    nosigchld = 1;
447c80476e4SDavid E. O'Brien 		}
44845e5710bSMark Peek 		sigemptyset(&set);
44945e5710bSMark Peek 		sigaddset(&set, SIGCHLD);
45045e5710bSMark Peek 		sigaddset(&set, SIGINT);
45145e5710bSMark Peek 		(void)sigprocmask(SIG_BLOCK, &set, &oset);
452c80476e4SDavid E. O'Brien 		ochild = child;
453c80476e4SDavid E. O'Brien 		osetintr = setintr;
454c80476e4SDavid E. O'Brien 		ohaderr = haderr;
455c80476e4SDavid E. O'Brien 		odidfds = didfds;
456c80476e4SDavid E. O'Brien # ifndef CLOSE_ON_EXEC
457c80476e4SDavid E. O'Brien 		odidcch = didcch;
458c80476e4SDavid E. O'Brien # endif /* !CLOSE_ON_EXEC */
459c80476e4SDavid E. O'Brien 		oSHIN = SHIN;
460c80476e4SDavid E. O'Brien 		oSHOUT = SHOUT;
461c80476e4SDavid E. O'Brien 		oSHDIAG = SHDIAG;
462c80476e4SDavid E. O'Brien 		oOLDSTD = OLDSTD;
463c80476e4SDavid E. O'Brien 		otpgrp = tpgrp;
464c80476e4SDavid E. O'Brien 		oisoutatty = isoutatty;
465c80476e4SDavid E. O'Brien 		oisdiagatty = isdiagatty;
46645e5710bSMark Peek 		ocsigset = csigset;
467c80476e4SDavid E. O'Brien 		onosigchld = nosigchld;
468c80476e4SDavid E. O'Brien 		Vsav = Vdp = 0;
469c80476e4SDavid E. O'Brien 		Vexpath = 0;
470c80476e4SDavid E. O'Brien 		Vt = 0;
471c80476e4SDavid E. O'Brien # ifdef SAVESIGVEC
47245e5710bSMark Peek 		savesigvec(savesv, savesm);
473c80476e4SDavid E. O'Brien # endif /* SAVESIGVEC */
474c80476e4SDavid E. O'Brien 		if (use_fork)
475c80476e4SDavid E. O'Brien 		    pid = fork();
476c80476e4SDavid E. O'Brien 		else
477c80476e4SDavid E. O'Brien 		    pid = vfork();
478c80476e4SDavid E. O'Brien 
479c80476e4SDavid E. O'Brien 		if (pid < 0) {
480c80476e4SDavid E. O'Brien # ifdef SAVESIGVEC
481c80476e4SDavid E. O'Brien 		    restoresigvec(savesv, savesm);
482c80476e4SDavid E. O'Brien # endif /* SAVESIGVEC */
48345e5710bSMark Peek 		    sigprocmask(SIG_SETMASK, &oset, NULL);
484c80476e4SDavid E. O'Brien 		    stderror(ERR_NOPROC);
485c80476e4SDavid E. O'Brien 		}
486c80476e4SDavid E. O'Brien 		forked++;
487c80476e4SDavid E. O'Brien 		if (pid) {	/* parent */
488c80476e4SDavid E. O'Brien # ifdef SAVESIGVEC
489c80476e4SDavid E. O'Brien 		    restoresigvec(savesv, savesm);
490c80476e4SDavid E. O'Brien # endif /* SAVESIGVEC */
491c80476e4SDavid E. O'Brien 		    child = ochild;
492c80476e4SDavid E. O'Brien 		    setintr = osetintr;
493c80476e4SDavid E. O'Brien 		    haderr = ohaderr;
494c80476e4SDavid E. O'Brien 		    didfds = odidfds;
495c80476e4SDavid E. O'Brien 		    SHIN = oSHIN;
496c80476e4SDavid E. O'Brien # ifndef CLOSE_ON_EXEC
497c80476e4SDavid E. O'Brien 		    didcch = odidcch;
498c80476e4SDavid E. O'Brien # endif /* !CLOSE_ON_EXEC */
499c80476e4SDavid E. O'Brien 		    SHOUT = oSHOUT;
500c80476e4SDavid E. O'Brien 		    SHDIAG = oSHDIAG;
501c80476e4SDavid E. O'Brien 		    OLDSTD = oOLDSTD;
502c80476e4SDavid E. O'Brien 		    tpgrp = otpgrp;
503c80476e4SDavid E. O'Brien 		    isoutatty = oisoutatty;
504c80476e4SDavid E. O'Brien 		    isdiagatty = oisdiagatty;
50545e5710bSMark Peek 		    csigset = ocsigset;
506c80476e4SDavid E. O'Brien 		    nosigchld = onosigchld;
507c80476e4SDavid E. O'Brien 
50845e5710bSMark Peek 		    xfree(Vsav);
509c80476e4SDavid E. O'Brien 		    Vsav = 0;
51045e5710bSMark Peek 		    xfree(Vdp);
511c80476e4SDavid E. O'Brien 		    Vdp = 0;
51245e5710bSMark Peek 		    xfree(Vexpath);
513c80476e4SDavid E. O'Brien 		    Vexpath = 0;
51445e5710bSMark Peek 		    blk_cleanup(Vt);
515c80476e4SDavid E. O'Brien 		    Vt = 0;
516c80476e4SDavid E. O'Brien 		    /* this is from pfork() */
517c80476e4SDavid E. O'Brien 		    palloc(pid, t);
51845e5710bSMark Peek 		    sigprocmask(SIG_SETMASK, &oset, NULL);
519c80476e4SDavid E. O'Brien 		}
520c80476e4SDavid E. O'Brien 		else {		/* child */
521c80476e4SDavid E. O'Brien 		    /* this is from pfork() */
52245e5710bSMark Peek 		    pid_t pgrp;
52323338178SMark Peek 		    int    ignint = 0;
524c80476e4SDavid E. O'Brien 		    if (nosigchld) {
52545e5710bSMark Peek 			sigprocmask(SIG_SETMASK, &csigset, NULL);
526c80476e4SDavid E. O'Brien 			nosigchld = 0;
527c80476e4SDavid E. O'Brien 		    }
528c80476e4SDavid E. O'Brien 
529c80476e4SDavid E. O'Brien 		    if (setintr)
530c80476e4SDavid E. O'Brien 			ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
531c80476e4SDavid E. O'Brien 				|| (gointr && eq(gointr, STRminus));
532c80476e4SDavid E. O'Brien 		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
533c80476e4SDavid E. O'Brien 		    child++;
534c80476e4SDavid E. O'Brien 		    if (setintr) {
535c80476e4SDavid E. O'Brien 			setintr = 0;
536c80476e4SDavid E. O'Brien /*
537c80476e4SDavid E. O'Brien  * casts made right for SunOS 4.0 by Douglas C. Schmidt
538c80476e4SDavid E. O'Brien  * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
539c80476e4SDavid E. O'Brien  * (thanks! -- PWP)
540c80476e4SDavid E. O'Brien  *
541c80476e4SDavid E. O'Brien  * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
542c80476e4SDavid E. O'Brien  * (thanks again)
543c80476e4SDavid E. O'Brien  */
544c80476e4SDavid E. O'Brien 			if (ignint) {
545c80476e4SDavid E. O'Brien 			    (void) signal(SIGINT, SIG_IGN);
546c80476e4SDavid E. O'Brien 			    (void) signal(SIGQUIT, SIG_IGN);
547c80476e4SDavid E. O'Brien 			}
548c80476e4SDavid E. O'Brien 			else {
549c80476e4SDavid E. O'Brien 			    (void) signal(SIGINT, vffree);
550c80476e4SDavid E. O'Brien 			    (void) signal(SIGQUIT, SIG_DFL);
551c80476e4SDavid E. O'Brien 			}
552c80476e4SDavid E. O'Brien # ifdef BSDJOBS
553c80476e4SDavid E. O'Brien 			if (wanttty >= 0) {
554c80476e4SDavid E. O'Brien 			    (void) signal(SIGTSTP, SIG_DFL);
555c80476e4SDavid E. O'Brien 			    (void) signal(SIGTTIN, SIG_DFL);
556c80476e4SDavid E. O'Brien 			    (void) signal(SIGTTOU, SIG_DFL);
557c80476e4SDavid E. O'Brien 			}
558c80476e4SDavid E. O'Brien # endif /* BSDJOBS */
559c80476e4SDavid E. O'Brien 
56045e5710bSMark Peek 			sigaction(SIGTERM, &parterm, NULL);
561c80476e4SDavid E. O'Brien 		    }
562c80476e4SDavid E. O'Brien 		    else if (tpgrp == -1 &&
563c80476e4SDavid E. O'Brien 			     (t->t_dflg & F_NOINTERRUPT)) {
564c80476e4SDavid E. O'Brien 			(void) signal(SIGINT, SIG_IGN);
565c80476e4SDavid E. O'Brien 			(void) signal(SIGQUIT, SIG_IGN);
566c80476e4SDavid E. O'Brien 		    }
567c80476e4SDavid E. O'Brien 
568c80476e4SDavid E. O'Brien 		    pgetty(wanttty, pgrp);
569c80476e4SDavid E. O'Brien 
570c80476e4SDavid E. O'Brien 		    if (t->t_dflg & F_NOHUP)
571c80476e4SDavid E. O'Brien 			(void) signal(SIGHUP, SIG_IGN);
572c80476e4SDavid E. O'Brien 		    if (t->t_dflg & F_HUP)
573c80476e4SDavid E. O'Brien 			(void) signal(SIGHUP, SIG_DFL);
574c80476e4SDavid E. O'Brien 		    if (t->t_dflg & F_NICE) {
575c80476e4SDavid E. O'Brien 			int nval = SIGN_EXTEND_CHAR(t->t_nice);
576*19d2e3deSDmitry Chagin # if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
5776767bd61SMark Peek 			if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
5786767bd61SMark Peek 				stderror(ERR_SYSTEM, "setpriority",
5796767bd61SMark Peek 				    strerror(errno));
580*19d2e3deSDmitry Chagin # else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
581c80476e4SDavid E. O'Brien 			(void) nice(nval);
582*19d2e3deSDmitry Chagin # endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
583c80476e4SDavid E. O'Brien 		    }
584c80476e4SDavid E. O'Brien # ifdef F_VER
585c80476e4SDavid E. O'Brien 		    if (t->t_dflg & F_VER) {
586c80476e4SDavid E. O'Brien 			tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
587c80476e4SDavid E. O'Brien 			dohash(NULL, NULL);
588c80476e4SDavid E. O'Brien 		    }
589c80476e4SDavid E. O'Brien # endif /* F_VER */
590c80476e4SDavid E. O'Brien 		}
591c80476e4SDavid E. O'Brien 
592c80476e4SDavid E. O'Brien 	    }
593c80476e4SDavid E. O'Brien #endif /* VFORK */
5943b6eaa7bSAndrey A. Chernov 	}
595c80476e4SDavid E. O'Brien 	if (pid != 0) {
596c80476e4SDavid E. O'Brien 	    /*
597c80476e4SDavid E. O'Brien 	     * It would be better if we could wait for the whole job when we
598c80476e4SDavid E. O'Brien 	     * knew the last process had been started.  Pwait, in fact, does
599c80476e4SDavid E. O'Brien 	     * wait for the whole job anyway, but this test doesn't really
600c80476e4SDavid E. O'Brien 	     * express our intentions.
601c80476e4SDavid E. O'Brien 	     */
602c80476e4SDavid E. O'Brien #ifdef BACKPIPE
603c80476e4SDavid E. O'Brien 	    if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
60445e5710bSMark Peek 		xclose(pipeout[0]);
60545e5710bSMark Peek 		xclose(pipeout[1]);
606c80476e4SDavid E. O'Brien 	    }
607c80476e4SDavid E. O'Brien 	    if ((t->t_dflg & F_PIPEIN) != 0)
608c80476e4SDavid E. O'Brien 		break;
609c80476e4SDavid E. O'Brien #else /* !BACKPIPE */
610c80476e4SDavid E. O'Brien 	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
61145e5710bSMark Peek 		xclose(pipein[0]);
61245e5710bSMark Peek 		xclose(pipein[1]);
613c80476e4SDavid E. O'Brien 	    }
614c80476e4SDavid E. O'Brien 	    if ((t->t_dflg & F_PIPEOUT) != 0)
615c80476e4SDavid E. O'Brien 		break;
616c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
617c80476e4SDavid E. O'Brien 
618c80476e4SDavid E. O'Brien 	    if (nosigchld) {
61945e5710bSMark Peek 		sigprocmask(SIG_SETMASK, &csigset, NULL);
620c80476e4SDavid E. O'Brien 		nosigchld = 0;
621c80476e4SDavid E. O'Brien 	    }
622c80476e4SDavid E. O'Brien 	    if ((t->t_dflg & F_AMPERSAND) == 0)
623c80476e4SDavid E. O'Brien 		pwait();
624c80476e4SDavid E. O'Brien 	    break;
625c80476e4SDavid E. O'Brien 	}
626c80476e4SDavid E. O'Brien 
627c80476e4SDavid E. O'Brien 	doio(t, pipein, pipeout);
628c80476e4SDavid E. O'Brien #ifdef BACKPIPE
629c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_PIPEIN) {
63045e5710bSMark Peek 	    xclose(pipein[0]);
63145e5710bSMark Peek 	    xclose(pipein[1]);
632c80476e4SDavid E. O'Brien 	}
633c80476e4SDavid E. O'Brien #else /* !BACKPIPE */
634c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_PIPEOUT) {
63545e5710bSMark Peek 	    xclose(pipeout[0]);
63645e5710bSMark Peek 	    xclose(pipeout[1]);
637c80476e4SDavid E. O'Brien 	}
638c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
639c80476e4SDavid E. O'Brien 	/*
640c80476e4SDavid E. O'Brien 	 * Perform a builtin function. If we are not forked, arrange for
641c80476e4SDavid E. O'Brien 	 * possible stopping
642c80476e4SDavid E. O'Brien 	 */
643c80476e4SDavid E. O'Brien 	if (bifunc) {
6449ccc37e3SMark Peek 	    if (forked) {
645c80476e4SDavid E. O'Brien 		func(t, bifunc);
646c80476e4SDavid E. O'Brien 		exitstat();
6479ccc37e3SMark Peek 	    } else {
6489ccc37e3SMark Peek 		jmp_buf_t oldexit;
6499ccc37e3SMark Peek 		int ohaderr = haderr;
6509ccc37e3SMark Peek 
6519ccc37e3SMark Peek 		getexit(oldexit);
6529ccc37e3SMark Peek 		if (setexit() == 0)
6539ccc37e3SMark Peek 		    func(t, bifunc);
6549ccc37e3SMark Peek 		resexit(oldexit);
6559ccc37e3SMark Peek 		haderr = ohaderr;
6569ccc37e3SMark Peek 
65723338178SMark Peek 		if (adrof(STRprintexitvalue)) {
65823338178SMark Peek 		    int rv = getn(varval(STRstatus));
65923338178SMark Peek 		    if (rv != 0)
66023338178SMark Peek 			xprintf(CGETS(17, 2, "Exit %d\n"), rv);
66123338178SMark Peek 		}
66223338178SMark Peek 	    }
663c80476e4SDavid E. O'Brien 	    break;
664c80476e4SDavid E. O'Brien 	}
665c80476e4SDavid E. O'Brien 	if (t->t_dtyp != NODE_PAREN) {
66629301572SMark Peek 	    doexec(t, do_glob);
667c80476e4SDavid E. O'Brien 	    /* NOTREACHED */
668c80476e4SDavid E. O'Brien 	}
669c80476e4SDavid E. O'Brien 	/*
670c80476e4SDavid E. O'Brien 	 * For () commands must put new 0,1,2 in FSH* and recurse
671c80476e4SDavid E. O'Brien 	 */
6729ccc37e3SMark Peek 	if ((OLDSTD = dcopy(0, FOLDSTD)) >= 0)
6739ccc37e3SMark Peek 	    (void)close_on_exec(OLDSTD, 1);
6749ccc37e3SMark Peek 	if ((SHOUT = dcopy(1, FSHOUT)) >= 0) {
6759ccc37e3SMark Peek 	    (void)close_on_exec(SHOUT, 1);
676c80476e4SDavid E. O'Brien 	    isoutatty = isatty(SHOUT);
6779ccc37e3SMark Peek 	}
6789ccc37e3SMark Peek 	if ((SHDIAG = dcopy(2, FSHDIAG)) >= 0) {
6799ccc37e3SMark Peek 	    (void)close_on_exec(SHDIAG, 1);
680c80476e4SDavid E. O'Brien 	    isdiagatty = isatty(SHDIAG);
6819ccc37e3SMark Peek     	}
68245e5710bSMark Peek 	xclose(SHIN);
683c80476e4SDavid E. O'Brien 	SHIN = -1;
684c80476e4SDavid E. O'Brien #ifndef CLOSE_ON_EXEC
685c80476e4SDavid E. O'Brien 	didcch = 0;
68623338178SMark Peek #else
68723338178SMark Peek 	(void) close_on_exec(FSHOUT, 1);
68823338178SMark Peek 	(void) close_on_exec(FSHDIAG, 1);
68923338178SMark Peek 	(void) close_on_exec(FOLDSTD, 1);
690c80476e4SDavid E. O'Brien #endif /* !CLOSE_ON_EXEC */
691c80476e4SDavid E. O'Brien 	didfds = 0;
692c80476e4SDavid E. O'Brien 	wanttty = -1;
6939ccc37e3SMark Peek 	t->t_dspr->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
69429301572SMark Peek 	execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
695c80476e4SDavid E. O'Brien 	exitstat();
696c80476e4SDavid E. O'Brien 
697c80476e4SDavid E. O'Brien     case NODE_PIPE:
698c80476e4SDavid E. O'Brien #ifdef BACKPIPE
699c80476e4SDavid E. O'Brien 	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
7009ccc37e3SMark Peek 	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
70129301572SMark Peek 	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
7029ccc37e3SMark Peek 	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
7039ccc37e3SMark Peek 	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
70429301572SMark Peek 	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
705c80476e4SDavid E. O'Brien #else /* !BACKPIPE */
7069ccc37e3SMark Peek 	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
7079ccc37e3SMark Peek 	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
70829301572SMark Peek 	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
709c80476e4SDavid E. O'Brien 	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
7109ccc37e3SMark Peek 	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
71129301572SMark Peek 	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
712c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
713c80476e4SDavid E. O'Brien 	break;
714c80476e4SDavid E. O'Brien 
715c80476e4SDavid E. O'Brien     case NODE_LIST:
716c80476e4SDavid E. O'Brien 	if (t->t_dcar) {
7179ccc37e3SMark Peek 	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
71829301572SMark Peek 	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
719c80476e4SDavid E. O'Brien 	    /*
720c80476e4SDavid E. O'Brien 	     * In strange case of A&B make a new job after A
721c80476e4SDavid E. O'Brien 	     */
722c80476e4SDavid E. O'Brien 	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
723c80476e4SDavid E. O'Brien 		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
724c80476e4SDavid E. O'Brien 		pendjob();
725c80476e4SDavid E. O'Brien 	}
726c80476e4SDavid E. O'Brien 	if (t->t_dcdr) {
727c80476e4SDavid E. O'Brien 	    t->t_dcdr->t_dflg |= t->t_dflg &
7289ccc37e3SMark Peek 		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
72929301572SMark Peek 	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
730c80476e4SDavid E. O'Brien 	}
731c80476e4SDavid E. O'Brien 	break;
732c80476e4SDavid E. O'Brien 
733c80476e4SDavid E. O'Brien     case NODE_OR:
734c80476e4SDavid E. O'Brien     case NODE_AND:
735c80476e4SDavid E. O'Brien 	if (t->t_dcar) {
7369ccc37e3SMark Peek 	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
73729301572SMark Peek 	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
738c80476e4SDavid E. O'Brien 	    if ((getn(varval(STRstatus)) == 0) !=
739c80476e4SDavid E. O'Brien 		(t->t_dtyp == NODE_AND)) {
740c80476e4SDavid E. O'Brien 		return;
741c80476e4SDavid E. O'Brien 	    }
742c80476e4SDavid E. O'Brien 	}
743c80476e4SDavid E. O'Brien 	if (t->t_dcdr) {
744c80476e4SDavid E. O'Brien 	    t->t_dcdr->t_dflg |= t->t_dflg &
7459ccc37e3SMark Peek 		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
74629301572SMark Peek 	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
747c80476e4SDavid E. O'Brien 	}
748c80476e4SDavid E. O'Brien 	break;
749c80476e4SDavid E. O'Brien 
750c80476e4SDavid E. O'Brien     default:
751c80476e4SDavid E. O'Brien 	break;
752c80476e4SDavid E. O'Brien     }
753c80476e4SDavid E. O'Brien     /*
754c80476e4SDavid E. O'Brien      * Fall through for all breaks from switch
755c80476e4SDavid E. O'Brien      *
756c80476e4SDavid E. O'Brien      * If there will be no more executions of this command, flush all file
757c80476e4SDavid E. O'Brien      * descriptors. Places that turn on the F_REPEAT bit are responsible for
758c80476e4SDavid E. O'Brien      * doing donefds after the last re-execution
759c80476e4SDavid E. O'Brien      */
760c80476e4SDavid E. O'Brien     if (didfds && !(t->t_dflg & F_REPEAT))
761c80476e4SDavid E. O'Brien 	donefds();
762c80476e4SDavid E. O'Brien }
763c80476e4SDavid E. O'Brien 
764c80476e4SDavid E. O'Brien #ifdef VFORK
76545e5710bSMark Peek static void
766c80476e4SDavid E. O'Brien /*ARGSUSED*/
vffree(int snum)76745e5710bSMark Peek vffree(int snum)
768c80476e4SDavid E. O'Brien {
769c80476e4SDavid E. O'Brien     USE(snum);
770c80476e4SDavid E. O'Brien 
771c80476e4SDavid E. O'Brien     _exit(1);
772c80476e4SDavid E. O'Brien }
773c80476e4SDavid E. O'Brien #endif /* VFORK */
774c80476e4SDavid E. O'Brien 
775c80476e4SDavid E. O'Brien /*
776c80476e4SDavid E. O'Brien  * Expand and glob the words after an i/o redirection.
777c80476e4SDavid E. O'Brien  * If more than one word is generated, then update the command vector.
778c80476e4SDavid E. O'Brien  *
779c80476e4SDavid E. O'Brien  * This is done differently in all the shells:
780c80476e4SDavid E. O'Brien  * 1. in the bourne shell and ksh globbing is not performed
781c80476e4SDavid E. O'Brien  * 2. Bash/csh say ambiguous
782c80476e4SDavid E. O'Brien  * 3. zsh does i/o to/from all the files
783c80476e4SDavid E. O'Brien  * 4. itcsh concatenates the words.
784c80476e4SDavid E. O'Brien  *
785c80476e4SDavid E. O'Brien  * I don't know what is best to do. I think that Ambiguous is better
786c80476e4SDavid E. O'Brien  * than restructuring the command vector, because the user can get
787c80476e4SDavid E. O'Brien  * unexpected results. In any case, the command vector restructuring
788c80476e4SDavid E. O'Brien  * code is present and the user can choose it by setting noambiguous
789c80476e4SDavid E. O'Brien  */
790c80476e4SDavid E. O'Brien static Char *
splicepipe(struct command * t,Char * cp)79145e5710bSMark Peek splicepipe(struct command *t, Char *cp)
792c80476e4SDavid E. O'Brien {
793c80476e4SDavid E. O'Brien     Char *blk[2];
794c80476e4SDavid E. O'Brien 
795c80476e4SDavid E. O'Brien     if (adrof(STRnoambiguous)) {
796c80476e4SDavid E. O'Brien 	Char **pv;
79745e5710bSMark Peek 	int gflag;
798c80476e4SDavid E. O'Brien 
799c80476e4SDavid E. O'Brien 	blk[0] = Dfix1(cp); /* expand $ */
800c80476e4SDavid E. O'Brien 	blk[1] = NULL;
801c80476e4SDavid E. O'Brien 
80245e5710bSMark Peek 	gflag = tglob(blk);
803c80476e4SDavid E. O'Brien 	if (gflag) {
80445e5710bSMark Peek 	    pv = globall(blk, gflag);
805c80476e4SDavid E. O'Brien 	    if (pv == NULL) {
806c80476e4SDavid E. O'Brien 		setname(short2str(blk[0]));
80745e5710bSMark Peek 		xfree(blk[0]);
808c80476e4SDavid E. O'Brien 		stderror(ERR_NAME | ERR_NOMATCH);
809c80476e4SDavid E. O'Brien 	    }
810c80476e4SDavid E. O'Brien 	    if (pv[1] != NULL) { /* we need to fix the command vector */
811c80476e4SDavid E. O'Brien 		Char **av = blkspl(t->t_dcom, &pv[1]);
81245e5710bSMark Peek 		xfree(t->t_dcom);
813c80476e4SDavid E. O'Brien 		t->t_dcom = av;
814c80476e4SDavid E. O'Brien 	    }
81545e5710bSMark Peek 	    xfree(blk[0]);
816c80476e4SDavid E. O'Brien 	    blk[0] = pv[0];
81745e5710bSMark Peek 	    xfree(pv);
818c80476e4SDavid E. O'Brien 	}
819c80476e4SDavid E. O'Brien     }
820c80476e4SDavid E. O'Brien     else {
82145e5710bSMark Peek 	Char *buf;
822c80476e4SDavid E. O'Brien 
82345e5710bSMark Peek 	buf = Dfix1(cp);
82445e5710bSMark Peek 	cleanup_push(buf, xfree);
825c80476e4SDavid E. O'Brien 	blk[0] = globone(buf, G_ERROR);
82645e5710bSMark Peek 	cleanup_until(buf);
827c80476e4SDavid E. O'Brien     }
828c80476e4SDavid E. O'Brien     return(blk[0]);
829c80476e4SDavid E. O'Brien }
830c80476e4SDavid E. O'Brien 
831c80476e4SDavid E. O'Brien /*
832c80476e4SDavid E. O'Brien  * Perform io redirection.
833c80476e4SDavid E. O'Brien  * We may or maynot be forked here.
834c80476e4SDavid E. O'Brien  */
835c80476e4SDavid E. O'Brien static void
doio(struct command * t,int * pipein,int * pipeout)83645e5710bSMark Peek doio(struct command *t, int *pipein, int *pipeout)
837c80476e4SDavid E. O'Brien {
83823338178SMark Peek     int fd;
83923338178SMark Peek     Char *cp;
84023338178SMark Peek     unsigned long flags = t->t_dflg;
841c80476e4SDavid E. O'Brien 
842c80476e4SDavid E. O'Brien     if (didfds || (flags & F_REPEAT))
843c80476e4SDavid E. O'Brien 	return;
844c80476e4SDavid E. O'Brien     if ((flags & F_READ) == 0) {/* F_READ already done */
845c80476e4SDavid E. O'Brien 	if (t->t_dlef) {
84645e5710bSMark Peek 	    char *tmp;
847c80476e4SDavid E. O'Brien 
848c80476e4SDavid E. O'Brien 	    /*
849c80476e4SDavid E. O'Brien 	     * so < /dev/std{in,out,err} work
850c80476e4SDavid E. O'Brien 	     */
851c80476e4SDavid E. O'Brien 	    (void) dcopy(SHIN, 0);
852c80476e4SDavid E. O'Brien 	    (void) dcopy(SHOUT, 1);
853c80476e4SDavid E. O'Brien 	    (void) dcopy(SHDIAG, 2);
854c80476e4SDavid E. O'Brien 	    cp = splicepipe(t, t->t_dlef);
85545e5710bSMark Peek 	    tmp = strsave(short2str(cp));
85645e5710bSMark Peek 	    xfree(cp);
85745e5710bSMark Peek 	    cleanup_push(tmp, xfree);
85845e5710bSMark Peek 	    if ((fd = xopen(tmp, O_RDONLY|O_LARGEFILE)) < 0)
859c80476e4SDavid E. O'Brien 		stderror(ERR_SYSTEM, tmp, strerror(errno));
86045e5710bSMark Peek 	    cleanup_until(tmp);
861c80476e4SDavid E. O'Brien 	    /* allow input files larger than 2Gb  */
862b2d5d167SMark Peek #ifndef WINNT_NATIVE
86323338178SMark Peek 	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
864b2d5d167SMark Peek #endif /*!WINNT_NATIVE*/
865c80476e4SDavid E. O'Brien 	    (void) dmove(fd, 0);
866c80476e4SDavid E. O'Brien 	}
867c80476e4SDavid E. O'Brien 	else if (flags & F_PIPEIN) {
86845e5710bSMark Peek 	    xclose(0);
8699ccc37e3SMark Peek 	    TCSH_IGNORE(dup(pipein[0]));
87045e5710bSMark Peek 	    xclose(pipein[0]);
87145e5710bSMark Peek 	    xclose(pipein[1]);
872c80476e4SDavid E. O'Brien 	}
873c80476e4SDavid E. O'Brien 	else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
87445e5710bSMark Peek 	    xclose(0);
87545e5710bSMark Peek 	    (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
876c80476e4SDavid E. O'Brien 	}
877c80476e4SDavid E. O'Brien 	else {
87845e5710bSMark Peek 	    xclose(0);
8799ccc37e3SMark Peek 	    TCSH_IGNORE(dup(OLDSTD));
880c80476e4SDavid E. O'Brien #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
881c80476e4SDavid E. O'Brien 	    /*
882c80476e4SDavid E. O'Brien 	     * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
883c80476e4SDavid E. O'Brien 	     * across dup()s, so we have to UNSET it here or else we get a
884c80476e4SDavid E. O'Brien 	     * command with NO stdin, stdout, or stderr at all (a bad thing
885c80476e4SDavid E. O'Brien 	     * indeed)
886c80476e4SDavid E. O'Brien 	     */
887c80476e4SDavid E. O'Brien 	    (void) close_on_exec(0, 0);
888c80476e4SDavid E. O'Brien #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
889c80476e4SDavid E. O'Brien 	}
890c80476e4SDavid E. O'Brien     }
891c80476e4SDavid E. O'Brien     if (t->t_drit) {
89245e5710bSMark Peek 	char *tmp;
893c80476e4SDavid E. O'Brien 
894c80476e4SDavid E. O'Brien 	cp = splicepipe(t, t->t_drit);
89545e5710bSMark Peek 	tmp = strsave(short2str(cp));
89645e5710bSMark Peek 	xfree(cp);
89745e5710bSMark Peek 	cleanup_push(tmp, xfree);
898c80476e4SDavid E. O'Brien 	/*
899c80476e4SDavid E. O'Brien 	 * so > /dev/std{out,err} work
900c80476e4SDavid E. O'Brien 	 */
901c80476e4SDavid E. O'Brien 	(void) dcopy(SHOUT, 1);
902c80476e4SDavid E. O'Brien 	(void) dcopy(SHDIAG, 2);
903c80476e4SDavid E. O'Brien 	if ((flags & F_APPEND) != 0) {
904c80476e4SDavid E. O'Brien #ifdef O_APPEND
90545e5710bSMark Peek 	    fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
906c80476e4SDavid E. O'Brien #else /* !O_APPEND */
90745e5710bSMark Peek 	    fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
908c80476e4SDavid E. O'Brien 	    (void) lseek(fd, (off_t) 0, L_XTND);
909c80476e4SDavid E. O'Brien #endif /* O_APPEND */
910c80476e4SDavid E. O'Brien 	}
911c80476e4SDavid E. O'Brien 	else
912c80476e4SDavid E. O'Brien 	    fd = 0;
913c80476e4SDavid E. O'Brien 	if ((flags & F_APPEND) == 0 || fd == -1) {
914*19d2e3deSDmitry Chagin 	    if (!(flags & F_OVERWRITE) && no_clobber) {
915c80476e4SDavid E. O'Brien 		if (flags & F_APPEND)
916c80476e4SDavid E. O'Brien 		    stderror(ERR_SYSTEM, tmp, strerror(errno));
917c80476e4SDavid E. O'Brien 		chkclob(tmp);
918c80476e4SDavid E. O'Brien 	    }
91945e5710bSMark Peek 	    if ((fd = xcreat(tmp, 0666)) < 0)
920c80476e4SDavid E. O'Brien 		stderror(ERR_SYSTEM, tmp, strerror(errno));
921c80476e4SDavid E. O'Brien 	    /* allow input files larger than 2Gb  */
922b2d5d167SMark Peek #ifndef WINNT_NATIVE
92323338178SMark Peek 	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
924b2d5d167SMark Peek #endif /*!WINNT_NATIVE*/
925c80476e4SDavid E. O'Brien 	}
92645e5710bSMark Peek 	cleanup_until(tmp);
927c80476e4SDavid E. O'Brien 	(void) dmove(fd, 1);
928c80476e4SDavid E. O'Brien 	is1atty = isatty(1);
929c80476e4SDavid E. O'Brien     }
930c80476e4SDavid E. O'Brien     else if (flags & F_PIPEOUT) {
93145e5710bSMark Peek 	xclose(1);
9329ccc37e3SMark Peek 	TCSH_IGNORE(dup(pipeout[1]));
933c80476e4SDavid E. O'Brien 	is1atty = 0;
934c80476e4SDavid E. O'Brien     }
935c80476e4SDavid E. O'Brien     else {
93645e5710bSMark Peek 	xclose(1);
9379ccc37e3SMark Peek 	TCSH_IGNORE(dup(SHOUT));
938c80476e4SDavid E. O'Brien 	is1atty = isoutatty;
939c80476e4SDavid E. O'Brien # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
940c80476e4SDavid E. O'Brien 	(void) close_on_exec(1, 0);
941c80476e4SDavid E. O'Brien # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
942c80476e4SDavid E. O'Brien     }
943c80476e4SDavid E. O'Brien 
94445e5710bSMark Peek     xclose(2);
945c80476e4SDavid E. O'Brien     if (flags & F_STDERR) {
9469ccc37e3SMark Peek 	TCSH_IGNORE(dup(1));
947c80476e4SDavid E. O'Brien 	is2atty = is1atty;
948c80476e4SDavid E. O'Brien     }
949c80476e4SDavid E. O'Brien     else {
9509ccc37e3SMark Peek 	TCSH_IGNORE(dup(SHDIAG));
951c80476e4SDavid E. O'Brien 	is2atty = isdiagatty;
952c80476e4SDavid E. O'Brien # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
953c80476e4SDavid E. O'Brien 	(void) close_on_exec(2, 0);
954c80476e4SDavid E. O'Brien # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
955c80476e4SDavid E. O'Brien     }
956c80476e4SDavid E. O'Brien     didfds = 1;
957c80476e4SDavid E. O'Brien }
958c80476e4SDavid E. O'Brien 
959c80476e4SDavid E. O'Brien void
mypipe(int * pv)96045e5710bSMark Peek mypipe(int *pv)
961c80476e4SDavid E. O'Brien {
962c80476e4SDavid E. O'Brien 
963c80476e4SDavid E. O'Brien     if (pipe(pv) < 0)
964c80476e4SDavid E. O'Brien 	goto oops;
96523338178SMark Peek     (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
96623338178SMark Peek     (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
967c80476e4SDavid E. O'Brien     if (pv[0] >= 0 && pv[1] >= 0)
968c80476e4SDavid E. O'Brien 	return;
96945e5710bSMark Peek     if (pv[0] >= 0)
97045e5710bSMark Peek 	xclose(pv[0]);
97145e5710bSMark Peek     if (pv[1] >= 0)
97245e5710bSMark Peek 	xclose(pv[1]);
973c80476e4SDavid E. O'Brien oops:
974c80476e4SDavid E. O'Brien     stderror(ERR_PIPE);
975c80476e4SDavid E. O'Brien }
976c80476e4SDavid E. O'Brien 
977c80476e4SDavid E. O'Brien static void
chkclob(const char * cp)97845e5710bSMark Peek chkclob(const char *cp)
979c80476e4SDavid E. O'Brien {
980c80476e4SDavid E. O'Brien     struct stat stb;
981c80476e4SDavid E. O'Brien 
982c80476e4SDavid E. O'Brien     if (stat(cp, &stb) < 0)
983c80476e4SDavid E. O'Brien 	return;
984c80476e4SDavid E. O'Brien     if (S_ISCHR(stb.st_mode))
985c80476e4SDavid E. O'Brien 	return;
986*19d2e3deSDmitry Chagin     if (no_clobber & NOCLOBBER_NOTEMPTY && stb.st_size == 0)
987*19d2e3deSDmitry Chagin 	return;
988*19d2e3deSDmitry Chagin     if (no_clobber & NOCLOBBER_ASK) {
989*19d2e3deSDmitry Chagin 	if (getYN(CGETS(22, 15,
990*19d2e3deSDmitry Chagin 	    "Do you really want to overwrite an existing file? [N/y] ")))
991*19d2e3deSDmitry Chagin 	    return;
992*19d2e3deSDmitry Chagin     }
993*19d2e3deSDmitry Chagin 
994c80476e4SDavid E. O'Brien     stderror(ERR_EXISTS, cp);
995c80476e4SDavid E. O'Brien }
996