xref: /titanic_51/usr/src/cmd/sh/io.c (revision 965005c81e0f731867d47892b9fb677030b102df)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*965005c8Schin 
23*965005c8Schin /*
24*965005c8Schin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*965005c8Schin  * Use is subject to license terms.
26*965005c8Schin  */
27*965005c8Schin 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31*965005c8Schin #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * UNIX shell
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	"defs.h"
377c478bd9Sstevel@tonic-gate #include	"dup.h"
38*965005c8Schin #include	<stdio.h>
397c478bd9Sstevel@tonic-gate #include	<fcntl.h>
407c478bd9Sstevel@tonic-gate #include	<sys/types.h>
417c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
427c478bd9Sstevel@tonic-gate #include	<errno.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate short topfd;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* ========	input output and file copying ======== */
477c478bd9Sstevel@tonic-gate 
48*965005c8Schin void
49*965005c8Schin initf(int fd)
507c478bd9Sstevel@tonic-gate {
51*965005c8Schin 	struct fileblk *f = standin;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	f->fdes = fd;
547c478bd9Sstevel@tonic-gate 	f->fsiz = ((flags & oneflg) == 0 ? BUFFERSIZE : 1);
557c478bd9Sstevel@tonic-gate 	f->fnxt = f->fend = f->fbuf;
567c478bd9Sstevel@tonic-gate 	f->nxtoff = f->endoff = 0;
577c478bd9Sstevel@tonic-gate 	f->feval = 0;
587c478bd9Sstevel@tonic-gate 	f->flin = 1;
597c478bd9Sstevel@tonic-gate 	f->feof = FALSE;
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
62*965005c8Schin int
63*965005c8Schin estabf(unsigned char *s)
647c478bd9Sstevel@tonic-gate {
65*965005c8Schin 	struct fileblk *f;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	(f = standin)->fdes = -1;
687c478bd9Sstevel@tonic-gate 	f->fend = length(s) + (f->fnxt = s);
697c478bd9Sstevel@tonic-gate 	f->nxtoff = 0;
707c478bd9Sstevel@tonic-gate 	f->endoff = length(s);
717c478bd9Sstevel@tonic-gate 	f->flin = 1;
727c478bd9Sstevel@tonic-gate 	return (f->feof = (s == 0));
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
75*965005c8Schin void
76*965005c8Schin push(struct fileblk *af)
777c478bd9Sstevel@tonic-gate {
78*965005c8Schin 	struct fileblk *f;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	(f = af)->fstak = standin;
817c478bd9Sstevel@tonic-gate 	f->feof = 0;
827c478bd9Sstevel@tonic-gate 	f->feval = 0;
837c478bd9Sstevel@tonic-gate 	standin = f;
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
86*965005c8Schin int
87*965005c8Schin pop(void)
887c478bd9Sstevel@tonic-gate {
89*965005c8Schin 	struct fileblk *f;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((f = standin)->fstak)
927c478bd9Sstevel@tonic-gate 	{
937c478bd9Sstevel@tonic-gate 		if (f->fdes >= 0)
947c478bd9Sstevel@tonic-gate 			close(f->fdes);
957c478bd9Sstevel@tonic-gate 		standin = f->fstak;
967c478bd9Sstevel@tonic-gate 		return (TRUE);
977c478bd9Sstevel@tonic-gate 	}else
987c478bd9Sstevel@tonic-gate 		return (FALSE);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate struct tempblk *tmpfptr;
1027c478bd9Sstevel@tonic-gate 
103*965005c8Schin void
104*965005c8Schin pushtemp(int fd, struct tempblk *tb)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	tb->fdes = fd;
1077c478bd9Sstevel@tonic-gate 	tb->fstak = tmpfptr;
1087c478bd9Sstevel@tonic-gate 	tmpfptr = tb;
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
111*965005c8Schin int
112*965005c8Schin poptemp(void)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	if (tmpfptr){
1157c478bd9Sstevel@tonic-gate 		close(tmpfptr->fdes);
1167c478bd9Sstevel@tonic-gate 		tmpfptr = tmpfptr->fstak;
1177c478bd9Sstevel@tonic-gate 		return (TRUE);
1187c478bd9Sstevel@tonic-gate 	}else
1197c478bd9Sstevel@tonic-gate 		return (FALSE);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
122*965005c8Schin void
123*965005c8Schin chkpipe(int *pv)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	if (pipe(pv) < 0 || pv[INPIPE] < 0 || pv[OTPIPE] < 0)
1267c478bd9Sstevel@tonic-gate 		error(piperr);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
129*965005c8Schin int
130*965005c8Schin chkopen(unsigned char *idf, int mode)
1317c478bd9Sstevel@tonic-gate {
132*965005c8Schin 	int	rc;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if ((rc = open((char *)idf, mode, 0666)) < 0)
1357c478bd9Sstevel@tonic-gate 		failed(idf, badopen);
1367c478bd9Sstevel@tonic-gate 	else
1377c478bd9Sstevel@tonic-gate 		return (rc);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Make f2 be a synonym (including the close-on-exec flag) for f1, which is
1427c478bd9Sstevel@tonic-gate  * then closed.  If f2 is descriptor 0, modify the global ioset variable
1437c478bd9Sstevel@tonic-gate  * accordingly.
1447c478bd9Sstevel@tonic-gate  */
145*965005c8Schin void
146*965005c8Schin renamef(int f1, int f2)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate #ifdef RES
1497c478bd9Sstevel@tonic-gate 	if (f1 != f2)
1507c478bd9Sstevel@tonic-gate 	{
1517c478bd9Sstevel@tonic-gate 		dup(f1 | DUPFLG, f2);
1527c478bd9Sstevel@tonic-gate 		close(f1);
1537c478bd9Sstevel@tonic-gate 		if (f2 == 0)
1547c478bd9Sstevel@tonic-gate 			ioset |= 1;
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate #else
1577c478bd9Sstevel@tonic-gate 	int	fs;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (f1 != f2)
1607c478bd9Sstevel@tonic-gate 	{
1617c478bd9Sstevel@tonic-gate 		fs = fcntl(f2, 1, 0);
1627c478bd9Sstevel@tonic-gate 		close(f2);
1637c478bd9Sstevel@tonic-gate 		fcntl(f1, 0, f2);
1647c478bd9Sstevel@tonic-gate 		close(f1);
1657c478bd9Sstevel@tonic-gate 		if (fs == 1)
1667c478bd9Sstevel@tonic-gate 			fcntl(f2, 2, 1);
1677c478bd9Sstevel@tonic-gate 		if (f2 == 0)
1687c478bd9Sstevel@tonic-gate 			ioset |= 1;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
173*965005c8Schin int
174*965005c8Schin create(unsigned char *s)
1757c478bd9Sstevel@tonic-gate {
176*965005c8Schin 	int	rc;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if ((rc = creat((char *)s, 0666)) < 0)
1797c478bd9Sstevel@tonic-gate 		failed(s, badcreate);
1807c478bd9Sstevel@tonic-gate 	else
1817c478bd9Sstevel@tonic-gate 		return (rc);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 
185*965005c8Schin int
186*965005c8Schin tmpfil(struct tempblk *tb)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	int fd;
189*965005c8Schin 	int len;
190*965005c8Schin 	size_t size_left = TMPOUTSZ - tmpout_offset;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* make sure tmp file does not already exist. */
1937c478bd9Sstevel@tonic-gate 	do {
194*965005c8Schin 		len = snprintf((char *)&tmpout[tmpout_offset], size_left,
195*965005c8Schin 		    "%u", serial);
196*965005c8Schin 		fd = open((char *)tmpout, O_RDWR|O_CREAT|O_EXCL, 0600);
197*965005c8Schin 		serial++;
198*965005c8Schin 		if ((serial >= UINT_MAX) || (len >= size_left)) {
199*965005c8Schin 			/*
200*965005c8Schin 			 * We've already cycled through all the possible
201*965005c8Schin 			 * numbers or the tmp file name is being
202*965005c8Schin 			 * truncated anyway (although TMPOUTSZ should be
203*965005c8Schin 			 * big enough), so start over.
204*965005c8Schin 			 */
205*965005c8Schin 			serial = 0;
206*965005c8Schin 			break;
207*965005c8Schin 		}
2087c478bd9Sstevel@tonic-gate 	} while ((fd == -1) && (errno == EEXIST));
2097c478bd9Sstevel@tonic-gate 	if (fd != -1) {
2107c478bd9Sstevel@tonic-gate 		pushtemp(fd, tb);
2117c478bd9Sstevel@tonic-gate 		return (fd);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	else
2147c478bd9Sstevel@tonic-gate 		failed(tmpout, badcreate);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * set by trim
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate extern BOOL		nosubst;
2217c478bd9Sstevel@tonic-gate #define			CPYSIZ		512
2227c478bd9Sstevel@tonic-gate 
223*965005c8Schin void
224*965005c8Schin copy(struct ionod	*ioparg)
2257c478bd9Sstevel@tonic-gate {
226*965005c8Schin 	unsigned char	*cline;
227*965005c8Schin 	unsigned char	*clinep;
228*965005c8Schin 	struct ionod	*iop;
2297c478bd9Sstevel@tonic-gate 	unsigned int	c;
2307c478bd9Sstevel@tonic-gate 	unsigned char	*ends;
2317c478bd9Sstevel@tonic-gate 	unsigned char	*start;
2327c478bd9Sstevel@tonic-gate 	int		fd;
2337c478bd9Sstevel@tonic-gate 	int		i;
2347c478bd9Sstevel@tonic-gate 	int		stripflg;
2357c478bd9Sstevel@tonic-gate 	unsigned char	*pc;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (iop = ioparg)
2397c478bd9Sstevel@tonic-gate 	{
2407c478bd9Sstevel@tonic-gate 		struct tempblk tb;
2417c478bd9Sstevel@tonic-gate 		copy(iop->iolst);
2427c478bd9Sstevel@tonic-gate 		ends = mactrim(iop->ioname);
2437c478bd9Sstevel@tonic-gate 		stripflg = iop->iofile & IOSTRIP;
2447c478bd9Sstevel@tonic-gate 		if (nosubst)
2457c478bd9Sstevel@tonic-gate 			iop->iofile &= ~IODOC;
2467c478bd9Sstevel@tonic-gate 		fd = tmpfil(&tb);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		if (fndef)
2497c478bd9Sstevel@tonic-gate 			iop->ioname = (char *) make(tmpout);
2507c478bd9Sstevel@tonic-gate 		else
2517c478bd9Sstevel@tonic-gate 			iop->ioname = (char *) cpystak(tmpout);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		iop->iolst = iotemp;
2547c478bd9Sstevel@tonic-gate 		iotemp = iop;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		cline = clinep = start = locstak();
2577c478bd9Sstevel@tonic-gate 		if (stripflg)
2587c478bd9Sstevel@tonic-gate 		{
2597c478bd9Sstevel@tonic-gate 			iop->iofile &= ~IOSTRIP;
2607c478bd9Sstevel@tonic-gate 			while (*ends == '\t')
2617c478bd9Sstevel@tonic-gate 				ends++;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		for (;;)
2647c478bd9Sstevel@tonic-gate 		{
2657c478bd9Sstevel@tonic-gate 			chkpr();
2667c478bd9Sstevel@tonic-gate 			if (nosubst)
2677c478bd9Sstevel@tonic-gate 			{
2687c478bd9Sstevel@tonic-gate 				c = readwc();
2697c478bd9Sstevel@tonic-gate 				if (stripflg)
2707c478bd9Sstevel@tonic-gate 					while (c == '\t')
2717c478bd9Sstevel@tonic-gate 						c = readwc();
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 				while (!eolchar(c))
2747c478bd9Sstevel@tonic-gate 				{
2757c478bd9Sstevel@tonic-gate 					pc = readw(c);
2767c478bd9Sstevel@tonic-gate 					while (*pc) {
2777c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
2787c478bd9Sstevel@tonic-gate 							growstak(clinep);
2797c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
2807c478bd9Sstevel@tonic-gate 					}
2817c478bd9Sstevel@tonic-gate 					c = readwc();
2827c478bd9Sstevel@tonic-gate 				}
2837c478bd9Sstevel@tonic-gate 			}else{
2847c478bd9Sstevel@tonic-gate 				c = nextwc();
2857c478bd9Sstevel@tonic-gate 				if (stripflg)
2867c478bd9Sstevel@tonic-gate 					while (c == '\t')
2877c478bd9Sstevel@tonic-gate 						c = nextwc();
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 				while (!eolchar(c))
2907c478bd9Sstevel@tonic-gate 				{
2917c478bd9Sstevel@tonic-gate 					pc = readw(c);
2927c478bd9Sstevel@tonic-gate 					while (*pc) {
2937c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
2947c478bd9Sstevel@tonic-gate 							growstak(clinep);
2957c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
2967c478bd9Sstevel@tonic-gate 					}
2977c478bd9Sstevel@tonic-gate 					if (c == '\\')
2987c478bd9Sstevel@tonic-gate 					{
2997c478bd9Sstevel@tonic-gate 						pc = readw(readwc());
3007c478bd9Sstevel@tonic-gate 						/* *pc might be NULL */
3017c478bd9Sstevel@tonic-gate 						if (*pc) {
3027c478bd9Sstevel@tonic-gate 							while (*pc) {
3037c478bd9Sstevel@tonic-gate 								if (clinep >= brkend)
3047c478bd9Sstevel@tonic-gate 									growstak(clinep);
3057c478bd9Sstevel@tonic-gate 								*clinep++ = *pc++;
3067c478bd9Sstevel@tonic-gate 							}
3077c478bd9Sstevel@tonic-gate 						} else {
3087c478bd9Sstevel@tonic-gate 							if (clinep >= brkend)
3097c478bd9Sstevel@tonic-gate 								growstak(clinep);
3107c478bd9Sstevel@tonic-gate 							*clinep++ = *pc;
3117c478bd9Sstevel@tonic-gate 						}
3127c478bd9Sstevel@tonic-gate 					}
3137c478bd9Sstevel@tonic-gate 					c = nextwc();
3147c478bd9Sstevel@tonic-gate 				}
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			if (clinep >= brkend)
3187c478bd9Sstevel@tonic-gate 				growstak(clinep);
3197c478bd9Sstevel@tonic-gate 			*clinep = 0;
3207c478bd9Sstevel@tonic-gate 			if (eof || eq(cline, ends))
3217c478bd9Sstevel@tonic-gate 			{
3227c478bd9Sstevel@tonic-gate 				if ((i = cline - start) > 0)
3237c478bd9Sstevel@tonic-gate 					write(fd, start, i);
3247c478bd9Sstevel@tonic-gate 				break;
3257c478bd9Sstevel@tonic-gate 			}else{
3267c478bd9Sstevel@tonic-gate 				if (clinep >= brkend)
3277c478bd9Sstevel@tonic-gate 					growstak(clinep);
3287c478bd9Sstevel@tonic-gate 				*clinep++ = NL;
3297c478bd9Sstevel@tonic-gate 			}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			if ((i = clinep - start) < CPYSIZ)
3327c478bd9Sstevel@tonic-gate 				cline = clinep;
3337c478bd9Sstevel@tonic-gate 			else
3347c478bd9Sstevel@tonic-gate 			{
3357c478bd9Sstevel@tonic-gate 				write(fd, start, i);
3367c478bd9Sstevel@tonic-gate 				cline = clinep = start;
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		poptemp();	/*
3417c478bd9Sstevel@tonic-gate 				 * pushed in tmpfil -- bug fix for problem
3427c478bd9Sstevel@tonic-gate 				 * deleting in-line scripts
3437c478bd9Sstevel@tonic-gate 				 */
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
347*965005c8Schin void
348*965005c8Schin link_iodocs(struct ionod *i)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	int r;
351*965005c8Schin 	int len;
352*965005c8Schin 	size_t size_left = TMPOUTSZ - tmpout_offset;
3537c478bd9Sstevel@tonic-gate 
354*965005c8Schin 	while (i) {
3557c478bd9Sstevel@tonic-gate 		free(i->iolink);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		/* make sure tmp file does not already exist. */
3587c478bd9Sstevel@tonic-gate 		do {
359*965005c8Schin 			len = snprintf((char *)&tmpout[tmpout_offset],
360*965005c8Schin 			    size_left, "%u", serial);
361*965005c8Schin 			serial++;
3627c478bd9Sstevel@tonic-gate 			r = link(i->ioname, (char *)tmpout);
363*965005c8Schin 			if ((serial >= UINT_MAX) || (len >= size_left)) {
364*965005c8Schin 			/*
365*965005c8Schin 			 * We've already cycled through all the possible
366*965005c8Schin 			 * numbers or the tmp file name is being
367*965005c8Schin 			 * truncated anyway, so start over.
368*965005c8Schin 			 */
369*965005c8Schin 				serial = 0;
370*965005c8Schin 				break;
371*965005c8Schin 			}
3727c478bd9Sstevel@tonic-gate 		} while (r == -1 && errno == EEXIST);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if (r != -1) {
3757c478bd9Sstevel@tonic-gate 			i->iolink = (char *)make(tmpout);
3767c478bd9Sstevel@tonic-gate 			i = i->iolst;
3777c478bd9Sstevel@tonic-gate 		} else
3787c478bd9Sstevel@tonic-gate 			failed(tmpout, badcreate);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
383*965005c8Schin void
384*965005c8Schin swap_iodoc_nm(struct ionod *i)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	while (i)
3877c478bd9Sstevel@tonic-gate 	{
3887c478bd9Sstevel@tonic-gate 		free(i->ioname);
3897c478bd9Sstevel@tonic-gate 		i->ioname = i->iolink;
3907c478bd9Sstevel@tonic-gate 		i->iolink = 0;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		i = i->iolst;
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
396*965005c8Schin int
397*965005c8Schin savefd(int fd)
3987c478bd9Sstevel@tonic-gate {
399*965005c8Schin 	int	f;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	f = fcntl(fd, F_DUPFD, 10);
4027c478bd9Sstevel@tonic-gate 	return (f);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
405*965005c8Schin void
406*965005c8Schin restore(int last)
4077c478bd9Sstevel@tonic-gate {
408*965005c8Schin 	int 	i;
409*965005c8Schin 	int	dupfd;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	for (i = topfd - 1; i >= last; i--)
4127c478bd9Sstevel@tonic-gate 	{
4137c478bd9Sstevel@tonic-gate 		if ((dupfd = fdmap[i].dup_fd) > 0)
4147c478bd9Sstevel@tonic-gate 			renamef(dupfd, fdmap[i].org_fd);
4157c478bd9Sstevel@tonic-gate 		else
4167c478bd9Sstevel@tonic-gate 			close(fdmap[i].org_fd);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	topfd = last;
4197c478bd9Sstevel@tonic-gate }
420