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
51846Scraigm * Common Development and Distribution License (the "License").
61846Scraigm * 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 */
211846Scraigm
220Sstevel@tonic-gate /*
23*10754SGarrett.Damore@Sun.COM * Copyright 2009 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 /*LINTLIBRARY*/
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include "file64.h"
440Sstevel@tonic-gate #include <stdio.h>
45*10754SGarrett.Damore@Sun.COM #include <sys/fcntl.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <sys/file.h>
480Sstevel@tonic-gate #include "stdiom.h"
490Sstevel@tonic-gate
500Sstevel@tonic-gate /* Final argument to _endopen depends on build environment */
510Sstevel@tonic-gate #define ALWAYS_LARGE_OPEN 1
520Sstevel@tonic-gate #define LARGE_OPEN (_FILE_OFFSET_BITS == 64)
530Sstevel@tonic-gate
540Sstevel@tonic-gate static FILE *
_endopen(const char * file,const char * mode,FILE * iop,int largefile)550Sstevel@tonic-gate _endopen(const char *file, const char *mode, FILE *iop, int largefile)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate int plus, oflag, fd;
580Sstevel@tonic-gate
590Sstevel@tonic-gate if (iop == NULL || file == NULL || file[0] == '\0')
600Sstevel@tonic-gate return (NULL);
610Sstevel@tonic-gate plus = (mode[1] == '+');
620Sstevel@tonic-gate switch (mode[0]) {
630Sstevel@tonic-gate case 'w':
640Sstevel@tonic-gate oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
650Sstevel@tonic-gate break;
660Sstevel@tonic-gate case 'a':
670Sstevel@tonic-gate oflag = (plus ? O_RDWR : O_WRONLY) | O_CREAT;
680Sstevel@tonic-gate break;
690Sstevel@tonic-gate case 'r':
700Sstevel@tonic-gate oflag = plus ? O_RDWR : O_RDONLY;
710Sstevel@tonic-gate break;
720Sstevel@tonic-gate default:
730Sstevel@tonic-gate return (NULL);
740Sstevel@tonic-gate }
751846Scraigm
760Sstevel@tonic-gate if (largefile) {
770Sstevel@tonic-gate fd = open64(file, oflag, 0666); /* mapped to open() for V9 */
780Sstevel@tonic-gate } else {
790Sstevel@tonic-gate fd = open(file, oflag, 0666);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate if (fd < 0)
820Sstevel@tonic-gate return (NULL);
830Sstevel@tonic-gate iop->_cnt = 0;
841846Scraigm #ifdef _LP64
851846Scraigm iop->_file = fd;
861846Scraigm #else
871846Scraigm if (fd <= _FILE_FD_MAX) {
881846Scraigm SET_FILE(iop, fd);
891846Scraigm } else if (_file_set(iop, fd, mode) != 0) {
901846Scraigm /* errno set in _file_set() */
911846Scraigm (void) close(fd);
921846Scraigm return (NULL);
931846Scraigm }
941846Scraigm #endif
950Sstevel@tonic-gate iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
960Sstevel@tonic-gate if (mode[0] == 'a') {
970Sstevel@tonic-gate if ((lseek64(fd, 0L, SEEK_END)) < 0) {
980Sstevel@tonic-gate (void) close(fd);
990Sstevel@tonic-gate return (NULL);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate iop->_base = iop->_ptr = NULL;
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * Sys5 does not support _bufsiz
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * iop->_bufsiz = 0;
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate return (iop);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate FILE *
fopen(const char * file,const char * mode)1120Sstevel@tonic-gate fopen(const char *file, const char *mode)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate FILE *iop;
1150Sstevel@tonic-gate FILE *rc;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate iop = _findiop();
1180Sstevel@tonic-gate rc = _endopen(file, mode, iop, LARGE_OPEN);
1190Sstevel@tonic-gate if (rc == NULL && iop != NULL)
1200Sstevel@tonic-gate iop->_flag = 0; /* release iop */
1210Sstevel@tonic-gate return (rc);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * For _LP64, all fopen() calls are 64-bit calls, i.e., open64() system call.
1260Sstevel@tonic-gate * There should not be fopen64() calls.
1270Sstevel@tonic-gate * Similar for freopen64().
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate #if !defined(_LP64)
1300Sstevel@tonic-gate FILE *
fopen64(const char * file,const char * mode)1310Sstevel@tonic-gate fopen64(const char *file, const char *mode)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate FILE *iop;
1340Sstevel@tonic-gate FILE *rc;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate iop = _findiop();
1370Sstevel@tonic-gate rc = _endopen(file, mode, iop, ALWAYS_LARGE_OPEN);
1380Sstevel@tonic-gate if (rc == NULL && iop != NULL)
1390Sstevel@tonic-gate iop->_flag = 0; /* release iop */
1400Sstevel@tonic-gate return (rc);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate #endif
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate FILE *
freopen(const char * file,const char * mode,FILE * iop)1450Sstevel@tonic-gate freopen(const char *file, const char *mode, FILE *iop)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate (void) fclose(iop); /* doesn't matter if this fails */
1480Sstevel@tonic-gate return (_endopen(file, mode, iop, LARGE_OPEN));
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #if !defined(_LP64)
1520Sstevel@tonic-gate FILE *
freopen64(const char * file,const char * mode,FILE * iop)1530Sstevel@tonic-gate freopen64(const char *file, const char *mode, FILE *iop)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate (void) fclose(iop); /* doesn't matter if this fails */
1560Sstevel@tonic-gate return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN));
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate #endif
159