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