xref: /illumos-gate/usr/src/lib/libc/port/stdio/fopen.c (revision cd62a92d)
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
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * 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 /*
237257d1b4Sraf  * 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 
40*cd62a92dSRobert Mustacchi /*
41*cd62a92dSRobert Mustacchi  * Copyright 2020 Robert Mustacchi
42*cd62a92dSRobert Mustacchi  */
437c478bd9Sstevel@tonic-gate 
447257d1b4Sraf #include "lint.h"
457c478bd9Sstevel@tonic-gate #include "file64.h"
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <mtlib.h>
497c478bd9Sstevel@tonic-gate #include <fcntl.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <limits.h>
527c478bd9Sstevel@tonic-gate #include <thread.h>
537c478bd9Sstevel@tonic-gate #include <synch.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
567c478bd9Sstevel@tonic-gate #include "stdiom.h"
577c478bd9Sstevel@tonic-gate #include "xpg6.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Final argument to _endopen depends on build environment */
607c478bd9Sstevel@tonic-gate #define	LARGE_OPEN		(_FILE_OFFSET_BITS == 64)
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate FILE *
fopen(const char * name,const char * type)637c478bd9Sstevel@tonic-gate fopen(const char *name, const char *type) /* open name, return new stream */
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	FILE *iop;
667c478bd9Sstevel@tonic-gate 	FILE  *rc;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	iop = _findiop();
697c478bd9Sstevel@tonic-gate 	/*
707c478bd9Sstevel@tonic-gate 	 * Note that iop is not locked here, since no other thread could
717c478bd9Sstevel@tonic-gate 	 * possibly call _endopen with the same iop at this point.
727c478bd9Sstevel@tonic-gate 	 */
737c478bd9Sstevel@tonic-gate 	rc = _endopen(name, type, iop, LARGE_OPEN);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (rc == NULL && iop != NULL)
767c478bd9Sstevel@tonic-gate 		iop->_flag = 0; /* release iop */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (rc);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static FILE *
_freopen_null(const char * type,FILE * iop)827c478bd9Sstevel@tonic-gate _freopen_null(const char *type, FILE *iop)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	char	plus, mode;
857c478bd9Sstevel@tonic-gate 	int	oflag, nflag, fd, accmode;
867c478bd9Sstevel@tonic-gate 	mbstate_t	*mb;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (iop == NULL || iop->_flag == 0) {
897c478bd9Sstevel@tonic-gate 		errno = EBADF;
907c478bd9Sstevel@tonic-gate 		return (NULL);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
93*cd62a92dSRobert Mustacchi 	/*
94*cd62a92dSRobert Mustacchi 	 * If this is not a file-based stream (as in we have no file
95*cd62a92dSRobert Mustacchi 	 * descriptor), then we need to close this, but still actually return an
96*cd62a92dSRobert Mustacchi 	 * error.
97*cd62a92dSRobert Mustacchi 	 */
98*cd62a92dSRobert Mustacchi 	if (_get_fd(iop) == -1) {
99*cd62a92dSRobert Mustacchi 		(void) close_fd(iop);
100*cd62a92dSRobert Mustacchi 		errno = EBADF;
101*cd62a92dSRobert Mustacchi 		return (NULL);
102*cd62a92dSRobert Mustacchi 	}
103*cd62a92dSRobert Mustacchi 
1047c478bd9Sstevel@tonic-gate 	if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
1057c478bd9Sstevel@tonic-gate 		(void) _fflush_u(iop);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (iop->_flag & _IOMYBUF) {
1087c478bd9Sstevel@tonic-gate 		free((char *)iop->_base - PUSHBACK);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	iop->_base = NULL;
1117c478bd9Sstevel@tonic-gate 	iop->_ptr = NULL;
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * Clear stream orientation, clear stream encoding rule, and set
1147c478bd9Sstevel@tonic-gate 	 * stream's mbstate_t object to describe an initial conversion state.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	mb = _getmbstate(iop);
1177c478bd9Sstevel@tonic-gate 	if (mb != NULL)
1187c478bd9Sstevel@tonic-gate 		(void) memset(mb, 0, sizeof (mbstate_t));
1197c478bd9Sstevel@tonic-gate 	iop->_cnt = 0;
1207c478bd9Sstevel@tonic-gate 	_setorientation(iop, _NO_MODE);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	fd = FILENO(iop);
1237c478bd9Sstevel@tonic-gate 	mode = type[0];
1247c478bd9Sstevel@tonic-gate 	if (mode != 'r' && mode != 'w' && mode != 'a') {
1257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1267c478bd9Sstevel@tonic-gate 		goto errret;
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((oflag = fcntl(fd, F_GETFL)) == -1)
1307c478bd9Sstevel@tonic-gate 		goto errret;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if ((plus = type[1]) == 'b')
1337c478bd9Sstevel@tonic-gate 		plus = type[2];
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * Because the filename has not been specified, the underlying file
1377c478bd9Sstevel@tonic-gate 	 * will not be closed and reopened.  The access modes of an open
1387c478bd9Sstevel@tonic-gate 	 * file descriptor can't be changed via fcntl().  When '+' is
1397c478bd9Sstevel@tonic-gate 	 * specified, the old access mode needs to be O_RDWR.  When 'r' is
1407c478bd9Sstevel@tonic-gate 	 * specified, the old access mode needs to be O_RDONLY or O_RDWR.
1417c478bd9Sstevel@tonic-gate 	 * When 'a' or 'w' is specified, the old access mode needs to be
1427c478bd9Sstevel@tonic-gate 	 * O_WRONLY or O_RDWR.  Otherwise, fail with EBADF, indicating that
1437c478bd9Sstevel@tonic-gate 	 * the underlying file descriptor was not opened with a mode that
1447c478bd9Sstevel@tonic-gate 	 * would allow the stream to do successful I/O with the requested mode.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	accmode = oflag & O_ACCMODE;
1487c478bd9Sstevel@tonic-gate 	if ((accmode == O_RDONLY && (mode != 'r' || plus == '+')) ||
1497c478bd9Sstevel@tonic-gate 	    (accmode == O_WRONLY && (mode == 'r' || plus == '+'))) {
1507c478bd9Sstevel@tonic-gate 		(void) close(fd);
1517c478bd9Sstevel@tonic-gate 		errno = EBADF;
1527c478bd9Sstevel@tonic-gate 		goto errret_noclose;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #ifdef	_LP64
156*cd62a92dSRobert Mustacchi 	iop->_flag &= ~_DEF_FLAG_MASK;	/* clear lower 8-bits */
1577c478bd9Sstevel@tonic-gate 	if (mode == 'r') {
1587c478bd9Sstevel@tonic-gate 		iop->_flag |= _IOREAD;
1597c478bd9Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1607c478bd9Sstevel@tonic-gate 	} else if (mode == 'w') {
1617c478bd9Sstevel@tonic-gate 		iop->_flag |= _IOWRT;
1627c478bd9Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1637c478bd9Sstevel@tonic-gate 	} else {
1647c478bd9Sstevel@tonic-gate 		iop->_flag |= _IOWRT;
1657c478bd9Sstevel@tonic-gate 		nflag = oflag | O_APPEND;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	if (plus == '+') {
1687c478bd9Sstevel@tonic-gate 		iop->_flag = (iop->_flag & ~(_IOREAD | _IOWRT)) | _IORW;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate #else
1717c478bd9Sstevel@tonic-gate 	if (mode == 'r') {
1727c478bd9Sstevel@tonic-gate 		iop->_flag = _IOREAD;
1737c478bd9Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1747c478bd9Sstevel@tonic-gate 	} else if (mode == 'w') {
1757c478bd9Sstevel@tonic-gate 		iop->_flag = _IOWRT;
1767c478bd9Sstevel@tonic-gate 		nflag = oflag & ~O_APPEND;
1777c478bd9Sstevel@tonic-gate 	} else {
1787c478bd9Sstevel@tonic-gate 		iop->_flag = _IOWRT;
1797c478bd9Sstevel@tonic-gate 		nflag = oflag | O_APPEND;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	if (plus == '+') {
1827c478bd9Sstevel@tonic-gate 		iop->_flag = _IORW;
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 	/*
1867c478bd9Sstevel@tonic-gate 	 * Change mode of underlying fd as much as possible without closing
1877c478bd9Sstevel@tonic-gate 	 * and reopening it.  Ignore truncate failures, eg. with stdout.
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	if (mode == 'w')
1907c478bd9Sstevel@tonic-gate 		(void) ftruncate64(fd, (off64_t)0);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, nflag) == -1)
1937c478bd9Sstevel@tonic-gate 		goto errret;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/* ignore seek failures, eg. with pipes */
1967c478bd9Sstevel@tonic-gate 	(void) lseek64(fd, (off64_t)0, SEEK_SET);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (iop);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate errret:
2017c478bd9Sstevel@tonic-gate 	if (errno != EBADF)
2027c478bd9Sstevel@tonic-gate 		(void) close(fd);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate errret_noclose:
2057c478bd9Sstevel@tonic-gate 	iop->_flag = 0;		/* release iop */
2067c478bd9Sstevel@tonic-gate 	return (NULL);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate FILE *
freopen(const char * name,const char * type,FILE * iop)2107c478bd9Sstevel@tonic-gate freopen(const char *name, const char *type, FILE *iop)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	FILE *rc;
2137c478bd9Sstevel@tonic-gate 	rmutex_t *lk;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (name == NULL && __xpg6 & _C99SUSv3_freopen_NULL_filename) {
2167c478bd9Sstevel@tonic-gate 		/*
2177c478bd9Sstevel@tonic-gate 		 * XPG6:  If name is a null pointer, freopen will attempt to
2187c478bd9Sstevel@tonic-gate 		 * change the mode of the stream to that specified by type.
2197c478bd9Sstevel@tonic-gate 		 */
2207c478bd9Sstevel@tonic-gate 		FLOCKFILE(lk, iop);
2217c478bd9Sstevel@tonic-gate 		rc = _freopen_null(type, iop);
2227c478bd9Sstevel@tonic-gate 		FUNLOCKFILE(lk);
2237c478bd9Sstevel@tonic-gate 		return (rc);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * there may be concurrent calls to reopen the same stream - need
2277c478bd9Sstevel@tonic-gate 	 * to make freopen() atomic
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * new function to do everything that fclose() does, except
2327c478bd9Sstevel@tonic-gate 	 * to release the iop - this cannot yet be released since
2337c478bd9Sstevel@tonic-gate 	 * _endopen() is yet to be called on this iop
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	(void) close_fd(iop);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	rc = _endopen(name, type, iop, LARGE_OPEN);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (rc == NULL)
2417c478bd9Sstevel@tonic-gate 		iop->_flag = 0; /* release iop */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	FUNLOCKFILE(lk);
2447c478bd9Sstevel@tonic-gate 	return (rc);
2457c478bd9Sstevel@tonic-gate }
246