xref: /minix3/lib/libc/stdio/vswscanf.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: vswscanf.c,v 1.12 2013/05/17 12:55:57 joerg Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Donn Seeley at UUNET Technologies, Inc.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. All advertising materials mentioning features or use of this software
192fe8fb19SBen Gras  *    must display the following acknowledgement:
202fe8fb19SBen Gras  *	This product includes software developed by the University of
212fe8fb19SBen Gras  *	California, Berkeley and its contributors.
222fe8fb19SBen Gras  * 4. Neither the name of the University nor the names of its contributors
232fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
242fe8fb19SBen Gras  *    without specific prior written permission.
252fe8fb19SBen Gras  *
262fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
272fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
282fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
292fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
302fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
312fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
322fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
332fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
342fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
352fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
362fe8fb19SBen Gras  * SUCH DAMAGE.
372fe8fb19SBen Gras  */
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)vsscanf.c	8.1 (Berkeley) 6/4/93";
432fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/libc/stdio/vswscanf.c,v 1.3 2004/04/07 09:55:05 tjr Exp $");
442fe8fb19SBen Gras #else
45*84d9c625SLionel Sambuc __RCSID("$NetBSD: vswscanf.c,v 1.12 2013/05/17 12:55:57 joerg Exp $");
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
482fe8fb19SBen Gras 
49*84d9c625SLionel Sambuc #include "namespace.h"
50*84d9c625SLionel Sambuc 
51f14fb602SLionel Sambuc #include <assert.h>
522fe8fb19SBen Gras #include <limits.h>
53*84d9c625SLionel Sambuc #include <locale.h>
542fe8fb19SBen Gras #include <stdarg.h>
552fe8fb19SBen Gras #include <stdio.h>
562fe8fb19SBen Gras #include <stdlib.h>
572fe8fb19SBen Gras #include <string.h>
582fe8fb19SBen Gras #include <wchar.h>
592fe8fb19SBen Gras #include "reentrant.h"
60*84d9c625SLionel Sambuc #include "setlocale_local.h"
612fe8fb19SBen Gras #include "local.h"
622fe8fb19SBen Gras 
__weak_alias(vswscanf_l,_vswscanf_l)63*84d9c625SLionel Sambuc __weak_alias(vswscanf_l, _vswscanf_l)
64*84d9c625SLionel Sambuc 
65f14fb602SLionel Sambuc static ssize_t
662fe8fb19SBen Gras /*ARGSUSED*/
67f14fb602SLionel Sambuc eofread(void *cookie, void *buf, size_t len)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras 
70f14fb602SLionel Sambuc 	return 0;
712fe8fb19SBen Gras }
722fe8fb19SBen Gras 
732fe8fb19SBen Gras int
vswscanf_l(const wchar_t * __restrict str,locale_t loc,const wchar_t * __restrict fmt,va_list ap)74*84d9c625SLionel Sambuc vswscanf_l(const wchar_t * __restrict str, locale_t loc,
75*84d9c625SLionel Sambuc     const wchar_t * __restrict fmt, va_list ap)
762fe8fb19SBen Gras {
772fe8fb19SBen Gras 	static const mbstate_t initial;
782fe8fb19SBen Gras 	mbstate_t mbs;
792fe8fb19SBen Gras 	FILE f;
802fe8fb19SBen Gras 	char *mbstr;
812fe8fb19SBen Gras 	size_t mlen;
822fe8fb19SBen Gras 	int r;
832fe8fb19SBen Gras 	const wchar_t *rstr = str;
842fe8fb19SBen Gras 	struct __sfileext fext;
852fe8fb19SBen Gras 
862fe8fb19SBen Gras 	/*
872fe8fb19SBen Gras 	 * XXX Convert the wide character string to multibyte, which
882fe8fb19SBen Gras 	 * __vfwscanf() will convert back to wide characters.
892fe8fb19SBen Gras 	 */
90*84d9c625SLionel Sambuc 	mbstr = malloc(wcslen(str) * MB_CUR_MAX_L(loc) + 1);
91*84d9c625SLionel Sambuc 	if (mbstr == NULL)
92f14fb602SLionel Sambuc 		return EOF;
932fe8fb19SBen Gras 	mbs = initial;
94*84d9c625SLionel Sambuc 	if ((mlen = wcsrtombs_l(mbstr, &rstr, SIZE_T_MAX, &mbs, loc))
95*84d9c625SLionel Sambuc 	    == (size_t)-1) {
962fe8fb19SBen Gras 		free(mbstr);
97f14fb602SLionel Sambuc 		return EOF;
982fe8fb19SBen Gras 	}
992fe8fb19SBen Gras 	_FILEEXT_SETUP(&f, &fext);
1002fe8fb19SBen Gras 	(void)memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data));
1012fe8fb19SBen Gras 	f._file = -1;
1022fe8fb19SBen Gras 	f._flags = __SRD;
1032fe8fb19SBen Gras 	f._bf._base = f._p = (unsigned char *)mbstr;
104f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, mlen));
105f14fb602SLionel Sambuc 	f._bf._size = f._r = (int)mlen;
1062fe8fb19SBen Gras 	f._read = eofread;
1072fe8fb19SBen Gras 	_UB(&f)._base = NULL;
108*84d9c625SLionel Sambuc 	r = __vfwscanf_unlocked_l(&f, loc, fmt, ap);
1092fe8fb19SBen Gras 	free(mbstr);
1102fe8fb19SBen Gras 
111f14fb602SLionel Sambuc 	return r;
1122fe8fb19SBen Gras }
113*84d9c625SLionel Sambuc 
114*84d9c625SLionel Sambuc int
vswscanf(const wchar_t * __restrict str,const wchar_t * __restrict fmt,va_list ap)115*84d9c625SLionel Sambuc vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
116*84d9c625SLionel Sambuc     va_list ap)
117*84d9c625SLionel Sambuc {
118*84d9c625SLionel Sambuc 	return vswscanf_l(str, _current_locale(), fmt, ap);
119*84d9c625SLionel Sambuc }
120