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