1*e0ac190eSjoerg /* $NetBSD: vswscanf.c,v 1.12 2013/05/17 12:55:57 joerg Exp $ */
2f432bbb6Schristos
3f432bbb6Schristos /*-
4f432bbb6Schristos * Copyright (c) 1990, 1993
5f432bbb6Schristos * The Regents of the University of California. All rights reserved.
6f432bbb6Schristos *
7f432bbb6Schristos * This code is derived from software contributed to Berkeley by
8f432bbb6Schristos * Donn Seeley at UUNET Technologies, Inc.
9f432bbb6Schristos *
10f432bbb6Schristos * Redistribution and use in source and binary forms, with or without
11f432bbb6Schristos * modification, are permitted provided that the following conditions
12f432bbb6Schristos * are met:
13f432bbb6Schristos * 1. Redistributions of source code must retain the above copyright
14f432bbb6Schristos * notice, this list of conditions and the following disclaimer.
15f432bbb6Schristos * 2. Redistributions in binary form must reproduce the above copyright
16f432bbb6Schristos * notice, this list of conditions and the following disclaimer in the
17f432bbb6Schristos * documentation and/or other materials provided with the distribution.
18f432bbb6Schristos * 3. All advertising materials mentioning features or use of this software
19f432bbb6Schristos * must display the following acknowledgement:
20f432bbb6Schristos * This product includes software developed by the University of
21f432bbb6Schristos * California, Berkeley and its contributors.
22f432bbb6Schristos * 4. Neither the name of the University nor the names of its contributors
23f432bbb6Schristos * may be used to endorse or promote products derived from this software
24f432bbb6Schristos * without specific prior written permission.
25f432bbb6Schristos *
26f432bbb6Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27f432bbb6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28f432bbb6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29f432bbb6Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30f432bbb6Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31f432bbb6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32f432bbb6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33f432bbb6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34f432bbb6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35f432bbb6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36f432bbb6Schristos * SUCH DAMAGE.
37f432bbb6Schristos */
38f432bbb6Schristos
39f432bbb6Schristos #include <sys/cdefs.h>
40f432bbb6Schristos #if defined(LIBC_SCCS) && !defined(lint)
41f432bbb6Schristos #if 0
42f432bbb6Schristos static char sccsid[] = "@(#)vsscanf.c 8.1 (Berkeley) 6/4/93";
43f432bbb6Schristos __FBSDID("$FreeBSD: src/lib/libc/stdio/vswscanf.c,v 1.3 2004/04/07 09:55:05 tjr Exp $");
44f432bbb6Schristos #else
45*e0ac190eSjoerg __RCSID("$NetBSD: vswscanf.c,v 1.12 2013/05/17 12:55:57 joerg Exp $");
46f432bbb6Schristos #endif
47f432bbb6Schristos #endif /* LIBC_SCCS and not lint */
48f432bbb6Schristos
499790c07aSjoerg #include "namespace.h"
509790c07aSjoerg
51c5e820caSchristos #include <assert.h>
52f432bbb6Schristos #include <limits.h>
539790c07aSjoerg #include <locale.h>
54f432bbb6Schristos #include <stdarg.h>
55f432bbb6Schristos #include <stdio.h>
56f432bbb6Schristos #include <stdlib.h>
57f432bbb6Schristos #include <string.h>
58f432bbb6Schristos #include <wchar.h>
59f432bbb6Schristos #include "reentrant.h"
609790c07aSjoerg #include "setlocale_local.h"
61f432bbb6Schristos #include "local.h"
62f432bbb6Schristos
__weak_alias(vswscanf_l,_vswscanf_l)639790c07aSjoerg __weak_alias(vswscanf_l, _vswscanf_l)
649790c07aSjoerg
65de001ba2Schristos static ssize_t
66f432bbb6Schristos /*ARGSUSED*/
67de001ba2Schristos eofread(void *cookie, void *buf, size_t len)
68f432bbb6Schristos {
69f432bbb6Schristos
70526d9427Schristos return 0;
71f432bbb6Schristos }
72f432bbb6Schristos
73f432bbb6Schristos int
vswscanf_l(const wchar_t * __restrict str,locale_t loc,const wchar_t * __restrict fmt,va_list ap)749790c07aSjoerg vswscanf_l(const wchar_t * __restrict str, locale_t loc,
759790c07aSjoerg const wchar_t * __restrict fmt, va_list ap)
76f432bbb6Schristos {
77f432bbb6Schristos static const mbstate_t initial;
78f432bbb6Schristos mbstate_t mbs;
79f432bbb6Schristos FILE f;
80f432bbb6Schristos char *mbstr;
81f432bbb6Schristos size_t mlen;
82f432bbb6Schristos int r;
8303256c6eSchristos const wchar_t *rstr = str;
84f432bbb6Schristos struct __sfileext fext;
85f432bbb6Schristos
86f432bbb6Schristos /*
87f432bbb6Schristos * XXX Convert the wide character string to multibyte, which
88f432bbb6Schristos * __vfwscanf() will convert back to wide characters.
89f432bbb6Schristos */
909790c07aSjoerg mbstr = malloc(wcslen(str) * MB_CUR_MAX_L(loc) + 1);
919790c07aSjoerg if (mbstr == NULL)
92526d9427Schristos return EOF;
93f432bbb6Schristos mbs = initial;
949790c07aSjoerg if ((mlen = wcsrtombs_l(mbstr, &rstr, SIZE_T_MAX, &mbs, loc))
959790c07aSjoerg == (size_t)-1) {
96f432bbb6Schristos free(mbstr);
97526d9427Schristos return EOF;
98f432bbb6Schristos }
99f432bbb6Schristos _FILEEXT_SETUP(&f, &fext);
1009ebdd729Schristos (void)memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data));
101f432bbb6Schristos f._file = -1;
102f432bbb6Schristos f._flags = __SRD;
103f432bbb6Schristos f._bf._base = f._p = (unsigned char *)mbstr;
104c5e820caSchristos _DIAGASSERT(__type_fit(int, mlen));
105c5e820caSchristos f._bf._size = f._r = (int)mlen;
106f432bbb6Schristos f._read = eofread;
107f432bbb6Schristos _UB(&f)._base = NULL;
1089790c07aSjoerg r = __vfwscanf_unlocked_l(&f, loc, fmt, ap);
109f432bbb6Schristos free(mbstr);
110f432bbb6Schristos
111526d9427Schristos return r;
112f432bbb6Schristos }
1139790c07aSjoerg
1149790c07aSjoerg int
vswscanf(const wchar_t * __restrict str,const wchar_t * __restrict fmt,va_list ap)1159790c07aSjoerg vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
1169790c07aSjoerg va_list ap)
1179790c07aSjoerg {
118*e0ac190eSjoerg return vswscanf_l(str, _current_locale(), fmt, ap);
1199790c07aSjoerg }
120