fputwc.c revision 129583
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 2002-2004 Tim J. Robbins.
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241541Srgrimes * SUCH DAMAGE.
251541Srgrimes */
261541Srgrimes
271541Srgrimes#include <sys/cdefs.h>
281541Srgrimes__FBSDID("$FreeBSD: head/lib/libc/stdio/fputwc.c 129583 2004-05-22 15:19:41Z tjr $");
291541Srgrimes
301541Srgrimes#include "namespace.h"
311541Srgrimes#include <errno.h>
321541Srgrimes#include <limits.h>
3310939Swollman#include <stdio.h>
3450477Speter#include <stdlib.h>
351541Srgrimes#include <wchar.h>
361541Srgrimes#include "un-namespace.h"
372169Spaul#include "libc_private.h"
382169Spaul#include "local.h"
392169Spaul
407280Swollman/*
4184102Sjlemon * Non-MT-safe version.
427280Swollman */
431541Srgrimeswint_t
441541Srgrimes__fputwc(wchar_t wc, FILE *fp)
4521666Swollman{
461541Srgrimes	char buf[MB_LEN_MAX];
471541Srgrimes	size_t i, len;
481541Srgrimes
491541Srgrimes	if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) {
501541Srgrimes		/*
511541Srgrimes		 * Assume single-byte locale with no special encoding.
521541Srgrimes		 * A more careful test would be to check
531541Srgrimes		 * _CurrentRuneLocale->encoding.
541541Srgrimes		 */
551541Srgrimes		*buf = (unsigned char)wc;
561541Srgrimes		len = 1;
571541Srgrimes	} else {
581541Srgrimes		if ((len = wcrtomb(buf, wc, &fp->_extra->mbstate)) ==
5984102Sjlemon		    (size_t)-1) {
6084102Sjlemon			fp->_flags |= __SERR;
611541Srgrimes			return (WEOF);
621541Srgrimes		}
631541Srgrimes	}
641541Srgrimes
651541Srgrimes	for (i = 0; i < len; i++)
661541Srgrimes		if (__sputc((unsigned char)buf[i], fp) == EOF)
671541Srgrimes			return (WEOF);
681541Srgrimes
691541Srgrimes	return ((wint_t)wc);
701541Srgrimes}
711541Srgrimes
721541Srgrimes/*
731541Srgrimes * MT-safe version.
741541Srgrimes */
751541Srgrimeswint_t
761541Srgrimesfputwc(wchar_t wc, FILE *fp)
771541Srgrimes{
783865Sswallace	wint_t r;
793865Sswallace
801541Srgrimes	FLOCKFILE(fp);
811541Srgrimes	ORIENT(fp, 1);
821541Srgrimes	r = __fputwc(wc, fp);
831541Srgrimes	FUNLOCKFILE(fp);
848876Srgrimes
8555205Speter	return (r);
861541Srgrimes}
877090Sbde