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) 1988 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
44 #include <sys/types.h>
57 /* Final argument to _endopen depends on build environment */
58 #define LARGE_OPEN (_FILE_OFFSET_BITS == 64)
61 fopen(const char *name
, const char *type
) /* open name, return new stream */
68 * Note that iop is not locked here, since no other thread could
69 * possibly call _endopen with the same iop at this point.
71 rc
= _endopen(name
, type
, iop
, LARGE_OPEN
);
73 if (rc
== NULL
&& iop
!= NULL
)
74 iop
->_flag
= 0; /* release iop */
80 _freopen_null(const char *type
, FILE *iop
)
83 int oflag
, nflag
, fd
, accmode
;
86 if (iop
== NULL
|| iop
->_flag
== 0) {
91 if (!(iop
->_flag
& _IONBF
) && (iop
->_flag
& (_IOWRT
| _IOREAD
| _IORW
)))
92 (void) _fflush_u(iop
);
94 if (iop
->_flag
& _IOMYBUF
) {
95 free((char *)iop
->_base
- PUSHBACK
);
100 * Clear stream orientation, clear stream encoding rule, and set
101 * stream's mbstate_t object to describe an initial conversion state.
103 mb
= _getmbstate(iop
);
105 (void) memset(mb
, 0, sizeof (mbstate_t));
107 _setorientation(iop
, _NO_MODE
);
111 if (mode
!= 'r' && mode
!= 'w' && mode
!= 'a') {
116 if ((oflag
= fcntl(fd
, F_GETFL
)) == -1)
119 if ((plus
= type
[1]) == 'b')
123 * Because the filename has not been specified, the underlying file
124 * will not be closed and reopened. The access modes of an open
125 * file descriptor can't be changed via fcntl(). When '+' is
126 * specified, the old access mode needs to be O_RDWR. When 'r' is
127 * specified, the old access mode needs to be O_RDONLY or O_RDWR.
128 * When 'a' or 'w' is specified, the old access mode needs to be
129 * O_WRONLY or O_RDWR. Otherwise, fail with EBADF, indicating that
130 * the underlying file descriptor was not opened with a mode that
131 * would allow the stream to do successful I/O with the requested mode.
134 accmode
= oflag
& O_ACCMODE
;
135 if ((accmode
== O_RDONLY
&& (mode
!= 'r' || plus
== '+')) ||
136 (accmode
== O_WRONLY
&& (mode
== 'r' || plus
== '+'))) {
143 iop
->_flag
&= ~0377; /* clear lower 8-bits */
145 iop
->_flag
|= _IOREAD
;
146 nflag
= oflag
& ~O_APPEND
;
147 } else if (mode
== 'w') {
148 iop
->_flag
|= _IOWRT
;
149 nflag
= oflag
& ~O_APPEND
;
151 iop
->_flag
|= _IOWRT
;
152 nflag
= oflag
| O_APPEND
;
155 iop
->_flag
= (iop
->_flag
& ~(_IOREAD
| _IOWRT
)) | _IORW
;
159 iop
->_flag
= _IOREAD
;
160 nflag
= oflag
& ~O_APPEND
;
161 } else if (mode
== 'w') {
163 nflag
= oflag
& ~O_APPEND
;
166 nflag
= oflag
| O_APPEND
;
173 * Change mode of underlying fd as much as possible without closing
174 * and reopening it. Ignore truncate failures, eg. with stdout.
177 (void) ftruncate64(fd
, (off64_t
)0);
179 if (fcntl(fd
, F_SETFL
, nflag
) == -1)
182 /* ignore seek failures, eg. with pipes */
183 (void) lseek64(fd
, (off64_t
)0, SEEK_SET
);
192 iop
->_flag
= 0; /* release iop */
197 freopen(const char *name
, const char *type
, FILE *iop
)
202 if (name
== NULL
&& __xpg6
& _C99SUSv3_freopen_NULL_filename
) {
204 * XPG6: If name is a null pointer, freopen will attempt to
205 * change the mode of the stream to that specified by type.
208 rc
= _freopen_null(type
, iop
);
213 * there may be concurrent calls to reopen the same stream - need
214 * to make freopen() atomic
218 * new function to do everything that fclose() does, except
219 * to release the iop - this cannot yet be released since
220 * _endopen() is yet to be called on this iop
223 (void) close_fd(iop
);
225 rc
= _endopen(name
, type
, iop
, LARGE_OPEN
);
228 iop
->_flag
= 0; /* release iop */