xref: /titanic_44/usr/src/ucblib/libucb/port/stdio/fopen.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include <sys/types.h>
45 #include "file64.h"
46 #include <stdio.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <sys/file.h>
50 #include "stdiom.h"
51 
52 /* Final argument to _endopen depends on build environment */
53 #define	ALWAYS_LARGE_OPEN	1
54 #define	LARGE_OPEN	(_FILE_OFFSET_BITS == 64)
55 
56 static FILE *
57 _endopen(const char *file, const char *mode, FILE *iop, int largefile)
58 {
59 	int	plus, oflag, fd;
60 
61 	if (iop == NULL || file == NULL || file[0] == '\0')
62 		return (NULL);
63 	plus = (mode[1] == '+');
64 	switch (mode[0]) {
65 	case 'w':
66 		oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
67 		break;
68 	case 'a':
69 		oflag = (plus ? O_RDWR : O_WRONLY) | O_CREAT;
70 		break;
71 	case 'r':
72 		oflag = plus ? O_RDWR : O_RDONLY;
73 		break;
74 	default:
75 		return (NULL);
76 	}
77 
78 	if (largefile) {
79 		fd = open64(file, oflag, 0666);	/* mapped to open() for V9 */
80 	} else {
81 		fd = open(file, oflag, 0666);
82 	}
83 	if (fd < 0)
84 		return (NULL);
85 	iop->_cnt = 0;
86 #ifdef _LP64
87 	iop->_file = fd;
88 #else
89 	if (fd <= _FILE_FD_MAX) {
90 		SET_FILE(iop, fd);
91 	} else if (_file_set(iop, fd, mode) != 0) {
92 		/* errno set in _file_set() */
93 		(void) close(fd);
94 		return (NULL);
95 	}
96 #endif
97 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
98 	if (mode[0] == 'a')   {
99 		if ((lseek64(fd, 0L, SEEK_END)) < 0)  {
100 			(void) close(fd);
101 			return (NULL);
102 		}
103 	}
104 	iop->_base = iop->_ptr = NULL;
105 	/*
106 	 * Sys5 does not support _bufsiz
107 	 *
108 	 * iop->_bufsiz = 0;
109 	 */
110 	return (iop);
111 }
112 
113 FILE *
114 fopen(const char *file, const char *mode)
115 {
116 	FILE	*iop;
117 	FILE	*rc;
118 
119 	iop = _findiop();
120 	rc = _endopen(file, mode, iop, LARGE_OPEN);
121 	if (rc == NULL && iop != NULL)
122 		iop->_flag = 0;	/* release iop */
123 	return (rc);
124 }
125 
126 /*
127  * For _LP64, all fopen() calls are 64-bit calls, i.e., open64() system call.
128  * There should not be fopen64() calls.
129  * Similar for freopen64().
130  */
131 #if !defined(_LP64)
132 FILE *
133 fopen64(const char *file, const char *mode)
134 {
135 	FILE	*iop;
136 	FILE	*rc;
137 
138 	iop = _findiop();
139 	rc = _endopen(file, mode, iop, ALWAYS_LARGE_OPEN);
140 	if (rc == NULL && iop != NULL)
141 		iop->_flag = 0;	/* release iop */
142 	return (rc);
143 }
144 #endif
145 
146 FILE *
147 freopen(const char *file, const char *mode, FILE *iop)
148 {
149 	(void) fclose(iop); /* doesn't matter if this fails */
150 	return (_endopen(file, mode, iop, LARGE_OPEN));
151 }
152 
153 #if !defined(_LP64)
154 FILE *
155 freopen64(const char *file, const char *mode, FILE *iop)
156 {
157 	(void) fclose(iop); /* doesn't matter if this fails */
158 	return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN));
159 }
160 #endif
161