vswscanf.c revision 205021
1103856Stjr/*-
2103856Stjr * Copyright (c) 1990, 1993
3103856Stjr *	The Regents of the University of California.  All rights reserved.
4103856Stjr *
5103856Stjr * This code is derived from software contributed to Berkeley by
6103856Stjr * Donn Seeley at UUNET Technologies, Inc.
7103856Stjr *
8103856Stjr * Redistribution and use in source and binary forms, with or without
9103856Stjr * modification, are permitted provided that the following conditions
10103856Stjr * are met:
11103856Stjr * 1. Redistributions of source code must retain the above copyright
12103856Stjr *    notice, this list of conditions and the following disclaimer.
13103856Stjr * 2. Redistributions in binary form must reproduce the above copyright
14103856Stjr *    notice, this list of conditions and the following disclaimer in the
15103856Stjr *    documentation and/or other materials provided with the distribution.
16103856Stjr * 4. Neither the name of the University nor the names of its contributors
17103856Stjr *    may be used to endorse or promote products derived from this software
18103856Stjr *    without specific prior written permission.
19103856Stjr *
20103856Stjr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21103856Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22103856Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23103856Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24103856Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25103856Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26103856Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27103856Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28103856Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29103856Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30103856Stjr * SUCH DAMAGE.
31103856Stjr */
32103856Stjr
33103856Stjr#include <sys/cdefs.h>
34103856Stjr#if 0
35103856Stjr#if defined(LIBC_SCCS) && !defined(lint)
36103856Stjrstatic char sccsid[] = "@(#)vsscanf.c	8.1 (Berkeley) 6/4/93";
37103856Stjr#endif /* LIBC_SCCS and not lint */
38103856Stjr__FBSDID("FreeBSD: src/lib/libc/stdio/vsscanf.c,v 1.11 2002/08/21 16:19:57 mike Exp ");
39103856Stjr#endif
40103856Stjr__FBSDID("$FreeBSD: head/lib/libc/stdio/vswscanf.c 205021 2010-03-11 17:03:32Z jhb $");
41103856Stjr
42103856Stjr#include <limits.h>
43103856Stjr#include <stdarg.h>
44103856Stjr#include <stdio.h>
45103856Stjr#include <stdlib.h>
46103856Stjr#include <string.h>
47103856Stjr#include <wchar.h>
48103856Stjr#include "local.h"
49103856Stjr
50103856Stjrstatic int	eofread(void *, char *, int);
51103856Stjr
52103856Stjrstatic int
53103856Stjreofread(void *cookie, char *buf, int len)
54103856Stjr{
55103856Stjr
56103856Stjr	return (0);
57103856Stjr}
58103856Stjr
59103856Stjrint
60103856Stjrvswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
61103856Stjr    va_list ap)
62103856Stjr{
63128002Stjr	static const mbstate_t initial;
64128002Stjr	mbstate_t mbs;
65205021Sjhb	FILE f = FAKE_FILE;
66103856Stjr	char *mbstr;
67103856Stjr	size_t mlen;
68103856Stjr	int r;
69187302Srdivacky	const wchar_t *strp;
70103856Stjr
71103856Stjr	/*
72103856Stjr	 * XXX Convert the wide character string to multibyte, which
73103856Stjr	 * __vfwscanf() will convert back to wide characters.
74103856Stjr	 */
75103856Stjr	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
76103856Stjr		return (EOF);
77128002Stjr	mbs = initial;
78187302Srdivacky	strp = str;
79187302Srdivacky	if ((mlen = wcsrtombs(mbstr, &strp, SIZE_T_MAX, &mbs)) == (size_t)-1) {
80103856Stjr		free(mbstr);
81103856Stjr		return (EOF);
82103856Stjr	}
83103856Stjr	f._flags = __SRD;
84103856Stjr	f._bf._base = f._p = (unsigned char *)mbstr;
85103856Stjr	f._bf._size = f._r = mlen;
86103856Stjr	f._read = eofread;
87103856Stjr	r = __vfwscanf(&f, fmt, ap);
88103856Stjr	free(mbstr);
89103856Stjr
90103856Stjr	return (r);
91103856Stjr}
92