xref: /onnv-gate/usr/src/ucblib/libucb/port/stdio/fopen.c (revision 1846:376b8b33ed65)
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*1846Scraigm  * Common Development and Distribution License (the "License").
6*1846Scraigm  * 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  */
21*1846Scraigm 
220Sstevel@tonic-gate /*
23*1846Scraigm  * Copyright 2006 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) 1984, 1986, 1987, 1988, 1989 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 
420Sstevel@tonic-gate /*LINTLIBRARY*/
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include "file64.h"
460Sstevel@tonic-gate #include <stdio.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <sys/file.h>
500Sstevel@tonic-gate #include "stdiom.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /* Final argument to _endopen depends on build environment */
530Sstevel@tonic-gate #define	ALWAYS_LARGE_OPEN	1
540Sstevel@tonic-gate #define	LARGE_OPEN	(_FILE_OFFSET_BITS == 64)
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static FILE *
570Sstevel@tonic-gate _endopen(const char *file, const char *mode, FILE *iop, int largefile)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	int	plus, oflag, fd;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if (iop == NULL || file == NULL || file[0] == '\0')
620Sstevel@tonic-gate 		return (NULL);
630Sstevel@tonic-gate 	plus = (mode[1] == '+');
640Sstevel@tonic-gate 	switch (mode[0]) {
650Sstevel@tonic-gate 	case 'w':
660Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
670Sstevel@tonic-gate 		break;
680Sstevel@tonic-gate 	case 'a':
690Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | O_CREAT;
700Sstevel@tonic-gate 		break;
710Sstevel@tonic-gate 	case 'r':
720Sstevel@tonic-gate 		oflag = plus ? O_RDWR : O_RDONLY;
730Sstevel@tonic-gate 		break;
740Sstevel@tonic-gate 	default:
750Sstevel@tonic-gate 		return (NULL);
760Sstevel@tonic-gate 	}
77*1846Scraigm 
780Sstevel@tonic-gate 	if (largefile) {
790Sstevel@tonic-gate 		fd = open64(file, oflag, 0666);	/* mapped to open() for V9 */
800Sstevel@tonic-gate 	} else {
810Sstevel@tonic-gate 		fd = open(file, oflag, 0666);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	if (fd < 0)
840Sstevel@tonic-gate 		return (NULL);
850Sstevel@tonic-gate 	iop->_cnt = 0;
86*1846Scraigm #ifdef _LP64
87*1846Scraigm 	iop->_file = fd;
88*1846Scraigm #else
89*1846Scraigm 	if (fd <= _FILE_FD_MAX) {
90*1846Scraigm 		SET_FILE(iop, fd);
91*1846Scraigm 	} else if (_file_set(iop, fd, mode) != 0) {
92*1846Scraigm 		/* errno set in _file_set() */
93*1846Scraigm 		(void) close(fd);
94*1846Scraigm 		return (NULL);
95*1846Scraigm 	}
96*1846Scraigm #endif
970Sstevel@tonic-gate 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
980Sstevel@tonic-gate 	if (mode[0] == 'a')   {
990Sstevel@tonic-gate 		if ((lseek64(fd, 0L, SEEK_END)) < 0)  {
1000Sstevel@tonic-gate 			(void) close(fd);
1010Sstevel@tonic-gate 			return (NULL);
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	iop->_base = iop->_ptr = NULL;
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * Sys5 does not support _bufsiz
1070Sstevel@tonic-gate 	 *
1080Sstevel@tonic-gate 	 * iop->_bufsiz = 0;
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	return (iop);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate FILE *
1140Sstevel@tonic-gate fopen(const char *file, const char *mode)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	FILE	*iop;
1170Sstevel@tonic-gate 	FILE	*rc;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	iop = _findiop();
1200Sstevel@tonic-gate 	rc = _endopen(file, mode, iop, LARGE_OPEN);
1210Sstevel@tonic-gate 	if (rc == NULL && iop != NULL)
1220Sstevel@tonic-gate 		iop->_flag = 0;	/* release iop */
1230Sstevel@tonic-gate 	return (rc);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * For _LP64, all fopen() calls are 64-bit calls, i.e., open64() system call.
1280Sstevel@tonic-gate  * There should not be fopen64() calls.
1290Sstevel@tonic-gate  * Similar for freopen64().
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate #if !defined(_LP64)
1320Sstevel@tonic-gate FILE *
1330Sstevel@tonic-gate fopen64(const char *file, const char *mode)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	FILE	*iop;
1360Sstevel@tonic-gate 	FILE	*rc;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	iop = _findiop();
1390Sstevel@tonic-gate 	rc = _endopen(file, mode, iop, ALWAYS_LARGE_OPEN);
1400Sstevel@tonic-gate 	if (rc == NULL && iop != NULL)
1410Sstevel@tonic-gate 		iop->_flag = 0;	/* release iop */
1420Sstevel@tonic-gate 	return (rc);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate FILE *
1470Sstevel@tonic-gate freopen(const char *file, const char *mode, FILE *iop)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	(void) fclose(iop); /* doesn't matter if this fails */
1500Sstevel@tonic-gate 	return (_endopen(file, mode, iop, LARGE_OPEN));
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #if !defined(_LP64)
1540Sstevel@tonic-gate FILE *
1550Sstevel@tonic-gate freopen64(const char *file, const char *mode, FILE *iop)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	(void) fclose(iop); /* doesn't matter if this fails */
1580Sstevel@tonic-gate 	return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN));
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate #endif
161