xref: /freebsd/lib/libc/stdio/vswscanf.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
11f4ff850STim J. Robbins /*-
21f4ff850STim J. Robbins  * Copyright (c) 1990, 1993
31f4ff850STim J. Robbins  *	The Regents of the University of California.  All rights reserved.
41f4ff850STim J. Robbins  *
51f4ff850STim J. Robbins  * This code is derived from software contributed to Berkeley by
61f4ff850STim J. Robbins  * Donn Seeley at UUNET Technologies, Inc.
71f4ff850STim J. Robbins  *
8*3c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
9*3c87aa1dSDavid Chisnall  * All rights reserved.
10*3c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
11*3c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
12*3c87aa1dSDavid Chisnall  *
131f4ff850STim J. Robbins  * Redistribution and use in source and binary forms, with or without
141f4ff850STim J. Robbins  * modification, are permitted provided that the following conditions
151f4ff850STim J. Robbins  * are met:
161f4ff850STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
171f4ff850STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
181f4ff850STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
191f4ff850STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
201f4ff850STim J. Robbins  *    documentation and/or other materials provided with the distribution.
211f4ff850STim J. Robbins  * 4. Neither the name of the University nor the names of its contributors
221f4ff850STim J. Robbins  *    may be used to endorse or promote products derived from this software
231f4ff850STim J. Robbins  *    without specific prior written permission.
241f4ff850STim J. Robbins  *
251f4ff850STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261f4ff850STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271f4ff850STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281f4ff850STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291f4ff850STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301f4ff850STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311f4ff850STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321f4ff850STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331f4ff850STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341f4ff850STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351f4ff850STim J. Robbins  * SUCH DAMAGE.
361f4ff850STim J. Robbins  */
371f4ff850STim J. Robbins 
381f4ff850STim J. Robbins #include <sys/cdefs.h>
391f4ff850STim J. Robbins #if 0
401f4ff850STim J. Robbins #if defined(LIBC_SCCS) && !defined(lint)
411f4ff850STim J. Robbins static char sccsid[] = "@(#)vsscanf.c	8.1 (Berkeley) 6/4/93";
421f4ff850STim J. Robbins #endif /* LIBC_SCCS and not lint */
431f4ff850STim J. Robbins __FBSDID("FreeBSD: src/lib/libc/stdio/vsscanf.c,v 1.11 2002/08/21 16:19:57 mike Exp ");
441f4ff850STim J. Robbins #endif
451f4ff850STim J. Robbins __FBSDID("$FreeBSD$");
461f4ff850STim J. Robbins 
471f4ff850STim J. Robbins #include <limits.h>
481f4ff850STim J. Robbins #include <stdarg.h>
491f4ff850STim J. Robbins #include <stdio.h>
501f4ff850STim J. Robbins #include <stdlib.h>
511f4ff850STim J. Robbins #include <string.h>
521f4ff850STim J. Robbins #include <wchar.h>
531f4ff850STim J. Robbins #include "local.h"
54*3c87aa1dSDavid Chisnall #include "xlocale_private.h"
551f4ff850STim J. Robbins 
561f4ff850STim J. Robbins static int	eofread(void *, char *, int);
571f4ff850STim J. Robbins 
581f4ff850STim J. Robbins static int
591f4ff850STim J. Robbins eofread(void *cookie, char *buf, int len)
601f4ff850STim J. Robbins {
611f4ff850STim J. Robbins 
621f4ff850STim J. Robbins 	return (0);
631f4ff850STim J. Robbins }
641f4ff850STim J. Robbins 
651f4ff850STim J. Robbins int
66*3c87aa1dSDavid Chisnall vswscanf_l(const wchar_t * __restrict str, locale_t locale,
67*3c87aa1dSDavid Chisnall 		const wchar_t * __restrict fmt, va_list ap)
681f4ff850STim J. Robbins {
6993996f6dSTim J. Robbins 	static const mbstate_t initial;
7093996f6dSTim J. Robbins 	mbstate_t mbs;
711b0181dfSJohn Baldwin 	FILE f = FAKE_FILE;
721f4ff850STim J. Robbins 	char *mbstr;
731f4ff850STim J. Robbins 	size_t mlen;
741f4ff850STim J. Robbins 	int r;
75f27b1c06SRoman Divacky 	const wchar_t *strp;
76*3c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
771f4ff850STim J. Robbins 
781f4ff850STim J. Robbins 	/*
791f4ff850STim J. Robbins 	 * XXX Convert the wide character string to multibyte, which
801f4ff850STim J. Robbins 	 * __vfwscanf() will convert back to wide characters.
811f4ff850STim J. Robbins 	 */
821f4ff850STim J. Robbins 	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
831f4ff850STim J. Robbins 		return (EOF);
8493996f6dSTim J. Robbins 	mbs = initial;
85f27b1c06SRoman Divacky 	strp = str;
86*3c87aa1dSDavid Chisnall 	if ((mlen = wcsrtombs_l(mbstr, &strp, SIZE_T_MAX, &mbs, locale)) == (size_t)-1) {
871f4ff850STim J. Robbins 		free(mbstr);
881f4ff850STim J. Robbins 		return (EOF);
891f4ff850STim J. Robbins 	}
901f4ff850STim J. Robbins 	f._flags = __SRD;
911f4ff850STim J. Robbins 	f._bf._base = f._p = (unsigned char *)mbstr;
921f4ff850STim J. Robbins 	f._bf._size = f._r = mlen;
931f4ff850STim J. Robbins 	f._read = eofread;
94*3c87aa1dSDavid Chisnall 	r = __vfwscanf(&f, locale, fmt, ap);
951f4ff850STim J. Robbins 	free(mbstr);
961f4ff850STim J. Robbins 
971f4ff850STim J. Robbins 	return (r);
981f4ff850STim J. Robbins }
99*3c87aa1dSDavid Chisnall int
100*3c87aa1dSDavid Chisnall vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
101*3c87aa1dSDavid Chisnall     va_list ap)
102*3c87aa1dSDavid Chisnall {
103*3c87aa1dSDavid Chisnall 	return vswscanf_l(str, __get_locale(), fmt, ap);
104*3c87aa1dSDavid Chisnall }
105