xref: /illumos-gate/usr/src/lib/libc/port/stdio/mse.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
30*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
31*7c478bd9Sstevel@tonic-gate #include "mbstatet.h"
32*7c478bd9Sstevel@tonic-gate #include "file64.h"
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <wchar.h>
36*7c478bd9Sstevel@tonic-gate #include <thread.h>
37*7c478bd9Sstevel@tonic-gate #include <synch.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include "libc.h"
41*7c478bd9Sstevel@tonic-gate #include "stdiom.h"
42*7c478bd9Sstevel@tonic-gate #include "mse.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:
46*7c478bd9Sstevel@tonic-gate  * This function sets the error indicator for the specified stream.
47*7c478bd9Sstevel@tonic-gate  * This is a private API for the L10N method functions, especially
48*7c478bd9Sstevel@tonic-gate  * for fgetwc().
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * The stream needs to have been properly locked.  Usually, the wrapper
51*7c478bd9Sstevel@tonic-gate  * function of fgetwc() locks the stream.
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate void
54*7c478bd9Sstevel@tonic-gate __fseterror_u(FILE *iop)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	iop->_flag |= _IOERR;
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:
61*7c478bd9Sstevel@tonic-gate  * This function/macro gets the orientation bound to the specified iop.
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  * RETURNS:
64*7c478bd9Sstevel@tonic-gate  * _WC_MODE	if iop has been bound to Wide orientation
65*7c478bd9Sstevel@tonic-gate  * _BYTE_MODE	if iop has been bound to Byte orientation
66*7c478bd9Sstevel@tonic-gate  * _NO_MODE	if iop has been bound to neither Wide nor Byte
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate _IOP_orientation_t
69*7c478bd9Sstevel@tonic-gate _getorientation(FILE *iop)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	if (GET_BYTE_MODE(iop))
72*7c478bd9Sstevel@tonic-gate 		return (_BYTE_MODE);
73*7c478bd9Sstevel@tonic-gate 	else if (GET_WC_MODE(iop))
74*7c478bd9Sstevel@tonic-gate 		return (_WC_MODE);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	return (_NO_MODE);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:
81*7c478bd9Sstevel@tonic-gate  * This function/macro sets the orientation to the specified iop.
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * INPUT:
84*7c478bd9Sstevel@tonic-gate  * flag may take one of the following:
85*7c478bd9Sstevel@tonic-gate  *	_WC_MODE	Wide orientation
86*7c478bd9Sstevel@tonic-gate  *	_BYTE_MODE	Byte orientation
87*7c478bd9Sstevel@tonic-gate  *	_NO_MODE	Unoriented
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate void
90*7c478bd9Sstevel@tonic-gate _setorientation(FILE *iop, _IOP_orientation_t mode)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	switch (mode) {
93*7c478bd9Sstevel@tonic-gate 	case _NO_MODE:
94*7c478bd9Sstevel@tonic-gate 		CLEAR_BYTE_MODE(iop);
95*7c478bd9Sstevel@tonic-gate 		CLEAR_WC_MODE(iop);
96*7c478bd9Sstevel@tonic-gate 		break;
97*7c478bd9Sstevel@tonic-gate 	case _BYTE_MODE:
98*7c478bd9Sstevel@tonic-gate 		CLEAR_WC_MODE(iop);
99*7c478bd9Sstevel@tonic-gate 		SET_BYTE_MODE(iop);
100*7c478bd9Sstevel@tonic-gate 		break;
101*7c478bd9Sstevel@tonic-gate 	case _WC_MODE:
102*7c478bd9Sstevel@tonic-gate 		CLEAR_BYTE_MODE(iop);
103*7c478bd9Sstevel@tonic-gate 		SET_WC_MODE(iop);
104*7c478bd9Sstevel@tonic-gate 		break;
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate static mbstate_t	**__top_mbstates = NULL;
109*7c478bd9Sstevel@tonic-gate static mutex_t	__top_mbstates_lock = DEFAULTMUTEX;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate void
112*7c478bd9Sstevel@tonic-gate _clear_internal_mbstate(void)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	int	i;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	lmutex_lock(&__top_mbstates_lock);
117*7c478bd9Sstevel@tonic-gate 	if (__top_mbstates) {
118*7c478bd9Sstevel@tonic-gate 		for (i = 0; i <= _MAX_MB_FUNC; i++) {
119*7c478bd9Sstevel@tonic-gate 			if (*(__top_mbstates + i)) {
120*7c478bd9Sstevel@tonic-gate 				lfree(*(__top_mbstates + i),
121*7c478bd9Sstevel@tonic-gate 				    sizeof (mbstate_t));
122*7c478bd9Sstevel@tonic-gate 			}
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 		lfree(__top_mbstates,
125*7c478bd9Sstevel@tonic-gate 		    (_MAX_MB_FUNC + 1) * sizeof (mbstate_t *));
126*7c478bd9Sstevel@tonic-gate 		__top_mbstates = NULL;
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__top_mbstates_lock);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate mbstate_t *
132*7c478bd9Sstevel@tonic-gate _get_internal_mbstate(int item)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	if (item < 0 || item > _MAX_MB_FUNC)
135*7c478bd9Sstevel@tonic-gate 		return (NULL);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	lmutex_lock(&__top_mbstates_lock);
138*7c478bd9Sstevel@tonic-gate 	if (__top_mbstates == NULL) {
139*7c478bd9Sstevel@tonic-gate 		__top_mbstates =
140*7c478bd9Sstevel@tonic-gate 			lmalloc((_MAX_MB_FUNC + 1) * sizeof (mbstate_t *));
141*7c478bd9Sstevel@tonic-gate 		if (__top_mbstates == NULL) {
142*7c478bd9Sstevel@tonic-gate 			lmutex_unlock(&__top_mbstates_lock);
143*7c478bd9Sstevel@tonic-gate 			return (NULL);
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 		*(__top_mbstates + item) = lmalloc(sizeof (mbstate_t));
146*7c478bd9Sstevel@tonic-gate 		if (*(__top_mbstates + item) == NULL) {
147*7c478bd9Sstevel@tonic-gate 			lmutex_unlock(&__top_mbstates_lock);
148*7c478bd9Sstevel@tonic-gate 			return (NULL);
149*7c478bd9Sstevel@tonic-gate 		}
150*7c478bd9Sstevel@tonic-gate 		lmutex_unlock(&__top_mbstates_lock);
151*7c478bd9Sstevel@tonic-gate 		return (*(__top_mbstates + item));
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	if (*(__top_mbstates + item) == NULL) {
154*7c478bd9Sstevel@tonic-gate 		*(__top_mbstates + item) = lmalloc(sizeof (mbstate_t));
155*7c478bd9Sstevel@tonic-gate 		if (*(__top_mbstates + item) == NULL) {
156*7c478bd9Sstevel@tonic-gate 			lmutex_unlock(&__top_mbstates_lock);
157*7c478bd9Sstevel@tonic-gate 			return (NULL);
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__top_mbstates_lock);
161*7c478bd9Sstevel@tonic-gate 	return (*(__top_mbstates + item));
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate /*
165*7c478bd9Sstevel@tonic-gate  * From page 32 of XSH5
166*7c478bd9Sstevel@tonic-gate  * Once a wide-character I/O function has been applied
167*7c478bd9Sstevel@tonic-gate  * to a stream without orientation, the stream becomes
168*7c478bd9Sstevel@tonic-gate  * wide-orientated.  Similarly, once a byte I/O function
169*7c478bd9Sstevel@tonic-gate  * has been applied to a stream without orientation,
170*7c478bd9Sstevel@tonic-gate  * the stream becomes byte-orientated.  Only a call to
171*7c478bd9Sstevel@tonic-gate  * the freopen() function or the fwide() function can
172*7c478bd9Sstevel@tonic-gate  * otherwise alter the orientation of a stream.
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate /*
176*7c478bd9Sstevel@tonic-gate  * void
177*7c478bd9Sstevel@tonic-gate  * _set_orientation_byte(FILE *iop)
178*7c478bd9Sstevel@tonic-gate  *
179*7c478bd9Sstevel@tonic-gate  * Note: this is now implemented as macro __SET_ORIENTATION_BYTE()
180*7c478bd9Sstevel@tonic-gate  *       (in libc/inc/mse.h) for performance improvement.
181*7c478bd9Sstevel@tonic-gate  */
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /* Returns the value of 'ps->__nconsumed' */
184*7c478bd9Sstevel@tonic-gate char
185*7c478bd9Sstevel@tonic-gate __mbst_get_nconsumed(const mbstate_t *ps)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	return (ps->__nconsumed);
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /* Sets 'n' to 'ps->__nconsumed' */
191*7c478bd9Sstevel@tonic-gate void
192*7c478bd9Sstevel@tonic-gate __mbst_set_nconsumed(mbstate_t *ps, char n)
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate 	ps->__nconsumed = n;
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate /* Copies 'len' bytes from '&ps->__consumed[index]' to 'str' */
198*7c478bd9Sstevel@tonic-gate int
199*7c478bd9Sstevel@tonic-gate __mbst_get_consumed_array(const mbstate_t *ps, char *str,
200*7c478bd9Sstevel@tonic-gate 	size_t index, size_t len)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	if ((index + len) > 8) {
203*7c478bd9Sstevel@tonic-gate 		/* The max size of __consumed[] is 8 */
204*7c478bd9Sstevel@tonic-gate 		return (-1);
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *)str, (const void *)&ps->__consumed[index],
207*7c478bd9Sstevel@tonic-gate 		len);
208*7c478bd9Sstevel@tonic-gate 	return (0);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /* Copies 'len' bytes from 'str' to '&ps->__consumed[index]' */
212*7c478bd9Sstevel@tonic-gate int
213*7c478bd9Sstevel@tonic-gate __mbst_set_consumed_array(mbstate_t *ps, const char *str,
214*7c478bd9Sstevel@tonic-gate 	size_t index, size_t len)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	if ((index + len) > 8) {
217*7c478bd9Sstevel@tonic-gate 		/* The max size of __consumed[] is 8 */
218*7c478bd9Sstevel@tonic-gate 		return (-1);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *)&ps->__consumed[index], (const void *)str,
221*7c478bd9Sstevel@tonic-gate 		len);
222*7c478bd9Sstevel@tonic-gate 	return (0);
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate /* Returns 'ps->__lc_locale' */
226*7c478bd9Sstevel@tonic-gate void *
227*7c478bd9Sstevel@tonic-gate __mbst_get_locale(const mbstate_t *ps)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 	return (ps->__lc_locale);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate /* Sets 'loc' to 'ps->__lc_locale' */
233*7c478bd9Sstevel@tonic-gate void
234*7c478bd9Sstevel@tonic-gate __mbst_set_locale(mbstate_t *ps, const void *loc)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	ps->__lc_locale = (void *)loc;
237*7c478bd9Sstevel@tonic-gate }
238