xref: /onnv-gate/usr/src/lib/libc/port/stdio/fopen.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
42*6812Sraf #include "lint.h"
430Sstevel@tonic-gate #include "file64.h"
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <mtlib.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <limits.h>
500Sstevel@tonic-gate #include <thread.h>
510Sstevel@tonic-gate #include <synch.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <errno.h>
540Sstevel@tonic-gate #include "stdiom.h"
550Sstevel@tonic-gate #include "xpg6.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /* Final argument to _endopen depends on build environment */
580Sstevel@tonic-gate #define	LARGE_OPEN		(_FILE_OFFSET_BITS == 64)
590Sstevel@tonic-gate 
600Sstevel@tonic-gate FILE *
fopen(const char * name,const char * type)610Sstevel@tonic-gate fopen(const char *name, const char *type) /* open name, return new stream */
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	FILE *iop;
640Sstevel@tonic-gate 	FILE  *rc;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	iop = _findiop();
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * Note that iop is not locked here, since no other thread could
690Sstevel@tonic-gate 	 * possibly call _endopen with the same iop at this point.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 	rc = _endopen(name, type, iop, LARGE_OPEN);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (rc == NULL && iop != NULL)
740Sstevel@tonic-gate 		iop->_flag = 0; /* release iop */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	return (rc);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static FILE *
_freopen_null(const char * type,FILE * iop)800Sstevel@tonic-gate _freopen_null(const char *type, FILE *iop)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	char	plus, mode;
830Sstevel@tonic-gate 	int	oflag, nflag, fd, accmode;
840Sstevel@tonic-gate 	mbstate_t	*mb;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (iop == NULL || iop->_flag == 0) {
870Sstevel@tonic-gate 		errno = EBADF;
880Sstevel@tonic-gate 		return (NULL);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
920Sstevel@tonic-gate 		(void) _fflush_u(iop);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (iop->_flag & _IOMYBUF) {
950Sstevel@tonic-gate 		free((char *)iop->_base - PUSHBACK);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	iop->_base = NULL;
980Sstevel@tonic-gate 	iop->_ptr = NULL;
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * Clear stream orientation, clear stream encoding rule, and set
1010Sstevel@tonic-gate 	 * stream's mbstate_t object to describe an initial conversion state.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	mb = _getmbstate(iop);
1040Sstevel@tonic-gate 	if (mb != NULL)
1050Sstevel@tonic-gate 		(void) memset(mb, 0, sizeof (mbstate_t));
1060Sstevel@tonic-gate 	iop->_cnt = 0;
1070Sstevel@tonic-gate 	_setorientation(iop, _NO_MODE);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	fd = FILENO(iop);
1100Sstevel@tonic-gate 	mode = type[0];
1110Sstevel@tonic-gate 	if (mode != 'r' && mode != 'w' && mode != 'a') {
1120Sstevel@tonic-gate 		errno = EINVAL;
1130Sstevel@tonic-gate 		goto errret;
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if ((oflag = fcntl(fd, F_GETFL)) == -1)
1170Sstevel@tonic-gate 		goto errret;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((plus = type[1]) == 'b')
1200Sstevel@tonic-gate 		plus = type[2];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	/*
1230Sstevel@tonic-gate 	 * Because the filename has not been specified, the underlying file
1240Sstevel@tonic-gate 	 * will not be closed and reopened.  The access modes of an open
1250Sstevel@tonic-gate 	 * file descriptor can't be changed via fcntl().  When '+' is
1260Sstevel@tonic-gate 	 * specified, the old access mode needs to be O_RDWR.  When 'r' is
1270Sstevel@tonic-gate 	 * specified, the old access mode needs to be O_RDONLY or O_RDWR.
1280Sstevel@tonic-gate 	 * When 'a' or 'w' is specified, the old access mode needs to be
1290Sstevel@tonic-gate 	 * O_WRONLY or O_RDWR.  Otherwise, fail with EBADF, indicating that
1300Sstevel@tonic-gate 	 * the underlying file descriptor was not opened with a mode that
1310Sstevel@tonic-gate 	 * would allow the stream to do successful I/O with the requested mode.
1320Sstevel@tonic-gate 	 */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	accmode = oflag & O_ACCMODE;
1350Sstevel@tonic-gate 	if ((accmode == O_RDONLY && (mode != 'r' || plus == '+')) ||
1360Sstevel@tonic-gate 	    (accmode == O_WRONLY && (mode == 'r' || plus == '+'))) {
1370Sstevel@tonic-gate 		(void) close(fd);
1380Sstevel@tonic-gate 		errno = EBADF;
1390Sstevel@tonic-gate 		goto errret_noclose;
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #ifdef	_LP64
1430Sstevel@tonic-gate 	iop->_flag &= ~0377;	/* clear lower 8-bits */
1440Sstevel@tonic-gate 	if (mode == 'r') {
1450Sstevel@tonic-gate 		iop->_flag |= _IOREAD;
1460Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1470Sstevel@tonic-gate 	} else if (mode == 'w') {
1480Sstevel@tonic-gate 		iop->_flag |= _IOWRT;
1490Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1500Sstevel@tonic-gate 	} else {
1510Sstevel@tonic-gate 		iop->_flag |= _IOWRT;
1520Sstevel@tonic-gate 		nflag = oflag | O_APPEND;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	if (plus == '+') {
1550Sstevel@tonic-gate 		iop->_flag = (iop->_flag & ~(_IOREAD | _IOWRT)) | _IORW;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate #else
1580Sstevel@tonic-gate 	if (mode == 'r') {
1590Sstevel@tonic-gate 		iop->_flag = _IOREAD;
1600Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1610Sstevel@tonic-gate 	} else if (mode == 'w') {
1620Sstevel@tonic-gate 		iop->_flag = _IOWRT;
1630Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1640Sstevel@tonic-gate 	} else {
1650Sstevel@tonic-gate 		iop->_flag = _IOWRT;
1660Sstevel@tonic-gate 		nflag = oflag | O_APPEND;
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 	if (plus == '+') {
1690Sstevel@tonic-gate 		iop->_flag = _IORW;
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Change mode of underlying fd as much as possible without closing
1740Sstevel@tonic-gate 	 * and reopening it.  Ignore truncate failures, eg. with stdout.
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 	if (mode == 'w')
1770Sstevel@tonic-gate 		(void) ftruncate64(fd, (off64_t)0);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, nflag) == -1)
1800Sstevel@tonic-gate 		goto errret;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* ignore seek failures, eg. with pipes */
1830Sstevel@tonic-gate 	(void) lseek64(fd, (off64_t)0, SEEK_SET);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (iop);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate errret:
1880Sstevel@tonic-gate 	if (errno != EBADF)
1890Sstevel@tonic-gate 		(void) close(fd);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate errret_noclose:
1920Sstevel@tonic-gate 	iop->_flag = 0;		/* release iop */
1930Sstevel@tonic-gate 	return (NULL);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate FILE *
freopen(const char * name,const char * type,FILE * iop)1970Sstevel@tonic-gate freopen(const char *name, const char *type, FILE *iop)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	FILE *rc;
2000Sstevel@tonic-gate 	rmutex_t *lk;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (name == NULL && __xpg6 & _C99SUSv3_freopen_NULL_filename) {
2030Sstevel@tonic-gate 		/*
2040Sstevel@tonic-gate 		 * XPG6:  If name is a null pointer, freopen will attempt to
2050Sstevel@tonic-gate 		 * change the mode of the stream to that specified by type.
2060Sstevel@tonic-gate 		 */
2070Sstevel@tonic-gate 		FLOCKFILE(lk, iop);
2080Sstevel@tonic-gate 		rc = _freopen_null(type, iop);
2090Sstevel@tonic-gate 		FUNLOCKFILE(lk);
2100Sstevel@tonic-gate 		return (rc);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * there may be concurrent calls to reopen the same stream - need
2140Sstevel@tonic-gate 	 * to make freopen() atomic
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * new function to do everything that fclose() does, except
2190Sstevel@tonic-gate 	 * to release the iop - this cannot yet be released since
2200Sstevel@tonic-gate 	 * _endopen() is yet to be called on this iop
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	(void) close_fd(iop);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	rc = _endopen(name, type, iop, LARGE_OPEN);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (rc == NULL)
2280Sstevel@tonic-gate 		iop->_flag = 0; /* release iop */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	FUNLOCKFILE(lk);
2310Sstevel@tonic-gate 	return (rc);
2320Sstevel@tonic-gate }
233