8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / ucblib / libucb / port / stdio / fopen.c
blob01ab2dde2c8f7584bc7c18d58c4993a14c931ba2
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 /*LINTLIBRARY*/
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"
50 /* Final argument to _endopen depends on build environment */
51 #define ALWAYS_LARGE_OPEN 1
52 #define LARGE_OPEN (_FILE_OFFSET_BITS == 64)
54 static FILE *
55 _endopen(const char *file, const char *mode, FILE *iop, int largefile)
57 int plus, oflag, fd;
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);
76 if (largefile) {
77 fd = open64(file, oflag, 0666); /* mapped to open() for V9 */
78 } else {
79 fd = open(file, oflag, 0666);
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);
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);
102 iop->_base = iop->_ptr = NULL;
104 * Sys5 does not support _bufsiz
106 * iop->_bufsiz = 0;
108 return (iop);
111 FILE *
112 fopen(const char *file, const char *mode)
114 FILE *iop;
115 FILE *rc;
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);
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().
129 #if !defined(_LP64)
130 FILE *
131 fopen64(const char *file, const char *mode)
133 FILE *iop;
134 FILE *rc;
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);
142 #endif
144 FILE *
145 freopen(const char *file, const char *mode, FILE *iop)
147 (void) fclose(iop); /* doesn't matter if this fails */
148 return (_endopen(file, mode, iop, LARGE_OPEN));
151 #if !defined(_LP64)
152 FILE *
153 freopen64(const char *file, const char *mode, FILE *iop)
155 (void) fclose(iop); /* doesn't matter if this fails */
156 return (_endopen(file, mode, iop, ALWAYS_LARGE_OPEN));
158 #endif