2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include "mtlib.h"
2N/A#include "mbstatet.h"
2N/A#include "file64.h"
2N/A#include <sys/types.h>
2N/A#include <stdio.h>
2N/A#include <wchar.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include "libc.h"
2N/A#include "stdiom.h"
2N/A#include "mse.h"
2N/A
2N/A/*
2N/A * DESCRIPTION:
2N/A * This function sets the error indicator for the specified stream.
2N/A * This is a private API for the L10N method functions, especially
2N/A * for fgetwc().
2N/A *
2N/A * The stream needs to have been properly locked. Usually, the wrapper
2N/A * function of fgetwc() locks the stream.
2N/A */
2N/Avoid
2N/A__fseterror_u(FILE *iop)
2N/A{
2N/A iop->_flag |= _IOERR;
2N/A}
2N/A
2N/A/*
2N/A * DESCRIPTION:
2N/A * This function/macro gets the orientation bound to the specified iop.
2N/A *
2N/A * RETURNS:
2N/A * _WC_MODE if iop has been bound to Wide orientation
2N/A * _BYTE_MODE if iop has been bound to Byte orientation
2N/A * _NO_MODE if iop has been bound to neither Wide nor Byte
2N/A */
2N/A_IOP_orientation_t
2N/A_getorientation(FILE *iop)
2N/A{
2N/A if (GET_BYTE_MODE(iop))
2N/A return (_BYTE_MODE);
2N/A else if (GET_WC_MODE(iop))
2N/A return (_WC_MODE);
2N/A
2N/A return (_NO_MODE);
2N/A}
2N/A
2N/A/*
2N/A * DESCRIPTION:
2N/A * This function/macro sets the orientation to the specified iop.
2N/A *
2N/A * INPUT:
2N/A * flag may take one of the following:
2N/A * _WC_MODE Wide orientation
2N/A * _BYTE_MODE Byte orientation
2N/A * _NO_MODE Unoriented
2N/A */
2N/Avoid
2N/A_setorientation(FILE *iop, _IOP_orientation_t mode)
2N/A{
2N/A switch (mode) {
2N/A case _NO_MODE:
2N/A CLEAR_BYTE_MODE(iop);
2N/A CLEAR_WC_MODE(iop);
2N/A break;
2N/A case _BYTE_MODE:
2N/A CLEAR_WC_MODE(iop);
2N/A SET_BYTE_MODE(iop);
2N/A break;
2N/A case _WC_MODE:
2N/A CLEAR_BYTE_MODE(iop);
2N/A SET_WC_MODE(iop);
2N/A break;
2N/A }
2N/A}
2N/A
2N/Astatic mbstate_t **__top_mbstates = NULL;
2N/Astatic mutex_t __top_mbstates_lock = DEFAULTMUTEX;
2N/A
2N/Avoid
2N/A_clear_internal_mbstate(void)
2N/A{
2N/A int i;
2N/A
2N/A lmutex_lock(&__top_mbstates_lock);
2N/A if (__top_mbstates) {
2N/A for (i = 0; i <= _MAX_MB_FUNC; i++) {
2N/A if (*(__top_mbstates + i)) {
2N/A lfree(*(__top_mbstates + i),
2N/A sizeof (mbstate_t));
2N/A }
2N/A }
2N/A lfree(__top_mbstates,
2N/A (_MAX_MB_FUNC + 1) * sizeof (mbstate_t *));
2N/A __top_mbstates = NULL;
2N/A }
2N/A lmutex_unlock(&__top_mbstates_lock);
2N/A}
2N/A
2N/Ambstate_t *
2N/A_get_internal_mbstate(int item)
2N/A{
2N/A if (item < 0 || item > _MAX_MB_FUNC)
2N/A return (NULL);
2N/A
2N/A lmutex_lock(&__top_mbstates_lock);
2N/A if (__top_mbstates == NULL) {
2N/A __top_mbstates =
2N/A lmalloc((_MAX_MB_FUNC + 1) * sizeof (mbstate_t *));
2N/A if (__top_mbstates == NULL) {
2N/A lmutex_unlock(&__top_mbstates_lock);
2N/A return (NULL);
2N/A }
2N/A *(__top_mbstates + item) = lmalloc(sizeof (mbstate_t));
2N/A if (*(__top_mbstates + item) == NULL) {
2N/A lmutex_unlock(&__top_mbstates_lock);
2N/A return (NULL);
2N/A }
2N/A lmutex_unlock(&__top_mbstates_lock);
2N/A return (*(__top_mbstates + item));
2N/A }
2N/A if (*(__top_mbstates + item) == NULL) {
2N/A *(__top_mbstates + item) = lmalloc(sizeof (mbstate_t));
2N/A if (*(__top_mbstates + item) == NULL) {
2N/A lmutex_unlock(&__top_mbstates_lock);
2N/A return (NULL);
2N/A }
2N/A }
2N/A lmutex_unlock(&__top_mbstates_lock);
2N/A return (*(__top_mbstates + item));
2N/A}
2N/A
2N/A/*
2N/A * From page 32 of XSH5
2N/A * Once a wide-character I/O function has been applied
2N/A * to a stream without orientation, the stream becomes
2N/A * wide-orientated. Similarly, once a byte I/O function
2N/A * has been applied to a stream without orientation,
2N/A * the stream becomes byte-orientated. Only a call to
2N/A * the freopen() function or the fwide() function can
2N/A * otherwise alter the orientation of a stream.
2N/A */
2N/A
2N/A/*
2N/A * void
2N/A * _set_orientation_byte(FILE *iop)
2N/A *
2N/A * Note: this is now implemented as macro __SET_ORIENTATION_BYTE()
2N/A * (in libc/inc/mse.h) for performance improvement.
2N/A */
2N/A
2N/A/* Returns the value of 'ps->__nconsumed' */
2N/Achar
2N/A__mbst_get_nconsumed(const mbstate_t *ps)
2N/A{
2N/A return (ps->__nconsumed);
2N/A}
2N/A
2N/A/* Sets 'n' to 'ps->__nconsumed' */
2N/Avoid
2N/A__mbst_set_nconsumed(mbstate_t *ps, char n)
2N/A{
2N/A ps->__nconsumed = n;
2N/A}
2N/A
2N/A/* Copies 'len' bytes from '&ps->__consumed[index]' to 'str' */
2N/Aint
2N/A__mbst_get_consumed_array(const mbstate_t *ps, char *str,
2N/A size_t index, size_t len)
2N/A{
2N/A if ((index + len) > 8) {
2N/A /* The max size of __consumed[] is 8 */
2N/A return (-1);
2N/A }
2N/A (void) memcpy((void *)str, (const void *)&ps->__consumed[index], len);
2N/A return (0);
2N/A}
2N/A
2N/A/* Copies 'len' bytes from 'str' to '&ps->__consumed[index]' */
2N/Aint
2N/A__mbst_set_consumed_array(mbstate_t *ps, const char *str,
2N/A size_t index, size_t len)
2N/A{
2N/A if ((index + len) > 8) {
2N/A /* The max size of __consumed[] is 8 */
2N/A return (-1);
2N/A }
2N/A (void) memcpy((void *)&ps->__consumed[index], (const void *)str, len);
2N/A return (0);
2N/A}
2N/A
2N/A/* Returns 'ps->__lc_locale' */
2N/Avoid *
2N/A__mbst_get_locale(const mbstate_t *ps)
2N/A{
2N/A return (ps->__lc_locale);
2N/A}
2N/A
2N/A/* Sets 'loc' to 'ps->__lc_locale' */
2N/Avoid
2N/A__mbst_set_locale(mbstate_t *ps, const void *loc)
2N/A{
2N/A mark_locale_as_oriented();
2N/A ps->__lc_locale = (void *)loc;
2N/A}