fputwc.c revision 121851
156043Syokota/*- 256043Syokota * Copyright (c) 2002 Tim J. Robbins. 356043Syokota * All rights reserved. 456043Syokota * 556043Syokota * Redistribution and use in source and binary forms, with or without 656043Syokota * modification, are permitted provided that the following conditions 756043Syokota * are met: 856043Syokota * 1. Redistributions of source code must retain the above copyright 956043Syokota * notice, this list of conditions and the following disclaimer. 1056043Syokota * 2. Redistributions in binary form must reproduce the above copyright 1156043Syokota * notice, this list of conditions and the following disclaimer in the 1256043Syokota * documentation and/or other materials provided with the distribution. 1356043Syokota * 1456043Syokota * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1556043Syokota * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1656043Syokota * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1756043Syokota * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1856043Syokota * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1956043Syokota * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2056043Syokota * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2156043Syokota * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2256043Syokota * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2356043Syokota * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2456043Syokota * SUCH DAMAGE. 2556043Syokota */ 2656043Syokota 2756043Syokota#include <sys/cdefs.h> 2856043Syokota__FBSDID("$FreeBSD: head/lib/libc/stdio/fputwc.c 121851 2003-11-01 08:18:18Z tjr $"); 2956043Syokota 3056043Syokota#include "namespace.h" 3156043Syokota#include <errno.h> 3256043Syokota#include <limits.h> 3356043Syokota#include <stdio.h> 3456043Syokota#include <stdlib.h> 3556043Syokota#include <wchar.h> 3656043Syokota#include "un-namespace.h" 3756043Syokota#include "libc_private.h" 3856043Syokota#include "local.h" 3956043Syokota 4056043Syokota/* 4156043Syokota * Non-MT-safe version. 4256043Syokota */ 4356043Syokotawint_t 4456043Syokota__fputwc(wchar_t wc, FILE *fp) 4556043Syokota{ 4656043Syokota char buf[MB_LEN_MAX]; 4756043Syokota mbstate_t mbs; 4856043Syokota size_t i, len; 4956043Syokota 5056043Syokota if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) { 5156043Syokota /* 5256043Syokota * Assume single-byte locale with no special encoding. 5356043Syokota * A more careful test would be to check 5456043Syokota * _CurrentRuneLocale->encoding. 5556043Syokota */ 5656043Syokota *buf = (unsigned char)wc; 5756043Syokota len = 1; 5856043Syokota } else { 5956043Syokota memset(&mbs, 0, sizeof(mbs)); 6056043Syokota if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) { 6156043Syokota fp->_flags |= __SERR; 6256043Syokota return (WEOF); 6356043Syokota } 6456043Syokota } 6556043Syokota 6656043Syokota for (i = 0; i < len; i++) 6756043Syokota if (__sputc((unsigned char)buf[i], fp) == EOF) 6856043Syokota return (WEOF); 6956043Syokota 7056043Syokota return ((wint_t)wc); 7156043Syokota} 7256043Syokota 7356043Syokota/* 7456043Syokota * MT-safe version. 7556043Syokota */ 7656043Syokotawint_t 7756043Syokotafputwc(wchar_t wc, FILE *fp) 7856043Syokota{ 7956043Syokota wint_t r; 8056043Syokota 8156043Syokota FLOCKFILE(fp); 8256043Syokota ORIENT(fp, 1); 8356043Syokota r = __fputwc(wc, fp); 8456043Syokota FUNLOCKFILE(fp); 8556043Syokota 8656043Syokota return (r); 8756043Syokota} 8856043Syokota