4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
38 #include <sys/types.h>
44 /* ======== input output and file copying ======== */
49 struct fileblk
*f
= standin
;
52 f
->fsiz
= ((flags
& oneflg
) == 0 ? BUFFERSIZE
: 1);
53 f
->fnxt
= f
->fend
= f
->fbuf
;
54 f
->nxtoff
= f
->endoff
= 0;
61 estabf(unsigned char *s
)
65 (f
= standin
)->fdes
= -1;
66 f
->fend
= length(s
) + (f
->fnxt
= s
);
68 f
->endoff
= length(s
);
70 return (f
->feof
= (s
== 0));
74 push(struct fileblk
*af
)
78 (f
= af
)->fstak
= standin
;
89 if ((f
= standin
)->fstak
) {
98 struct tempblk
*tmpfptr
;
101 pushtemp(int fd
, struct tempblk
*tb
)
112 close(tmpfptr
->fdes
);
113 tmpfptr
= tmpfptr
->fstak
;
122 if (pipe(pv
) < 0 || pv
[INPIPE
] < 0 || pv
[OTPIPE
] < 0)
127 chkopen(unsigned char *idf
, int mode
)
131 if ((rc
= open((char *)idf
, mode
, 0666)) < 0)
132 failed(idf
, badopen
);
138 * Make f2 be a synonym (including the close-on-exec flag) for f1, which is
139 * then closed. If f2 is descriptor 0, modify the global ioset variable
143 renamef(int f1
, int f2
)
147 dup(f1
| DUPFLG
, f2
);
156 fs
= fcntl(f2
, 1, 0);
169 create(unsigned char *s
)
173 if ((rc
= creat((char *)s
, 0666)) < 0)
174 failed(s
, badcreate
);
181 tmpfil(struct tempblk
*tb
)
185 size_t size_left
= TMPOUTSZ
- tmpout_offset
;
187 /* make sure tmp file does not already exist. */
189 len
= snprintf((char *)&tmpout
[tmpout_offset
], size_left
,
191 fd
= open((char *)tmpout
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
193 if ((serial
>= UINT_MAX
) || (len
>= size_left
)) {
195 * We've already cycled through all the possible
196 * numbers or the tmp file name is being
197 * truncated anyway (although TMPOUTSZ should be
198 * big enough), so start over.
203 } while ((fd
== -1) && (errno
== EEXIST
));
209 failed(tmpout
, badcreate
);
219 copy(struct ionod
*ioparg
)
221 unsigned char *cline
;
222 unsigned char *clinep
;
226 unsigned char *start
;
236 ends
= mactrim(iop
->ioname
);
237 stripflg
= iop
->iofile
& IOSTRIP
;
239 iop
->iofile
&= ~IODOC_SUBST
;
243 iop
->ioname
= (char *)make(tmpout
);
245 iop
->ioname
= (char *)cpystak(tmpout
);
250 cline
= clinep
= start
= locstak();
252 iop
->iofile
&= ~IOSTRIP
;
253 while (*ends
== '\t')
264 while (!eolchar(c
)) {
267 if (clinep
>= brkend
)
279 while (!eolchar(c
)) {
282 if (clinep
>= brkend
)
287 pc
= readw(readwc());
288 /* *pc might be NULL */
292 if (clinep
>= brkend
)
297 if (clinep
>= brkend
)
307 if (clinep
>= brkend
)
310 if (eof
|| eq(cline
, ends
)) {
311 if ((i
= cline
- start
) > 0)
315 if (clinep
>= brkend
)
320 if ((i
= clinep
- start
) < CPYSIZ
)
325 cline
= clinep
= start
;
330 * Pushed in tmpfil -- bug fix for problem
331 * deleting in-line script.
338 link_iodocs(struct ionod
*i
)
342 size_t size_left
= TMPOUTSZ
- tmpout_offset
;
347 /* make sure tmp file does not already exist. */
349 len
= snprintf((char *)&tmpout
[tmpout_offset
],
350 size_left
, "%u", serial
);
352 r
= link(i
->ioname
, (char *)tmpout
);
353 if ((serial
>= UINT_MAX
) || (len
>= size_left
)) {
355 * We've already cycled through all the possible
356 * numbers or the tmp file name is being
357 * truncated anyway, so start over.
362 } while (r
== -1 && errno
== EEXIST
);
365 i
->iolink
= (char *)make(tmpout
);
368 failed(tmpout
, badcreate
);
374 swap_iodoc_nm(struct ionod
*i
)
378 i
->ioname
= i
->iolink
;
390 f
= fcntl(fd
, F_DUPFD
, 10);
391 /* this saved fd should not be found in an exec'ed cmd */
392 (void) fcntl(f
, F_SETFD
, FD_CLOEXEC
);
402 for (i
= topfd
- 1; i
>= last
; i
--) {
403 if ((dupfd
= fdmap
[i
].dup_fd
) > 0)
404 renamef(dupfd
, fdmap
[i
].org_fd
);
406 close(fdmap
[i
].org_fd
);