xref: /illumos-gate/usr/src/cmd/sh/io.c (revision 1573d361)
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
5*1573d361Snakanon  * Common Development and Distribution License (the "License").
6*1573d361Snakanon  * 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 /*
23*1573d361Snakanon  * 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 
30965005c8Schin #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * UNIX shell
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate #include	"dup.h"
37965005c8Schin #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<fcntl.h>
397c478bd9Sstevel@tonic-gate #include	<sys/types.h>
407c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
417c478bd9Sstevel@tonic-gate #include	<errno.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate short topfd;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* ========	input output and file copying ======== */
467c478bd9Sstevel@tonic-gate 
47965005c8Schin void
48965005c8Schin initf(int fd)
497c478bd9Sstevel@tonic-gate {
50965005c8Schin 	struct fileblk *f = standin;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	f->fdes = fd;
537c478bd9Sstevel@tonic-gate 	f->fsiz = ((flags & oneflg) == 0 ? BUFFERSIZE : 1);
547c478bd9Sstevel@tonic-gate 	f->fnxt = f->fend = f->fbuf;
557c478bd9Sstevel@tonic-gate 	f->nxtoff = f->endoff = 0;
567c478bd9Sstevel@tonic-gate 	f->feval = 0;
577c478bd9Sstevel@tonic-gate 	f->flin = 1;
587c478bd9Sstevel@tonic-gate 	f->feof = FALSE;
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
61965005c8Schin int
62965005c8Schin estabf(unsigned char *s)
637c478bd9Sstevel@tonic-gate {
64965005c8Schin 	struct fileblk *f;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	(f = standin)->fdes = -1;
677c478bd9Sstevel@tonic-gate 	f->fend = length(s) + (f->fnxt = s);
687c478bd9Sstevel@tonic-gate 	f->nxtoff = 0;
697c478bd9Sstevel@tonic-gate 	f->endoff = length(s);
707c478bd9Sstevel@tonic-gate 	f->flin = 1;
717c478bd9Sstevel@tonic-gate 	return (f->feof = (s == 0));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
74965005c8Schin void
75965005c8Schin push(struct fileblk *af)
767c478bd9Sstevel@tonic-gate {
77965005c8Schin 	struct fileblk *f;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	(f = af)->fstak = standin;
807c478bd9Sstevel@tonic-gate 	f->feof = 0;
817c478bd9Sstevel@tonic-gate 	f->feval = 0;
827c478bd9Sstevel@tonic-gate 	standin = f;
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
85965005c8Schin int
86965005c8Schin pop(void)
877c478bd9Sstevel@tonic-gate {
88965005c8Schin 	struct fileblk *f;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if ((f = standin)->fstak)
917c478bd9Sstevel@tonic-gate 	{
927c478bd9Sstevel@tonic-gate 		if (f->fdes >= 0)
937c478bd9Sstevel@tonic-gate 			close(f->fdes);
947c478bd9Sstevel@tonic-gate 		standin = f->fstak;
957c478bd9Sstevel@tonic-gate 		return (TRUE);
967c478bd9Sstevel@tonic-gate 	}else
977c478bd9Sstevel@tonic-gate 		return (FALSE);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate struct tempblk *tmpfptr;
1017c478bd9Sstevel@tonic-gate 
102965005c8Schin void
103965005c8Schin pushtemp(int fd, struct tempblk *tb)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	tb->fdes = fd;
1067c478bd9Sstevel@tonic-gate 	tb->fstak = tmpfptr;
1077c478bd9Sstevel@tonic-gate 	tmpfptr = tb;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
110965005c8Schin int
111965005c8Schin poptemp(void)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	if (tmpfptr){
1147c478bd9Sstevel@tonic-gate 		close(tmpfptr->fdes);
1157c478bd9Sstevel@tonic-gate 		tmpfptr = tmpfptr->fstak;
1167c478bd9Sstevel@tonic-gate 		return (TRUE);
1177c478bd9Sstevel@tonic-gate 	}else
1187c478bd9Sstevel@tonic-gate 		return (FALSE);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
121965005c8Schin void
122965005c8Schin chkpipe(int *pv)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	if (pipe(pv) < 0 || pv[INPIPE] < 0 || pv[OTPIPE] < 0)
1257c478bd9Sstevel@tonic-gate 		error(piperr);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
128965005c8Schin int
129965005c8Schin chkopen(unsigned char *idf, int mode)
1307c478bd9Sstevel@tonic-gate {
131965005c8Schin 	int	rc;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if ((rc = open((char *)idf, mode, 0666)) < 0)
1347c478bd9Sstevel@tonic-gate 		failed(idf, badopen);
1357c478bd9Sstevel@tonic-gate 	else
1367c478bd9Sstevel@tonic-gate 		return (rc);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Make f2 be a synonym (including the close-on-exec flag) for f1, which is
1417c478bd9Sstevel@tonic-gate  * then closed.  If f2 is descriptor 0, modify the global ioset variable
1427c478bd9Sstevel@tonic-gate  * accordingly.
1437c478bd9Sstevel@tonic-gate  */
144965005c8Schin void
145965005c8Schin renamef(int f1, int f2)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate #ifdef RES
1487c478bd9Sstevel@tonic-gate 	if (f1 != f2)
1497c478bd9Sstevel@tonic-gate 	{
1507c478bd9Sstevel@tonic-gate 		dup(f1 | DUPFLG, f2);
1517c478bd9Sstevel@tonic-gate 		close(f1);
1527c478bd9Sstevel@tonic-gate 		if (f2 == 0)
1537c478bd9Sstevel@tonic-gate 			ioset |= 1;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate #else
1567c478bd9Sstevel@tonic-gate 	int	fs;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (f1 != f2)
1597c478bd9Sstevel@tonic-gate 	{
1607c478bd9Sstevel@tonic-gate 		fs = fcntl(f2, 1, 0);
1617c478bd9Sstevel@tonic-gate 		close(f2);
1627c478bd9Sstevel@tonic-gate 		fcntl(f1, 0, f2);
1637c478bd9Sstevel@tonic-gate 		close(f1);
1647c478bd9Sstevel@tonic-gate 		if (fs == 1)
1657c478bd9Sstevel@tonic-gate 			fcntl(f2, 2, 1);
1667c478bd9Sstevel@tonic-gate 		if (f2 == 0)
1677c478bd9Sstevel@tonic-gate 			ioset |= 1;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
172965005c8Schin int
173965005c8Schin create(unsigned char *s)
1747c478bd9Sstevel@tonic-gate {
175965005c8Schin 	int	rc;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if ((rc = creat((char *)s, 0666)) < 0)
1787c478bd9Sstevel@tonic-gate 		failed(s, badcreate);
1797c478bd9Sstevel@tonic-gate 	else
1807c478bd9Sstevel@tonic-gate 		return (rc);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 
184965005c8Schin int
185965005c8Schin tmpfil(struct tempblk *tb)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	int fd;
188965005c8Schin 	int len;
189965005c8Schin 	size_t size_left = TMPOUTSZ - tmpout_offset;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* make sure tmp file does not already exist. */
1927c478bd9Sstevel@tonic-gate 	do {
193965005c8Schin 		len = snprintf((char *)&tmpout[tmpout_offset], size_left,
194965005c8Schin 		    "%u", serial);
195965005c8Schin 		fd = open((char *)tmpout, O_RDWR|O_CREAT|O_EXCL, 0600);
196965005c8Schin 		serial++;
197965005c8Schin 		if ((serial >= UINT_MAX) || (len >= size_left)) {
198965005c8Schin 			/*
199965005c8Schin 			 * We've already cycled through all the possible
200965005c8Schin 			 * numbers or the tmp file name is being
201965005c8Schin 			 * truncated anyway (although TMPOUTSZ should be
202965005c8Schin 			 * big enough), so start over.
203965005c8Schin 			 */
204965005c8Schin 			serial = 0;
205965005c8Schin 			break;
206965005c8Schin 		}
2077c478bd9Sstevel@tonic-gate 	} while ((fd == -1) && (errno == EEXIST));
2087c478bd9Sstevel@tonic-gate 	if (fd != -1) {
2097c478bd9Sstevel@tonic-gate 		pushtemp(fd, tb);
2107c478bd9Sstevel@tonic-gate 		return (fd);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	else
2137c478bd9Sstevel@tonic-gate 		failed(tmpout, badcreate);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * set by trim
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate extern BOOL		nosubst;
2207c478bd9Sstevel@tonic-gate #define			CPYSIZ		512
2217c478bd9Sstevel@tonic-gate 
222965005c8Schin void
223965005c8Schin copy(struct ionod	*ioparg)
2247c478bd9Sstevel@tonic-gate {
225965005c8Schin 	unsigned char	*cline;
226965005c8Schin 	unsigned char	*clinep;
227965005c8Schin 	struct ionod	*iop;
2287c478bd9Sstevel@tonic-gate 	unsigned int	c;
2297c478bd9Sstevel@tonic-gate 	unsigned char	*ends;
2307c478bd9Sstevel@tonic-gate 	unsigned char	*start;
2317c478bd9Sstevel@tonic-gate 	int		fd;
2327c478bd9Sstevel@tonic-gate 	int		i;
2337c478bd9Sstevel@tonic-gate 	int		stripflg;
2347c478bd9Sstevel@tonic-gate 	unsigned char	*pc;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (iop = ioparg)
2387c478bd9Sstevel@tonic-gate 	{
2397c478bd9Sstevel@tonic-gate 		struct tempblk tb;
2407c478bd9Sstevel@tonic-gate 		copy(iop->iolst);
2417c478bd9Sstevel@tonic-gate 		ends = mactrim(iop->ioname);
2427c478bd9Sstevel@tonic-gate 		stripflg = iop->iofile & IOSTRIP;
2437c478bd9Sstevel@tonic-gate 		if (nosubst)
244*1573d361Snakanon 			iop->iofile &= ~IODOC_SUBST;
2457c478bd9Sstevel@tonic-gate 		fd = tmpfil(&tb);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		if (fndef)
2487c478bd9Sstevel@tonic-gate 			iop->ioname = (char *) make(tmpout);
2497c478bd9Sstevel@tonic-gate 		else
2507c478bd9Sstevel@tonic-gate 			iop->ioname = (char *) cpystak(tmpout);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		iop->iolst = iotemp;
2537c478bd9Sstevel@tonic-gate 		iotemp = iop;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		cline = clinep = start = locstak();
2567c478bd9Sstevel@tonic-gate 		if (stripflg)
2577c478bd9Sstevel@tonic-gate 		{
2587c478bd9Sstevel@tonic-gate 			iop->iofile &= ~IOSTRIP;
2597c478bd9Sstevel@tonic-gate 			while (*ends == '\t')
2607c478bd9Sstevel@tonic-gate 				ends++;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 		for (;;)
2637c478bd9Sstevel@tonic-gate 		{
2647c478bd9Sstevel@tonic-gate 			chkpr();
2657c478bd9Sstevel@tonic-gate 			if (nosubst)
2667c478bd9Sstevel@tonic-gate 			{
2677c478bd9Sstevel@tonic-gate 				c = readwc();
2687c478bd9Sstevel@tonic-gate 				if (stripflg)
2697c478bd9Sstevel@tonic-gate 					while (c == '\t')
2707c478bd9Sstevel@tonic-gate 						c = readwc();
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 				while (!eolchar(c))
2737c478bd9Sstevel@tonic-gate 				{
2747c478bd9Sstevel@tonic-gate 					pc = readw(c);
2757c478bd9Sstevel@tonic-gate 					while (*pc) {
2767c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
2777c478bd9Sstevel@tonic-gate 							growstak(clinep);
2787c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
2797c478bd9Sstevel@tonic-gate 					}
2807c478bd9Sstevel@tonic-gate 					c = readwc();
2817c478bd9Sstevel@tonic-gate 				}
2827c478bd9Sstevel@tonic-gate 			}else{
2837c478bd9Sstevel@tonic-gate 				c = nextwc();
2847c478bd9Sstevel@tonic-gate 				if (stripflg)
2857c478bd9Sstevel@tonic-gate 					while (c == '\t')
2867c478bd9Sstevel@tonic-gate 						c = nextwc();
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 				while (!eolchar(c))
2897c478bd9Sstevel@tonic-gate 				{
2907c478bd9Sstevel@tonic-gate 					pc = readw(c);
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 					if (c == '\\')
2977c478bd9Sstevel@tonic-gate 					{
2987c478bd9Sstevel@tonic-gate 						pc = readw(readwc());
2997c478bd9Sstevel@tonic-gate 						/* *pc might be NULL */
3007c478bd9Sstevel@tonic-gate 						if (*pc) {
3017c478bd9Sstevel@tonic-gate 							while (*pc) {
3027c478bd9Sstevel@tonic-gate 								if (clinep >= brkend)
3037c478bd9Sstevel@tonic-gate 									growstak(clinep);
3047c478bd9Sstevel@tonic-gate 								*clinep++ = *pc++;
3057c478bd9Sstevel@tonic-gate 							}
3067c478bd9Sstevel@tonic-gate 						} else {
3077c478bd9Sstevel@tonic-gate 							if (clinep >= brkend)
3087c478bd9Sstevel@tonic-gate 								growstak(clinep);
3097c478bd9Sstevel@tonic-gate 							*clinep++ = *pc;
3107c478bd9Sstevel@tonic-gate 						}
3117c478bd9Sstevel@tonic-gate 					}
3127c478bd9Sstevel@tonic-gate 					c = nextwc();
3137c478bd9Sstevel@tonic-gate 				}
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			if (clinep >= brkend)
3177c478bd9Sstevel@tonic-gate 				growstak(clinep);
3187c478bd9Sstevel@tonic-gate 			*clinep = 0;
3197c478bd9Sstevel@tonic-gate 			if (eof || eq(cline, ends))
3207c478bd9Sstevel@tonic-gate 			{
3217c478bd9Sstevel@tonic-gate 				if ((i = cline - start) > 0)
3227c478bd9Sstevel@tonic-gate 					write(fd, start, i);
3237c478bd9Sstevel@tonic-gate 				break;
3247c478bd9Sstevel@tonic-gate 			}else{
3257c478bd9Sstevel@tonic-gate 				if (clinep >= brkend)
3267c478bd9Sstevel@tonic-gate 					growstak(clinep);
3277c478bd9Sstevel@tonic-gate 				*clinep++ = NL;
3287c478bd9Sstevel@tonic-gate 			}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 			if ((i = clinep - start) < CPYSIZ)
3317c478bd9Sstevel@tonic-gate 				cline = clinep;
3327c478bd9Sstevel@tonic-gate 			else
3337c478bd9Sstevel@tonic-gate 			{
3347c478bd9Sstevel@tonic-gate 				write(fd, start, i);
3357c478bd9Sstevel@tonic-gate 				cline = clinep = start;
3367c478bd9Sstevel@tonic-gate 			}
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		poptemp();	/*
3407c478bd9Sstevel@tonic-gate 				 * pushed in tmpfil -- bug fix for problem
3417c478bd9Sstevel@tonic-gate 				 * deleting in-line scripts
3427c478bd9Sstevel@tonic-gate 				 */
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
346965005c8Schin void
347965005c8Schin link_iodocs(struct ionod *i)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	int r;
350965005c8Schin 	int len;
351965005c8Schin 	size_t size_left = TMPOUTSZ - tmpout_offset;
3527c478bd9Sstevel@tonic-gate 
353965005c8Schin 	while (i) {
3547c478bd9Sstevel@tonic-gate 		free(i->iolink);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		/* make sure tmp file does not already exist. */
3577c478bd9Sstevel@tonic-gate 		do {
358965005c8Schin 			len = snprintf((char *)&tmpout[tmpout_offset],
359965005c8Schin 			    size_left, "%u", serial);
360965005c8Schin 			serial++;
3617c478bd9Sstevel@tonic-gate 			r = link(i->ioname, (char *)tmpout);
362965005c8Schin 			if ((serial >= UINT_MAX) || (len >= size_left)) {
363965005c8Schin 			/*
364965005c8Schin 			 * We've already cycled through all the possible
365965005c8Schin 			 * numbers or the tmp file name is being
366965005c8Schin 			 * truncated anyway, so start over.
367965005c8Schin 			 */
368965005c8Schin 				serial = 0;
369965005c8Schin 				break;
370965005c8Schin 			}
3717c478bd9Sstevel@tonic-gate 		} while (r == -1 && errno == EEXIST);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		if (r != -1) {
3747c478bd9Sstevel@tonic-gate 			i->iolink = (char *)make(tmpout);
3757c478bd9Sstevel@tonic-gate 			i = i->iolst;
3767c478bd9Sstevel@tonic-gate 		} else
3777c478bd9Sstevel@tonic-gate 			failed(tmpout, badcreate);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
382965005c8Schin void
383965005c8Schin swap_iodoc_nm(struct ionod *i)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	while (i)
3867c478bd9Sstevel@tonic-gate 	{
3877c478bd9Sstevel@tonic-gate 		free(i->ioname);
3887c478bd9Sstevel@tonic-gate 		i->ioname = i->iolink;
3897c478bd9Sstevel@tonic-gate 		i->iolink = 0;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		i = i->iolst;
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
395965005c8Schin int
396965005c8Schin savefd(int fd)
3977c478bd9Sstevel@tonic-gate {
398965005c8Schin 	int	f;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	f = fcntl(fd, F_DUPFD, 10);
4017c478bd9Sstevel@tonic-gate 	return (f);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
404965005c8Schin void
405965005c8Schin restore(int last)
4067c478bd9Sstevel@tonic-gate {
407965005c8Schin 	int 	i;
408965005c8Schin 	int	dupfd;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	for (i = topfd - 1; i >= last; i--)
4117c478bd9Sstevel@tonic-gate 	{
4127c478bd9Sstevel@tonic-gate 		if ((dupfd = fdmap[i].dup_fd) > 0)
4137c478bd9Sstevel@tonic-gate 			renamef(dupfd, fdmap[i].org_fd);
4147c478bd9Sstevel@tonic-gate 		else
4157c478bd9Sstevel@tonic-gate 			close(fdmap[i].org_fd);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	topfd = last;
4187c478bd9Sstevel@tonic-gate }
419