1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /*
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
34 *
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
38 */
39
40 /*LINTLIBRARY*/
41
42 #include <sys/types.h>
43 #include "file64.h"
44 #include <stdio.h>
45 #include <sys/fcntl.h>
46 #include <unistd.h>
47 #include <sys/file.h>
48 #include "stdiom.h"
49
50 /* Final argument to _endopen depends on build environment */
51 #define ALWAYS_LARGE_OPEN 1
52 #define LARGE_OPEN (_FILE_OFFSET_BITS == 64)
53
54 static FILE *
_endopen(const char * file,const char * mode,FILE * iop,int largefile)55 _endopen(const char *file, const char *mode, FILE *iop, int largefile)
56 {
57 int plus, oflag, fd;
58
59 if (iop == NULL || file == NULL || file[0] == '\0')
60 return (NULL);
61 plus = (mode[1] == '+');
62 switch (mode[0]) {
63 case 'w':
64 oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
65 break;
66 case 'a':
67 oflag = (plus ? O_RDWR : O_WRONLY) | O_CREAT;
68 break;
69 case 'r':
70 oflag = plus ? O_RDWR : O_RDONLY;
71 break;
72 default:
73 return (NULL);
74 }
75
76 if (largefile) {
77 fd = open64(file, oflag, 0666); /* mapped to open() for V9 */
78 } else {
79 fd = open(file, oflag, 0666);
80 }
81 if (fd < 0)
82 return (NULL);
83 iop->_cnt = 0;
84 #ifdef _LP64
85 iop->_file = fd;
86 #else
87 if (fd <= _FILE_FD_MAX) {
88 SET_FILE(iop, fd);
89 } else if (_file_set(iop, fd, mode) != 0) {
90 /* errno set in _file_set() */
91 (void) close(fd);
92 return (NULL);
93 }
94 #endif
95 iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
96 if (mode[0] == 'a') {
97 if ((lseek64(fd, 0L, SEEK_END)) < 0) {
98 (void) close(fd);
99 return (NULL);
100 }
101 }
102 iop->_base = iop->_ptr = NULL;
103 /*
104 * Sys5 does not support _bufsiz
105 *
106 * iop->_bufsiz = 0;
107 */
108 return (iop);
109 }
110
111 FILE *
fopen(const char * file,const char * mode)112 fopen(const char *file, const char *mode)
113 {
114 FILE *iop;
115 FILE *rc;
116
117 iop = _findiop();
118 rc = _endopen(file, mode, iop, LARGE_OPEN);
119 if (rc == NULL && iop != NULL)
120 iop->_flag = 0; /* release iop */
121 return (rc);
122 }
123
124 /*
125 * For _LP64, all fopen() calls are 64-bit calls, i.e., open64() system call.
126 * There should not be fopen64() calls.
127 * Similar for freopen64().
128 */
129 #if !defined(_LP64)
130 FILE *
fopen64(const char * file,const char * mode)131 fopen64(const char *file, const char *mode)
132 {
133 FILE *iop;
134 FILE *rc;
135
136 iop = _findiop();
137 rc = _endopen(file, mode, iop, ALWAYS_LARGE_OPEN);
138 if (rc == NULL && iop != NULL)
139 iop->_flag = 0; /* release iop */
140 return (rc);
141 }
142 #endif
143
144 FILE *
freopen(const char * file,const char * mode,FILE * iop)145 freopen(const char *file, const char *mode, FILE *iop)
146 {
147 (void) fclose(iop); /* doesn't matter if this fails */
148 return (_endopen(file, mode, iop, LARGE_OPEN));
149 }
150
151 #if !defined(_LP64)
152 FILE *
freopen64(const char * file,const char * mode,FILE * iop)153 freopen64(const char *file, const char *mode, FILE *iop)
154 {
155 (void) fclose(iop); /* doesn't matter if this fails */
156 return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN));
157 }
158 #endif
159