Home | History | Annotate | Line # | Download | only in sh
redir.c revision 1.59.4.3
      1  1.59.4.3    martin /*	$NetBSD: redir.c,v 1.59.4.3 2020/04/21 19:37:34 martin Exp $	*/
      2      1.11       cgd 
      3       1.1       cgd /*-
      4       1.7       jtc  * Copyright (c) 1991, 1993
      5       1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Kenneth Almquist.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35      1.16  christos #include <sys/cdefs.h>
     36       1.1       cgd #ifndef lint
     37      1.11       cgd #if 0
     38      1.12  christos static char sccsid[] = "@(#)redir.c	8.2 (Berkeley) 5/4/95";
     39      1.11       cgd #else
     40  1.59.4.3    martin __RCSID("$NetBSD: redir.c,v 1.59.4.3 2020/04/21 19:37:34 martin Exp $");
     41      1.11       cgd #endif
     42       1.1       cgd #endif /* not lint */
     43       1.1       cgd 
     44      1.12  christos #include <sys/types.h>
     45      1.17  christos #include <sys/param.h>	/* PIPE_BUF */
     46      1.48  christos #include <sys/stat.h>
     47      1.12  christos #include <signal.h>
     48      1.12  christos #include <string.h>
     49      1.12  christos #include <fcntl.h>
     50      1.12  christos #include <errno.h>
     51      1.12  christos #include <unistd.h>
     52      1.12  christos #include <stdlib.h>
     53      1.12  christos 
     54       1.1       cgd /*
     55       1.1       cgd  * Code for dealing with input/output redirection.
     56       1.1       cgd  */
     57       1.1       cgd 
     58      1.25  christos #include "main.h"
     59      1.50  christos #include "builtins.h"
     60       1.1       cgd #include "shell.h"
     61       1.1       cgd #include "nodes.h"
     62       1.1       cgd #include "jobs.h"
     63      1.23  christos #include "options.h"
     64       1.1       cgd #include "expand.h"
     65       1.1       cgd #include "redir.h"
     66       1.1       cgd #include "output.h"
     67       1.1       cgd #include "memalloc.h"
     68      1.51       kre #include "mystring.h"
     69       1.1       cgd #include "error.h"
     70      1.59       kre #include "show.h"
     71       1.1       cgd 
     72       1.1       cgd 
     73       1.1       cgd #define EMPTY -2		/* marks an unused slot in redirtab */
     74      1.44       kre #define CLOSED -1		/* fd was not open before redir */
     75      1.17  christos #ifndef PIPE_BUF
     76      1.17  christos # define PIPESIZE 4096		/* amount of buffering in a pipe */
     77      1.17  christos #else
     78      1.17  christos # define PIPESIZE PIPE_BUF
     79      1.17  christos #endif
     80       1.1       cgd 
     81  1.59.4.1  christos #ifndef FD_CLOEXEC
     82  1.59.4.1  christos # define FD_CLOEXEC	1	/* well known from before there was a name */
     83  1.59.4.1  christos #endif
     84  1.59.4.1  christos 
     85  1.59.4.1  christos #ifndef F_DUPFD_CLOEXEC
     86  1.59.4.1  christos #define F_DUPFD_CLOEXEC	F_DUPFD
     87  1.59.4.1  christos #define CLOEXEC(fd)	(fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC))
     88  1.59.4.1  christos #else
     89  1.59.4.1  christos #define CLOEXEC(fd)
     90  1.59.4.1  christos #endif
     91  1.59.4.1  christos 
     92       1.1       cgd 
     93       1.1       cgd MKINIT
     94      1.43  christos struct renamelist {
     95      1.43  christos 	struct renamelist *next;
     96      1.43  christos 	int orig;
     97      1.43  christos 	int into;
     98      1.43  christos };
     99      1.43  christos 
    100      1.43  christos MKINIT
    101       1.1       cgd struct redirtab {
    102       1.1       cgd 	struct redirtab *next;
    103      1.43  christos 	struct renamelist *renamed;
    104       1.1       cgd };
    105       1.1       cgd 
    106       1.1       cgd 
    107       1.1       cgd MKINIT struct redirtab *redirlist;
    108       1.1       cgd 
    109      1.13  christos /*
    110       1.7       jtc  * We keep track of whether or not fd0 has been redirected.  This is for
    111       1.7       jtc  * background commands, where we want to redirect fd0 to /dev/null only
    112      1.13  christos  * if it hasn't already been redirected.
    113      1.43  christos  */
    114      1.43  christos STATIC int fd0_redirected = 0;
    115       1.1       cgd 
    116      1.43  christos /*
    117      1.43  christos  * And also where to put internal use fds that should be out of the
    118      1.43  christos  * way of user defined fds (normally)
    119      1.43  christos  */
    120      1.43  christos STATIC int big_sh_fd = 0;
    121      1.43  christos 
    122      1.43  christos STATIC const struct renamelist *is_renamed(const struct renamelist *, int);
    123      1.43  christos STATIC void fd_rename(struct redirtab *, int, int);
    124      1.43  christos STATIC void free_rl(struct redirtab *, int);
    125      1.29  christos STATIC void openredirect(union node *, char[10], int);
    126      1.34      yamt STATIC int openhere(const union node *);
    127      1.47       kre STATIC int copyfd(int, int, int);
    128      1.43  christos STATIC void find_big_fd(void);
    129      1.43  christos 
    130      1.54       kre 
    131      1.54       kre struct shell_fds {		/* keep track of internal shell fds */
    132      1.54       kre 	struct shell_fds *nxt;
    133      1.54       kre 	void (*cb)(int, int);
    134      1.54       kre 	int fd;
    135      1.54       kre };
    136      1.54       kre 
    137      1.54       kre STATIC struct shell_fds *sh_fd_list;
    138      1.54       kre 
    139      1.54       kre STATIC void renumber_sh_fd(struct shell_fds *);
    140      1.54       kre STATIC struct shell_fds *sh_fd(int);
    141      1.54       kre 
    142      1.43  christos STATIC const struct renamelist *
    143      1.43  christos is_renamed(const struct renamelist *rl, int fd)
    144      1.43  christos {
    145      1.43  christos 	while (rl != NULL) {
    146      1.43  christos 		if (rl->orig == fd)
    147      1.43  christos 			return rl;
    148      1.43  christos 		rl = rl->next;
    149      1.43  christos 	}
    150      1.43  christos 	return NULL;
    151      1.43  christos }
    152      1.43  christos 
    153      1.43  christos STATIC void
    154      1.43  christos free_rl(struct redirtab *rt, int reset)
    155      1.43  christos {
    156      1.43  christos 	struct renamelist *rl, *rn = rt->renamed;
    157      1.43  christos 
    158      1.43  christos 	while ((rl = rn) != NULL) {
    159      1.43  christos 		rn = rl->next;
    160      1.43  christos 		if (rl->orig == 0)
    161      1.43  christos 			fd0_redirected--;
    162  1.59.4.1  christos 		VTRACE(DBG_REDIR, ("popredir %d%s: %s",
    163  1.59.4.1  christos 		    rl->orig, rl->orig==0 ? " (STDIN)" : "",
    164  1.59.4.1  christos 		    reset ? "" : "no reset\n"));
    165      1.43  christos 		if (reset) {
    166  1.59.4.1  christos 			if (rl->into < 0) {
    167  1.59.4.1  christos 				VTRACE(DBG_REDIR, ("closed\n"));
    168      1.43  christos 				close(rl->orig);
    169  1.59.4.1  christos 			} else {
    170  1.59.4.1  christos 				VTRACE(DBG_REDIR, ("from %d\n", rl->into));
    171      1.43  christos 				movefd(rl->into, rl->orig);
    172  1.59.4.1  christos 			}
    173      1.43  christos 		}
    174      1.43  christos 		ckfree(rl);
    175      1.43  christos 	}
    176      1.43  christos 	rt->renamed = NULL;
    177      1.43  christos }
    178       1.1       cgd 
    179      1.43  christos STATIC void
    180      1.43  christos fd_rename(struct redirtab *rt, int from, int to)
    181      1.43  christos {
    182  1.59.4.1  christos 	/* XXX someday keep a short list (8..10) of freed renamelists XXX */
    183      1.43  christos 	struct renamelist *rl = ckmalloc(sizeof(struct renamelist));
    184      1.43  christos 
    185      1.43  christos 	rl->next = rt->renamed;
    186      1.43  christos 	rt->renamed = rl;
    187      1.43  christos 
    188      1.43  christos 	rl->orig = from;
    189      1.43  christos 	rl->into = to;
    190      1.43  christos }
    191       1.1       cgd 
    192       1.1       cgd /*
    193       1.1       cgd  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
    194       1.1       cgd  * old file descriptors are stashed away so that the redirection can be
    195       1.1       cgd  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
    196       1.1       cgd  * standard output, and the standard error if it becomes a duplicate of
    197       1.1       cgd  * stdout, is saved in memory.
    198       1.1       cgd  */
    199       1.1       cgd 
    200       1.1       cgd void
    201      1.27  christos redirect(union node *redir, int flags)
    202      1.27  christos {
    203       1.1       cgd 	union node *n;
    204      1.16  christos 	struct redirtab *sv = NULL;
    205       1.1       cgd 	int i;
    206       1.1       cgd 	int fd;
    207      1.13  christos 	char memory[10];	/* file descriptors to write to memory */
    208       1.1       cgd 
    209  1.59.4.1  christos 	CTRACE(DBG_REDIR, ("redirect(F=0x%x):%s\n", flags, redir?"":" NONE"));
    210       1.1       cgd 	for (i = 10 ; --i >= 0 ; )
    211       1.1       cgd 		memory[i] = 0;
    212       1.1       cgd 	memory[1] = flags & REDIR_BACKQ;
    213       1.1       cgd 	if (flags & REDIR_PUSH) {
    214  1.59.4.1  christos 		/*
    215  1.59.4.1  christos 		 * We don't have to worry about REDIR_VFORK here, as
    216      1.24  christos 		 * flags & REDIR_PUSH is never true if REDIR_VFORK is set.
    217      1.24  christos 		 */
    218       1.1       cgd 		sv = ckmalloc(sizeof (struct redirtab));
    219      1.43  christos 		sv->renamed = NULL;
    220       1.1       cgd 		sv->next = redirlist;
    221       1.1       cgd 		redirlist = sv;
    222       1.1       cgd 	}
    223       1.1       cgd 	for (n = redir ; n ; n = n->nfile.next) {
    224       1.1       cgd 		fd = n->nfile.fd;
    225  1.59.4.1  christos 		VTRACE(DBG_REDIR, ("redir %d (max=%d) ", fd, max_user_fd));
    226      1.53       kre 		if (fd > max_user_fd)
    227      1.53       kre 			max_user_fd = fd;
    228      1.54       kre 		renumber_sh_fd(sh_fd(fd));
    229      1.13  christos 		if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
    230      1.43  christos 		    n->ndup.dupfd == fd) {
    231      1.43  christos 			/* redirect from/to same file descriptor */
    232      1.47       kre 			/* make sure it stays open */
    233      1.47       kre 			if (fcntl(fd, F_SETFD, 0) < 0)
    234      1.47       kre 				error("fd %d: %s", fd, strerror(errno));
    235  1.59.4.1  christos 			VTRACE(DBG_REDIR, ("!cloexec\n"));
    236      1.43  christos 			continue;
    237      1.43  christos 		}
    238      1.15  christos 
    239      1.43  christos 		if ((flags & REDIR_PUSH) && !is_renamed(sv->renamed, fd)) {
    240       1.1       cgd 			INTOFF;
    241      1.43  christos 			if (big_sh_fd < 10)
    242      1.43  christos 				find_big_fd();
    243      1.43  christos 			if ((i = fcntl(fd, F_DUPFD, big_sh_fd)) == -1) {
    244      1.15  christos 				switch (errno) {
    245      1.15  christos 				case EBADF:
    246      1.35      yamt 					i = CLOSED;
    247      1.35      yamt 					break;
    248      1.43  christos 				case EMFILE:
    249      1.43  christos 				case EINVAL:
    250      1.43  christos 					find_big_fd();
    251      1.43  christos 					i = fcntl(fd, F_DUPFD, big_sh_fd);
    252      1.43  christos 					if (i >= 0)
    253      1.43  christos 						break;
    254      1.43  christos 					/* FALLTHRU */
    255      1.15  christos 				default:
    256      1.43  christos 					i = errno;
    257      1.43  christos 					error("%d: %s", fd, strerror(i));
    258      1.19   mycroft 					/* NOTREACHED */
    259      1.15  christos 				}
    260      1.43  christos 			}
    261      1.43  christos 			if (i >= 0)
    262      1.35      yamt 				(void)fcntl(i, F_SETFD, FD_CLOEXEC);
    263      1.43  christos 			fd_rename(sv, fd, i);
    264  1.59.4.1  christos 			VTRACE(DBG_REDIR, ("saved as %d ", i));
    265       1.1       cgd 			INTON;
    266       1.1       cgd 		}
    267  1.59.4.1  christos 		VTRACE(DBG_REDIR, ("%s\n", fd == 0 ? "STDIN" : ""));
    268      1.44       kre 		if (fd == 0)
    269      1.44       kre 			fd0_redirected++;
    270      1.35      yamt 		openredirect(n, memory, flags);
    271       1.1       cgd 	}
    272       1.1       cgd 	if (memory[1])
    273       1.1       cgd 		out1 = &memout;
    274       1.1       cgd 	if (memory[2])
    275       1.1       cgd 		out2 = &memout;
    276       1.1       cgd }
    277       1.1       cgd 
    278       1.1       cgd 
    279       1.1       cgd STATIC void
    280      1.29  christos openredirect(union node *redir, char memory[10], int flags)
    281      1.27  christos {
    282      1.36  christos 	struct stat sb;
    283       1.1       cgd 	int fd = redir->nfile.fd;
    284       1.1       cgd 	char *fname;
    285       1.1       cgd 	int f;
    286      1.42  christos 	int eflags, cloexec;
    287       1.1       cgd 
    288       1.1       cgd 	/*
    289       1.1       cgd 	 * We suppress interrupts so that we won't leave open file
    290       1.1       cgd 	 * descriptors around.  This may not be such a good idea because
    291       1.1       cgd 	 * an open of a device or a fifo can block indefinitely.
    292       1.1       cgd 	 */
    293       1.1       cgd 	INTOFF;
    294      1.43  christos 	if (fd < 10)
    295      1.43  christos 		memory[fd] = 0;
    296       1.1       cgd 	switch (redir->nfile.type) {
    297       1.1       cgd 	case NFROM:
    298       1.1       cgd 		fname = redir->nfile.expfname;
    299      1.29  christos 		if (flags & REDIR_VFORK)
    300      1.29  christos 			eflags = O_NONBLOCK;
    301      1.29  christos 		else
    302      1.29  christos 			eflags = 0;
    303      1.29  christos 		if ((f = open(fname, O_RDONLY|eflags)) < 0)
    304      1.20  christos 			goto eopen;
    305      1.59       kre 		VTRACE(DBG_REDIR, ("openredirect(< '%s') -> %d [%#x]",
    306      1.59       kre 		    fname, f, eflags));
    307      1.29  christos 		if (eflags)
    308      1.29  christos 			(void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);
    309      1.20  christos 		break;
    310      1.20  christos 	case NFROMTO:
    311      1.20  christos 		fname = redir->nfile.expfname;
    312  1.59.4.1  christos 		if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
    313      1.20  christos 			goto ecreate;
    314      1.59       kre 		VTRACE(DBG_REDIR, ("openredirect(<> '%s') -> %d", fname, f));
    315       1.1       cgd 		break;
    316       1.1       cgd 	case NTO:
    317      1.36  christos 		if (Cflag) {
    318      1.36  christos 			fname = redir->nfile.expfname;
    319      1.37  christos 			if ((f = open(fname, O_WRONLY)) == -1) {
    320      1.36  christos 				if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL,
    321      1.36  christos 				    0666)) < 0)
    322      1.36  christos 					goto ecreate;
    323      1.37  christos 			} else if (fstat(f, &sb) == -1) {
    324      1.37  christos 				int serrno = errno;
    325      1.37  christos 				close(f);
    326      1.37  christos 				errno = serrno;
    327      1.37  christos 				goto ecreate;
    328      1.37  christos 			} else if (S_ISREG(sb.st_mode)) {
    329      1.37  christos 				close(f);
    330      1.36  christos 				errno = EEXIST;
    331      1.36  christos 				goto ecreate;
    332      1.36  christos 			}
    333  1.59.4.1  christos 			VTRACE(DBG_REDIR, ("openredirect(>| '%s') -> %d",
    334  1.59.4.1  christos 			    fname, f));
    335      1.36  christos 			break;
    336      1.36  christos 		}
    337      1.23  christos 		/* FALLTHROUGH */
    338      1.23  christos 	case NCLOBBER:
    339       1.1       cgd 		fname = redir->nfile.expfname;
    340      1.36  christos 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
    341      1.20  christos 			goto ecreate;
    342      1.59       kre 		VTRACE(DBG_REDIR, ("openredirect(> '%s') -> %d", fname, f));
    343      1.20  christos 		break;
    344       1.1       cgd 	case NAPPEND:
    345       1.1       cgd 		fname = redir->nfile.expfname;
    346       1.1       cgd 		if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
    347      1.20  christos 			goto ecreate;
    348      1.59       kre 		VTRACE(DBG_REDIR, ("openredirect(>> '%s') -> %d", fname, f));
    349      1.20  christos 		break;
    350       1.1       cgd 	case NTOFD:
    351       1.1       cgd 	case NFROMFD:
    352       1.1       cgd 		if (redir->ndup.dupfd >= 0) {	/* if not ">&-" */
    353      1.43  christos 			if (fd < 10 && redir->ndup.dupfd < 10 &&
    354      1.43  christos 			    memory[redir->ndup.dupfd])
    355       1.1       cgd 				memory[fd] = 1;
    356      1.47       kre 			else if (copyfd(redir->ndup.dupfd, fd,
    357      1.49  christos 			    (flags & REDIR_KEEP) == 0) < 0)
    358      1.47       kre 				error("Redirect (from %d to %d) failed: %s",
    359      1.47       kre 				    redir->ndup.dupfd, fd, strerror(errno));
    360      1.59       kre 			VTRACE(DBG_REDIR, ("openredirect: %d%c&%d\n", fd,
    361      1.59       kre 			    "<>"[redir->nfile.type==NTOFD], redir->ndup.dupfd));
    362      1.59       kre 		} else {
    363      1.47       kre 			(void) close(fd);
    364      1.59       kre 			VTRACE(DBG_REDIR, ("openredirect: %d%c&-\n", fd,
    365      1.59       kre 			    "<>"[redir->nfile.type==NTOFD]));
    366      1.59       kre 		}
    367      1.20  christos 		INTON;
    368      1.20  christos 		return;
    369       1.1       cgd 	case NHERE:
    370       1.1       cgd 	case NXHERE:
    371      1.59       kre 		VTRACE(DBG_REDIR, ("openredirect: %d<<...", fd));
    372  1.59.4.1  christos 		f = openhere(redir);
    373      1.20  christos 		break;
    374       1.1       cgd 	default:
    375       1.1       cgd 		abort();
    376       1.1       cgd 	}
    377      1.20  christos 
    378      1.55       kre 	cloexec = fd > 2 && (flags & REDIR_KEEP) == 0 && !posix;
    379      1.20  christos 	if (f != fd) {
    380      1.59       kre 		VTRACE(DBG_REDIR, (" -> %d", fd));
    381      1.47       kre 		if (copyfd(f, fd, cloexec) < 0) {
    382      1.47       kre 			int e = errno;
    383      1.47       kre 
    384      1.47       kre 			close(f);
    385      1.47       kre 			error("redirect reassignment (fd %d) failed: %s", fd,
    386      1.47       kre 			    strerror(e));
    387      1.47       kre 		}
    388      1.20  christos 		close(f);
    389      1.42  christos 	} else if (cloexec)
    390      1.38  christos 		(void)fcntl(f, F_SETFD, FD_CLOEXEC);
    391      1.59       kre 	VTRACE(DBG_REDIR, ("%s\n", cloexec ? " cloexec" : ""));
    392      1.38  christos 
    393       1.1       cgd 	INTON;
    394      1.20  christos 	return;
    395  1.59.4.1  christos  ecreate:
    396      1.30   msaitoh 	exerrno = 1;
    397      1.20  christos 	error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    398  1.59.4.1  christos  eopen:
    399      1.30   msaitoh 	exerrno = 1;
    400      1.20  christos 	error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
    401       1.1       cgd }
    402       1.1       cgd 
    403       1.1       cgd 
    404       1.1       cgd /*
    405       1.1       cgd  * Handle here documents.  Normally we fork off a process to write the
    406       1.1       cgd  * data to a pipe.  If the document is short, we can stuff the data in
    407       1.1       cgd  * the pipe without forking.
    408       1.1       cgd  */
    409       1.1       cgd 
    410       1.1       cgd STATIC int
    411      1.34      yamt openhere(const union node *redir)
    412      1.27  christos {
    413       1.1       cgd 	int pip[2];
    414      1.12  christos 	int len = 0;
    415       1.1       cgd 
    416       1.1       cgd 	if (pipe(pip) < 0)
    417       1.1       cgd 		error("Pipe call failed");
    418       1.1       cgd 	if (redir->type == NHERE) {
    419       1.1       cgd 		len = strlen(redir->nhere.doc->narg.text);
    420       1.1       cgd 		if (len <= PIPESIZE) {
    421       1.1       cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    422       1.1       cgd 			goto out;
    423       1.1       cgd 		}
    424       1.1       cgd 	}
    425  1.59.4.1  christos 	VTRACE(DBG_REDIR, (" forking [%d,%d]\n", pip[0], pip[1]));
    426      1.32    plunky 	if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
    427       1.1       cgd 		close(pip[0]);
    428       1.1       cgd 		signal(SIGINT, SIG_IGN);
    429       1.1       cgd 		signal(SIGQUIT, SIG_IGN);
    430       1.1       cgd 		signal(SIGHUP, SIG_IGN);
    431       1.1       cgd #ifdef SIGTSTP
    432       1.1       cgd 		signal(SIGTSTP, SIG_IGN);
    433       1.1       cgd #endif
    434       1.1       cgd 		signal(SIGPIPE, SIG_DFL);
    435       1.1       cgd 		if (redir->type == NHERE)
    436       1.1       cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    437       1.1       cgd 		else
    438       1.1       cgd 			expandhere(redir->nhere.doc, pip[1]);
    439  1.59.4.1  christos 		VTRACE(DBG_PROCS|DBG_REDIR, ("wrote here doc.  exiting\n"));
    440       1.1       cgd 		_exit(0);
    441       1.1       cgd 	}
    442  1.59.4.1  christos 	VTRACE(DBG_REDIR, ("openhere (closing %d)", pip[1]));
    443  1.59.4.1  christos  out:
    444       1.1       cgd 	close(pip[1]);
    445  1.59.4.1  christos 	VTRACE(DBG_REDIR, (" (pipe fd=%d)", pip[0]));
    446       1.1       cgd 	return pip[0];
    447       1.1       cgd }
    448       1.1       cgd 
    449       1.1       cgd 
    450       1.1       cgd 
    451       1.1       cgd /*
    452       1.1       cgd  * Undo the effects of the last redirection.
    453       1.1       cgd  */
    454       1.1       cgd 
    455       1.1       cgd void
    456      1.27  christos popredir(void)
    457      1.27  christos {
    458      1.14       tls 	struct redirtab *rp = redirlist;
    459       1.1       cgd 
    460       1.1       cgd 	INTOFF;
    461      1.43  christos 	free_rl(rp, 1);
    462       1.1       cgd 	redirlist = rp->next;
    463       1.1       cgd 	ckfree(rp);
    464       1.1       cgd 	INTON;
    465       1.1       cgd }
    466       1.1       cgd 
    467       1.1       cgd /*
    468       1.1       cgd  * Undo all redirections.  Called on error or interrupt.
    469       1.1       cgd  */
    470       1.1       cgd 
    471       1.1       cgd #ifdef mkinit
    472       1.1       cgd 
    473       1.1       cgd INCLUDE "redir.h"
    474       1.1       cgd 
    475       1.1       cgd RESET {
    476       1.1       cgd 	while (redirlist)
    477       1.1       cgd 		popredir();
    478       1.1       cgd }
    479       1.1       cgd 
    480       1.1       cgd SHELLPROC {
    481      1.24  christos 	clearredir(0);
    482       1.1       cgd }
    483       1.1       cgd 
    484       1.1       cgd #endif
    485       1.1       cgd 
    486       1.7       jtc /* Return true if fd 0 has already been redirected at least once.  */
    487       1.7       jtc int
    488      1.43  christos fd0_redirected_p(void)
    489      1.43  christos {
    490      1.44       kre 	return fd0_redirected != 0;
    491       1.7       jtc }
    492       1.1       cgd 
    493       1.1       cgd /*
    494       1.1       cgd  * Discard all saved file descriptors.
    495       1.1       cgd  */
    496       1.1       cgd 
    497       1.1       cgd void
    498      1.33      matt clearredir(int vforked)
    499      1.24  christos {
    500      1.14       tls 	struct redirtab *rp;
    501      1.43  christos 	struct renamelist *rl;
    502       1.1       cgd 
    503       1.1       cgd 	for (rp = redirlist ; rp ; rp = rp->next) {
    504      1.43  christos 		if (!vforked)
    505      1.43  christos 			free_rl(rp, 0);
    506      1.43  christos 		else for (rl = rp->renamed; rl; rl = rl->next)
    507      1.43  christos 			if (rl->into >= 0)
    508      1.43  christos 				close(rl->into);
    509       1.1       cgd 	}
    510       1.1       cgd }
    511       1.1       cgd 
    512       1.1       cgd 
    513       1.1       cgd 
    514       1.1       cgd /*
    515      1.47       kre  * Copy a file descriptor to be == to.
    516      1.47       kre  * cloexec indicates if we want close-on-exec or not.
    517      1.47       kre  * Returns -1 if any error occurs.
    518       1.1       cgd  */
    519       1.1       cgd 
    520      1.47       kre STATIC int
    521      1.47       kre copyfd(int from, int to, int cloexec)
    522       1.9       cgd {
    523       1.1       cgd 	int newfd;
    524       1.1       cgd 
    525  1.59.4.1  christos 	if (cloexec && to > 2) {
    526  1.59.4.1  christos #ifdef O_CLOEXEC
    527      1.47       kre 		newfd = dup3(from, to, O_CLOEXEC);
    528  1.59.4.1  christos #else
    529  1.59.4.1  christos 		newfd = dup2(from, to);
    530  1.59.4.1  christos 		fcntl(newfd, F_SETFD, fcntl(newfd,F_GETFD) | FD_CLOEXEC);
    531  1.59.4.1  christos #endif
    532  1.59.4.1  christos 	} else
    533      1.47       kre 		newfd = dup2(from, to);
    534      1.47       kre 
    535       1.1       cgd 	return newfd;
    536       1.1       cgd }
    537      1.43  christos 
    538      1.47       kre /*
    539      1.47       kre  * rename fd from to be fd to (closing from).
    540      1.47       kre  * close-on-exec is never set on 'to' (unless
    541      1.47       kre  * from==to and it was set on from) - ie: a no-op
    542  1.59.4.1  christos  * returns to (or errors() if an error occurs).
    543      1.47       kre  *
    544      1.47       kre  * This is mostly used for rearranging the
    545      1.47       kre  * results from pipe().
    546      1.47       kre  */
    547      1.43  christos int
    548      1.43  christos movefd(int from, int to)
    549      1.43  christos {
    550      1.43  christos 	if (from == to)
    551      1.43  christos 		return to;
    552      1.43  christos 
    553      1.43  christos 	(void) close(to);
    554      1.47       kre 	if (copyfd(from, to, 0) != to) {
    555      1.47       kre 		int e = errno;
    556      1.47       kre 
    557      1.47       kre 		(void) close(from);
    558      1.47       kre 		error("Unable to make fd %d: %s", to, strerror(e));
    559      1.47       kre 	}
    560      1.43  christos 	(void) close(from);
    561      1.43  christos 
    562      1.43  christos 	return to;
    563      1.43  christos }
    564      1.43  christos 
    565      1.43  christos STATIC void
    566      1.43  christos find_big_fd(void)
    567      1.43  christos {
    568      1.43  christos 	int i, fd;
    569      1.57       kre 	static int last_start = 3; /* aim to keep sh fd's under 20 */
    570      1.43  christos 
    571      1.54       kre 	if (last_start < 10)
    572      1.54       kre 		last_start++;
    573      1.54       kre 
    574      1.54       kre 	for (i = (1 << last_start); i >= 10; i >>= 1) {
    575      1.43  christos 		if ((fd = fcntl(0, F_DUPFD, i - 1)) >= 0) {
    576      1.43  christos 			close(fd);
    577      1.43  christos 			break;
    578      1.43  christos 		}
    579      1.43  christos 	}
    580      1.43  christos 
    581      1.43  christos 	fd = (i / 5) * 4;
    582      1.54       kre 	if (fd < 10)
    583      1.43  christos 		fd = 10;
    584      1.43  christos 
    585      1.43  christos 	big_sh_fd = fd;
    586      1.43  christos }
    587      1.43  christos 
    588      1.47       kre /*
    589      1.47       kre  * If possible, move file descriptor fd out of the way
    590      1.47       kre  * of expected user fd values.   Returns the new fd
    591      1.47       kre  * (which may be the input fd if things do not go well.)
    592      1.47       kre  * Always set close-on-exec on the result, and close
    593      1.47       kre  * the input fd unless it is to be our result.
    594      1.47       kre  */
    595      1.43  christos int
    596      1.43  christos to_upper_fd(int fd)
    597      1.43  christos {
    598      1.43  christos 	int i;
    599      1.43  christos 
    600      1.59       kre 	VTRACE(DBG_REDIR|DBG_OUTPUT, ("to_upper_fd(%d)", fd));
    601      1.43  christos 	if (big_sh_fd < 10)
    602      1.43  christos 		find_big_fd();
    603      1.43  christos 	do {
    604      1.46       kre 		i = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd);
    605      1.43  christos 		if (i >= 0) {
    606      1.43  christos 			if (fd != i)
    607      1.43  christos 				close(fd);
    608      1.59       kre 			VTRACE(DBG_REDIR|DBG_OUTPUT, ("-> %d\n", i));
    609      1.43  christos 			return i;
    610      1.43  christos 		}
    611      1.54       kre 		if (errno != EMFILE && errno != EINVAL)
    612      1.43  christos 			break;
    613      1.43  christos 		find_big_fd();
    614      1.43  christos 	} while (big_sh_fd > 10);
    615      1.43  christos 
    616      1.46       kre 	/*
    617      1.47       kre 	 * If we wanted to move this fd to some random high number
    618      1.46       kre 	 * we certainly do not intend to pass it through exec, even
    619      1.46       kre 	 * if the reassignment failed.
    620      1.46       kre 	 */
    621      1.46       kre 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    622      1.59       kre 	VTRACE(DBG_REDIR|DBG_OUTPUT, (" fails ->%d\n", fd));
    623      1.43  christos 	return fd;
    624      1.43  christos }
    625      1.50  christos 
    626      1.54       kre void
    627      1.54       kre register_sh_fd(int fd, void (*cb)(int, int))
    628      1.54       kre {
    629      1.54       kre 	struct shell_fds *fp;
    630      1.54       kre 
    631      1.54       kre 	fp = ckmalloc(sizeof (struct shell_fds));
    632      1.54       kre 	if (fp != NULL) {
    633      1.54       kre 		fp->nxt = sh_fd_list;
    634      1.54       kre 		sh_fd_list = fp;
    635      1.54       kre 
    636      1.54       kre 		fp->fd = fd;
    637      1.54       kre 		fp->cb = cb;
    638      1.54       kre 	}
    639      1.54       kre }
    640      1.54       kre 
    641      1.54       kre void
    642      1.54       kre sh_close(int fd)
    643      1.54       kre {
    644      1.54       kre 	struct shell_fds **fpp, *fp;
    645      1.54       kre 
    646      1.54       kre 	fpp = &sh_fd_list;
    647      1.54       kre 	while ((fp = *fpp) != NULL) {
    648      1.54       kre 		if (fp->fd == fd) {
    649      1.54       kre 			*fpp = fp->nxt;
    650      1.54       kre 			ckfree(fp);
    651      1.54       kre 			break;
    652      1.54       kre 		}
    653      1.54       kre 		fpp = &fp->nxt;
    654      1.54       kre 	}
    655      1.54       kre 	(void)close(fd);
    656      1.54       kre }
    657      1.54       kre 
    658      1.54       kre STATIC struct shell_fds *
    659      1.54       kre sh_fd(int fd)
    660      1.54       kre {
    661      1.54       kre 	struct shell_fds *fp;
    662      1.54       kre 
    663      1.54       kre 	for (fp = sh_fd_list; fp != NULL; fp = fp->nxt)
    664      1.54       kre 		if (fp->fd == fd)
    665      1.54       kre 			return fp;
    666      1.54       kre 	return NULL;
    667      1.54       kre }
    668      1.54       kre 
    669      1.54       kre STATIC void
    670      1.54       kre renumber_sh_fd(struct shell_fds *fp)
    671      1.54       kre {
    672      1.54       kre 	int to;
    673      1.54       kre 
    674      1.54       kre 	if (fp == NULL)
    675      1.54       kre 		return;
    676      1.54       kre 
    677      1.57       kre 	/*
    678      1.57       kre 	 * if we have had a collision, and the sh fd was a "big" one
    679      1.57       kre 	 * try moving the sh fd base to a higher number (if possible)
    680      1.57       kre 	 * so future sh fds are less likely to be in the user's sights
    681      1.57       kre 	 * (incl this one when moved)
    682      1.57       kre 	 */
    683      1.57       kre 	if (fp->fd >= big_sh_fd)
    684      1.57       kre 		find_big_fd();
    685      1.57       kre 
    686      1.54       kre 	to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd);
    687      1.54       kre 	if (to == -1)
    688      1.54       kre 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2);
    689      1.54       kre 	if (to == -1)
    690      1.54       kre 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, fp->fd + 1);
    691      1.54       kre 	if (to == -1)
    692      1.54       kre 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 10);
    693      1.54       kre 	if (to == -1)
    694      1.54       kre 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 3);
    695      1.54       kre 	if (to == -1)
    696      1.54       kre 		error("insufficient file descriptors available");
    697      1.54       kre 	CLOEXEC(to);
    698      1.54       kre 
    699      1.54       kre 	if (fp->fd == to)	/* impossible? */
    700      1.54       kre 		return;
    701      1.54       kre 
    702      1.54       kre 	(*fp->cb)(fp->fd, to);
    703      1.54       kre 	(void)close(fp->fd);
    704      1.54       kre 	fp->fd = to;
    705      1.54       kre }
    706      1.54       kre 
    707      1.51       kre static const struct flgnames {
    708      1.50  christos 	const char *name;
    709      1.56       kre 	uint16_t minch;
    710      1.50  christos 	uint32_t value;
    711      1.50  christos } nv[] = {
    712      1.50  christos #ifdef O_APPEND
    713      1.56       kre 	{ "append",	2,	O_APPEND 	},
    714  1.59.4.1  christos #else
    715  1.59.4.1  christos # define O_APPEND 0
    716      1.50  christos #endif
    717      1.50  christos #ifdef O_ASYNC
    718      1.56       kre 	{ "async",	2,	O_ASYNC		},
    719  1.59.4.1  christos #else
    720  1.59.4.1  christos # define O_ASYNC 0
    721      1.50  christos #endif
    722      1.50  christos #ifdef O_SYNC
    723      1.56       kre 	{ "sync",	2,	O_SYNC		},
    724  1.59.4.1  christos #else
    725  1.59.4.1  christos # define O_SYNC 0
    726      1.50  christos #endif
    727      1.50  christos #ifdef O_NONBLOCK
    728      1.56       kre 	{ "nonblock",	3,	O_NONBLOCK	},
    729  1.59.4.1  christos #else
    730  1.59.4.1  christos # define O_NONBLOCK 0
    731      1.50  christos #endif
    732      1.50  christos #ifdef O_FSYNC
    733      1.56       kre 	{ "fsync",	2,	O_FSYNC		},
    734  1.59.4.1  christos #else
    735  1.59.4.1  christos # define O_FSYNC 0
    736      1.50  christos #endif
    737      1.50  christos #ifdef O_DSYNC
    738      1.56       kre 	{ "dsync",	2,	O_DSYNC		},
    739  1.59.4.1  christos #else
    740  1.59.4.1  christos # define O_DSYNC 0
    741      1.50  christos #endif
    742      1.50  christos #ifdef O_RSYNC
    743      1.56       kre 	{ "rsync",	2,	O_RSYNC		},
    744  1.59.4.1  christos #else
    745  1.59.4.1  christos # define O_RSYNC 0
    746      1.50  christos #endif
    747  1.59.4.1  christos #ifdef O_ALT_IO
    748      1.56       kre 	{ "altio",	2,	O_ALT_IO	},
    749  1.59.4.1  christos #else
    750  1.59.4.1  christos # define O_ALT_IO 0
    751      1.50  christos #endif
    752      1.50  christos #ifdef O_DIRECT
    753      1.56       kre 	{ "direct",	2,	O_DIRECT	},
    754  1.59.4.1  christos #else
    755  1.59.4.1  christos # define O_DIRECT 0
    756      1.50  christos #endif
    757      1.50  christos #ifdef O_NOSIGPIPE
    758      1.56       kre 	{ "nosigpipe",	3,	O_NOSIGPIPE	},
    759  1.59.4.1  christos #else
    760  1.59.4.1  christos # define O_NOSIGPIPE 0
    761      1.50  christos #endif
    762  1.59.4.1  christos 
    763  1.59.4.1  christos #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\
    764  1.59.4.1  christos     O_ALT_IO|O_DIRECT|O_NOSIGPIPE)
    765  1.59.4.1  christos 
    766  1.59.4.1  christos #ifndef	O_CLOEXEC
    767  1.59.4.1  christos # define O_CLOEXEC	((~ALLFLAGS) ^ ((~ALLFLAGS) & ((~ALLFLAGS) - 1)))
    768      1.50  christos #endif
    769  1.59.4.1  christos 
    770  1.59.4.1  christos 	/* for any system we support, close on exec is always defined */
    771  1.59.4.1  christos 	{ "cloexec",	2,	O_CLOEXEC	},
    772      1.56       kre 	{ 0, 0, 0 }
    773      1.50  christos };
    774  1.59.4.1  christos 
    775  1.59.4.1  christos #ifndef O_ACCMODE
    776  1.59.4.1  christos # define O_ACCMODE	0
    777  1.59.4.1  christos #endif
    778  1.59.4.1  christos #ifndef O_RDONLY
    779  1.59.4.1  christos # define O_RDONLY	0
    780  1.59.4.1  christos #endif
    781  1.59.4.1  christos #ifndef O_WRONLY
    782  1.59.4.1  christos # define O_WRONLY	0
    783  1.59.4.1  christos #endif
    784  1.59.4.1  christos #ifndef O_RWDR
    785  1.59.4.1  christos # define O_RWDR		0
    786  1.59.4.1  christos #endif
    787  1.59.4.1  christos #ifndef O_SHLOCK
    788  1.59.4.1  christos # define O_SHLOCK	0
    789  1.59.4.1  christos #endif
    790  1.59.4.1  christos #ifndef O_EXLOCK
    791  1.59.4.1  christos # define O_EXLOCK	0
    792  1.59.4.1  christos #endif
    793  1.59.4.1  christos #ifndef O_NOFOLLOW
    794  1.59.4.1  christos # define O_NOFOLLOW	0
    795  1.59.4.1  christos #endif
    796  1.59.4.1  christos #ifndef O_CREAT
    797  1.59.4.1  christos # define O_CREAT	0
    798  1.59.4.1  christos #endif
    799  1.59.4.1  christos #ifndef O_TRUNC
    800  1.59.4.1  christos # define O_TRUNC	0
    801  1.59.4.1  christos #endif
    802  1.59.4.1  christos #ifndef O_EXCL
    803  1.59.4.1  christos # define O_EXCL		0
    804  1.59.4.1  christos #endif
    805  1.59.4.1  christos #ifndef O_NOCTTY
    806  1.59.4.1  christos # define O_NOCTTY	0
    807  1.59.4.1  christos #endif
    808  1.59.4.1  christos #ifndef O_DIRECTORY
    809  1.59.4.1  christos # define O_DIRECTORY	0
    810  1.59.4.1  christos #endif
    811  1.59.4.1  christos #ifndef O_REGULAR
    812  1.59.4.1  christos # define O_REGULAR	0
    813  1.59.4.1  christos #endif
    814  1.59.4.1  christos /*
    815  1.59.4.1  christos  * flags that F_GETFL might return that we want to ignore
    816  1.59.4.1  christos  *
    817  1.59.4.1  christos  * F_GETFL should not actually return these, they're all just open()
    818  1.59.4.1  christos  * modifiers, rather than state, but just in case...
    819  1.59.4.1  christos  */
    820  1.59.4.1  christos #define IGNFLAGS (O_ACCMODE|O_RDONLY|O_WRONLY|O_RDWR|O_SHLOCK|O_EXLOCK| \
    821  1.59.4.1  christos     O_NOFOLLOW|O_CREAT|O_TRUNC|O_EXCL|O_NOCTTY|O_DIRECTORY|O_REGULAR)
    822      1.50  christos 
    823      1.50  christos static int
    824      1.50  christos getflags(int fd, int p)
    825      1.50  christos {
    826      1.50  christos 	int c, f;
    827      1.50  christos 
    828      1.54       kre 	if (sh_fd(fd) != NULL) {
    829      1.54       kre 		if (!p)
    830      1.54       kre 			return -1;
    831      1.54       kre 		error("Can't get status for fd=%d (%s)", fd,
    832      1.54       kre 		    "Bad file descriptor");			/*XXX*/
    833      1.54       kre 	}
    834      1.54       kre 
    835      1.50  christos 	if ((c = fcntl(fd, F_GETFD)) == -1) {
    836      1.50  christos 		if (!p)
    837      1.50  christos 			return -1;
    838      1.50  christos 		error("Can't get status for fd=%d (%s)", fd, strerror(errno));
    839      1.50  christos 	}
    840      1.50  christos 	if ((f = fcntl(fd, F_GETFL)) == -1) {
    841      1.50  christos 		if (!p)
    842      1.50  christos 			return -1;
    843      1.50  christos 		error("Can't get flags for fd=%d (%s)", fd, strerror(errno));
    844      1.50  christos 	}
    845  1.59.4.1  christos 	f &= ~IGNFLAGS;		/* clear anything we know about, but ignore */
    846      1.51       kre 	if (c & FD_CLOEXEC)
    847      1.50  christos 		f |= O_CLOEXEC;
    848  1.59.4.1  christos 	return f;
    849      1.50  christos }
    850      1.50  christos 
    851      1.50  christos static void
    852      1.51       kre printone(int fd, int p, int verbose, int pfd)
    853      1.50  christos {
    854      1.50  christos 	int f = getflags(fd, p);
    855      1.51       kre 	const struct flgnames *fn;
    856      1.50  christos 
    857      1.50  christos 	if (f == -1)
    858      1.50  christos 		return;
    859      1.50  christos 
    860      1.51       kre 	if (pfd)
    861      1.51       kre 		outfmt(out1, "%d: ", fd);
    862      1.51       kre 	for (fn = nv; fn->name; fn++) {
    863      1.51       kre 		if (f & fn->value) {
    864      1.51       kre 			outfmt(out1, "%s%s", verbose ? "+" : "", fn->name);
    865      1.51       kre 			f &= ~fn->value;
    866      1.50  christos 		} else if (verbose)
    867      1.51       kre 			outfmt(out1, "-%s", fn->name);
    868      1.50  christos 		else
    869      1.50  christos 			continue;
    870      1.51       kre 		if (f || (verbose && fn[1].name))
    871      1.50  christos 			outfmt(out1, ",");
    872      1.50  christos 	}
    873      1.51       kre 	if (verbose && f)		/* f should be normally be 0 */
    874      1.51       kre 		outfmt(out1, " +%#x", f);
    875      1.50  christos 	outfmt(out1, "\n");
    876      1.50  christos }
    877      1.50  christos 
    878      1.51       kre static void
    879      1.50  christos parseflags(char *s, int *p, int *n)
    880      1.50  christos {
    881      1.51       kre 	int *v, *w;
    882      1.51       kre 	const struct flgnames *fn;
    883      1.56       kre 	size_t len;
    884      1.50  christos 
    885      1.50  christos 	*p = 0;
    886      1.50  christos 	*n = 0;
    887      1.50  christos 	for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
    888      1.51       kre 		switch (*s++) {
    889      1.50  christos 		case '+':
    890      1.50  christos 			v = p;
    891      1.51       kre 			w = n;
    892      1.50  christos 			break;
    893      1.50  christos 		case '-':
    894      1.50  christos 			v = n;
    895      1.51       kre 			w = p;
    896      1.50  christos 			break;
    897      1.50  christos 		default:
    898      1.51       kre 			error("Missing +/- indicator before flag %s", s-1);
    899      1.50  christos 		}
    900  1.59.4.1  christos 
    901      1.56       kre 		len = strlen(s);
    902      1.51       kre 		for (fn = nv; fn->name; fn++)
    903      1.56       kre 			if (len >= fn->minch && strncmp(s,fn->name,len) == 0) {
    904      1.51       kre 				*v |= fn->value;
    905      1.51       kre 				*w &=~ fn->value;
    906      1.50  christos 				break;
    907      1.50  christos 			}
    908      1.51       kre 		if (fn->name == 0)
    909      1.50  christos 			error("Bad flag `%s'", s);
    910      1.50  christos 	}
    911      1.50  christos }
    912      1.50  christos 
    913      1.50  christos static void
    914      1.50  christos setone(int fd, int pos, int neg, int verbose)
    915      1.50  christos {
    916      1.50  christos 	int f = getflags(fd, 1);
    917      1.51       kre 	int n, cloexec;
    918      1.51       kre 
    919      1.50  christos 	if (f == -1)
    920      1.50  christos 		return;
    921      1.50  christos 
    922      1.51       kre 	cloexec = -1;
    923      1.50  christos 	if ((pos & O_CLOEXEC) && !(f & O_CLOEXEC))
    924      1.50  christos 		cloexec = FD_CLOEXEC;
    925      1.50  christos 	if ((neg & O_CLOEXEC) && (f & O_CLOEXEC))
    926      1.50  christos 		cloexec = 0;
    927      1.50  christos 
    928      1.50  christos 	if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1)
    929      1.50  christos 		error("Can't set status for fd=%d (%s)", fd, strerror(errno));
    930      1.50  christos 
    931      1.50  christos 	pos &= ~O_CLOEXEC;
    932      1.50  christos 	neg &= ~O_CLOEXEC;
    933      1.50  christos 	f &= ~O_CLOEXEC;
    934      1.51       kre 	n = f;
    935      1.50  christos 	n |= pos;
    936      1.50  christos 	n &= ~neg;
    937      1.50  christos 	if (n != f && fcntl(fd, F_SETFL, n) == -1)
    938      1.50  christos 		error("Can't set flags for fd=%d (%s)", fd, strerror(errno));
    939      1.50  christos 	if (verbose)
    940      1.51       kre 		printone(fd, 1, verbose, 1);
    941      1.50  christos }
    942      1.50  christos 
    943      1.50  christos int
    944      1.50  christos fdflagscmd(int argc, char *argv[])
    945      1.50  christos {
    946      1.50  christos 	char *num;
    947      1.50  christos 	int verbose = 0, ch, pos = 0, neg = 0;
    948      1.50  christos 	char *setflags = NULL;
    949      1.50  christos 
    950      1.50  christos 	optreset = 1; optind = 1; /* initialize getopt */
    951      1.50  christos 	while ((ch = getopt(argc, argv, ":vs:")) != -1)
    952      1.50  christos 		switch ((char)ch) {
    953      1.50  christos 		case 'v':
    954      1.50  christos 			verbose = 1;
    955      1.50  christos 			break;
    956      1.50  christos 		case 's':
    957      1.51       kre 			if (setflags)
    958      1.51       kre 				goto msg;
    959      1.50  christos 			setflags = optarg;
    960      1.50  christos 			break;
    961      1.50  christos 		case '?':
    962      1.50  christos 		default:
    963      1.50  christos 		msg:
    964      1.51       kre 			error("Usage: fdflags [-v] [-s <flags> fd] [fd...]");
    965      1.50  christos 			/* NOTREACHED */
    966      1.50  christos 		}
    967      1.50  christos 
    968      1.50  christos 	argc -= optind, argv += optind;
    969      1.50  christos 
    970      1.51       kre 	if (setflags)
    971      1.51       kre 		parseflags(setflags, &pos, &neg);
    972      1.50  christos 
    973      1.50  christos 	if (argc == 0) {
    974      1.53       kre 		int i;
    975      1.51       kre 
    976      1.50  christos 		if (setflags)
    977      1.50  christos 			goto msg;
    978      1.51       kre 
    979      1.53       kre 		for (i = 0; i <= max_user_fd; i++)
    980      1.51       kre 			printone(i, 0, verbose, 1);
    981      1.50  christos 		return 0;
    982      1.50  christos 	}
    983      1.50  christos 
    984      1.50  christos 	while ((num = *argv++) != NULL) {
    985      1.51       kre 		int fd = number(num);
    986      1.51       kre 
    987      1.52       kre 		while (num[0] == '0' && num[1] != '\0')		/* skip 0's */
    988      1.52       kre 			num++;
    989      1.51       kre 		if (strlen(num) > 5)
    990      1.51       kre 			error("%s too big to be a file descriptor", num);
    991      1.51       kre 
    992      1.50  christos 		if (setflags)
    993      1.50  christos 			setone(fd, pos, neg, verbose);
    994      1.50  christos 		else
    995      1.51       kre 			printone(fd, 1, verbose, argc > 1);
    996      1.50  christos 	}
    997      1.50  christos 	return 0;
    998      1.50  christos }
    999      1.58       kre 
   1000      1.58       kre #undef MAX		/* in case we inherited them from somewhere */
   1001      1.58       kre #undef MIN
   1002      1.58       kre 
   1003      1.58       kre #define	MIN(a,b)	(/*CONSTCOND*/((a)<=(b)) ? (a) : (b))
   1004      1.58       kre #define	MAX(a,b)	(/*CONSTCOND*/((a)>=(b)) ? (a) : (b))
   1005      1.58       kre 
   1006      1.58       kre 		/* now make the compiler work for us... */
   1007      1.58       kre #define	MIN_REDIR	MIN(MIN(MIN(MIN(NTO,NFROM), MIN(NTOFD,NFROMFD)), \
   1008      1.58       kre 		   MIN(MIN(NCLOBBER,NAPPEND), MIN(NHERE,NXHERE))), NFROMTO)
   1009      1.58       kre #define	MAX_REDIR	MAX(MAX(MAX(MAX(NTO,NFROM), MAX(NTOFD,NFROMFD)), \
   1010      1.58       kre 		   MAX(MAX(NCLOBBER,NAPPEND), MAX(NHERE,NXHERE))), NFROMTO)
   1011      1.58       kre 
   1012      1.58       kre static const char *redir_sym[MAX_REDIR - MIN_REDIR + 1] = {
   1013      1.58       kre 	[NTO      - MIN_REDIR]=	">",
   1014      1.58       kre 	[NFROM    - MIN_REDIR]=	"<",
   1015      1.58       kre 	[NTOFD    - MIN_REDIR]=	">&",
   1016      1.58       kre 	[NFROMFD  - MIN_REDIR]=	"<&",
   1017      1.58       kre 	[NCLOBBER - MIN_REDIR]=	">|",
   1018      1.58       kre 	[NAPPEND  - MIN_REDIR]=	">>",
   1019      1.58       kre 	[NHERE    - MIN_REDIR]=	"<<",
   1020      1.58       kre 	[NXHERE   - MIN_REDIR]=	"<<",
   1021      1.58       kre 	[NFROMTO  - MIN_REDIR]=	"<>",
   1022      1.58       kre };
   1023      1.58       kre 
   1024      1.58       kre int
   1025      1.58       kre outredir(struct output *out, union node *n, int sep)
   1026      1.58       kre {
   1027      1.58       kre 	if (n == NULL)
   1028      1.58       kre 		return 0;
   1029      1.58       kre 	if (n->type < MIN_REDIR || n->type > MAX_REDIR ||
   1030      1.58       kre 	    redir_sym[n->type - MIN_REDIR] == NULL)
   1031      1.58       kre 		return 0;
   1032      1.58       kre 
   1033      1.58       kre 	if (sep)
   1034      1.58       kre 		outc(sep, out);
   1035      1.58       kre 
   1036      1.58       kre 	/*
   1037      1.58       kre 	 * ugly, but all redir node types have "fd" in same slot...
   1038      1.58       kre 	 *	(and code other places assumes it as well)
   1039      1.58       kre 	 */
   1040      1.58       kre 	if ((redir_sym[n->type - MIN_REDIR][0] == '<' && n->nfile.fd != 0) ||
   1041      1.58       kre 	    (redir_sym[n->type - MIN_REDIR][0] == '>' && n->nfile.fd != 1))
   1042      1.58       kre 		outfmt(out, "%d", n->nfile.fd);
   1043      1.58       kre 
   1044      1.58       kre 	outstr(redir_sym[n->type - MIN_REDIR], out);
   1045      1.58       kre 
   1046      1.58       kre 	switch (n->type) {
   1047      1.58       kre 	case NHERE:
   1048      1.58       kre 		outstr("'...'", out);
   1049      1.58       kre 		break;
   1050      1.58       kre 	case NXHERE:
   1051      1.58       kre 		outstr("...", out);
   1052      1.58       kre 		break;
   1053      1.58       kre 	case NTOFD:
   1054      1.58       kre 	case NFROMFD:
   1055      1.58       kre 		if (n->ndup.dupfd < 0)
   1056      1.58       kre 			outc('-', out);
   1057      1.58       kre 		else
   1058      1.58       kre 			outfmt(out, "%d", n->ndup.dupfd);
   1059      1.58       kre 		break;
   1060      1.58       kre 	default:
   1061      1.58       kre 		outstr(n->nfile.expfname, out);
   1062      1.58       kre 		break;
   1063      1.58       kre 	}
   1064      1.58       kre 	return 1;
   1065      1.58       kre }
   1066