xref: /illumos-gate/usr/src/cmd/sh/io.c (revision 1c8fe102)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51573d361Snakanon  * Common Development and Distribution License (the "License").
61573d361Snakanon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
231573d361Snakanon  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24965005c8Schin  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27965005c8Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28965005c8Schin /*	  All Rights Reserved  	*/
29965005c8Schin 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * UNIX shell
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	"defs.h"
357c478bd9Sstevel@tonic-gate #include	"dup.h"
36965005c8Schin #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<fcntl.h>
387c478bd9Sstevel@tonic-gate #include	<sys/types.h>
397c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate short topfd;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* ========	input output and file copying ======== */
457c478bd9Sstevel@tonic-gate 
46965005c8Schin void
initf(int fd)47965005c8Schin initf(int fd)
487c478bd9Sstevel@tonic-gate {
49965005c8Schin 	struct fileblk *f = standin;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	f->fdes = fd;
527c478bd9Sstevel@tonic-gate 	f->fsiz = ((flags & oneflg) == 0 ? BUFFERSIZE : 1);
537c478bd9Sstevel@tonic-gate 	f->fnxt = f->fend = f->fbuf;
547c478bd9Sstevel@tonic-gate 	f->nxtoff = f->endoff = 0;
557c478bd9Sstevel@tonic-gate 	f->feval = 0;
567c478bd9Sstevel@tonic-gate 	f->flin = 1;
577c478bd9Sstevel@tonic-gate 	f->feof = FALSE;
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
60965005c8Schin int
estabf(unsigned char * s)61965005c8Schin estabf(unsigned char *s)
627c478bd9Sstevel@tonic-gate {
63965005c8Schin 	struct fileblk *f;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	(f = standin)->fdes = -1;
667c478bd9Sstevel@tonic-gate 	f->fend = length(s) + (f->fnxt = s);
677c478bd9Sstevel@tonic-gate 	f->nxtoff = 0;
687c478bd9Sstevel@tonic-gate 	f->endoff = length(s);
697c478bd9Sstevel@tonic-gate 	f->flin = 1;
707c478bd9Sstevel@tonic-gate 	return (f->feof = (s == 0));
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
73965005c8Schin void
push(struct fileblk * af)74965005c8Schin push(struct fileblk *af)
757c478bd9Sstevel@tonic-gate {
76965005c8Schin 	struct fileblk *f;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	(f = af)->fstak = standin;
797c478bd9Sstevel@tonic-gate 	f->feof = 0;
807c478bd9Sstevel@tonic-gate 	f->feval = 0;
817c478bd9Sstevel@tonic-gate 	standin = f;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
84965005c8Schin int
pop(void)85965005c8Schin pop(void)
867c478bd9Sstevel@tonic-gate {
87965005c8Schin 	struct fileblk *f;
887c478bd9Sstevel@tonic-gate 
89*1c8fe102SWilliam Roche 	if ((f = standin)->fstak) {
907c478bd9Sstevel@tonic-gate 		if (f->fdes >= 0)
917c478bd9Sstevel@tonic-gate 			close(f->fdes);
927c478bd9Sstevel@tonic-gate 		standin = f->fstak;
937c478bd9Sstevel@tonic-gate 		return (TRUE);
94*1c8fe102SWilliam Roche 	} else
957c478bd9Sstevel@tonic-gate 		return (FALSE);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate struct tempblk *tmpfptr;
997c478bd9Sstevel@tonic-gate 
100965005c8Schin void
pushtemp(int fd,struct tempblk * tb)101965005c8Schin pushtemp(int fd, struct tempblk *tb)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	tb->fdes = fd;
1047c478bd9Sstevel@tonic-gate 	tb->fstak = tmpfptr;
1057c478bd9Sstevel@tonic-gate 	tmpfptr = tb;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
108965005c8Schin int
poptemp(void)109965005c8Schin poptemp(void)
1107c478bd9Sstevel@tonic-gate {
111*1c8fe102SWilliam Roche 	if (tmpfptr) {
1127c478bd9Sstevel@tonic-gate 		close(tmpfptr->fdes);
1137c478bd9Sstevel@tonic-gate 		tmpfptr = tmpfptr->fstak;
1147c478bd9Sstevel@tonic-gate 		return (TRUE);
115*1c8fe102SWilliam Roche 	} else
1167c478bd9Sstevel@tonic-gate 		return (FALSE);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
119965005c8Schin void
chkpipe(int * pv)120965005c8Schin chkpipe(int *pv)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	if (pipe(pv) < 0 || pv[INPIPE] < 0 || pv[OTPIPE] < 0)
1237c478bd9Sstevel@tonic-gate 		error(piperr);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
126965005c8Schin int
chkopen(unsigned char * idf,int mode)127965005c8Schin chkopen(unsigned char *idf, int mode)
1287c478bd9Sstevel@tonic-gate {
129965005c8Schin 	int	rc;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((rc = open((char *)idf, mode, 0666)) < 0)
1327c478bd9Sstevel@tonic-gate 		failed(idf, badopen);
1337c478bd9Sstevel@tonic-gate 	else
1347c478bd9Sstevel@tonic-gate 		return (rc);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Make f2 be a synonym (including the close-on-exec flag) for f1, which is
1397c478bd9Sstevel@tonic-gate  * then closed.  If f2 is descriptor 0, modify the global ioset variable
1407c478bd9Sstevel@tonic-gate  * accordingly.
1417c478bd9Sstevel@tonic-gate  */
142965005c8Schin void
renamef(int f1,int f2)143965005c8Schin renamef(int f1, int f2)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate #ifdef RES
146*1c8fe102SWilliam Roche 	if (f1 != f2) {
1477c478bd9Sstevel@tonic-gate 		dup(f1 | DUPFLG, f2);
1487c478bd9Sstevel@tonic-gate 		close(f1);
1497c478bd9Sstevel@tonic-gate 		if (f2 == 0)
1507c478bd9Sstevel@tonic-gate 			ioset |= 1;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate #else
1537c478bd9Sstevel@tonic-gate 	int	fs;
1547c478bd9Sstevel@tonic-gate 
155*1c8fe102SWilliam Roche 	if (f1 != f2) {
1567c478bd9Sstevel@tonic-gate 		fs = fcntl(f2, 1, 0);
1577c478bd9Sstevel@tonic-gate 		close(f2);
1587c478bd9Sstevel@tonic-gate 		fcntl(f1, 0, f2);
1597c478bd9Sstevel@tonic-gate 		close(f1);
1607c478bd9Sstevel@tonic-gate 		if (fs == 1)
1617c478bd9Sstevel@tonic-gate 			fcntl(f2, 2, 1);
1627c478bd9Sstevel@tonic-gate 		if (f2 == 0)
1637c478bd9Sstevel@tonic-gate 			ioset |= 1;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
168965005c8Schin int
create(unsigned char * s)169965005c8Schin create(unsigned char *s)
1707c478bd9Sstevel@tonic-gate {
171965005c8Schin 	int	rc;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if ((rc = creat((char *)s, 0666)) < 0)
1747c478bd9Sstevel@tonic-gate 		failed(s, badcreate);
1757c478bd9Sstevel@tonic-gate 	else
1767c478bd9Sstevel@tonic-gate 		return (rc);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
180965005c8Schin int
tmpfil(struct tempblk * tb)181965005c8Schin tmpfil(struct tempblk *tb)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int fd;
184965005c8Schin 	int len;
185965005c8Schin 	size_t size_left = TMPOUTSZ - tmpout_offset;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* make sure tmp file does not already exist. */
1887c478bd9Sstevel@tonic-gate 	do {
189965005c8Schin 		len = snprintf((char *)&tmpout[tmpout_offset], size_left,
190965005c8Schin 		    "%u", serial);
191965005c8Schin 		fd = open((char *)tmpout, O_RDWR|O_CREAT|O_EXCL, 0600);
192965005c8Schin 		serial++;
193965005c8Schin 		if ((serial >= UINT_MAX) || (len >= size_left)) {
194965005c8Schin 			/*
195965005c8Schin 			 * We've already cycled through all the possible
196965005c8Schin 			 * numbers or the tmp file name is being
197965005c8Schin 			 * truncated anyway (although TMPOUTSZ should be
198965005c8Schin 			 * big enough), so start over.
199965005c8Schin 			 */
200965005c8Schin 			serial = 0;
201965005c8Schin 			break;
202965005c8Schin 		}
2037c478bd9Sstevel@tonic-gate 	} while ((fd == -1) && (errno == EEXIST));
2047c478bd9Sstevel@tonic-gate 	if (fd != -1) {
2057c478bd9Sstevel@tonic-gate 		pushtemp(fd, tb);
2067c478bd9Sstevel@tonic-gate 		return (fd);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	else
2097c478bd9Sstevel@tonic-gate 		failed(tmpout, badcreate);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * set by trim
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate extern BOOL		nosubst;
2167c478bd9Sstevel@tonic-gate #define			CPYSIZ		512
2177c478bd9Sstevel@tonic-gate 
218965005c8Schin void
copy(struct ionod * ioparg)219965005c8Schin copy(struct ionod	*ioparg)
2207c478bd9Sstevel@tonic-gate {
221965005c8Schin 	unsigned char	*cline;
222965005c8Schin 	unsigned char	*clinep;
223965005c8Schin 	struct ionod	*iop;
2247c478bd9Sstevel@tonic-gate 	unsigned int	c;
2257c478bd9Sstevel@tonic-gate 	unsigned char	*ends;
2267c478bd9Sstevel@tonic-gate 	unsigned char	*start;
2277c478bd9Sstevel@tonic-gate 	int		fd;
2287c478bd9Sstevel@tonic-gate 	int		i;
2297c478bd9Sstevel@tonic-gate 	int		stripflg;
2307c478bd9Sstevel@tonic-gate 	unsigned char	*pc;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 
233*1c8fe102SWilliam Roche 	if (iop = ioparg) {
2347c478bd9Sstevel@tonic-gate 		struct tempblk tb;
2357c478bd9Sstevel@tonic-gate 		copy(iop->iolst);
2367c478bd9Sstevel@tonic-gate 		ends = mactrim(iop->ioname);
2377c478bd9Sstevel@tonic-gate 		stripflg = iop->iofile & IOSTRIP;
2387c478bd9Sstevel@tonic-gate 		if (nosubst)
2391573d361Snakanon 			iop->iofile &= ~IODOC_SUBST;
2407c478bd9Sstevel@tonic-gate 		fd = tmpfil(&tb);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		if (fndef)
243*1c8fe102SWilliam Roche 			iop->ioname = (char *)make(tmpout);
2447c478bd9Sstevel@tonic-gate 		else
245*1c8fe102SWilliam Roche 			iop->ioname = (char *)cpystak(tmpout);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		iop->iolst = iotemp;
2487c478bd9Sstevel@tonic-gate 		iotemp = iop;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		cline = clinep = start = locstak();
251*1c8fe102SWilliam Roche 		if (stripflg) {
2527c478bd9Sstevel@tonic-gate 			iop->iofile &= ~IOSTRIP;
2537c478bd9Sstevel@tonic-gate 			while (*ends == '\t')
2547c478bd9Sstevel@tonic-gate 				ends++;
2557c478bd9Sstevel@tonic-gate 		}
256*1c8fe102SWilliam Roche 		for (;;) {
2577c478bd9Sstevel@tonic-gate 			chkpr();
258*1c8fe102SWilliam Roche 			if (nosubst) {
2597c478bd9Sstevel@tonic-gate 				c = readwc();
2607c478bd9Sstevel@tonic-gate 				if (stripflg)
2617c478bd9Sstevel@tonic-gate 					while (c == '\t')
2627c478bd9Sstevel@tonic-gate 						c = readwc();
2637c478bd9Sstevel@tonic-gate 
264*1c8fe102SWilliam Roche 				while (!eolchar(c)) {
2657c478bd9Sstevel@tonic-gate 					pc = readw(c);
2667c478bd9Sstevel@tonic-gate 					while (*pc) {
2677c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
2687c478bd9Sstevel@tonic-gate 							growstak(clinep);
2697c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
2707c478bd9Sstevel@tonic-gate 					}
2717c478bd9Sstevel@tonic-gate 					c = readwc();
2727c478bd9Sstevel@tonic-gate 				}
273*1c8fe102SWilliam Roche 			} else {
2747c478bd9Sstevel@tonic-gate 				c = nextwc();
2757c478bd9Sstevel@tonic-gate 				if (stripflg)
2767c478bd9Sstevel@tonic-gate 					while (c == '\t')
2777c478bd9Sstevel@tonic-gate 						c = nextwc();
2787c478bd9Sstevel@tonic-gate 
279*1c8fe102SWilliam Roche 				while (!eolchar(c)) {
2807c478bd9Sstevel@tonic-gate 					pc = readw(c);
2817c478bd9Sstevel@tonic-gate 					while (*pc) {
2827c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
2837c478bd9Sstevel@tonic-gate 							growstak(clinep);
2847c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
2857c478bd9Sstevel@tonic-gate 					}
286*1c8fe102SWilliam Roche 					if (c == '\\') {
2877c478bd9Sstevel@tonic-gate 						pc = readw(readwc());
2887c478bd9Sstevel@tonic-gate 						/* *pc might be NULL */
289*1c8fe102SWilliam Roche 						/* BEGIN CSTYLED */
2907c478bd9Sstevel@tonic-gate 						if (*pc) {
2917c478bd9Sstevel@tonic-gate 							while (*pc) {
2927c478bd9Sstevel@tonic-gate 								if (clinep >= brkend)
2937c478bd9Sstevel@tonic-gate 									growstak(clinep);
2947c478bd9Sstevel@tonic-gate 								*clinep++ = *pc++;
2957c478bd9Sstevel@tonic-gate 							}
2967c478bd9Sstevel@tonic-gate 						} else {
2977c478bd9Sstevel@tonic-gate 							if (clinep >= brkend)
2987c478bd9Sstevel@tonic-gate 								growstak(clinep);
2997c478bd9Sstevel@tonic-gate 							*clinep++ = *pc;
3007c478bd9Sstevel@tonic-gate 						}
301*1c8fe102SWilliam Roche 						/* END CSTYLED */
3027c478bd9Sstevel@tonic-gate 					}
3037c478bd9Sstevel@tonic-gate 					c = nextwc();
3047c478bd9Sstevel@tonic-gate 				}
3057c478bd9Sstevel@tonic-gate 			}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 			if (clinep >= brkend)
3087c478bd9Sstevel@tonic-gate 				growstak(clinep);
3097c478bd9Sstevel@tonic-gate 			*clinep = 0;
310*1c8fe102SWilliam Roche 			if (eof || eq(cline, ends)) {
3117c478bd9Sstevel@tonic-gate 				if ((i = cline - start) > 0)
3127c478bd9Sstevel@tonic-gate 					write(fd, start, i);
3137c478bd9Sstevel@tonic-gate 				break;
314*1c8fe102SWilliam Roche 			} else {
3157c478bd9Sstevel@tonic-gate 				if (clinep >= brkend)
3167c478bd9Sstevel@tonic-gate 					growstak(clinep);
3177c478bd9Sstevel@tonic-gate 				*clinep++ = NL;
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 			if ((i = clinep - start) < CPYSIZ)
3217c478bd9Sstevel@tonic-gate 				cline = clinep;
3227c478bd9Sstevel@tonic-gate 			else
3237c478bd9Sstevel@tonic-gate 			{
3247c478bd9Sstevel@tonic-gate 				write(fd, start, i);
3257c478bd9Sstevel@tonic-gate 				cline = clinep = start;
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
329*1c8fe102SWilliam Roche 		/*
330*1c8fe102SWilliam Roche 		 * Pushed in tmpfil -- bug fix for problem
331*1c8fe102SWilliam Roche 		 * deleting in-line script.
332*1c8fe102SWilliam Roche 		 */
333*1c8fe102SWilliam Roche 		poptemp();
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
337965005c8Schin void
link_iodocs(struct ionod * i)338965005c8Schin link_iodocs(struct ionod *i)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	int r;
341965005c8Schin 	int len;
342965005c8Schin 	size_t size_left = TMPOUTSZ - tmpout_offset;
3437c478bd9Sstevel@tonic-gate 
344965005c8Schin 	while (i) {
3457c478bd9Sstevel@tonic-gate 		free(i->iolink);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		/* make sure tmp file does not already exist. */
3487c478bd9Sstevel@tonic-gate 		do {
349965005c8Schin 			len = snprintf((char *)&tmpout[tmpout_offset],
350965005c8Schin 			    size_left, "%u", serial);
351965005c8Schin 			serial++;
3527c478bd9Sstevel@tonic-gate 			r = link(i->ioname, (char *)tmpout);
353965005c8Schin 			if ((serial >= UINT_MAX) || (len >= size_left)) {
354965005c8Schin 			/*
355965005c8Schin 			 * We've already cycled through all the possible
356965005c8Schin 			 * numbers or the tmp file name is being
357*1c8fe102SWilliam Roche 			 * truncated anyway, so start over.
358965005c8Schin 			 */
359965005c8Schin 				serial = 0;
360965005c8Schin 				break;
361965005c8Schin 			}
3627c478bd9Sstevel@tonic-gate 		} while (r == -1 && errno == EEXIST);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		if (r != -1) {
3657c478bd9Sstevel@tonic-gate 			i->iolink = (char *)make(tmpout);
3667c478bd9Sstevel@tonic-gate 			i = i->iolst;
3677c478bd9Sstevel@tonic-gate 		} else
3687c478bd9Sstevel@tonic-gate 			failed(tmpout, badcreate);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
373965005c8Schin void
swap_iodoc_nm(struct ionod * i)374965005c8Schin swap_iodoc_nm(struct ionod *i)
3757c478bd9Sstevel@tonic-gate {
376*1c8fe102SWilliam Roche 	while (i) {
3777c478bd9Sstevel@tonic-gate 		free(i->ioname);
3787c478bd9Sstevel@tonic-gate 		i->ioname = i->iolink;
3797c478bd9Sstevel@tonic-gate 		i->iolink = 0;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		i = i->iolst;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
385965005c8Schin int
savefd(int fd)386965005c8Schin savefd(int fd)
3877c478bd9Sstevel@tonic-gate {
388965005c8Schin 	int	f;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	f = fcntl(fd, F_DUPFD, 10);
391*1c8fe102SWilliam Roche 	/* this saved fd should not be found in an exec'ed cmd */
392*1c8fe102SWilliam Roche 	(void) fcntl(f, F_SETFD, FD_CLOEXEC);
3937c478bd9Sstevel@tonic-gate 	return (f);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
396965005c8Schin void
restore(int last)397965005c8Schin restore(int last)
3987c478bd9Sstevel@tonic-gate {
399965005c8Schin 	int 	i;
400965005c8Schin 	int	dupfd;
4017c478bd9Sstevel@tonic-gate 
402*1c8fe102SWilliam Roche 	for (i = topfd - 1; i >= last; i--) {
4037c478bd9Sstevel@tonic-gate 		if ((dupfd = fdmap[i].dup_fd) > 0)
4047c478bd9Sstevel@tonic-gate 			renamef(dupfd, fdmap[i].org_fd);
4057c478bd9Sstevel@tonic-gate 		else
4067c478bd9Sstevel@tonic-gate 			close(fdmap[i].org_fd);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	topfd = last;
4097c478bd9Sstevel@tonic-gate }
410