1*9b9d2a55Sguenther /* $OpenBSD: ungetwc.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2c2fb3212Sespie /* $NetBSD: ungetwc.c,v 1.2 2003/01/18 11:29:59 thorpej Exp $ */
3c2fb3212Sespie
4c2fb3212Sespie /*-
5c2fb3212Sespie * Copyright (c)2001 Citrus Project,
6c2fb3212Sespie * All rights reserved.
7c2fb3212Sespie *
8c2fb3212Sespie * Redistribution and use in source and binary forms, with or without
9c2fb3212Sespie * modification, are permitted provided that the following conditions
10c2fb3212Sespie * are met:
11c2fb3212Sespie * 1. Redistributions of source code must retain the above copyright
12c2fb3212Sespie * notice, this list of conditions and the following disclaimer.
13c2fb3212Sespie * 2. Redistributions in binary form must reproduce the above copyright
14c2fb3212Sespie * notice, this list of conditions and the following disclaimer in the
15c2fb3212Sespie * documentation and/or other materials provided with the distribution.
16c2fb3212Sespie *
17c2fb3212Sespie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c2fb3212Sespie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c2fb3212Sespie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c2fb3212Sespie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c2fb3212Sespie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c2fb3212Sespie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c2fb3212Sespie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c2fb3212Sespie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c2fb3212Sespie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c2fb3212Sespie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c2fb3212Sespie * SUCH DAMAGE.
28c2fb3212Sespie *
29c2fb3212Sespie * $Citrus$
30c2fb3212Sespie */
31c2fb3212Sespie
32c2fb3212Sespie #include <errno.h>
33c2fb3212Sespie #include <stdio.h>
34c2fb3212Sespie #include <wchar.h>
35c2fb3212Sespie #include "local.h"
36c2fb3212Sespie
37c2fb3212Sespie wint_t
__ungetwc(wint_t wc,FILE * fp)382d31a56bSstsp __ungetwc(wint_t wc, FILE *fp)
39c2fb3212Sespie {
40c2fb3212Sespie struct wchar_io_data *wcio;
41c2fb3212Sespie
42c2fb3212Sespie if (wc == WEOF)
43c2fb3212Sespie return WEOF;
44c2fb3212Sespie
45c2fb3212Sespie _SET_ORIENTATION(fp, 1);
46c2fb3212Sespie /*
47c2fb3212Sespie * XXX since we have no way to transform a wchar string to
48c2fb3212Sespie * a char string in reverse order, we can't use ungetc.
49c2fb3212Sespie */
50c2fb3212Sespie /* XXX should we flush ungetc buffer? */
51c2fb3212Sespie
52c2fb3212Sespie wcio = WCIO_GET(fp);
53c2fb3212Sespie if (wcio == 0) {
54c2fb3212Sespie errno = ENOMEM; /* XXX */
55c2fb3212Sespie return WEOF;
56c2fb3212Sespie }
57c2fb3212Sespie
58c2fb3212Sespie if (wcio->wcio_ungetwc_inbuf >= WCIO_UNGETWC_BUFSIZE) {
59c2fb3212Sespie return WEOF;
60c2fb3212Sespie }
61c2fb3212Sespie
62c2fb3212Sespie wcio->wcio_ungetwc_buf[wcio->wcio_ungetwc_inbuf++] = wc;
63c2fb3212Sespie __sclearerr(fp);
64c2fb3212Sespie
65c2fb3212Sespie return wc;
66c2fb3212Sespie }
672d31a56bSstsp
682d31a56bSstsp wint_t
ungetwc(wint_t wc,FILE * fp)692d31a56bSstsp ungetwc(wint_t wc, FILE *fp)
702d31a56bSstsp {
712d31a56bSstsp wint_t r;
722d31a56bSstsp
732d31a56bSstsp FLOCKFILE(fp);
742d31a56bSstsp r = __ungetwc(wc, fp);
752d31a56bSstsp FUNLOCKFILE(fp);
762d31a56bSstsp return (r);
772d31a56bSstsp }
78*9b9d2a55Sguenther DEF_STRONG(ungetwc);
79