]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/vswscanf.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / StdLib / LibC / Stdio / vswscanf.c
CommitLineData
2aa62f2b 1/*-\r
2 * Copyright (c) 1990, 1993\r
3 * The Regents of the University of California. All rights reserved.\r
4 *\r
5 * This code is derived from software contributed to Berkeley by\r
6 * Donn Seeley at UUNET Technologies, Inc.\r
7 *\r
8 * Redistribution and use in source and binary forms, with or without\r
9 * modification, are permitted provided that the following conditions\r
10 * are met:\r
11 * 1. Redistributions of source code must retain the above copyright\r
12 * notice, this list of conditions and the following disclaimer.\r
13 * 2. Redistributions in binary form must reproduce the above copyright\r
14 * notice, this list of conditions and the following disclaimer in the\r
15 * documentation and/or other materials provided with the distribution.\r
16 * 3. All advertising materials mentioning features or use of this software\r
17 * must display the following acknowledgement:\r
18 * This product includes software developed by the University of\r
19 * California, Berkeley and its contributors.\r
20 * 4. Neither the name of the University nor the names of its contributors\r
21 * may be used to endorse or promote products derived from this software\r
22 * without specific prior written permission.\r
23 *\r
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
34 * SUCH DAMAGE.\r
35\r
36 FreeBSD: src/lib/libc/stdio/vswscanf.c,v 1.3 2004/04/07 09:55:05 tjr Exp\r
37 NetBSD: vswscanf.c,v 1.3 2005/12/02 13:51:22 yamt Exp\r
38 */\r
39#include <LibConfig.h>\r
40#include <sys/EfiCdefs.h>\r
41\r
42#include <limits.h>\r
43#include <stdarg.h>\r
44#include <stdio.h>\r
45#include <stdlib.h>\r
46#include <string.h>\r
47#include <wchar.h>\r
48#include <sys/types.h>\r
49#include "reentrant.h"\r
50#include "local.h"\r
51\r
52static int eofread(void *, char *, int);\r
53\r
54static int\r
55/*ARGSUSED*/\r
56eofread(void *cookie, char *buf, int len)\r
57{\r
58 return (0);\r
59}\r
60\r
61int\r
62vswscanf(\r
63 const wchar_t * __restrict str,\r
64 const wchar_t * __restrict fmt,\r
65 va_list ap\r
66 )\r
67{\r
68 static const mbstate_t initial = { 0 };\r
69 mbstate_t mbs;\r
70 FILE f;\r
71 char *mbstr;\r
72 size_t mlen;\r
73 int r;\r
74 const wchar_t *rstr = str;\r
6f347d0f 75 struct __sfileext fext = { { 0 } };\r
2aa62f2b 76\r
77 /*\r
78 * XXX Convert the wide character string to multibyte, which\r
79 * __vfwscanf() will convert back to wide characters.\r
80 */\r
81 if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)\r
82 return (EOF);\r
83 mbs = initial;\r
84 if ((mlen = wcsrtombs(mbstr, &rstr, SIZE_T_MAX, &mbs)) == (size_t)-1) {\r
85 free(mbstr);\r
86 return (EOF);\r
87 }\r
88 _FILEEXT_SETUP(&f, &fext);\r
89 f._file = -1;\r
90 f._flags = __SRD;\r
91 f._bf._base = f._p = (unsigned char *)mbstr;\r
92 f._bf._size = f._r = (int)mlen;\r
93 f._read = eofread;\r
94 _UB(&f)._base = NULL;\r
95 f._lb._base = NULL;\r
96 r = __vfwscanf_unlocked(&f, fmt, ap);\r
97 free(mbstr);\r
98\r
99 return (r);\r
100}\r