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