xref: /titanic_52/usr/src/ucblib/libucb/port/stdio/fopen.c (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1998 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 	if (largefile) {
78 		fd = open64(file, oflag, 0666);	/* mapped to open() for V9 */
79 	} else {
80 		fd = open(file, oflag, 0666);
81 	}
82 	if (fd < 0)
83 		return (NULL);
84 	iop->_cnt = 0;
85 	iop->_file = (unsigned char) fd;
86 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
87 	if (mode[0] == 'a')   {
88 		if ((lseek64(fd, 0L, SEEK_END)) < 0)  {
89 			(void) close(fd);
90 			return (NULL);
91 		}
92 	}
93 	iop->_base = iop->_ptr = NULL;
94 	/*
95 	 * Sys5 does not support _bufsiz
96 	 *
97 	 * iop->_bufsiz = 0;
98 	 */
99 	return (iop);
100 }
101 
102 FILE *
103 fopen(const char *file, const char *mode)
104 {
105 	FILE	*iop;
106 	FILE	*rc;
107 
108 	iop = _findiop();
109 	rc = _endopen(file, mode, iop, LARGE_OPEN);
110 	if (rc == NULL && iop != NULL)
111 		iop->_flag = 0;	/* release iop */
112 	return (rc);
113 }
114 
115 /*
116  * For _LP64, all fopen() calls are 64-bit calls, i.e., open64() system call.
117  * There should not be fopen64() calls.
118  * Similar for freopen64().
119  */
120 #if !defined(_LP64)
121 FILE *
122 fopen64(const char *file, const char *mode)
123 {
124 	FILE	*iop;
125 	FILE	*rc;
126 
127 	iop = _findiop();
128 	rc = _endopen(file, mode, iop, ALWAYS_LARGE_OPEN);
129 	if (rc == NULL && iop != NULL)
130 		iop->_flag = 0;	/* release iop */
131 	return (rc);
132 }
133 #endif
134 
135 FILE *
136 freopen(const char *file, const char *mode, FILE *iop)
137 {
138 	(void) fclose(iop); /* doesn't matter if this fails */
139 	return (_endopen(file, mode, iop, LARGE_OPEN));
140 }
141 
142 #if !defined(_LP64)
143 FILE *
144 freopen64(const char *file, const char *mode, FILE *iop)
145 {
146 	(void) fclose(iop); /* doesn't matter if this fails */
147 	return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN));
148 }
149 #endif
150