ungetwc.c revision 149313
1101776Stjr/*-
2129583Stjr * Copyright (c) 2002-2004 Tim J. Robbins.
3101776Stjr * All rights reserved.
4101776Stjr *
5101776Stjr * Redistribution and use in source and binary forms, with or without
6101776Stjr * modification, are permitted provided that the following conditions
7101776Stjr * are met:
8101776Stjr * 1. Redistributions of source code must retain the above copyright
9101776Stjr *    notice, this list of conditions and the following disclaimer.
10101776Stjr * 2. Redistributions in binary form must reproduce the above copyright
11101776Stjr *    notice, this list of conditions and the following disclaimer in the
12101776Stjr *    documentation and/or other materials provided with the distribution.
13101776Stjr *
14101776Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15101776Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16101776Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17101776Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18101776Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19101776Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20101776Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21101776Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22101776Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23101776Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24101776Stjr * SUCH DAMAGE.
25101776Stjr */
26101776Stjr
27101776Stjr#include <sys/cdefs.h>
28101776Stjr__FBSDID("$FreeBSD: head/lib/libc/stdio/ungetwc.c 149313 2005-08-20 07:59:13Z stefanf $");
29101776Stjr
30101776Stjr#include "namespace.h"
31101776Stjr#include <errno.h>
32149313Sstefanf#include <limits.h>
33101776Stjr#include <stdio.h>
34103523Stjr#include <stdlib.h>
35101776Stjr#include <wchar.h>
36101776Stjr#include "un-namespace.h"
37101776Stjr#include "libc_private.h"
38101776Stjr#include "local.h"
39132442Stjr#include "mblocal.h"
40101776Stjr
41103782Stjr/*
42103782Stjr * Non-MT-safe version.
43103782Stjr */
44101776Stjrwint_t
45103782Stjr__ungetwc(wint_t wc, FILE *fp)
46101776Stjr{
47103523Stjr	char buf[MB_LEN_MAX];
48103523Stjr	size_t len;
49101776Stjr
50103523Stjr	if (wc == WEOF)
51103782Stjr		return (WEOF);
52132442Stjr	if ((len = __wcrtomb(buf, wc, &fp->_extra->mbstate)) == (size_t)-1) {
53105234Stjr		fp->_flags |= __SERR;
54103782Stjr		return (WEOF);
55105234Stjr	}
56103523Stjr	while (len-- != 0)
57103677Stjr		if (__ungetc((unsigned char)buf[len], fp) == EOF)
58103782Stjr			return (WEOF);
59103523Stjr
60103523Stjr	return (wc);
61103782Stjr}
62103677Stjr
63103782Stjr/*
64103782Stjr * MT-safe version.
65103782Stjr */
66103782Stjrwint_t
67103782Stjrungetwc(wint_t wc, FILE *fp)
68103782Stjr{
69103782Stjr	wint_t r;
70103782Stjr
71103782Stjr	FLOCKFILE(fp);
72103782Stjr	ORIENT(fp, 1);
73103782Stjr	r = __ungetwc(wc, fp);
74103677Stjr	FUNLOCKFILE(fp);
75103782Stjr
76103782Stjr	return (r);
77101776Stjr}
78