Home | History | Annotate | Line # | Download | only in csh
sem.c revision 1.2
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1980, 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)sem.c	5.17 (Berkeley) 6/17/91";
     36  1.2  cgd static char rcsid[] = "$Id: sem.c,v 1.2 1993/03/22 08:04:00 cgd Exp $";
     37  1.1  cgd #endif /* not lint */
     38  1.1  cgd 
     39  1.1  cgd #include <sys/param.h>
     40  1.1  cgd #include <sys/ioctl.h>
     41  1.1  cgd #include <sys/stat.h>
     42  1.1  cgd #include <errno.h>
     43  1.1  cgd #include <fcntl.h>
     44  1.1  cgd #include <stdlib.h>
     45  1.1  cgd #include <string.h>
     46  1.1  cgd #include <unistd.h>
     47  1.1  cgd #if __STDC__
     48  1.1  cgd # include <stdarg.h>
     49  1.1  cgd #else
     50  1.1  cgd # include <varargs.h>
     51  1.1  cgd #endif
     52  1.1  cgd 
     53  1.1  cgd #include "csh.h"
     54  1.1  cgd #include "proc.h"
     55  1.1  cgd #include "extern.h"
     56  1.1  cgd 
     57  1.1  cgd static void	vffree __P((int));
     58  1.1  cgd static void	doio __P((struct command *t, int *, int *));
     59  1.1  cgd static void	chkclob __P((char *));
     60  1.1  cgd 
     61  1.1  cgd void
     62  1.1  cgd execute(t, wanttty, pipein, pipeout)
     63  1.1  cgd     register struct command *t;
     64  1.1  cgd     int     wanttty, *pipein, *pipeout;
     65  1.1  cgd {
     66  1.1  cgd     bool    forked = 0;
     67  1.1  cgd     struct biltins *bifunc;
     68  1.1  cgd     int     pid = 0;
     69  1.1  cgd     int     pv[2];
     70  1.1  cgd 
     71  1.1  cgd     static sigset_t csigmask;
     72  1.1  cgd 
     73  1.1  cgd     static sigset_t ocsigmask;
     74  1.1  cgd     static int onosigchld = 0;
     75  1.1  cgd     static int nosigchld = 0;
     76  1.1  cgd 
     77  1.1  cgd     if (t == 0)
     78  1.1  cgd 	return;
     79  1.1  cgd 
     80  1.1  cgd     if (t->t_dflg & F_AMPERSAND)
     81  1.1  cgd 	wanttty = 0;
     82  1.1  cgd     switch (t->t_dtyp) {
     83  1.1  cgd 
     84  1.1  cgd     case NODE_COMMAND:
     85  1.1  cgd 	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
     86  1.1  cgd 	    (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
     87  1.1  cgd 	if ((t->t_dflg & F_REPEAT) == 0)
     88  1.1  cgd 	    Dfix(t);		/* $ " ' \ */
     89  1.1  cgd 	if (t->t_dcom[0] == 0)
     90  1.1  cgd 	    return;
     91  1.1  cgd 	/* fall into... */
     92  1.1  cgd 
     93  1.1  cgd     case NODE_PAREN:
     94  1.1  cgd 	if (t->t_dflg & F_PIPEOUT)
     95  1.1  cgd 	    mypipe(pipeout);
     96  1.1  cgd 	/*
     97  1.1  cgd 	 * Must do << early so parent will know where input pointer should be.
     98  1.1  cgd 	 * If noexec then this is all we do.
     99  1.1  cgd 	 */
    100  1.1  cgd 	if (t->t_dflg & F_READ) {
    101  1.1  cgd 	    (void) close(0);
    102  1.1  cgd 	    heredoc(t->t_dlef);
    103  1.1  cgd 	    if (noexec)
    104  1.1  cgd 		(void) close(0);
    105  1.1  cgd 	}
    106  1.1  cgd 	if (noexec)
    107  1.1  cgd 	    break;
    108  1.1  cgd 
    109  1.1  cgd 	set(STRstatus, Strsave(STR0));
    110  1.1  cgd 
    111  1.1  cgd 	/*
    112  1.1  cgd 	 * This mess is the necessary kludge to handle the prefix builtins:
    113  1.1  cgd 	 * nice, nohup, time.  These commands can also be used by themselves,
    114  1.1  cgd 	 * and this is not handled here. This will also work when loops are
    115  1.1  cgd 	 * parsed.
    116  1.1  cgd 	 */
    117  1.1  cgd 	while (t->t_dtyp == NODE_COMMAND)
    118  1.1  cgd 	    if (eq(t->t_dcom[0], STRnice))
    119  1.1  cgd 		if (t->t_dcom[1])
    120  1.1  cgd 		    if (strchr("+-", t->t_dcom[1][0]))
    121  1.1  cgd 			if (t->t_dcom[2]) {
    122  1.1  cgd 			    setname("nice");
    123  1.1  cgd 			    t->t_nice =
    124  1.1  cgd 				getn(t->t_dcom[1]);
    125  1.1  cgd 			    lshift(t->t_dcom, 2);
    126  1.1  cgd 			    t->t_dflg |= F_NICE;
    127  1.1  cgd 			}
    128  1.1  cgd 			else
    129  1.1  cgd 			    break;
    130  1.1  cgd 		    else {
    131  1.1  cgd 			t->t_nice = 4;
    132  1.1  cgd 			lshift(t->t_dcom, 1);
    133  1.1  cgd 			t->t_dflg |= F_NICE;
    134  1.1  cgd 		    }
    135  1.1  cgd 		else
    136  1.1  cgd 		    break;
    137  1.1  cgd 	    else if (eq(t->t_dcom[0], STRnohup))
    138  1.1  cgd 		if (t->t_dcom[1]) {
    139  1.1  cgd 		    t->t_dflg |= F_NOHUP;
    140  1.1  cgd 		    lshift(t->t_dcom, 1);
    141  1.1  cgd 		}
    142  1.1  cgd 		else
    143  1.1  cgd 		    break;
    144  1.1  cgd 	    else if (eq(t->t_dcom[0], STRtime))
    145  1.1  cgd 		if (t->t_dcom[1]) {
    146  1.1  cgd 		    t->t_dflg |= F_TIME;
    147  1.1  cgd 		    lshift(t->t_dcom, 1);
    148  1.1  cgd 		}
    149  1.1  cgd 		else
    150  1.1  cgd 		    break;
    151  1.1  cgd 	    else
    152  1.1  cgd 		break;
    153  1.1  cgd 
    154  1.1  cgd 	/* is t a command */
    155  1.1  cgd 	if (t->t_dtyp == NODE_COMMAND) {
    156  1.1  cgd 	    /*
    157  1.1  cgd 	     * Check if we have a builtin function and remember which one.
    158  1.1  cgd 	     */
    159  1.1  cgd 	    bifunc = isbfunc(t);
    160  1.1  cgd 	}
    161  1.1  cgd 	else {			/* not a command */
    162  1.1  cgd 	    bifunc = NULL;
    163  1.1  cgd 	}
    164  1.1  cgd 
    165  1.1  cgd 	/*
    166  1.1  cgd 	 * We fork only if we are timed, or are not the end of a parenthesized
    167  1.1  cgd 	 * list and not a simple builtin function. Simple meaning one that is
    168  1.1  cgd 	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
    169  1.1  cgd 	 * fork in some of these cases.
    170  1.1  cgd 	 */
    171  1.1  cgd 	/*
    172  1.1  cgd 	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
    173  1.1  cgd 	 * shell not to change dir!
    174  1.1  cgd 	 */
    175  1.1  cgd 	if (bifunc && (bifunc->bfunct == dochngd ||
    176  1.1  cgd 		       bifunc->bfunct == dopushd ||
    177  1.1  cgd 		       bifunc->bfunct == dopopd))
    178  1.1  cgd 	    t->t_dflg &= ~(F_NICE);
    179  1.1  cgd 	if (((t->t_dflg & F_TIME) || (t->t_dflg & F_NOFORK) == 0 &&
    180  1.1  cgd 	     (!bifunc || t->t_dflg &
    181  1.1  cgd 	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP))) ||
    182  1.1  cgd 	/*
    183  1.1  cgd 	 * We have to fork for eval too.
    184  1.1  cgd 	 */
    185  1.1  cgd 	    (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
    186  1.1  cgd 	     bifunc->bfunct == doeval))
    187  1.1  cgd 	    if (t->t_dtyp == NODE_PAREN ||
    188  1.1  cgd 		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
    189  1.1  cgd 		forked++;
    190  1.1  cgd 		/*
    191  1.1  cgd 		 * We need to block SIGCHLD here, so that if the process does
    192  1.1  cgd 		 * not die before we can set the process group
    193  1.1  cgd 		 */
    194  1.1  cgd 		if (wanttty >= 0 && !nosigchld) {
    195  1.1  cgd 		    csigmask = sigblock(sigmask(SIGCHLD));
    196  1.1  cgd 		    nosigchld = 1;
    197  1.1  cgd 		}
    198  1.1  cgd 
    199  1.1  cgd 		pid = pfork(t, wanttty);
    200  1.1  cgd 		if (pid == 0 && nosigchld) {
    201  1.1  cgd 		    (void) sigsetmask(csigmask);
    202  1.1  cgd 		    nosigchld = 0;
    203  1.1  cgd 		}
    204  1.1  cgd 	    }
    205  1.1  cgd 	    else {
    206  1.1  cgd 		int     ochild, osetintr, ohaderr, odidfds;
    207  1.1  cgd 		int     oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
    208  1.1  cgd 		sigset_t omask;
    209  1.1  cgd 
    210  1.1  cgd 		/*
    211  1.1  cgd 		 * Prepare for the vfork by saving everything that the child
    212  1.1  cgd 		 * corrupts before it exec's. Note that in some signal
    213  1.1  cgd 		 * implementations which keep the signal info in user space
    214  1.1  cgd 		 * (e.g. Sun's) it will also be necessary to save and restore
    215  1.1  cgd 		 * the current sigvec's for the signals the child touches
    216  1.1  cgd 		 * before it exec's.
    217  1.1  cgd 		 */
    218  1.1  cgd 		if (wanttty >= 0 && !nosigchld && !noexec) {
    219  1.1  cgd 		    csigmask = sigblock(sigmask(SIGCHLD));
    220  1.1  cgd 		    nosigchld = 1;
    221  1.1  cgd 		}
    222  1.1  cgd 		omask = sigblock(sigmask(SIGCHLD) | sigmask(SIGINT));
    223  1.1  cgd 		ochild = child;
    224  1.1  cgd 		osetintr = setintr;
    225  1.1  cgd 		ohaderr = haderr;
    226  1.1  cgd 		odidfds = didfds;
    227  1.1  cgd 		oSHIN = SHIN;
    228  1.1  cgd 		oSHOUT = SHOUT;
    229  1.1  cgd 		oSHDIAG = SHDIAG;
    230  1.1  cgd 		oOLDSTD = OLDSTD;
    231  1.1  cgd 		otpgrp = tpgrp;
    232  1.1  cgd 		ocsigmask = csigmask;
    233  1.1  cgd 		onosigchld = nosigchld;
    234  1.1  cgd 		Vsav = Vdp = 0;
    235  1.1  cgd 		Vexpath = 0;
    236  1.1  cgd 		Vt = 0;
    237  1.1  cgd 		pid = vfork();
    238  1.1  cgd 
    239  1.1  cgd 		if (pid < 0) {
    240  1.1  cgd 		    (void) sigsetmask(omask);
    241  1.1  cgd 		    stderror(ERR_NOPROC);
    242  1.1  cgd 		}
    243  1.1  cgd 		forked++;
    244  1.1  cgd 		if (pid) {	/* parent */
    245  1.1  cgd 		    child = ochild;
    246  1.1  cgd 		    setintr = osetintr;
    247  1.1  cgd 		    haderr = ohaderr;
    248  1.1  cgd 		    didfds = odidfds;
    249  1.1  cgd 		    SHIN = oSHIN;
    250  1.1  cgd 		    SHOUT = oSHOUT;
    251  1.1  cgd 		    SHDIAG = oSHDIAG;
    252  1.1  cgd 		    OLDSTD = oOLDSTD;
    253  1.1  cgd 		    tpgrp = otpgrp;
    254  1.1  cgd 		    csigmask = ocsigmask;
    255  1.1  cgd 		    nosigchld = onosigchld;
    256  1.1  cgd 
    257  1.1  cgd 		    xfree((ptr_t) Vsav);
    258  1.1  cgd 		    Vsav = 0;
    259  1.1  cgd 		    xfree((ptr_t) Vdp);
    260  1.1  cgd 		    Vdp = 0;
    261  1.1  cgd 		    xfree((ptr_t) Vexpath);
    262  1.1  cgd 		    Vexpath = 0;
    263  1.1  cgd 		    blkfree((Char **) Vt);
    264  1.1  cgd 		    Vt = 0;
    265  1.1  cgd 		    /* this is from pfork() */
    266  1.1  cgd 		    palloc(pid, t);
    267  1.1  cgd 		    (void) sigsetmask(omask);
    268  1.1  cgd 		}
    269  1.1  cgd 		else {		/* child */
    270  1.1  cgd 		    /* this is from pfork() */
    271  1.1  cgd 		    int     pgrp;
    272  1.1  cgd 		    bool    ignint = 0;
    273  1.1  cgd 
    274  1.1  cgd 		    if (nosigchld) {
    275  1.1  cgd 			(void) sigsetmask(csigmask);
    276  1.1  cgd 			nosigchld = 0;
    277  1.1  cgd 		    }
    278  1.1  cgd 
    279  1.1  cgd 		    if (setintr)
    280  1.1  cgd 			ignint =
    281  1.1  cgd 			    (tpgrp == -1 &&
    282  1.1  cgd 			     (t->t_dflg & F_NOINTERRUPT))
    283  1.1  cgd 			    || gointr && eq(gointr, STRminus);
    284  1.1  cgd 		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
    285  1.1  cgd 		    child++;
    286  1.1  cgd 		    if (setintr) {
    287  1.1  cgd 			setintr = 0;
    288  1.1  cgd 			if (ignint) {
    289  1.1  cgd 			    (void) signal(SIGINT, SIG_IGN);
    290  1.1  cgd 			    (void) signal(SIGQUIT, SIG_IGN);
    291  1.1  cgd 			}
    292  1.1  cgd 			else {
    293  1.1  cgd 			    (void) signal(SIGINT, vffree);
    294  1.1  cgd 			    (void) signal(SIGQUIT, SIG_DFL);
    295  1.1  cgd 			}
    296  1.1  cgd 
    297  1.1  cgd 			if (wanttty >= 0) {
    298  1.1  cgd 			    (void) signal(SIGTSTP, SIG_DFL);
    299  1.1  cgd 			    (void) signal(SIGTTIN, SIG_DFL);
    300  1.1  cgd 			    (void) signal(SIGTTOU, SIG_DFL);
    301  1.1  cgd 			}
    302  1.1  cgd 
    303  1.1  cgd 			(void) signal(SIGTERM, parterm);
    304  1.1  cgd 		    }
    305  1.1  cgd 		    else if (tpgrp == -1 &&
    306  1.1  cgd 			     (t->t_dflg & F_NOINTERRUPT)) {
    307  1.1  cgd 			(void) signal(SIGINT, SIG_IGN);
    308  1.1  cgd 			(void) signal(SIGQUIT, SIG_IGN);
    309  1.1  cgd 		    }
    310  1.1  cgd 
    311  1.1  cgd 		    pgetty(wanttty, pgrp);
    312  1.1  cgd 		    if (t->t_dflg & F_NOHUP)
    313  1.1  cgd 			(void) signal(SIGHUP, SIG_IGN);
    314  1.1  cgd 		    if (t->t_dflg & F_NICE)
    315  1.1  cgd 			(void) setpriority(PRIO_PROCESS, 0, t->t_nice);
    316  1.1  cgd 		}
    317  1.1  cgd 
    318  1.1  cgd 	    }
    319  1.1  cgd 	if (pid != 0) {
    320  1.1  cgd 	    /*
    321  1.1  cgd 	     * It would be better if we could wait for the whole job when we
    322  1.1  cgd 	     * knew the last process had been started.  Pwait, in fact, does
    323  1.1  cgd 	     * wait for the whole job anyway, but this test doesn't really
    324  1.1  cgd 	     * express our intentions.
    325  1.1  cgd 	     */
    326  1.1  cgd 	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
    327  1.1  cgd 		(void) close(pipein[0]);
    328  1.1  cgd 		(void) close(pipein[1]);
    329  1.1  cgd 	    }
    330  1.1  cgd 	    if ((t->t_dflg & F_PIPEOUT) == 0) {
    331  1.1  cgd 		if (nosigchld) {
    332  1.1  cgd 		    (void) sigsetmask(csigmask);
    333  1.1  cgd 		    nosigchld = 0;
    334  1.1  cgd 		}
    335  1.1  cgd 		if ((t->t_dflg & F_AMPERSAND) == 0)
    336  1.1  cgd 		    pwait();
    337  1.1  cgd 	    }
    338  1.1  cgd 	    break;
    339  1.1  cgd 	}
    340  1.1  cgd 	doio(t, pipein, pipeout);
    341  1.1  cgd 	if (t->t_dflg & F_PIPEOUT) {
    342  1.1  cgd 	    (void) close(pipeout[0]);
    343  1.1  cgd 	    (void) close(pipeout[1]);
    344  1.1  cgd 	}
    345  1.1  cgd 	/*
    346  1.1  cgd 	 * Perform a builtin function. If we are not forked, arrange for
    347  1.1  cgd 	 * possible stopping
    348  1.1  cgd 	 */
    349  1.1  cgd 	if (bifunc) {
    350  1.1  cgd 	    func(t, bifunc);
    351  1.1  cgd 	    if (forked)
    352  1.1  cgd 		exitstat();
    353  1.1  cgd 	    break;
    354  1.1  cgd 	}
    355  1.1  cgd 	if (t->t_dtyp != NODE_PAREN) {
    356  1.1  cgd 	    doexec(t);
    357  1.1  cgd 	    /* NOTREACHED */
    358  1.1  cgd 	}
    359  1.1  cgd 	/*
    360  1.1  cgd 	 * For () commands must put new 0,1,2 in FSH* and recurse
    361  1.1  cgd 	 */
    362  1.1  cgd 	OLDSTD = dcopy(0, FOLDSTD);
    363  1.1  cgd 	SHOUT = dcopy(1, FSHOUT);
    364  1.1  cgd 	SHDIAG = dcopy(2, FSHDIAG);
    365  1.1  cgd 	(void) close(SHIN);
    366  1.1  cgd 	SHIN = -1;
    367  1.1  cgd 	didfds = 0;
    368  1.1  cgd 	wanttty = -1;
    369  1.1  cgd 	t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
    370  1.1  cgd 	execute(t->t_dspr, wanttty, NULL, NULL);
    371  1.1  cgd 	exitstat();
    372  1.1  cgd 
    373  1.1  cgd     case NODE_PIPE:
    374  1.1  cgd 	t->t_dcar->t_dflg |= F_PIPEOUT |
    375  1.1  cgd 	    (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
    376  1.1  cgd 	execute(t->t_dcar, wanttty, pipein, pv);
    377  1.1  cgd 	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
    378  1.1  cgd 			(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
    379  1.1  cgd 	if (wanttty > 0)
    380  1.1  cgd 	    wanttty = 0;	/* got tty already */
    381  1.1  cgd 	execute(t->t_dcdr, wanttty, pv, pipeout);
    382  1.1  cgd 	break;
    383  1.1  cgd 
    384  1.1  cgd     case NODE_LIST:
    385  1.1  cgd 	if (t->t_dcar) {
    386  1.1  cgd 	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
    387  1.1  cgd 	    execute(t->t_dcar, wanttty, NULL, NULL);
    388  1.1  cgd 	    /*
    389  1.1  cgd 	     * In strange case of A&B make a new job after A
    390  1.1  cgd 	     */
    391  1.1  cgd 	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
    392  1.1  cgd 		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
    393  1.1  cgd 		pendjob();
    394  1.1  cgd 	}
    395  1.1  cgd 	if (t->t_dcdr) {
    396  1.1  cgd 	    t->t_dcdr->t_dflg |= t->t_dflg &
    397  1.1  cgd 		(F_NOFORK | F_NOINTERRUPT);
    398  1.1  cgd 	    execute(t->t_dcdr, wanttty, NULL, NULL);
    399  1.1  cgd 	}
    400  1.1  cgd 	break;
    401  1.1  cgd 
    402  1.1  cgd     case NODE_OR:
    403  1.1  cgd     case NODE_AND:
    404  1.1  cgd 	if (t->t_dcar) {
    405  1.1  cgd 	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
    406  1.1  cgd 	    execute(t->t_dcar, wanttty, NULL, NULL);
    407  1.1  cgd 	    if ((getn(value(STRstatus)) == 0) !=
    408  1.1  cgd 		(t->t_dtyp == NODE_AND))
    409  1.1  cgd 		return;
    410  1.1  cgd 	}
    411  1.1  cgd 	if (t->t_dcdr) {
    412  1.1  cgd 	    t->t_dcdr->t_dflg |= t->t_dflg &
    413  1.1  cgd 		(F_NOFORK | F_NOINTERRUPT);
    414  1.1  cgd 	    execute(t->t_dcdr, wanttty, NULL, NULL);
    415  1.1  cgd 	}
    416  1.1  cgd 	break;
    417  1.1  cgd     }
    418  1.1  cgd     /*
    419  1.1  cgd      * Fall through for all breaks from switch
    420  1.1  cgd      *
    421  1.1  cgd      * If there will be no more executions of this command, flush all file
    422  1.1  cgd      * descriptors. Places that turn on the F_REPEAT bit are responsible for
    423  1.1  cgd      * doing donefds after the last re-execution
    424  1.1  cgd      */
    425  1.1  cgd     if (didfds && !(t->t_dflg & F_REPEAT))
    426  1.1  cgd 	donefds();
    427  1.1  cgd }
    428  1.1  cgd 
    429  1.1  cgd static void
    430  1.1  cgd vffree(i)
    431  1.1  cgd int i;
    432  1.1  cgd {
    433  1.1  cgd     register Char **v;
    434  1.1  cgd 
    435  1.1  cgd     if (v = gargv) {
    436  1.1  cgd 	gargv = 0;
    437  1.1  cgd 	xfree((ptr_t) v);
    438  1.1  cgd     }
    439  1.1  cgd     if (v = pargv) {
    440  1.1  cgd 	pargv = 0;
    441  1.1  cgd 	xfree((ptr_t) v);
    442  1.1  cgd     }
    443  1.1  cgd     _exit(i);
    444  1.1  cgd }
    445  1.1  cgd 
    446  1.1  cgd /*
    447  1.1  cgd  * Perform io redirection.
    448  1.1  cgd  * We may or maynot be forked here.
    449  1.1  cgd  */
    450  1.1  cgd static void
    451  1.1  cgd doio(t, pipein, pipeout)
    452  1.1  cgd     register struct command *t;
    453  1.1  cgd     int    *pipein, *pipeout;
    454  1.1  cgd {
    455  1.1  cgd     register int fd;
    456  1.1  cgd     register Char *cp;
    457  1.1  cgd     register int flags = t->t_dflg;
    458  1.1  cgd 
    459  1.1  cgd     if (didfds || (flags & F_REPEAT))
    460  1.1  cgd 	return;
    461  1.1  cgd     if ((flags & F_READ) == 0) {/* F_READ already done */
    462  1.1  cgd 	if (cp = t->t_dlef) {
    463  1.1  cgd 	    char    tmp[MAXPATHLEN+1];
    464  1.1  cgd 
    465  1.1  cgd 	    /*
    466  1.1  cgd 	     * so < /dev/std{in,out,err} work
    467  1.1  cgd 	     */
    468  1.1  cgd 	    (void) dcopy(SHIN, 0);
    469  1.1  cgd 	    (void) dcopy(SHOUT, 1);
    470  1.1  cgd 	    (void) dcopy(SHDIAG, 2);
    471  1.1  cgd 	    cp = globone(Dfix1(cp), G_IGNORE);
    472  1.1  cgd 	    (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
    473  1.1  cgd 	    tmp[MAXPATHLEN] = '\0';
    474  1.1  cgd 	    xfree((ptr_t) cp);
    475  1.1  cgd 	    if ((fd = open(tmp, O_RDONLY)) < 0)
    476  1.1  cgd 		stderror(ERR_SYSTEM, tmp, strerror(errno));
    477  1.1  cgd 	    (void) dmove(fd, 0);
    478  1.1  cgd 	}
    479  1.1  cgd 	else if (flags & F_PIPEIN) {
    480  1.1  cgd 	    (void) close(0);
    481  1.1  cgd 	    (void) dup(pipein[0]);
    482  1.1  cgd 	    (void) close(pipein[0]);
    483  1.1  cgd 	    (void) close(pipein[1]);
    484  1.1  cgd 	}
    485  1.1  cgd 	else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
    486  1.1  cgd 	    (void) close(0);
    487  1.1  cgd 	    (void) open(_PATH_DEVNULL, O_RDONLY);
    488  1.1  cgd 	}
    489  1.1  cgd 	else {
    490  1.1  cgd 	    (void) close(0);
    491  1.1  cgd 	    (void) dup(OLDSTD);
    492  1.1  cgd 	    (void) ioctl(0, FIONCLEX, NULL);
    493  1.1  cgd 	}
    494  1.1  cgd     }
    495  1.1  cgd     if (cp = t->t_drit) {
    496  1.1  cgd 	char    tmp[MAXPATHLEN+1];
    497  1.1  cgd 
    498  1.1  cgd 	cp = globone(Dfix1(cp), G_IGNORE);
    499  1.1  cgd 	(void) strncpy(tmp, short2str(cp), MAXPATHLEN);
    500  1.1  cgd 	tmp[MAXPATHLEN] = '\0';
    501  1.1  cgd 	xfree((ptr_t) cp);
    502  1.1  cgd 	/*
    503  1.1  cgd 	 * so > /dev/std{out,err} work
    504  1.1  cgd 	 */
    505  1.1  cgd 	(void) dcopy(SHOUT, 1);
    506  1.1  cgd 	(void) dcopy(SHDIAG, 2);
    507  1.1  cgd 	if ((flags & F_APPEND) &&
    508  1.1  cgd #ifdef O_APPEND
    509  1.1  cgd 	    (fd = open(tmp, O_WRONLY | O_APPEND)) >= 0);
    510  1.1  cgd #else
    511  1.1  cgd 	    (fd = open(tmp, O_WRONLY)) >= 0)
    512  1.1  cgd 	    (void) lseek(1, (off_t) 0, L_XTND);
    513  1.1  cgd #endif
    514  1.1  cgd 	else {
    515  1.1  cgd 	    if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
    516  1.1  cgd 		if (flags & F_APPEND)
    517  1.1  cgd 		    stderror(ERR_SYSTEM, tmp, strerror(errno));
    518  1.1  cgd 		chkclob(tmp);
    519  1.1  cgd 	    }
    520  1.1  cgd 	    if ((fd = creat(tmp, 0666)) < 0)
    521  1.1  cgd 		stderror(ERR_SYSTEM, tmp, strerror(errno));
    522  1.1  cgd 	}
    523  1.1  cgd 	(void) dmove(fd, 1);
    524  1.1  cgd     }
    525  1.1  cgd     else if (flags & F_PIPEOUT) {
    526  1.1  cgd 	(void) close(1);
    527  1.1  cgd 	(void) dup(pipeout[1]);
    528  1.1  cgd     }
    529  1.1  cgd     else {
    530  1.1  cgd 	(void) close(1);
    531  1.1  cgd 	(void) dup(SHOUT);
    532  1.1  cgd 	(void) ioctl(1, FIONCLEX, NULL);
    533  1.1  cgd     }
    534  1.1  cgd 
    535  1.1  cgd     (void) close(2);
    536  1.1  cgd     if (flags & F_STDERR) {
    537  1.1  cgd 	(void) dup(1);
    538  1.1  cgd     }
    539  1.1  cgd     else {
    540  1.1  cgd 	(void) dup(SHDIAG);
    541  1.1  cgd 	(void) ioctl(2, FIONCLEX, NULL);
    542  1.1  cgd     }
    543  1.1  cgd     didfds = 1;
    544  1.1  cgd }
    545  1.1  cgd 
    546  1.1  cgd void
    547  1.1  cgd mypipe(pv)
    548  1.1  cgd     register int *pv;
    549  1.1  cgd {
    550  1.1  cgd 
    551  1.1  cgd     if (pipe(pv) < 0)
    552  1.1  cgd 	goto oops;
    553  1.1  cgd     pv[0] = dmove(pv[0], -1);
    554  1.1  cgd     pv[1] = dmove(pv[1], -1);
    555  1.1  cgd     if (pv[0] >= 0 && pv[1] >= 0)
    556  1.1  cgd 	return;
    557  1.1  cgd oops:
    558  1.1  cgd     stderror(ERR_PIPE);
    559  1.1  cgd }
    560  1.1  cgd 
    561  1.1  cgd static void
    562  1.1  cgd chkclob(cp)
    563  1.1  cgd     register char *cp;
    564  1.1  cgd {
    565  1.1  cgd     struct stat stb;
    566  1.1  cgd 
    567  1.1  cgd     if (stat(cp, &stb) < 0)
    568  1.1  cgd 	return;
    569  1.1  cgd     if ((stb.st_mode & S_IFMT) == S_IFCHR)
    570  1.1  cgd 	return;
    571  1.1  cgd     stderror(ERR_EXISTS, cp);
    572  1.1  cgd }
    573