1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.sem.c,v 3.90 2015/10/31 18:54:42 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("$tcsh: sh.sem.c,v 3.90 2015/10/31 18:54:42 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 void vffree (int);
61 static Char *splicepipe (struct command *, Char *);
62 static void doio (struct command *, int *, int *);
63 static void chkclob (const 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(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
91 const struct biltins * volatile bifunc;
95 static sigset_t csigset;
97 static int onosigchld = 0;
99 static int nosigchld = 0;
110 if ((varval(STRNTslowexec) == STRNULL) &&
111 !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
112 (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
114 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
115 (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
117 if (nt_try_fast_exec(t) == 0)
121 #endif /* WINNT_NATIVE */
124 * Ed hutchins@sgi.com & Dominic dbg@sgi.com
125 * Sat Feb 25 03:13:11 PST 1995
126 * try implicit cd if we have a 1 word command
128 if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
129 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
134 sCName = dollar(t->t_dcom[0]);
135 if (sCName != NULL && sCName[0] == '~') {
136 struct Strbuf buf = Strbuf_INIT;
137 const Char *name_end;
139 for (name_end = sCName + 1; *name_end != '\0' && *name_end != '/';
142 if (name_end != sCName + 1) {
145 name = Strnsave(sCName + 1, name_end - (sCName + 1));
146 home = gethdir(name);
148 Strbuf_append(&buf, home);
151 Strbuf_append(&buf, name);
154 Strbuf_append(&buf, varval(STRhome));
155 Strbuf_append(&buf, name_end);
157 sCName = Strbuf_finish(&buf);
160 pathname = short2str(sCName);
162 /* if this is a dir, tack a "cd" on as the first arg */
163 if (pathname != NULL &&
164 ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
166 || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
167 #endif /* WINNT_NATIVE */
170 Char **ot_dcom = t->t_dcom;
172 vCD[0] = Strsave(STRcd);
174 t->t_dcom = blkspl(vCD, ot_dcom);
176 if (implicit_cd > 1) {
184 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
185 * Don't check for wantty > 0...
187 if (t->t_dflg & F_AMPERSAND)
192 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
193 memmove(t->t_dcom[0], t->t_dcom[0] + 1,
194 (Strlen(t->t_dcom[0] + 1) + 1) * sizeof (*t->t_dcom[0]));
195 if ((t->t_dflg & F_REPEAT) == 0)
196 Dfix(t); /* $ " ' \ */
197 if (t->t_dcom[0] == 0) {
204 if (t->t_dflg & F_PIPEIN)
206 #else /* !BACKPIPE */
207 if (t->t_dflg & F_PIPEOUT)
209 #endif /* BACKPIPE */
211 * Must do << early so parent will know where input pointer should be.
212 * If noexec then this is all we do.
214 if (t->t_dflg & F_READ) {
215 int old_pintr_disabled;
219 pintr_push_enable(&old_pintr_disabled);
222 cleanup_until(&old_pintr_disabled);
227 setcopy(STRstatus, STR0, VAR_READWRITE);
230 * This mess is the necessary kludge to handle the prefix builtins:
231 * nice, nohup, time. These commands can also be used by themselves,
232 * and this is not handled here. This will also work when loops are
235 while (t->t_dtyp == NODE_COMMAND)
236 if (eq(t->t_dcom[0], STRnice)) {
238 if (strchr("+-", t->t_dcom[1][0])) {
241 t->t_nice = (unsigned char)getn(t->t_dcom[1]);
242 lshift(t->t_dcom, 2);
250 lshift(t->t_dcom, 1);
257 else if (eq(t->t_dcom[0], STRnohup)) {
259 t->t_dflg |= F_NOHUP;
260 lshift(t->t_dcom, 1);
265 else if (eq(t->t_dcom[0], STRhup)) {
268 lshift(t->t_dcom, 1);
273 else if (eq(t->t_dcom[0], STRtime)) {
276 lshift(t->t_dcom, 1);
282 else if (eq(t->t_dcom[0], STRver))
283 if (t->t_dcom[1] && t->t_dcom[2]) {
285 t->t_systype = getv(t->t_dcom[1]);
286 lshift(t->t_dcom, 2);
295 /* is it a command */
296 if (t->t_dtyp == NODE_COMMAND) {
298 * Check if we have a builtin function and remember which one.
303 * Continue for builtins that are part of the scripting language
307 if (bifunc->bfunct != (bfunc_t)dobreak &&
308 bifunc->bfunct != (bfunc_t)docontin &&
309 bifunc->bfunct != (bfunc_t)doelse &&
310 bifunc->bfunct != (bfunc_t)doend &&
311 bifunc->bfunct != (bfunc_t)doforeach&&
312 bifunc->bfunct != (bfunc_t)dogoto &&
313 bifunc->bfunct != (bfunc_t)doif &&
314 bifunc->bfunct != (bfunc_t)dorepeat &&
315 bifunc->bfunct != (bfunc_t)doswbrk &&
316 bifunc->bfunct != (bfunc_t)doswitch &&
317 bifunc->bfunct != (bfunc_t)dowhile &&
318 bifunc->bfunct != (bfunc_t)dozip)
322 else { /* not a command */
329 * GrP Executing a command - run jobcmd hook
330 * Don't run for builtins
331 * Don't run if we're not in a tty
332 * Don't run if we're not really executing
335 * CR - Charles Ross Aug 2005
337 * The new behavior is that the jobcmd won't be executed
338 * if stdout (SHOUT) isnt attached to a tty.. IE when
339 * redirecting, or using backquotes etc..
341 if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty && isoutatty) {
342 Char *cmd = unparse(t);
344 cleanup_push(cmd, xfree);
350 * We fork only if we are timed, or are not the end of a parenthesized
351 * list and not a simple builtin function. Simple meaning one that is
352 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
353 * fork in some of these cases.
357 * Can't have NOFORK for the tail of a pipe - because it is not the
358 * last command spawned (even if it is at the end of a parenthesised
361 if (t->t_dflg & F_PIPEIN)
362 t->t_dflg &= ~(F_NOFORK);
365 * "command | builtin" may cause major misbehaviour as noted in
366 * in the BUGS file entry
367 * Subject: Redirected input to built-in functions misbehaves badly
368 * forking when the builtin is the end of the pipe corrects the
371 if (bifunc && (t->t_dflg & F_PIPEIN))
372 t->t_dflg &= ~(F_NOFORK);
373 #endif /* BACKPIPE */
375 * Prevent forking cd, pushd, popd, chdir cause this will cause the
376 * shell not to change dir! (XXX: but only for nice?)
378 if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
379 bifunc->bfunct == (bfunc_t)dopushd ||
380 bifunc->bfunct == (bfunc_t)dopopd))
381 t->t_dflg &= ~(F_NICE);
383 if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
384 (!bifunc || t->t_dflg &
385 (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
387 * We have to fork for eval too.
389 (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
390 bifunc->bfunct == (bfunc_t)doeval)) {
392 if (t->t_dtyp == NODE_PAREN ||
393 t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
398 * We need to block SIGCHLD here, so that if the process does
399 * not die before we can set the process group
401 if (wanttty >= 0 && !nosigchld) {
403 sigaddset(&set, SIGCHLD);
404 (void)sigprocmask(SIG_BLOCK, &set, &csigset);
409 pid = pfork(t, wanttty);
410 if (pid == 0 && nosigchld) {
411 sigprocmask(SIG_SETMASK, &csigset, NULL);
414 else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
420 int ochild, osetintr, ohaderr, odidfds;
421 int oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
422 int oisoutatty, oisdiagatty;
423 sigset_t oset, ocsigset;
424 # ifndef CLOSE_ON_EXEC
426 # endif /* !CLOSE_ON_EXEC */
429 * Prepare for the vfork by saving everything that the child
430 * corrupts before it exec's. Note that in some signal
431 * implementations which keep the signal info in user space
432 * (e.g. Sun's) it will also be necessary to save and restore
433 * the current sigvec's for the signals the child touches
438 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
439 * Gilbrech - 11/22/87)
442 struct sigaction savesv[NSIGSAVED];
445 # endif /* SAVESIGVEC */
446 if (wanttty >= 0 && !nosigchld && !noexec) {
448 sigaddset(&set, SIGCHLD);
449 (void)sigprocmask(SIG_BLOCK, &set, &csigset);
453 sigaddset(&set, SIGCHLD);
454 sigaddset(&set, SIGINT);
455 (void)sigprocmask(SIG_BLOCK, &set, &oset);
460 # ifndef CLOSE_ON_EXEC
462 # endif /* !CLOSE_ON_EXEC */
468 oisoutatty = isoutatty;
469 oisdiagatty = isdiagatty;
471 onosigchld = nosigchld;
476 savesigvec(savesv, savesm);
477 # endif /* SAVESIGVEC */
485 restoresigvec(savesv, savesm);
486 # endif /* SAVESIGVEC */
487 sigprocmask(SIG_SETMASK, &oset, NULL);
488 stderror(ERR_NOPROC);
491 if (pid) { /* parent */
493 restoresigvec(savesv, savesm);
494 # endif /* SAVESIGVEC */
500 # ifndef CLOSE_ON_EXEC
502 # endif /* !CLOSE_ON_EXEC */
507 isoutatty = oisoutatty;
508 isdiagatty = oisdiagatty;
510 nosigchld = onosigchld;
520 /* this is from pfork() */
522 sigprocmask(SIG_SETMASK, &oset, NULL);
525 /* this is from pfork() */
529 sigprocmask(SIG_SETMASK, &csigset, NULL);
534 ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
535 || (gointr && eq(gointr, STRminus));
536 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
541 * casts made right for SunOS 4.0 by Douglas C. Schmidt
542 * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
545 * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
549 (void) signal(SIGINT, SIG_IGN);
550 (void) signal(SIGQUIT, SIG_IGN);
553 (void) signal(SIGINT, vffree);
554 (void) signal(SIGQUIT, SIG_DFL);
558 (void) signal(SIGTSTP, SIG_DFL);
559 (void) signal(SIGTTIN, SIG_DFL);
560 (void) signal(SIGTTOU, SIG_DFL);
562 # endif /* BSDJOBS */
564 sigaction(SIGTERM, &parterm, NULL);
566 else if (tpgrp == -1 &&
567 (t->t_dflg & F_NOINTERRUPT)) {
568 (void) signal(SIGINT, SIG_IGN);
569 (void) signal(SIGQUIT, SIG_IGN);
572 pgetty(wanttty, pgrp);
574 if (t->t_dflg & F_NOHUP)
575 (void) signal(SIGHUP, SIG_IGN);
576 if (t->t_dflg & F_HUP)
577 (void) signal(SIGHUP, SIG_DFL);
578 if (t->t_dflg & F_NICE) {
579 int nval = SIGN_EXTEND_CHAR(t->t_nice);
580 # if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
581 if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
582 stderror(ERR_SYSTEM, "setpriority",
584 # else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
586 # endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
589 if (t->t_dflg & F_VER) {
590 tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
601 * It would be better if we could wait for the whole job when we
602 * knew the last process had been started. Pwait, in fact, does
603 * wait for the whole job anyway, but this test doesn't really
604 * express our intentions.
607 if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
611 if ((t->t_dflg & F_PIPEIN) != 0)
613 #else /* !BACKPIPE */
614 if (didfds == 0 && t->t_dflg & F_PIPEIN) {
618 if ((t->t_dflg & F_PIPEOUT) != 0)
620 #endif /* BACKPIPE */
623 sigprocmask(SIG_SETMASK, &csigset, NULL);
626 if ((t->t_dflg & F_AMPERSAND) == 0)
631 doio(t, pipein, pipeout);
633 if (t->t_dflg & F_PIPEIN) {
637 #else /* !BACKPIPE */
638 if (t->t_dflg & F_PIPEOUT) {
642 #endif /* BACKPIPE */
644 * Perform a builtin function. If we are not forked, arrange for
653 int ohaderr = haderr;
661 if (adrof(STRprintexitvalue)) {
662 int rv = getn(varval(STRstatus));
664 xprintf(CGETS(17, 2, "Exit %d\n"), rv);
669 if (t->t_dtyp != NODE_PAREN) {
674 * For () commands must put new 0,1,2 in FSH* and recurse
676 if ((OLDSTD = dcopy(0, FOLDSTD)) >= 0)
677 (void)close_on_exec(OLDSTD, 1);
678 if ((SHOUT = dcopy(1, FSHOUT)) >= 0) {
679 (void)close_on_exec(SHOUT, 1);
680 isoutatty = isatty(SHOUT);
682 if ((SHDIAG = dcopy(2, FSHDIAG)) >= 0) {
683 (void)close_on_exec(SHDIAG, 1);
684 isdiagatty = isatty(SHDIAG);
688 #ifndef CLOSE_ON_EXEC
691 (void) close_on_exec(FSHOUT, 1);
692 (void) close_on_exec(FSHDIAG, 1);
693 (void) close_on_exec(FOLDSTD, 1);
694 #endif /* !CLOSE_ON_EXEC */
697 t->t_dspr->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
698 execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
703 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
704 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
705 execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
706 t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
707 (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
708 execute(t->t_dcar, wanttty, pipein, pv, do_glob);
709 #else /* !BACKPIPE */
710 t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
711 (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
712 execute(t->t_dcar, wanttty, pipein, pv, do_glob);
713 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
714 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
715 execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
716 #endif /* BACKPIPE */
721 t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
722 execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
724 * In strange case of A&B make a new job after A
726 if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
727 (t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
731 t->t_dcdr->t_dflg |= t->t_dflg &
732 (F_NOFORK | F_NOINTERRUPT | F_BACKQ);
733 execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
740 t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
741 execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
742 if ((getn(varval(STRstatus)) == 0) !=
743 (t->t_dtyp == NODE_AND)) {
748 t->t_dcdr->t_dflg |= t->t_dflg &
749 (F_NOFORK | F_NOINTERRUPT | F_BACKQ);
750 execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
758 * Fall through for all breaks from switch
760 * If there will be no more executions of this command, flush all file
761 * descriptors. Places that turn on the F_REPEAT bit are responsible for
762 * doing donefds after the last re-execution
764 if (didfds && !(t->t_dflg & F_REPEAT))
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
795 splicepipe(struct command *t, Char *cp)
799 if (adrof(STRnoambiguous)) {
803 blk[0] = Dfix1(cp); /* expand $ */
808 pv = globall(blk, gflag);
810 setname(short2str(blk[0]));
812 stderror(ERR_NAME | ERR_NOMATCH);
814 if (pv[1] != NULL) { /* we need to fix the command vector */
815 Char **av = blkspl(t->t_dcom, &pv[1]);
828 cleanup_push(buf, xfree);
829 blk[0] = globone(buf, G_ERROR);
836 * Perform io redirection.
837 * We may or maynot be forked here.
840 doio(struct command *t, int *pipein, int *pipeout)
844 unsigned long flags = t->t_dflg;
846 if (didfds || (flags & F_REPEAT))
848 if ((flags & F_READ) == 0) {/* F_READ already done */
853 * so < /dev/std{in,out,err} work
855 (void) dcopy(SHIN, 0);
856 (void) dcopy(SHOUT, 1);
857 (void) dcopy(SHDIAG, 2);
858 cp = splicepipe(t, t->t_dlef);
859 tmp = strsave(short2str(cp));
861 cleanup_push(tmp, xfree);
862 if ((fd = xopen(tmp, O_RDONLY|O_LARGEFILE)) < 0)
863 stderror(ERR_SYSTEM, tmp, strerror(errno));
865 /* allow input files larger than 2Gb */
867 (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
868 #endif /*!WINNT_NATIVE*/
871 else if (flags & F_PIPEIN) {
873 TCSH_IGNORE(dup(pipein[0]));
877 else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
879 (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
883 TCSH_IGNORE(dup(OLDSTD));
884 #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
886 * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
887 * across dup()s, so we have to UNSET it here or else we get a
888 * command with NO stdin, stdout, or stderr at all (a bad thing
891 (void) close_on_exec(0, 0);
892 #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
898 cp = splicepipe(t, t->t_drit);
899 tmp = strsave(short2str(cp));
901 cleanup_push(tmp, xfree);
903 * so > /dev/std{out,err} work
905 (void) dcopy(SHOUT, 1);
906 (void) dcopy(SHDIAG, 2);
907 if ((flags & F_APPEND) != 0) {
909 fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
910 #else /* !O_APPEND */
911 fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
912 (void) lseek(fd, (off_t) 0, L_XTND);
913 #endif /* O_APPEND */
917 if ((flags & F_APPEND) == 0 || fd == -1) {
918 if (!(flags & F_OVERWRITE) && no_clobber) {
919 if (flags & F_APPEND)
920 stderror(ERR_SYSTEM, tmp, strerror(errno));
923 if ((fd = xcreat(tmp, 0666)) < 0)
924 stderror(ERR_SYSTEM, tmp, strerror(errno));
925 /* allow input files larger than 2Gb */
927 (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
928 #endif /*!WINNT_NATIVE*/
934 else if (flags & F_PIPEOUT) {
936 TCSH_IGNORE(dup(pipeout[1]));
941 TCSH_IGNORE(dup(SHOUT));
943 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
944 (void) close_on_exec(1, 0);
945 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
949 if (flags & F_STDERR) {
954 TCSH_IGNORE(dup(SHDIAG));
955 is2atty = isdiagatty;
956 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
957 (void) close_on_exec(2, 0);
958 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
969 (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
970 (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
971 if (pv[0] >= 0 && pv[1] >= 0)
982 chkclob(const char *cp)
986 if (stat(cp, &stb) < 0)
988 if (S_ISCHR(stb.st_mode))
990 if (no_clobber & NOCLOBBER_NOTEMPTY && stb.st_size == 0)
992 if (no_clobber & NOCLOBBER_ASK) {
993 if (getYN(CGETS(22, 15,
994 "Do you really want to overwrite an existing file? [N/y] ")))
998 stderror(ERR_EXISTS, cp);