xref: /freebsd/lib/libc/stdio/vswscanf.c (revision 9fc0ff9d85b1497b45a4c6d74e0bc7eea9dcf37b)
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 <limits.h>
411f4ff850STim J. Robbins #include <stdarg.h>
421f4ff850STim J. Robbins #include <stdio.h>
431f4ff850STim J. Robbins #include <stdlib.h>
441f4ff850STim J. Robbins #include <string.h>
451f4ff850STim J. Robbins #include <wchar.h>
461f4ff850STim J. Robbins #include "local.h"
473c87aa1dSDavid Chisnall #include "xlocale_private.h"
481f4ff850STim J. Robbins 
491f4ff850STim J. Robbins static int	eofread(void *, char *, int);
501f4ff850STim J. Robbins 
511f4ff850STim J. Robbins static int
eofread(void * cookie,char * buf,int len)521f4ff850STim J. Robbins eofread(void *cookie, char *buf, int len)
531f4ff850STim J. Robbins {
541f4ff850STim J. Robbins 
551f4ff850STim J. Robbins 	return (0);
561f4ff850STim J. Robbins }
571f4ff850STim J. Robbins 
581f4ff850STim J. Robbins int
vswscanf_l(const wchar_t * __restrict str,locale_t locale,const wchar_t * __restrict fmt,va_list ap)593c87aa1dSDavid Chisnall vswscanf_l(const wchar_t * __restrict str, locale_t locale,
603c87aa1dSDavid Chisnall 		const wchar_t * __restrict fmt, va_list ap)
611f4ff850STim J. Robbins {
6293996f6dSTim J. Robbins 	static const mbstate_t initial;
6393996f6dSTim J. Robbins 	mbstate_t mbs;
641b0181dfSJohn Baldwin 	FILE f = FAKE_FILE;
651f4ff850STim J. Robbins 	char *mbstr;
661f4ff850STim J. Robbins 	size_t mlen;
671f4ff850STim J. Robbins 	int r;
68f27b1c06SRoman Divacky 	const wchar_t *strp;
693c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
701f4ff850STim J. Robbins 
711f4ff850STim J. Robbins 	/*
721f4ff850STim J. Robbins 	 * XXX Convert the wide character string to multibyte, which
731f4ff850STim J. Robbins 	 * __vfwscanf() will convert back to wide characters.
741f4ff850STim J. Robbins 	 */
751f4ff850STim J. Robbins 	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
761f4ff850STim J. Robbins 		return (EOF);
7793996f6dSTim J. Robbins 	mbs = initial;
78f27b1c06SRoman Divacky 	strp = str;
793c87aa1dSDavid Chisnall 	if ((mlen = wcsrtombs_l(mbstr, &strp, SIZE_T_MAX, &mbs, locale)) == (size_t)-1) {
801f4ff850STim J. Robbins 		free(mbstr);
811f4ff850STim J. Robbins 		return (EOF);
821f4ff850STim J. Robbins 	}
831f4ff850STim J. Robbins 	f._flags = __SRD;
841f4ff850STim J. Robbins 	f._bf._base = f._p = (unsigned char *)mbstr;
851f4ff850STim J. Robbins 	f._bf._size = f._r = mlen;
861f4ff850STim J. Robbins 	f._read = eofread;
873c87aa1dSDavid Chisnall 	r = __vfwscanf(&f, locale, fmt, ap);
881f4ff850STim J. Robbins 	free(mbstr);
891f4ff850STim J. Robbins 
901f4ff850STim J. Robbins 	return (r);
911f4ff850STim J. Robbins }
923c87aa1dSDavid Chisnall int
vswscanf(const wchar_t * __restrict str,const wchar_t * __restrict fmt,va_list ap)933c87aa1dSDavid Chisnall vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
943c87aa1dSDavid Chisnall     va_list ap)
953c87aa1dSDavid Chisnall {
963c87aa1dSDavid Chisnall 	return vswscanf_l(str, __get_locale(), fmt, ap);
973c87aa1dSDavid Chisnall }
98