117000Salanb/*-
217000Salanb * SPDX-License-Identifier: BSD-3-Clause
317000Salanb *
417000Salanb * Copyright (c) 1990, 1993
517000Salanb *	The Regents of the University of California.  All rights reserved.
617000Salanb *
717000Salanb * This code is derived from software contributed to Berkeley by
817000Salanb * Donn Seeley at UUNET Technologies, Inc.
917000Salanb *
1017000Salanb * Copyright (c) 2011 The FreeBSD Foundation
1117000Salanb *
1217000Salanb * Portions of this software were developed by David Chisnall
1317000Salanb * under sponsorship from the FreeBSD Foundation.
1417000Salanb *
1517000Salanb * Redistribution and use in source and binary forms, with or without
1617000Salanb * modification, are permitted provided that the following conditions
1717000Salanb * are met:
1817000Salanb * 1. Redistributions of source code must retain the above copyright
1917000Salanb *    notice, this list of conditions and the following disclaimer.
2017000Salanb * 2. Redistributions in binary form must reproduce the above copyright
2117000Salanb *    notice, this list of conditions and the following disclaimer in the
2217000Salanb *    documentation and/or other materials provided with the distribution.
2317000Salanb * 3. Neither the name of the University nor the names of its contributors
2417000Salanb *    may be used to endorse or promote products derived from this software
2517000Salanb *    without specific prior written permission.
2617000Salanb *
2717000Salanb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2817000Salanb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2917000Salanb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3017000Salanb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3117000Salanb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3217000Salanb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3317000Salanb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3417000Salanb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3517000Salanb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3617000Salanb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3717000Salanb * SUCH DAMAGE.
3817000Salanb */
3917000Salanb
4017000Salanb#include <limits.h>
41#include <stdarg.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <wchar.h>
46#include "local.h"
47#include "xlocale_private.h"
48
49static int	eofread(void *, char *, int);
50
51static int
52eofread(void *cookie, char *buf, int len)
53{
54
55	return (0);
56}
57
58int
59vswscanf_l(const wchar_t * __restrict str, locale_t locale,
60		const wchar_t * __restrict fmt, va_list ap)
61{
62	static const mbstate_t initial;
63	mbstate_t mbs;
64	FILE f = FAKE_FILE;
65	char *mbstr;
66	size_t mlen;
67	int r;
68	const wchar_t *strp;
69	FIX_LOCALE(locale);
70
71	/*
72	 * XXX Convert the wide character string to multibyte, which
73	 * __vfwscanf() will convert back to wide characters.
74	 */
75	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
76		return (EOF);
77	mbs = initial;
78	strp = str;
79	if ((mlen = wcsrtombs_l(mbstr, &strp, SIZE_T_MAX, &mbs, locale)) == (size_t)-1) {
80		free(mbstr);
81		return (EOF);
82	}
83	f._flags = __SRD;
84	f._bf._base = f._p = (unsigned char *)mbstr;
85	f._bf._size = f._r = mlen;
86	f._read = eofread;
87	r = __vfwscanf(&f, locale, fmt, ap);
88	free(mbstr);
89
90	return (r);
91}
92int
93vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
94    va_list ap)
95{
96	return vswscanf_l(str, __get_locale(), fmt, ap);
97}
98