xref: /titanic_51/usr/src/cmd/sh/io.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, 2001 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10.2.1	*/
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * UNIX shell
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include	"defs.h"
37*7c478bd9Sstevel@tonic-gate #include	"dup.h"
38*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
41*7c478bd9Sstevel@tonic-gate #include	<errno.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate short topfd;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /* ========	input output and file copying ======== */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate initf(fd)
48*7c478bd9Sstevel@tonic-gate int	fd;
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	register struct fileblk *f = standin;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	f->fdes = fd;
53*7c478bd9Sstevel@tonic-gate 	f->fsiz = ((flags & oneflg) == 0 ? BUFFERSIZE : 1);
54*7c478bd9Sstevel@tonic-gate 	f->fnxt = f->fend = f->fbuf;
55*7c478bd9Sstevel@tonic-gate 	f->nxtoff = f->endoff = 0;
56*7c478bd9Sstevel@tonic-gate 	f->feval = 0;
57*7c478bd9Sstevel@tonic-gate 	f->flin = 1;
58*7c478bd9Sstevel@tonic-gate 	f->feof = FALSE;
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate estabf(s)
62*7c478bd9Sstevel@tonic-gate register unsigned char *s;
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	register struct fileblk *f;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	(f = standin)->fdes = -1;
67*7c478bd9Sstevel@tonic-gate 	f->fend = length(s) + (f->fnxt = s);
68*7c478bd9Sstevel@tonic-gate 	f->nxtoff = 0;
69*7c478bd9Sstevel@tonic-gate 	f->endoff = length(s);
70*7c478bd9Sstevel@tonic-gate 	f->flin = 1;
71*7c478bd9Sstevel@tonic-gate 	return (f->feof = (s == 0));
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate push(af)
75*7c478bd9Sstevel@tonic-gate struct fileblk *af;
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	register struct fileblk *f;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	(f = af)->fstak = standin;
80*7c478bd9Sstevel@tonic-gate 	f->feof = 0;
81*7c478bd9Sstevel@tonic-gate 	f->feval = 0;
82*7c478bd9Sstevel@tonic-gate 	standin = f;
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate pop()
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	register struct fileblk *f;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if ((f = standin)->fstak)
90*7c478bd9Sstevel@tonic-gate 	{
91*7c478bd9Sstevel@tonic-gate 		if (f->fdes >= 0)
92*7c478bd9Sstevel@tonic-gate 			close(f->fdes);
93*7c478bd9Sstevel@tonic-gate 		standin = f->fstak;
94*7c478bd9Sstevel@tonic-gate 		return (TRUE);
95*7c478bd9Sstevel@tonic-gate 	}else
96*7c478bd9Sstevel@tonic-gate 		return (FALSE);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate struct tempblk *tmpfptr;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate pushtemp(fd, tb)
102*7c478bd9Sstevel@tonic-gate 	int fd;
103*7c478bd9Sstevel@tonic-gate 	struct tempblk *tb;
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	tb->fdes = fd;
106*7c478bd9Sstevel@tonic-gate 	tb->fstak = tmpfptr;
107*7c478bd9Sstevel@tonic-gate 	tmpfptr = tb;
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate poptemp()
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	if (tmpfptr){
113*7c478bd9Sstevel@tonic-gate 		close(tmpfptr->fdes);
114*7c478bd9Sstevel@tonic-gate 		tmpfptr = tmpfptr->fstak;
115*7c478bd9Sstevel@tonic-gate 		return (TRUE);
116*7c478bd9Sstevel@tonic-gate 	}else
117*7c478bd9Sstevel@tonic-gate 		return (FALSE);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate chkpipe(pv)
121*7c478bd9Sstevel@tonic-gate int	*pv;
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	if (pipe(pv) < 0 || pv[INPIPE] < 0 || pv[OTPIPE] < 0)
124*7c478bd9Sstevel@tonic-gate 		error(piperr);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate chkopen(idf, mode)
128*7c478bd9Sstevel@tonic-gate unsigned char *idf;
129*7c478bd9Sstevel@tonic-gate int mode;
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	register int	rc;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if ((rc = open((char *)idf, mode, 0666)) < 0)
134*7c478bd9Sstevel@tonic-gate 		failed(idf, badopen);
135*7c478bd9Sstevel@tonic-gate 	else
136*7c478bd9Sstevel@tonic-gate 		return (rc);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * Make f2 be a synonym (including the close-on-exec flag) for f1, which is
141*7c478bd9Sstevel@tonic-gate  * then closed.  If f2 is descriptor 0, modify the global ioset variable
142*7c478bd9Sstevel@tonic-gate  * accordingly.
143*7c478bd9Sstevel@tonic-gate  */
144*7c478bd9Sstevel@tonic-gate renamef(f1, f2)
145*7c478bd9Sstevel@tonic-gate register int	f1, f2;
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate #ifdef RES
148*7c478bd9Sstevel@tonic-gate 	if (f1 != f2)
149*7c478bd9Sstevel@tonic-gate 	{
150*7c478bd9Sstevel@tonic-gate 		dup(f1 | DUPFLG, f2);
151*7c478bd9Sstevel@tonic-gate 		close(f1);
152*7c478bd9Sstevel@tonic-gate 		if (f2 == 0)
153*7c478bd9Sstevel@tonic-gate 			ioset |= 1;
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate #else
156*7c478bd9Sstevel@tonic-gate 	int	fs;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if (f1 != f2)
159*7c478bd9Sstevel@tonic-gate 	{
160*7c478bd9Sstevel@tonic-gate 		fs = fcntl(f2, 1, 0);
161*7c478bd9Sstevel@tonic-gate 		close(f2);
162*7c478bd9Sstevel@tonic-gate 		fcntl(f1, 0, f2);
163*7c478bd9Sstevel@tonic-gate 		close(f1);
164*7c478bd9Sstevel@tonic-gate 		if (fs == 1)
165*7c478bd9Sstevel@tonic-gate 			fcntl(f2, 2, 1);
166*7c478bd9Sstevel@tonic-gate 		if (f2 == 0)
167*7c478bd9Sstevel@tonic-gate 			ioset |= 1;
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate #endif
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate create(s)
173*7c478bd9Sstevel@tonic-gate unsigned char *s;
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	register int	rc;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if ((rc = creat((char *)s, 0666)) < 0)
178*7c478bd9Sstevel@tonic-gate 		failed(s, badcreate);
179*7c478bd9Sstevel@tonic-gate 	else
180*7c478bd9Sstevel@tonic-gate 		return (rc);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate tmpfil(tb)
185*7c478bd9Sstevel@tonic-gate 	struct tempblk *tb;
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	int fd;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	/* make sure tmp file does not already exist. */
190*7c478bd9Sstevel@tonic-gate 	do {
191*7c478bd9Sstevel@tonic-gate 		itos(serial++);
192*7c478bd9Sstevel@tonic-gate 		movstr(numbuf, tmpname);
193*7c478bd9Sstevel@tonic-gate 		fd = open((char *)tmpout, O_RDWR|O_CREAT|O_EXCL, 0666);
194*7c478bd9Sstevel@tonic-gate 	} while ((fd == -1) && (errno == EEXIST));
195*7c478bd9Sstevel@tonic-gate 	if (fd != -1) {
196*7c478bd9Sstevel@tonic-gate 		pushtemp(fd, tb);
197*7c478bd9Sstevel@tonic-gate 		return (fd);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 	else
200*7c478bd9Sstevel@tonic-gate 		failed(tmpout, badcreate);
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * set by trim
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate extern BOOL		nosubst;
208*7c478bd9Sstevel@tonic-gate #define			CPYSIZ		512
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate copy(ioparg)
211*7c478bd9Sstevel@tonic-gate struct ionod	*ioparg;
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	register unsigned char	*cline;
214*7c478bd9Sstevel@tonic-gate 	register unsigned char	*clinep;
215*7c478bd9Sstevel@tonic-gate 	register struct ionod	*iop;
216*7c478bd9Sstevel@tonic-gate 	unsigned int	c;
217*7c478bd9Sstevel@tonic-gate 	unsigned char	*ends;
218*7c478bd9Sstevel@tonic-gate 	unsigned char	*start;
219*7c478bd9Sstevel@tonic-gate 	int		fd;
220*7c478bd9Sstevel@tonic-gate 	int		i;
221*7c478bd9Sstevel@tonic-gate 	int		stripflg;
222*7c478bd9Sstevel@tonic-gate 	unsigned char	*pc;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	if (iop = ioparg)
226*7c478bd9Sstevel@tonic-gate 	{
227*7c478bd9Sstevel@tonic-gate 		struct tempblk tb;
228*7c478bd9Sstevel@tonic-gate 		copy(iop->iolst);
229*7c478bd9Sstevel@tonic-gate 		ends = mactrim(iop->ioname);
230*7c478bd9Sstevel@tonic-gate 		stripflg = iop->iofile & IOSTRIP;
231*7c478bd9Sstevel@tonic-gate 		if (nosubst)
232*7c478bd9Sstevel@tonic-gate 			iop->iofile &= ~IODOC;
233*7c478bd9Sstevel@tonic-gate 		fd = tmpfil(&tb);
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 		if (fndef)
236*7c478bd9Sstevel@tonic-gate 			iop->ioname = (char *) make(tmpout);
237*7c478bd9Sstevel@tonic-gate 		else
238*7c478bd9Sstevel@tonic-gate 			iop->ioname = (char *) cpystak(tmpout);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 		iop->iolst = iotemp;
241*7c478bd9Sstevel@tonic-gate 		iotemp = iop;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 		cline = clinep = start = locstak();
244*7c478bd9Sstevel@tonic-gate 		if (stripflg)
245*7c478bd9Sstevel@tonic-gate 		{
246*7c478bd9Sstevel@tonic-gate 			iop->iofile &= ~IOSTRIP;
247*7c478bd9Sstevel@tonic-gate 			while (*ends == '\t')
248*7c478bd9Sstevel@tonic-gate 				ends++;
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 		for (;;)
251*7c478bd9Sstevel@tonic-gate 		{
252*7c478bd9Sstevel@tonic-gate 			chkpr();
253*7c478bd9Sstevel@tonic-gate 			if (nosubst)
254*7c478bd9Sstevel@tonic-gate 			{
255*7c478bd9Sstevel@tonic-gate 				c = readwc();
256*7c478bd9Sstevel@tonic-gate 				if (stripflg)
257*7c478bd9Sstevel@tonic-gate 					while (c == '\t')
258*7c478bd9Sstevel@tonic-gate 						c = readwc();
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 				while (!eolchar(c))
261*7c478bd9Sstevel@tonic-gate 				{
262*7c478bd9Sstevel@tonic-gate 					pc = readw(c);
263*7c478bd9Sstevel@tonic-gate 					while (*pc) {
264*7c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
265*7c478bd9Sstevel@tonic-gate 							growstak(clinep);
266*7c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
267*7c478bd9Sstevel@tonic-gate 					}
268*7c478bd9Sstevel@tonic-gate 					c = readwc();
269*7c478bd9Sstevel@tonic-gate 				}
270*7c478bd9Sstevel@tonic-gate 			}else{
271*7c478bd9Sstevel@tonic-gate 				c = nextwc();
272*7c478bd9Sstevel@tonic-gate 				if (stripflg)
273*7c478bd9Sstevel@tonic-gate 					while (c == '\t')
274*7c478bd9Sstevel@tonic-gate 						c = nextwc();
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 				while (!eolchar(c))
277*7c478bd9Sstevel@tonic-gate 				{
278*7c478bd9Sstevel@tonic-gate 					pc = readw(c);
279*7c478bd9Sstevel@tonic-gate 					while (*pc) {
280*7c478bd9Sstevel@tonic-gate 						if (clinep >= brkend)
281*7c478bd9Sstevel@tonic-gate 							growstak(clinep);
282*7c478bd9Sstevel@tonic-gate 						*clinep++ = *pc++;
283*7c478bd9Sstevel@tonic-gate 					}
284*7c478bd9Sstevel@tonic-gate 					if (c == '\\')
285*7c478bd9Sstevel@tonic-gate 					{
286*7c478bd9Sstevel@tonic-gate 						pc = readw(readwc());
287*7c478bd9Sstevel@tonic-gate 						/* *pc might be NULL */
288*7c478bd9Sstevel@tonic-gate 						if (*pc) {
289*7c478bd9Sstevel@tonic-gate 							while (*pc) {
290*7c478bd9Sstevel@tonic-gate 								if (clinep >= brkend)
291*7c478bd9Sstevel@tonic-gate 									growstak(clinep);
292*7c478bd9Sstevel@tonic-gate 								*clinep++ = *pc++;
293*7c478bd9Sstevel@tonic-gate 							}
294*7c478bd9Sstevel@tonic-gate 						} else {
295*7c478bd9Sstevel@tonic-gate 							if (clinep >= brkend)
296*7c478bd9Sstevel@tonic-gate 								growstak(clinep);
297*7c478bd9Sstevel@tonic-gate 							*clinep++ = *pc;
298*7c478bd9Sstevel@tonic-gate 						}
299*7c478bd9Sstevel@tonic-gate 					}
300*7c478bd9Sstevel@tonic-gate 					c = nextwc();
301*7c478bd9Sstevel@tonic-gate 				}
302*7c478bd9Sstevel@tonic-gate 			}
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 			if (clinep >= brkend)
305*7c478bd9Sstevel@tonic-gate 				growstak(clinep);
306*7c478bd9Sstevel@tonic-gate 			*clinep = 0;
307*7c478bd9Sstevel@tonic-gate 			if (eof || eq(cline, ends))
308*7c478bd9Sstevel@tonic-gate 			{
309*7c478bd9Sstevel@tonic-gate 				if ((i = cline - start) > 0)
310*7c478bd9Sstevel@tonic-gate 					write(fd, start, i);
311*7c478bd9Sstevel@tonic-gate 				break;
312*7c478bd9Sstevel@tonic-gate 			}else{
313*7c478bd9Sstevel@tonic-gate 				if (clinep >= brkend)
314*7c478bd9Sstevel@tonic-gate 					growstak(clinep);
315*7c478bd9Sstevel@tonic-gate 				*clinep++ = NL;
316*7c478bd9Sstevel@tonic-gate 			}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 			if ((i = clinep - start) < CPYSIZ)
319*7c478bd9Sstevel@tonic-gate 				cline = clinep;
320*7c478bd9Sstevel@tonic-gate 			else
321*7c478bd9Sstevel@tonic-gate 			{
322*7c478bd9Sstevel@tonic-gate 				write(fd, start, i);
323*7c478bd9Sstevel@tonic-gate 				cline = clinep = start;
324*7c478bd9Sstevel@tonic-gate 			}
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		poptemp();	/*
328*7c478bd9Sstevel@tonic-gate 				 * pushed in tmpfil -- bug fix for problem
329*7c478bd9Sstevel@tonic-gate 				 * deleting in-line scripts
330*7c478bd9Sstevel@tonic-gate 				 */
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate }
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate link_iodocs(i)
336*7c478bd9Sstevel@tonic-gate 	struct ionod	*i;
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	int r;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	while (i)
341*7c478bd9Sstevel@tonic-gate 	{
342*7c478bd9Sstevel@tonic-gate 		free(i->iolink);
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 		/* make sure tmp file does not already exist. */
345*7c478bd9Sstevel@tonic-gate 		do {
346*7c478bd9Sstevel@tonic-gate 			itos(serial++);
347*7c478bd9Sstevel@tonic-gate 			movstr(numbuf, tmpname);
348*7c478bd9Sstevel@tonic-gate 			r = link(i->ioname, (char *)tmpout);
349*7c478bd9Sstevel@tonic-gate 		} while (r == -1 && errno == EEXIST);
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 		if (r != -1) {
352*7c478bd9Sstevel@tonic-gate 			i->iolink = (char *)make(tmpout);
353*7c478bd9Sstevel@tonic-gate 			i = i->iolst;
354*7c478bd9Sstevel@tonic-gate 		} else
355*7c478bd9Sstevel@tonic-gate 			failed(tmpout, badcreate);
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate swap_iodoc_nm(i)
362*7c478bd9Sstevel@tonic-gate 	struct ionod	*i;
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	while (i)
365*7c478bd9Sstevel@tonic-gate 	{
366*7c478bd9Sstevel@tonic-gate 		free(i->ioname);
367*7c478bd9Sstevel@tonic-gate 		i->ioname = i->iolink;
368*7c478bd9Sstevel@tonic-gate 		i->iolink = 0;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 		i = i->iolst;
371*7c478bd9Sstevel@tonic-gate 	}
372*7c478bd9Sstevel@tonic-gate }
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate savefd(fd)
376*7c478bd9Sstevel@tonic-gate 	int fd;
377*7c478bd9Sstevel@tonic-gate {
378*7c478bd9Sstevel@tonic-gate 	register int	f;
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	f = fcntl(fd, F_DUPFD, 10);
381*7c478bd9Sstevel@tonic-gate 	return (f);
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate restore(last)
386*7c478bd9Sstevel@tonic-gate 	register int	last;
387*7c478bd9Sstevel@tonic-gate {
388*7c478bd9Sstevel@tonic-gate 	register int 	i;
389*7c478bd9Sstevel@tonic-gate 	register int	dupfd;
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	for (i = topfd - 1; i >= last; i--)
392*7c478bd9Sstevel@tonic-gate 	{
393*7c478bd9Sstevel@tonic-gate 		if ((dupfd = fdmap[i].dup_fd) > 0)
394*7c478bd9Sstevel@tonic-gate 			renamef(dupfd, fdmap[i].org_fd);
395*7c478bd9Sstevel@tonic-gate 		else
396*7c478bd9Sstevel@tonic-gate 			close(fdmap[i].org_fd);
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 	topfd = last;
399*7c478bd9Sstevel@tonic-gate }
400