]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/fgetwc.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / fgetwc.c
1 /*-
2 * Copyright (c)2001 Citrus Project,
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Citrus$
27
28 NetBSD: fgetwc.c,v 1.5 2006/07/03 17:06:36 tnozaki Exp
29 */
30 #include <LibConfig.h>
31 #include <sys/EfiCdefs.h>
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <wchar.h>
37 #include "reentrant.h"
38 #include "local.h"
39
40 wint_t
41 __fgetwc_unlock(FILE *fp)
42 {
43 struct wchar_io_data *wcio;
44 mbstate_t *st;
45 wchar_t wc;
46 size_t size;
47
48 _DIAGASSERT(fp != NULL);
49
50 _SET_ORIENTATION(fp, 1);
51 wcio = WCIO_GET(fp);
52 if (wcio == 0) {
53 errno = ENOMEM;
54 return WEOF;
55 }
56
57 /* if there're ungetwc'ed wchars, use them */
58 if (wcio->wcio_ungetwc_inbuf) {
59 wc = wcio->wcio_ungetwc_buf[--wcio->wcio_ungetwc_inbuf];
60
61 return wc;
62 }
63
64 st = &wcio->wcio_mbstate_in;
65
66 do {
67 char c;
68 int ch = __sgetc(fp);
69
70 if (ch == EOF) {
71 return WEOF;
72 }
73
74 c = (char)ch;
75 size = mbrtowc(&wc, &c, 1, st);
76 if (size == (size_t)-1) {
77 errno = EILSEQ;
78 fp->_flags |= __SERR;
79 return WEOF;
80 }
81 } while (size == (size_t)-2);
82
83 _DIAGASSERT(size == 1);
84
85 return wc;
86 }
87
88 wint_t
89 fgetwc(FILE *fp)
90 {
91 wint_t r;
92
93 _DIAGASSERT(fp != NULL);
94
95 FLOCKFILE(fp);
96 r = __fgetwc_unlock(fp);
97 FUNLOCKFILE(fp);
98
99 return (r);
100 }