vswscanf.c revision 103856
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 * 3. All advertising materials mentioning features or use of this software
17103856Stjr *    must display the following acknowledgement:
18103856Stjr *	This product includes software developed by the University of
19103856Stjr *	California, Berkeley and its contributors.
20103856Stjr * 4. Neither the name of the University nor the names of its contributors
21103856Stjr *    may be used to endorse or promote products derived from this software
22103856Stjr *    without specific prior written permission.
23103856Stjr *
24103856Stjr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25103856Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26103856Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27103856Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28103856Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29103856Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30103856Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31103856Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32103856Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33103856Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34103856Stjr * SUCH DAMAGE.
35103856Stjr */
36103856Stjr
37103856Stjr#include <sys/cdefs.h>
38103856Stjr#if 0
39103856Stjr#if defined(LIBC_SCCS) && !defined(lint)
40103856Stjrstatic char sccsid[] = "@(#)vsscanf.c	8.1 (Berkeley) 6/4/93";
41103856Stjr#endif /* LIBC_SCCS and not lint */
42103856Stjr__FBSDID("FreeBSD: src/lib/libc/stdio/vsscanf.c,v 1.11 2002/08/21 16:19:57 mike Exp ");
43103856Stjr#endif
44103856Stjr__FBSDID("$FreeBSD: head/lib/libc/stdio/vswscanf.c 103856 2002-09-23 12:40:06Z tjr $");
45103856Stjr
46103856Stjr#include <limits.h>
47103856Stjr#include <stdarg.h>
48103856Stjr#include <stdio.h>
49103856Stjr#include <stdlib.h>
50103856Stjr#include <string.h>
51103856Stjr#include <wchar.h>
52103856Stjr#include "local.h"
53103856Stjr
54103856Stjrstatic int	eofread(void *, char *, int);
55103856Stjr
56103856Stjrstatic int
57103856Stjreofread(void *cookie, char *buf, int len)
58103856Stjr{
59103856Stjr
60103856Stjr	return (0);
61103856Stjr}
62103856Stjr
63103856Stjrint
64103856Stjrvswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
65103856Stjr    va_list ap)
66103856Stjr{
67103856Stjr	FILE f;
68103856Stjr	mbstate_t mbs;
69103856Stjr	struct __sFILEX ext;
70103856Stjr	char *mbstr;
71103856Stjr	size_t mlen;
72103856Stjr	int r;
73103856Stjr
74103856Stjr	/*
75103856Stjr	 * XXX Convert the wide character string to multibyte, which
76103856Stjr	 * __vfwscanf() will convert back to wide characters.
77103856Stjr	 */
78103856Stjr	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
79103856Stjr		return (EOF);
80103856Stjr	memset(&mbs, 0, sizeof(mbs));
81103856Stjr	if ((mlen = wcsrtombs(mbstr, &str, SIZE_T_MAX, &mbs)) == (size_t)-1) {
82103856Stjr		free(mbstr);
83103856Stjr		return (EOF);
84103856Stjr	}
85103856Stjr	f._file = -1;
86103856Stjr	f._flags = __SRD;
87103856Stjr	f._bf._base = f._p = (unsigned char *)mbstr;
88103856Stjr	f._bf._size = f._r = mlen;
89103856Stjr	f._read = eofread;
90103856Stjr	f._ub._base = NULL;
91103856Stjr	f._lb._base = NULL;
92103856Stjr	f._extra = &ext;
93103856Stjr	INITEXTRA(&f);
94103856Stjr	r = __vfwscanf(&f, fmt, ap);
95103856Stjr	free(mbstr);
96103856Stjr
97103856Stjr	return (r);
98103856Stjr}
99