ungetwc.c revision 129583
190792Sgshapiro/*-
290792Sgshapiro * Copyright (c) 2002-2004 Tim J. Robbins.
390792Sgshapiro * All rights reserved.
490792Sgshapiro *
590792Sgshapiro * Redistribution and use in source and binary forms, with or without
690792Sgshapiro * modification, are permitted provided that the following conditions
790792Sgshapiro * are met:
890792Sgshapiro * 1. Redistributions of source code must retain the above copyright
990792Sgshapiro *    notice, this list of conditions and the following disclaimer.
1090792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
1190792Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1290792Sgshapiro *    documentation and/or other materials provided with the distribution.
1390792Sgshapiro *
1490792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1590792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1690792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1890792Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1990792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2090792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2190792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2290792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2390792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2490792Sgshapiro * SUCH DAMAGE.
2590792Sgshapiro */
2690792Sgshapiro
2790792Sgshapiro#include <sys/cdefs.h>
2890792Sgshapiro__FBSDID("$FreeBSD: head/lib/libc/stdio/ungetwc.c 129583 2004-05-22 15:19:41Z tjr $");
2990792Sgshapiro
3090792Sgshapiro#include "namespace.h"
3190792Sgshapiro#include <errno.h>
3290792Sgshapiro#include <stdio.h>
3390792Sgshapiro#include <stdlib.h>
3490792Sgshapiro#include <wchar.h>
3590792Sgshapiro#include "un-namespace.h"
3690792Sgshapiro#include "libc_private.h"
3790792Sgshapiro#include "local.h"
3890792Sgshapiro
3990792Sgshapiro/*
4090792Sgshapiro * Non-MT-safe version.
4190792Sgshapiro */
4290792Sgshapirowint_t
4390792Sgshapiro__ungetwc(wint_t wc, FILE *fp)
4490792Sgshapiro{
4590792Sgshapiro	char buf[MB_LEN_MAX];
4690792Sgshapiro	size_t len;
4790792Sgshapiro
4890792Sgshapiro	if (wc == WEOF)
4990792Sgshapiro		return (WEOF);
5090792Sgshapiro	if ((len = wcrtomb(buf, wc, &fp->_extra->mbstate)) == (size_t)-1) {
5190792Sgshapiro		fp->_flags |= __SERR;
5290792Sgshapiro		return (WEOF);
5390792Sgshapiro	}
5490792Sgshapiro	while (len-- != 0)
5590792Sgshapiro		if (__ungetc((unsigned char)buf[len], fp) == EOF)
5690792Sgshapiro			return (WEOF);
5790792Sgshapiro
5890792Sgshapiro	return (wc);
5990792Sgshapiro}
6090792Sgshapiro
6190792Sgshapiro/*
6290792Sgshapiro * MT-safe version.
6390792Sgshapiro */
6490792Sgshapirowint_t
6590792Sgshapiroungetwc(wint_t wc, FILE *fp)
6690792Sgshapiro{
6790792Sgshapiro	wint_t r;
6890792Sgshapiro
6990792Sgshapiro	FLOCKFILE(fp);
7090792Sgshapiro	ORIENT(fp, 1);
7190792Sgshapiro	r = __ungetwc(wc, fp);
7290792Sgshapiro	FUNLOCKFILE(fp);
7390792Sgshapiro
7490792Sgshapiro	return (r);
7590792Sgshapiro}
7690792Sgshapiro