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 5a5f69788Scraigm * Common Development and Distribution License (the "License"). 6a5f69788Scraigm * 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 */ 21a5f69788Scraigm 227c478bd9Sstevel@tonic-gate /* 23*5bbb4db2SGarrett D'Amore * Copyright 2009 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) 1984, 1986, 1987, 1988, 1989 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 407c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include "file64.h" 447c478bd9Sstevel@tonic-gate #include <stdio.h> 45*5bbb4db2SGarrett D'Amore #include <sys/fcntl.h> 467c478bd9Sstevel@tonic-gate #include <unistd.h> 477c478bd9Sstevel@tonic-gate #include <sys/file.h> 487c478bd9Sstevel@tonic-gate #include "stdiom.h" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* Final argument to _endopen depends on build environment */ 517c478bd9Sstevel@tonic-gate #define ALWAYS_LARGE_OPEN 1 527c478bd9Sstevel@tonic-gate #define LARGE_OPEN (_FILE_OFFSET_BITS == 64) 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate static FILE * 557c478bd9Sstevel@tonic-gate _endopen(const char *file, const char *mode, FILE *iop, int largefile) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate int plus, oflag, fd; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate if (iop == NULL || file == NULL || file[0] == '\0') 607c478bd9Sstevel@tonic-gate return (NULL); 617c478bd9Sstevel@tonic-gate plus = (mode[1] == '+'); 627c478bd9Sstevel@tonic-gate switch (mode[0]) { 637c478bd9Sstevel@tonic-gate case 'w': 647c478bd9Sstevel@tonic-gate oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT; 657c478bd9Sstevel@tonic-gate break; 667c478bd9Sstevel@tonic-gate case 'a': 677c478bd9Sstevel@tonic-gate oflag = (plus ? O_RDWR : O_WRONLY) | O_CREAT; 687c478bd9Sstevel@tonic-gate break; 697c478bd9Sstevel@tonic-gate case 'r': 707c478bd9Sstevel@tonic-gate oflag = plus ? O_RDWR : O_RDONLY; 717c478bd9Sstevel@tonic-gate break; 727c478bd9Sstevel@tonic-gate default: 737c478bd9Sstevel@tonic-gate return (NULL); 747c478bd9Sstevel@tonic-gate } 75a5f69788Scraigm 767c478bd9Sstevel@tonic-gate if (largefile) { 777c478bd9Sstevel@tonic-gate fd = open64(file, oflag, 0666); /* mapped to open() for V9 */ 787c478bd9Sstevel@tonic-gate } else { 797c478bd9Sstevel@tonic-gate fd = open(file, oflag, 0666); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate if (fd < 0) 827c478bd9Sstevel@tonic-gate return (NULL); 837c478bd9Sstevel@tonic-gate iop->_cnt = 0; 84a5f69788Scraigm #ifdef _LP64 85a5f69788Scraigm iop->_file = fd; 86a5f69788Scraigm #else 87a5f69788Scraigm if (fd <= _FILE_FD_MAX) { 88a5f69788Scraigm SET_FILE(iop, fd); 89a5f69788Scraigm } else if (_file_set(iop, fd, mode) != 0) { 90a5f69788Scraigm /* errno set in _file_set() */ 91a5f69788Scraigm (void) close(fd); 92a5f69788Scraigm return (NULL); 93a5f69788Scraigm } 94a5f69788Scraigm #endif 957c478bd9Sstevel@tonic-gate iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT; 967c478bd9Sstevel@tonic-gate if (mode[0] == 'a') { 977c478bd9Sstevel@tonic-gate if ((lseek64(fd, 0L, SEEK_END)) < 0) { 987c478bd9Sstevel@tonic-gate (void) close(fd); 997c478bd9Sstevel@tonic-gate return (NULL); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate iop->_base = iop->_ptr = NULL; 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * Sys5 does not support _bufsiz 1057c478bd9Sstevel@tonic-gate * 1067c478bd9Sstevel@tonic-gate * iop->_bufsiz = 0; 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate return (iop); 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate FILE * 1127c478bd9Sstevel@tonic-gate fopen(const char *file, const char *mode) 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate FILE *iop; 1157c478bd9Sstevel@tonic-gate FILE *rc; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate iop = _findiop(); 1187c478bd9Sstevel@tonic-gate rc = _endopen(file, mode, iop, LARGE_OPEN); 1197c478bd9Sstevel@tonic-gate if (rc == NULL && iop != NULL) 1207c478bd9Sstevel@tonic-gate iop->_flag = 0; /* release iop */ 1217c478bd9Sstevel@tonic-gate return (rc); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * For _LP64, all fopen() calls are 64-bit calls, i.e., open64() system call. 1267c478bd9Sstevel@tonic-gate * There should not be fopen64() calls. 1277c478bd9Sstevel@tonic-gate * Similar for freopen64(). 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate #if !defined(_LP64) 1307c478bd9Sstevel@tonic-gate FILE * 1317c478bd9Sstevel@tonic-gate fopen64(const char *file, const char *mode) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate FILE *iop; 1347c478bd9Sstevel@tonic-gate FILE *rc; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate iop = _findiop(); 1377c478bd9Sstevel@tonic-gate rc = _endopen(file, mode, iop, ALWAYS_LARGE_OPEN); 1387c478bd9Sstevel@tonic-gate if (rc == NULL && iop != NULL) 1397c478bd9Sstevel@tonic-gate iop->_flag = 0; /* release iop */ 1407c478bd9Sstevel@tonic-gate return (rc); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate #endif 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate FILE * 1457c478bd9Sstevel@tonic-gate freopen(const char *file, const char *mode, FILE *iop) 1467c478bd9Sstevel@tonic-gate { 1477c478bd9Sstevel@tonic-gate (void) fclose(iop); /* doesn't matter if this fails */ 1487c478bd9Sstevel@tonic-gate return (_endopen(file, mode, iop, LARGE_OPEN)); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate #if !defined(_LP64) 1527c478bd9Sstevel@tonic-gate FILE * 1537c478bd9Sstevel@tonic-gate freopen64(const char *file, const char *mode, FILE *iop) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate (void) fclose(iop); /* doesn't matter if this fails */ 1567c478bd9Sstevel@tonic-gate return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN)); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate #endif 159