1*88c3eadbSlukem /* $NetBSD: ungetwc.c,v 1.3 2005/06/12 05:21:27 lukem Exp $ */
217f3654aSyamt
317f3654aSyamt /*-
417f3654aSyamt * Copyright (c)2001 Citrus Project,
517f3654aSyamt * All rights reserved.
617f3654aSyamt *
717f3654aSyamt * Redistribution and use in source and binary forms, with or without
817f3654aSyamt * modification, are permitted provided that the following conditions
917f3654aSyamt * are met:
1017f3654aSyamt * 1. Redistributions of source code must retain the above copyright
1117f3654aSyamt * notice, this list of conditions and the following disclaimer.
1217f3654aSyamt * 2. Redistributions in binary form must reproduce the above copyright
1317f3654aSyamt * notice, this list of conditions and the following disclaimer in the
1417f3654aSyamt * documentation and/or other materials provided with the distribution.
1517f3654aSyamt *
1617f3654aSyamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1717f3654aSyamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1817f3654aSyamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1917f3654aSyamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2017f3654aSyamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2117f3654aSyamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2217f3654aSyamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2317f3654aSyamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2417f3654aSyamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2517f3654aSyamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2617f3654aSyamt * SUCH DAMAGE.
2717f3654aSyamt *
2817f3654aSyamt * $Citrus$
2917f3654aSyamt */
3017f3654aSyamt
31*88c3eadbSlukem #include <sys/cdefs.h>
32*88c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
33*88c3eadbSlukem __RCSID("$NetBSD: ungetwc.c,v 1.3 2005/06/12 05:21:27 lukem Exp $");
34*88c3eadbSlukem #endif /* LIBC_SCCS and not lint */
35*88c3eadbSlukem
3617f3654aSyamt #include <assert.h>
3717f3654aSyamt #include <errno.h>
3817f3654aSyamt #include <stdio.h>
3917f3654aSyamt #include <wchar.h>
4017f3654aSyamt #include "reentrant.h"
413fdac2b8Sthorpej #include "local.h"
4217f3654aSyamt
4317f3654aSyamt wint_t
ungetwc(wint_t wc,FILE * fp)4417f3654aSyamt ungetwc(wint_t wc, FILE *fp)
4517f3654aSyamt {
4617f3654aSyamt struct wchar_io_data *wcio;
4717f3654aSyamt
4817f3654aSyamt _DIAGASSERT(fp);
4917f3654aSyamt
5017f3654aSyamt if (wc == WEOF)
5117f3654aSyamt return WEOF;
5217f3654aSyamt
5317f3654aSyamt FLOCKFILE(fp);
5417f3654aSyamt _SET_ORIENTATION(fp, 1);
5517f3654aSyamt /*
5617f3654aSyamt * XXX since we have no way to transform a wchar string to
5717f3654aSyamt * a char string in reverse order, we can't use ungetc.
5817f3654aSyamt */
5917f3654aSyamt /* XXX should we flush ungetc buffer? */
6017f3654aSyamt
6117f3654aSyamt wcio = WCIO_GET(fp);
6217f3654aSyamt if (wcio == 0) {
6317f3654aSyamt FUNLOCKFILE(fp);
6417f3654aSyamt errno = ENOMEM; /* XXX */
6517f3654aSyamt return WEOF;
6617f3654aSyamt }
6717f3654aSyamt
6817f3654aSyamt if (wcio->wcio_ungetwc_inbuf >= WCIO_UNGETWC_BUFSIZE) {
6917f3654aSyamt FUNLOCKFILE(fp);
7017f3654aSyamt return WEOF;
7117f3654aSyamt }
7217f3654aSyamt
7317f3654aSyamt wcio->wcio_ungetwc_buf[wcio->wcio_ungetwc_inbuf++] = wc;
7417f3654aSyamt __sclearerr(fp);
7517f3654aSyamt FUNLOCKFILE(fp);
7617f3654aSyamt
7717f3654aSyamt return wc;
7817f3654aSyamt }
79