1 /* $Header: /src/pub/tcsh/sh.sem.c,v 3.69 2005/01/18 20:24:51 christos Exp $ */
3 * sh.sem.c: I/O redirections and job forking. A touchy issue!
4 * Most stuff with builtins is incorrect
7 * Copyright (c) 1980, 1991 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: sh.sem.c,v 3.69 2005/01/18 20:24:51 christos Exp $")
42 #endif /*WINNT_NATIVE*/
48 # endif /* CLEX_DUPS */
50 #endif /* CLOSE_ON_EXEC */
52 #if defined(__sparc__) || defined(sparc)
53 # if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(linux) && !defined(__GNU__) && !defined(__GLIBC__)
55 # endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !glibc */
56 #endif /* __sparc__ || sparc */
59 static RETSIGTYPE vffree __P((int));
61 static Char *splicepipe __P((struct command *, Char *));
62 static void doio __P((struct command *, int *, int *));
63 static void chkclob __P((char *));
70 * For SVR4, there are problems with pipelines having the first process as
71 * the group leader. The problem occurs when the first process exits before
72 * the others have a chance to setpgid(). This is because in SVR4 you can't
73 * have a zombie as a group leader. The solution I have used is to reverse
74 * the order in which pipelines are started, making the last process the
75 * group leader. (Note I am not using 'pipeline' in the generic sense -- I
76 * mean processes connected by '|'.) I don't know yet if this causes other
79 * All the changes for this are in execute(), and are enclosed in
82 * David Dawes (dawes@physics.su.oz.au) Oct 1991
87 execute(t, wanttty, pipein, pipeout, do_glob)
90 int *pipein, *pipeout;
94 struct biltins *bifunc;
98 static sigmask_t csigmask;
101 static int onosigchld = 0;
103 static int nosigchld = 0;
114 if ((varval(STRNTslowexec) == STRNULL) &&
115 !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
116 (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
118 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
119 (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
121 if (nt_try_fast_exec(t) == 0)
125 #endif /* WINNT_NATIVE */
128 * Ed hutchins@sgi.com & Dominic dbg@sgi.com
129 * Sat Feb 25 03:13:11 PST 1995
130 * try implicit cd if we have a 1 word command
132 if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
133 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
134 Char sCName[MAXPATHLEN];
139 dollar(sCName, t->t_dcom[0]);
142 Char sCPath[MAXPATHLEN];
146 while (*pCN && *pCN != '/')
152 (void) Strcpy(sCPath, varval(STRhome));
153 catn(sCPath, pCN, MAXPATHLEN);
154 (void) Strcpy(sCName, sCPath);
157 pathname = short2str(sCName);
158 /* if this is a dir, tack a "cd" on as the first arg */
159 if ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
161 || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
162 #endif /* WINNT_NATIVE */
165 Char **ot_dcom = t->t_dcom;
167 vCD[0] = Strsave(STRcd);
169 t->t_dcom = blkspl(vCD, ot_dcom);
170 if (implicit_cd > 1) {
174 xfree((ptr_t) ot_dcom);
179 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
180 * Don't check for wantty > 0...
182 if (t->t_dflg & F_AMPERSAND)
187 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
188 (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
189 if ((t->t_dflg & F_REPEAT) == 0)
190 Dfix(t); /* $ " ' \ */
191 if (t->t_dcom[0] == 0) {
198 if (t->t_dflg & F_PIPEIN)
200 #else /* !BACKPIPE */
201 if (t->t_dflg & F_PIPEOUT)
203 #endif /* BACKPIPE */
205 * Must do << early so parent will know where input pointer should be.
206 * If noexec then this is all we do.
208 if (t->t_dflg & F_READ) {
215 set(STRstatus, Strsave(STR0), VAR_READWRITE);
218 * This mess is the necessary kludge to handle the prefix builtins:
219 * nice, nohup, time. These commands can also be used by themselves,
220 * and this is not handled here. This will also work when loops are
223 while (t->t_dtyp == NODE_COMMAND)
224 if (eq(t->t_dcom[0], STRnice)) {
226 if (strchr("+-", t->t_dcom[1][0])) {
231 lshift(t->t_dcom, 2);
239 lshift(t->t_dcom, 1);
246 else if (eq(t->t_dcom[0], STRnohup)) {
248 t->t_dflg |= F_NOHUP;
249 lshift(t->t_dcom, 1);
254 else if (eq(t->t_dcom[0], STRhup)) {
257 lshift(t->t_dcom, 1);
262 else if (eq(t->t_dcom[0], STRtime)) {
265 lshift(t->t_dcom, 1);
271 else if (eq(t->t_dcom[0], STRver))
272 if (t->t_dcom[1] && t->t_dcom[2]) {
274 t->t_systype = getv(t->t_dcom[1]);
275 lshift(t->t_dcom, 2);
284 /* is it a command */
285 if (t->t_dtyp == NODE_COMMAND) {
287 * Check if we have a builtin function and remember which one.
290 if (noexec && bifunc) {
292 * Continue for builtins that are part of the scripting language
294 if (bifunc->bfunct != (bfunc_t)dobreak &&
295 bifunc->bfunct != (bfunc_t)docontin &&
296 bifunc->bfunct != (bfunc_t)doelse &&
297 bifunc->bfunct != (bfunc_t)doend &&
298 bifunc->bfunct != (bfunc_t)doforeach&&
299 bifunc->bfunct != (bfunc_t)dogoto &&
300 bifunc->bfunct != (bfunc_t)doif &&
301 bifunc->bfunct != (bfunc_t)dorepeat &&
302 bifunc->bfunct != (bfunc_t)doswbrk &&
303 bifunc->bfunct != (bfunc_t)doswitch &&
304 bifunc->bfunct != (bfunc_t)dowhile &&
305 bifunc->bfunct != (bfunc_t)dozip)
309 else { /* not a command */
316 * GrP Executing a command - run jobcmd hook
317 * Don't run for builtins
318 * Don't run if we're not in a tty
319 * Don't run if we're not really executing
321 if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty) {
322 Char *cmd = unparse(t);
328 * We fork only if we are timed, or are not the end of a parenthesized
329 * list and not a simple builtin function. Simple meaning one that is
330 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
331 * fork in some of these cases.
334 * Prevent forking cd, pushd, popd, chdir cause this will cause the
335 * shell not to change dir!
339 * Can't have NOFORK for the tail of a pipe - because it is not the
340 * last command spawned (even if it is at the end of a parenthesised
343 if (t->t_dflg & F_PIPEIN)
344 t->t_dflg &= ~(F_NOFORK);
345 #endif /* BACKPIPE */
346 if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
347 bifunc->bfunct == (bfunc_t)dopushd ||
348 bifunc->bfunct == (bfunc_t)dopopd))
349 t->t_dflg &= ~(F_NICE);
350 if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
351 (!bifunc || t->t_dflg &
352 (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
354 * We have to fork for eval too.
356 (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
357 bifunc->bfunct == (bfunc_t)doeval)) {
359 if (t->t_dtyp == NODE_PAREN ||
360 t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
365 * We need to block SIGCHLD here, so that if the process does
366 * not die before we can set the process group
368 if (wanttty >= 0 && !nosigchld) {
370 csigmask = sigblock(sigmask(SIGCHLD));
372 (void) sighold(SIGCHLD);
378 pid = pfork(t, wanttty);
379 if (pid == 0 && nosigchld) {
381 (void) sigsetmask(csigmask);
383 (void) sigrelse(SIGCHLD);
387 else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
393 int ochild, osetintr, ohaderr, odidfds;
394 int oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
395 int oisoutatty, oisdiagatty;
397 # ifndef CLOSE_ON_EXEC
399 # endif /* !CLOSE_ON_EXEC */
401 sigmask_t omask, ocsigmask;
402 # endif /* BSDSIGS */
405 * Prepare for the vfork by saving everything that the child
406 * corrupts before it exec's. Note that in some signal
407 * implementations which keep the signal info in user space
408 * (e.g. Sun's) it will also be necessary to save and restore
409 * the current sigvec's for the signals the child touches
414 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
415 * Gilbrech - 11/22/87)
418 sigvec_t savesv[NSIGSAVED];
421 # endif /* SAVESIGVEC */
422 if (wanttty >= 0 && !nosigchld && !noexec) {
424 csigmask = sigblock(sigmask(SIGCHLD));
425 # else /* !BSDSIGS */
426 (void) sighold(SIGCHLD);
427 # endif /* BSDSIGS */
431 omask = sigblock(sigmask(SIGCHLD)|sigmask(SIGINT));
432 # else /* !BSDSIGS */
433 (void) sighold(SIGCHLD);
434 (void) sighold(SIGINT);
435 # endif /* BSDSIGS */
440 # ifndef CLOSE_ON_EXEC
442 # endif /* !CLOSE_ON_EXEC */
448 oisoutatty = isoutatty;
449 oisdiagatty = isdiagatty;
451 ocsigmask = csigmask;
452 # endif /* BSDSIGS */
453 onosigchld = nosigchld;
458 savesm = savesigvec(savesv);
459 # endif /* SAVESIGVEC */
468 restoresigvec(savesv, savesm);
469 # endif /* SAVESIGVEC */
470 (void) sigsetmask(omask);
471 # else /* !BSDSIGS */
472 (void) sigrelse(SIGCHLD);
473 (void) sigrelse(SIGINT);
474 # endif /* BSDSIGS */
475 stderror(ERR_NOPROC);
478 if (pid) { /* parent */
480 restoresigvec(savesv, savesm);
481 # endif /* SAVESIGVEC */
487 # ifndef CLOSE_ON_EXEC
489 # endif /* !CLOSE_ON_EXEC */
494 isoutatty = oisoutatty;
495 isdiagatty = oisdiagatty;
497 csigmask = ocsigmask;
498 # endif /* BSDSIGS */
499 nosigchld = onosigchld;
505 xfree((ptr_t) Vexpath);
507 blkfree((Char **) Vt);
509 /* this is from pfork() */
512 (void) sigsetmask(omask);
513 # else /* !BSDSIGS */
514 (void) sigrelse(SIGCHLD);
515 (void) sigrelse(SIGINT);
516 # endif /* BSDSIGS */
519 /* this is from pfork() */
524 (void) sigsetmask(csigmask);
525 # else /* !BSDSIGS */
526 (void) sigrelse(SIGCHLD);
527 # endif /* BSDSIGS */
532 ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
533 || (gointr && eq(gointr, STRminus));
534 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
539 * casts made right for SunOS 4.0 by Douglas C. Schmidt
540 * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
543 * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
547 (void) signal(SIGINT, SIG_IGN);
548 (void) signal(SIGQUIT, SIG_IGN);
551 (void) signal(SIGINT, vffree);
552 (void) signal(SIGQUIT, SIG_DFL);
556 (void) signal(SIGTSTP, SIG_DFL);
557 (void) signal(SIGTTIN, SIG_DFL);
558 (void) signal(SIGTTOU, SIG_DFL);
560 # endif /* BSDJOBS */
562 (void) signal(SIGTERM, parterm);
564 else if (tpgrp == -1 &&
565 (t->t_dflg & F_NOINTERRUPT)) {
566 (void) signal(SIGINT, SIG_IGN);
567 (void) signal(SIGQUIT, SIG_IGN);
570 pgetty(wanttty, pgrp);
572 if (t->t_dflg & F_NOHUP)
573 (void) signal(SIGHUP, SIG_IGN);
574 if (t->t_dflg & F_HUP)
575 (void) signal(SIGHUP, SIG_DFL);
576 if (t->t_dflg & F_NICE) {
577 int nval = SIGN_EXTEND_CHAR(t->t_nice);
578 # ifdef HAVE_SETPRIORITY
579 if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
580 stderror(ERR_SYSTEM, "setpriority",
582 # else /* !HAVE_SETPRIORITY */
584 # endif /* HAVE_SETPRIORITY */
587 if (t->t_dflg & F_VER) {
588 tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
599 * It would be better if we could wait for the whole job when we
600 * knew the last process had been started. Pwait, in fact, does
601 * wait for the whole job anyway, but this test doesn't really
602 * express our intentions.
605 if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
606 (void) close(pipeout[0]);
607 (void) close(pipeout[1]);
609 if ((t->t_dflg & F_PIPEIN) != 0)
611 #else /* !BACKPIPE */
612 if (didfds == 0 && t->t_dflg & F_PIPEIN) {
613 (void) close(pipein[0]);
614 (void) close(pipein[1]);
616 if ((t->t_dflg & F_PIPEOUT) != 0)
618 #endif /* BACKPIPE */
622 (void) sigsetmask(csigmask);
624 (void) sigrelse(SIGCHLD);
628 if ((t->t_dflg & F_AMPERSAND) == 0)
633 doio(t, pipein, pipeout);
635 if (t->t_dflg & F_PIPEIN) {
636 (void) close(pipein[0]);
637 (void) close(pipein[1]);
639 #else /* !BACKPIPE */
640 if (t->t_dflg & F_PIPEOUT) {
641 (void) close(pipeout[0]);
642 (void) close(pipeout[1]);
644 #endif /* BACKPIPE */
646 * Perform a builtin function. If we are not forked, arrange for
654 if (adrof(STRprintexitvalue)) {
655 int rv = getn(varval(STRstatus));
657 xprintf(CGETS(17, 2, "Exit %d\n"), rv);
662 if (t->t_dtyp != NODE_PAREN) {
667 * For () commands must put new 0,1,2 in FSH* and recurse
669 (void)close_on_exec(OLDSTD = dcopy(0, FOLDSTD), 1);
670 (void)close_on_exec(SHOUT = dcopy(1, FSHOUT), 1);
671 isoutatty = isatty(SHOUT);
672 (void)close_on_exec(SHDIAG = dcopy(2, FSHDIAG), 1);
673 isdiagatty = isatty(SHDIAG);
676 #ifndef CLOSE_ON_EXEC
679 (void) close_on_exec(FSHOUT, 1);
680 (void) close_on_exec(FSHDIAG, 1);
681 (void) close_on_exec(FOLDSTD, 1);
682 #endif /* !CLOSE_ON_EXEC */
685 t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
686 execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
691 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
692 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
693 execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
694 t->t_dcar->t_dflg |= F_PIPEOUT |
695 (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
696 execute(t->t_dcar, wanttty, pipein, pv, do_glob);
697 #else /* !BACKPIPE */
698 t->t_dcar->t_dflg |= F_PIPEOUT |
699 (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
700 execute(t->t_dcar, wanttty, pipein, pv, do_glob);
701 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
702 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
703 execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
704 #endif /* BACKPIPE */
709 t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
710 execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
712 * In strange case of A&B make a new job after A
714 if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
715 (t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
719 t->t_dcdr->t_dflg |= t->t_dflg &
720 (F_NOFORK | F_NOINTERRUPT);
721 execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
728 t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
729 execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
730 if ((getn(varval(STRstatus)) == 0) !=
731 (t->t_dtyp == NODE_AND)) {
736 t->t_dcdr->t_dflg |= t->t_dflg &
737 (F_NOFORK | F_NOINTERRUPT);
738 execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
746 * Fall through for all breaks from switch
748 * If there will be no more executions of this command, flush all file
749 * descriptors. Places that turn on the F_REPEAT bit are responsible for
750 * doing donefds after the last re-execution
752 if (didfds && !(t->t_dflg & F_REPEAT))
765 if ((v = gargv) != 0) {
770 if ((v = pargv) != 0) {
780 * Expand and glob the words after an i/o redirection.
781 * If more than one word is generated, then update the command vector.
783 * This is done differently in all the shells:
784 * 1. in the bourne shell and ksh globbing is not performed
785 * 2. Bash/csh say ambiguous
786 * 3. zsh does i/o to/from all the files
787 * 4. itcsh concatenates the words.
789 * I don't know what is best to do. I think that Ambiguous is better
790 * than restructuring the command vector, because the user can get
791 * unexpected results. In any case, the command vector restructuring
792 * code is present and the user can choose it by setting noambiguous
797 Char *cp; /* word after < or > */
801 if (adrof(STRnoambiguous)) {
804 blk[0] = Dfix1(cp); /* expand $ */
807 gflag = 0, tglob(blk);
811 setname(short2str(blk[0]));
812 xfree((ptr_t) blk[0]);
813 stderror(ERR_NAME | ERR_NOMATCH);
816 if (pv[1] != NULL) { /* we need to fix the command vector */
817 Char **av = blkspl(t->t_dcom, &pv[1]);
818 xfree((ptr_t) t->t_dcom);
821 xfree((ptr_t) blk[0]);
829 (void) Strcpy(buf, blk[1] = Dfix1(cp));
830 xfree((ptr_t) blk[1]);
831 blk[0] = globone(buf, G_ERROR);
837 * Perform io redirection.
838 * We may or maynot be forked here.
841 doio(t, pipein, pipeout)
843 int *pipein, *pipeout;
847 unsigned long flags = t->t_dflg;
849 if (didfds || (flags & F_REPEAT))
851 if ((flags & F_READ) == 0) {/* F_READ already done */
853 char tmp[MAXPATHLEN+1];
856 * so < /dev/std{in,out,err} work
858 (void) dcopy(SHIN, 0);
859 (void) dcopy(SHOUT, 1);
860 (void) dcopy(SHDIAG, 2);
861 cp = splicepipe(t, t->t_dlef);
862 (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
863 tmp[MAXPATHLEN] = '\0';
865 if ((fd = open(tmp, O_RDONLY|O_LARGEFILE)) < 0)
866 stderror(ERR_SYSTEM, tmp, strerror(errno));
867 /* allow input files larger than 2Gb */
869 (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
870 #endif /*!WINNT_NATIVE*/
873 else if (flags & F_PIPEIN) {
875 (void) dup(pipein[0]);
876 (void) close(pipein[0]);
877 (void) close(pipein[1]);
879 else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
881 (void) open(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
886 #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
888 * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
889 * across dup()s, so we have to UNSET it here or else we get a
890 * command with NO stdin, stdout, or stderr at all (a bad thing
893 (void) close_on_exec(0, 0);
894 #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
898 char tmp[MAXPATHLEN+1];
900 cp = splicepipe(t, t->t_drit);
901 (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
902 tmp[MAXPATHLEN] = '\0';
905 * so > /dev/std{out,err} work
907 (void) dcopy(SHOUT, 1);
908 (void) dcopy(SHDIAG, 2);
909 if ((flags & F_APPEND) != 0) {
911 fd = open(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
912 #else /* !O_APPEND */
913 fd = open(tmp, O_WRONLY|O_LARGEFILE);
914 (void) lseek(fd, (off_t) 0, L_XTND);
915 #endif /* O_APPEND */
919 if ((flags & F_APPEND) == 0 || fd == -1) {
920 if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
921 if (flags & F_APPEND)
922 stderror(ERR_SYSTEM, tmp, strerror(errno));
925 if ((fd = creat(tmp, 0666)) < 0)
926 stderror(ERR_SYSTEM, tmp, strerror(errno));
927 /* allow input files larger than 2Gb */
929 (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
930 #endif /*!WINNT_NATIVE*/
935 else if (flags & F_PIPEOUT) {
937 (void) dup(pipeout[1]);
944 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
945 (void) close_on_exec(1, 0);
946 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
950 if (flags & F_STDERR) {
956 is2atty = isdiagatty;
957 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
958 (void) close_on_exec(2, 0);
959 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
971 (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
972 (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
973 if (pv[0] >= 0 && pv[1] >= 0)
985 if (stat(cp, &stb) < 0)
987 if (S_ISCHR(stb.st_mode))
989 stderror(ERR_EXISTS, cp);