1*526d9427Schristos /* $NetBSD: fputwc.c,v 1.5 2012/03/15 18:22:30 christos Exp $ */
217f3654aSyamt
317f3654aSyamt /*-
417f3654aSyamt * Copyright (c)2001 Citrus Project,
517f3654aSyamt * All rights reserved.
617f3654aSyamt *
717f3654aSyamt * Redistribution and use in source and binary forms, with or without
817f3654aSyamt * modification, are permitted provided that the following conditions
917f3654aSyamt * are met:
1017f3654aSyamt * 1. Redistributions of source code must retain the above copyright
1117f3654aSyamt * notice, this list of conditions and the following disclaimer.
1217f3654aSyamt * 2. Redistributions in binary form must reproduce the above copyright
1317f3654aSyamt * notice, this list of conditions and the following disclaimer in the
1417f3654aSyamt * documentation and/or other materials provided with the distribution.
1517f3654aSyamt *
1617f3654aSyamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1717f3654aSyamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1817f3654aSyamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1917f3654aSyamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2017f3654aSyamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2117f3654aSyamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2217f3654aSyamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2317f3654aSyamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2417f3654aSyamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2517f3654aSyamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2617f3654aSyamt * SUCH DAMAGE.
2717f3654aSyamt *
2817f3654aSyamt * $Citrus$
2917f3654aSyamt */
3017f3654aSyamt
3188c3eadbSlukem #include <sys/cdefs.h>
3288c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
33*526d9427Schristos __RCSID("$NetBSD: fputwc.c,v 1.5 2012/03/15 18:22:30 christos Exp $");
3488c3eadbSlukem #endif /* LIBC_SCCS and not lint */
3588c3eadbSlukem
3617f3654aSyamt #include <assert.h>
3717f3654aSyamt #include <errno.h>
3817f3654aSyamt #include <limits.h>
3917f3654aSyamt #include <stdio.h>
4017f3654aSyamt #include <wchar.h>
413fdac2b8Sthorpej #include "reentrant.h"
4217f3654aSyamt #include "local.h"
4317f3654aSyamt #include "fvwrite.h"
4417f3654aSyamt
4517f3654aSyamt wint_t
__fputwc_unlock(wchar_t wc,FILE * fp)465abc4b4fStshiozak __fputwc_unlock(wchar_t wc, FILE *fp)
4717f3654aSyamt {
4817f3654aSyamt struct wchar_io_data *wcio;
4917f3654aSyamt mbstate_t *st;
5017f3654aSyamt size_t size;
5117f3654aSyamt char buf[MB_LEN_MAX];
5217f3654aSyamt struct __suio uio;
5317f3654aSyamt struct __siov iov;
5417f3654aSyamt
5517f3654aSyamt _DIAGASSERT(fp != NULL);
5617f3654aSyamt
5717f3654aSyamt /* LINTED we don't play with buf */
5817f3654aSyamt iov.iov_base = (void *)buf;
5917f3654aSyamt uio.uio_iov = &iov;
6017f3654aSyamt uio.uio_iovcnt = 1;
6117f3654aSyamt
6217f3654aSyamt _SET_ORIENTATION(fp, 1);
6317f3654aSyamt wcio = WCIO_GET(fp);
6417f3654aSyamt if (wcio == 0) {
6517f3654aSyamt errno = ENOMEM;
6617f3654aSyamt return WEOF;
6717f3654aSyamt }
6817f3654aSyamt
6917f3654aSyamt wcio->wcio_ungetwc_inbuf = 0;
7017f3654aSyamt st = &wcio->wcio_mbstate_out;
7117f3654aSyamt
7217f3654aSyamt size = wcrtomb(buf, wc, st);
7317f3654aSyamt if (size == (size_t)-1) {
7417f3654aSyamt errno = EILSEQ;
7517f3654aSyamt return WEOF;
7617f3654aSyamt }
7717f3654aSyamt
7817f3654aSyamt _DIAGASSERT(size != 0);
7917f3654aSyamt
8017f3654aSyamt uio.uio_resid = iov.iov_len = size;
8117f3654aSyamt if (__sfvwrite(fp, &uio)) {
8217f3654aSyamt return WEOF;
8317f3654aSyamt }
8417f3654aSyamt
855abc4b4fStshiozak return (wint_t)wc;
865abc4b4fStshiozak }
875abc4b4fStshiozak
885abc4b4fStshiozak wint_t
fputwc(wchar_t wc,FILE * fp)895abc4b4fStshiozak fputwc(wchar_t wc, FILE *fp)
905abc4b4fStshiozak {
915abc4b4fStshiozak wint_t r;
925abc4b4fStshiozak
935abc4b4fStshiozak _DIAGASSERT(fp != NULL);
945abc4b4fStshiozak
955abc4b4fStshiozak FLOCKFILE(fp);
965abc4b4fStshiozak r = __fputwc_unlock(wc, fp);
9717f3654aSyamt FUNLOCKFILE(fp);
9817f3654aSyamt
99*526d9427Schristos return r;
10017f3654aSyamt }
101