4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 2002-2004 Tim J. Robbins.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * are met:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUCH DAMAGE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Use is subject to license terms.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lint.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "file64.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "mtlib.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "mse_int.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <errno.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <limits.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdio.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdlib.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <wchar.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <synch.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "mblocal.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "stdiom.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "mse.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#pragma weak _putwc = putwc
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FreeBSD had both a MT safe and non-MT safe version. For whatever reason,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * we don't need the non-MT safe version. We do this because its faster,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * since we don't have to lock the file while doing the potentially expensive
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * conversion from wide to mb.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Solaris also has XPG5 and legacy semantics. The new standard requires
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * that the stream orientation change, but legacy calls don't do that.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Note that we had the source for the XPG5 version of this, but it relied
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * on closed implementation bits that we lack, so we supply replacements
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * here.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic wint_t
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore__fputwc_impl(wchar_t wc, FILE *fp, int orient)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char buf[MB_LEN_MAX];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore size_t i, len;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rmutex_t *mx;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* If we are given WEOF, then we have to stop */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (wc == WEOF)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (WEOF);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Assume single-byte locale with no special encoding.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *buf = (unsigned char)wc;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore len = 1;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore } else {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FreeBSD used restartable wcrtomb. I think we can use
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * the simpler wctomb form here. We should have a complete
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * decode.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if ((len = wctomb(buf, wc)) == (size_t)-1) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore fp->_flag |= _IOERR;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore errno = EILSEQ;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (WEOF);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore FLOCKFILE(mx, fp);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This is used for XPG 5 semantics, which requires the stream
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * orientation to be changed when the function is called.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (orient && GET_NO_MODE(fp)) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore _setorientation(fp, _WC_MODE);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore for (i = 0; i < len; i++) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (PUTC((unsigned char)buf[i], fp) == EOF) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore FUNLOCKFILE(mx);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (WEOF);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore FUNLOCKFILE(mx);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return ((wint_t)wc);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorewint_t
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorefputwc(wchar_t wc, FILE *fp)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (__fputwc_impl(wc, fp, 0));
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Trivial functional form of the typical macro.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#undef __putwc
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorewint_t
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoreputwc(wchar_t wc, FILE *fp)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (__fputwc_impl(wc, fp, 0));
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorewint_t
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore__fputwc_xpg5(wint_t wc, FILE *fp)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (__fputwc_impl(wc, fp, 1));
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#undef __putwc_xpg5
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorewint_t
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore__putwc_xpg5(wint_t wc, FILE *fp)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (__fputwc_impl(wc, fp, 1));
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}