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