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