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*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * 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 */
21e8031f0aSraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
417c478bd9Sstevel@tonic-gate
42*7257d1b4Sraf #include "lint.h"
437c478bd9Sstevel@tonic-gate #include "file64.h"
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <mtlib.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <limits.h>
507c478bd9Sstevel@tonic-gate #include <thread.h>
517c478bd9Sstevel@tonic-gate #include <synch.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate #include "stdiom.h"
557c478bd9Sstevel@tonic-gate #include "xpg6.h"
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /* Final argument to _endopen depends on build environment */
587c478bd9Sstevel@tonic-gate #define LARGE_OPEN (_FILE_OFFSET_BITS == 64)
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate FILE *
fopen(const char * name,const char * type)617c478bd9Sstevel@tonic-gate fopen(const char *name, const char *type) /* open name, return new stream */
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate FILE *iop;
647c478bd9Sstevel@tonic-gate FILE *rc;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate iop = _findiop();
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * Note that iop is not locked here, since no other thread could
697c478bd9Sstevel@tonic-gate * possibly call _endopen with the same iop at this point.
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate rc = _endopen(name, type, iop, LARGE_OPEN);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate if (rc == NULL && iop != NULL)
747c478bd9Sstevel@tonic-gate iop->_flag = 0; /* release iop */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate return (rc);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate static FILE *
_freopen_null(const char * type,FILE * iop)807c478bd9Sstevel@tonic-gate _freopen_null(const char *type, FILE *iop)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate char plus, mode;
837c478bd9Sstevel@tonic-gate int oflag, nflag, fd, accmode;
847c478bd9Sstevel@tonic-gate mbstate_t *mb;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if (iop == NULL || iop->_flag == 0) {
877c478bd9Sstevel@tonic-gate errno = EBADF;
887c478bd9Sstevel@tonic-gate return (NULL);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
927c478bd9Sstevel@tonic-gate (void) _fflush_u(iop);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) {
957c478bd9Sstevel@tonic-gate free((char *)iop->_base - PUSHBACK);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate iop->_base = NULL;
987c478bd9Sstevel@tonic-gate iop->_ptr = NULL;
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * Clear stream orientation, clear stream encoding rule, and set
1017c478bd9Sstevel@tonic-gate * stream's mbstate_t object to describe an initial conversion state.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate mb = _getmbstate(iop);
1047c478bd9Sstevel@tonic-gate if (mb != NULL)
1057c478bd9Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t));
1067c478bd9Sstevel@tonic-gate iop->_cnt = 0;
1077c478bd9Sstevel@tonic-gate _setorientation(iop, _NO_MODE);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate fd = FILENO(iop);
1107c478bd9Sstevel@tonic-gate mode = type[0];
1117c478bd9Sstevel@tonic-gate if (mode != 'r' && mode != 'w' && mode != 'a') {
1127c478bd9Sstevel@tonic-gate errno = EINVAL;
1137c478bd9Sstevel@tonic-gate goto errret;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if ((oflag = fcntl(fd, F_GETFL)) == -1)
1177c478bd9Sstevel@tonic-gate goto errret;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate if ((plus = type[1]) == 'b')
1207c478bd9Sstevel@tonic-gate plus = type[2];
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Because the filename has not been specified, the underlying file
1247c478bd9Sstevel@tonic-gate * will not be closed and reopened. The access modes of an open
1257c478bd9Sstevel@tonic-gate * file descriptor can't be changed via fcntl(). When '+' is
1267c478bd9Sstevel@tonic-gate * specified, the old access mode needs to be O_RDWR. When 'r' is
1277c478bd9Sstevel@tonic-gate * specified, the old access mode needs to be O_RDONLY or O_RDWR.
1287c478bd9Sstevel@tonic-gate * When 'a' or 'w' is specified, the old access mode needs to be
1297c478bd9Sstevel@tonic-gate * O_WRONLY or O_RDWR. Otherwise, fail with EBADF, indicating that
1307c478bd9Sstevel@tonic-gate * the underlying file descriptor was not opened with a mode that
1317c478bd9Sstevel@tonic-gate * would allow the stream to do successful I/O with the requested mode.
1327c478bd9Sstevel@tonic-gate */
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate accmode = oflag & O_ACCMODE;
1357c478bd9Sstevel@tonic-gate if ((accmode == O_RDONLY && (mode != 'r' || plus == '+')) ||
1367c478bd9Sstevel@tonic-gate (accmode == O_WRONLY && (mode == 'r' || plus == '+'))) {
1377c478bd9Sstevel@tonic-gate (void) close(fd);
1387c478bd9Sstevel@tonic-gate errno = EBADF;
1397c478bd9Sstevel@tonic-gate goto errret_noclose;
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate #ifdef _LP64
1437c478bd9Sstevel@tonic-gate iop->_flag &= ~0377; /* clear lower 8-bits */
1447c478bd9Sstevel@tonic-gate if (mode == 'r') {
1457c478bd9Sstevel@tonic-gate iop->_flag |= _IOREAD;
1467c478bd9Sstevel@tonic-gate nflag = oflag & ~O_APPEND;
1477c478bd9Sstevel@tonic-gate } else if (mode == 'w') {
1487c478bd9Sstevel@tonic-gate iop->_flag |= _IOWRT;
1497c478bd9Sstevel@tonic-gate nflag = oflag & ~O_APPEND;
1507c478bd9Sstevel@tonic-gate } else {
1517c478bd9Sstevel@tonic-gate iop->_flag |= _IOWRT;
1527c478bd9Sstevel@tonic-gate nflag = oflag | O_APPEND;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate if (plus == '+') {
1557c478bd9Sstevel@tonic-gate iop->_flag = (iop->_flag & ~(_IOREAD | _IOWRT)) | _IORW;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate #else
1587c478bd9Sstevel@tonic-gate if (mode == 'r') {
1597c478bd9Sstevel@tonic-gate iop->_flag = _IOREAD;
1607c478bd9Sstevel@tonic-gate nflag = oflag & ~O_APPEND;
1617c478bd9Sstevel@tonic-gate } else if (mode == 'w') {
1627c478bd9Sstevel@tonic-gate iop->_flag = _IOWRT;
1637c478bd9Sstevel@tonic-gate nflag = oflag & ~O_APPEND;
1647c478bd9Sstevel@tonic-gate } else {
1657c478bd9Sstevel@tonic-gate iop->_flag = _IOWRT;
1667c478bd9Sstevel@tonic-gate nflag = oflag | O_APPEND;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate if (plus == '+') {
1697c478bd9Sstevel@tonic-gate iop->_flag = _IORW;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate #endif
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate * Change mode of underlying fd as much as possible without closing
1747c478bd9Sstevel@tonic-gate * and reopening it. Ignore truncate failures, eg. with stdout.
1757c478bd9Sstevel@tonic-gate */
1767c478bd9Sstevel@tonic-gate if (mode == 'w')
1777c478bd9Sstevel@tonic-gate (void) ftruncate64(fd, (off64_t)0);
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate if (fcntl(fd, F_SETFL, nflag) == -1)
1807c478bd9Sstevel@tonic-gate goto errret;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /* ignore seek failures, eg. with pipes */
1837c478bd9Sstevel@tonic-gate (void) lseek64(fd, (off64_t)0, SEEK_SET);
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate return (iop);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate errret:
1887c478bd9Sstevel@tonic-gate if (errno != EBADF)
1897c478bd9Sstevel@tonic-gate (void) close(fd);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate errret_noclose:
1927c478bd9Sstevel@tonic-gate iop->_flag = 0; /* release iop */
1937c478bd9Sstevel@tonic-gate return (NULL);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate FILE *
freopen(const char * name,const char * type,FILE * iop)1977c478bd9Sstevel@tonic-gate freopen(const char *name, const char *type, FILE *iop)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate FILE *rc;
2007c478bd9Sstevel@tonic-gate rmutex_t *lk;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate if (name == NULL && __xpg6 & _C99SUSv3_freopen_NULL_filename) {
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate * XPG6: If name is a null pointer, freopen will attempt to
2057c478bd9Sstevel@tonic-gate * change the mode of the stream to that specified by type.
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate FLOCKFILE(lk, iop);
2087c478bd9Sstevel@tonic-gate rc = _freopen_null(type, iop);
2097c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
2107c478bd9Sstevel@tonic-gate return (rc);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * there may be concurrent calls to reopen the same stream - need
2147c478bd9Sstevel@tonic-gate * to make freopen() atomic
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate FLOCKFILE(lk, iop);
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * new function to do everything that fclose() does, except
2197c478bd9Sstevel@tonic-gate * to release the iop - this cannot yet be released since
2207c478bd9Sstevel@tonic-gate * _endopen() is yet to be called on this iop
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate (void) close_fd(iop);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate rc = _endopen(name, type, iop, LARGE_OPEN);
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (rc == NULL)
2287c478bd9Sstevel@tonic-gate iop->_flag = 0; /* release iop */
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
2317c478bd9Sstevel@tonic-gate return (rc);
2327c478bd9Sstevel@tonic-gate }
233