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