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