xref: /illumos-gate/usr/src/lib/libc/port/stdio/_endopen.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  *	This routine is a special case, in that it is aware of
35  *	both small and large file interfaces. It must be built
36  *	in the small compilation environment.
37  */
38 
39 #include <sys/feature_tests.h>
40 
41 #define	close		_close
42 #define	lseek		_lseek
43 #define	open		_open
44 #if !defined(_LP64)
45 #define	lseek64		_lseek64
46 #define	open64		_open64
47 #endif
48 
49 #include "lint.h"
50 #include "file64.h"
51 #include <mtlib.h>
52 #include <sys/types.h>
53 #include <stdio.h>
54 #include <fcntl.h>
55 #include <unistd.h>
56 #include <limits.h>
57 #include <thread.h>
58 #include <synch.h>
59 #include "stdiom.h"
60 #include <errno.h>
61 
62 /*
63  * open UNIX file name, associate with iop
64  */
65 
66 FILE *
67 _endopen(const char *name, const char *type, FILE *iop, int largefile)
68 {
69 	int oflag, fd;
70 	char plus;
71 	rmutex_t *lk;
72 
73 	if (iop == NULL)
74 		return (NULL);
75 	switch (type[0]) {
76 	default:
77 		errno = EINVAL;
78 		return (NULL);
79 	case 'r':
80 		oflag = O_RDONLY;
81 		break;
82 	case 'w':
83 		oflag = O_WRONLY | O_TRUNC | O_CREAT;
84 		break;
85 	case 'a':
86 		oflag = O_WRONLY | O_APPEND | O_CREAT;
87 		break;
88 	}
89 	/* UNIX ignores 'b' and treats text and binary the same */
90 	if ((plus = type[1]) == 'b')
91 		plus = type[2];
92 	if (plus == '+')
93 		oflag = (oflag & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
94 
95 	/* select small or large file open based on flag */
96 	if (largefile) {
97 		fd = open64(name, oflag, 0666);
98 	} else {
99 		fd = open(name, oflag, 0666);
100 	}
101 	if (fd < 0)
102 		return (NULL);
103 
104 #ifdef	_LP64
105 	iop->_file = fd;
106 #else
107 	if (fd > UCHAR_MAX) {
108 		(void) close(fd);
109 		errno = EMFILE;
110 		return (NULL);
111 	}
112 	iop->_file = (unsigned char)fd; /* assume fits in unsigned char */
113 #endif	/*	_LP64	*/
114 
115 	FLOCKFILE(lk, iop);		/* this lock may be unnecessary */
116 
117 #ifdef	_LP64
118 	iop->_flag &= ~0377;	/* clear lower 8-bits */
119 	if (plus == '+')
120 		iop->_flag |= _IORW;
121 	else if (type[0] == 'r')
122 		iop->_flag |= _IOREAD;
123 	else
124 		iop->_flag |= _IOWRT;
125 #else
126 	if (plus == '+')
127 		iop->_flag = _IORW;
128 	else if (type[0] == 'r')
129 		iop->_flag = _IOREAD;
130 	else
131 		iop->_flag = _IOWRT;
132 #endif	/*	_LP64	*/
133 	FUNLOCKFILE(lk);
134 	if (oflag == (O_WRONLY | O_APPEND | O_CREAT)) {	/* type == "a" */
135 		if (lseek64(fd, (off64_t)0, SEEK_END) < (off64_t)0) {
136 			(void) close(fd);
137 			return (NULL);
138 		}
139 	}
140 
141 	return (iop);
142 }
143