ungetwc.c revision 103523
11297Salm/*-
21297Salm * Copyright (c) 2002 Tim J. Robbins.
31297Salm * All rights reserved.
41297Salm *
51297Salm * Redistribution and use in source and binary forms, with or without
61297Salm * modification, are permitted provided that the following conditions
71297Salm * are met:
81297Salm * 1. Redistributions of source code must retain the above copyright
91297Salm *    notice, this list of conditions and the following disclaimer.
101297Salm * 2. Redistributions in binary form must reproduce the above copyright
111297Salm *    notice, this list of conditions and the following disclaimer in the
121297Salm *    documentation and/or other materials provided with the distribution.
131297Salm *
141297Salm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151297Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161297Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171297Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181297Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191297Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201297Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211297Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221297Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231297Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241297Salm * SUCH DAMAGE.
251297Salm */
261297Salm
273044Sdg#include <sys/cdefs.h>
288855Srgrimes__FBSDID("$FreeBSD: head/lib/libc/stdio/ungetwc.c 103523 2002-09-18 05:58:11Z tjr $");
291297Salm
301297Salm#include "namespace.h"
311297Salm#include <errno.h>
321297Salm#include <stdio.h>
331297Salm#include <stdlib.h>
341297Salm#include <wchar.h>
351297Salm#include "un-namespace.h"
361297Salm#include "libc_private.h"
371297Salm#include "local.h"
381297Salm
391297Salmwint_t
401297Salmungetwc(wint_t wc, FILE *fp)
411297Salm{
421297Salm	char buf[MB_LEN_MAX];
431297Salm	mbstate_t mbs;
441297Salm	size_t len;
451297Salm
461297Salm	ORIENTLOCK(fp, 1);
471297Salm
481297Salm	if (wc == WEOF)
491297Salm		return (WEOF);
501297Salm
511297Salm	memset(&mbs, 0, sizeof(mbs));
521297Salm	if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1)
531297Salm		return (WEOF);
541297Salm	while (len-- != 0)
551297Salm		if (ungetc((unsigned char)buf[len], fp) == EOF)
561297Salm			return (WEOF);
571297Salm
581297Salm	return (wc);
591297Salm}
601297Salm