xref: /freebsd/lib/libc/stdio/vswscanf.c (revision 1f4ff8506a801b76186abcb5690294b81ca2b754)
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  *
81f4ff850STim J. Robbins  * Redistribution and use in source and binary forms, with or without
91f4ff850STim J. Robbins  * modification, are permitted provided that the following conditions
101f4ff850STim J. Robbins  * are met:
111f4ff850STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
121f4ff850STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
131f4ff850STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
141f4ff850STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
151f4ff850STim J. Robbins  *    documentation and/or other materials provided with the distribution.
161f4ff850STim J. Robbins  * 3. All advertising materials mentioning features or use of this software
171f4ff850STim J. Robbins  *    must display the following acknowledgement:
181f4ff850STim J. Robbins  *	This product includes software developed by the University of
191f4ff850STim J. Robbins  *	California, Berkeley and its contributors.
201f4ff850STim J. Robbins  * 4. Neither the name of the University nor the names of its contributors
211f4ff850STim J. Robbins  *    may be used to endorse or promote products derived from this software
221f4ff850STim J. Robbins  *    without specific prior written permission.
231f4ff850STim J. Robbins  *
241f4ff850STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251f4ff850STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261f4ff850STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271f4ff850STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281f4ff850STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291f4ff850STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301f4ff850STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311f4ff850STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321f4ff850STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331f4ff850STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341f4ff850STim J. Robbins  * SUCH DAMAGE.
351f4ff850STim J. Robbins  */
361f4ff850STim J. Robbins 
371f4ff850STim J. Robbins #include <sys/cdefs.h>
381f4ff850STim J. Robbins #if 0
391f4ff850STim J. Robbins #if defined(LIBC_SCCS) && !defined(lint)
401f4ff850STim J. Robbins static char sccsid[] = "@(#)vsscanf.c	8.1 (Berkeley) 6/4/93";
411f4ff850STim J. Robbins #endif /* LIBC_SCCS and not lint */
421f4ff850STim J. Robbins __FBSDID("FreeBSD: src/lib/libc/stdio/vsscanf.c,v 1.11 2002/08/21 16:19:57 mike Exp ");
431f4ff850STim J. Robbins #endif
441f4ff850STim J. Robbins __FBSDID("$FreeBSD$");
451f4ff850STim J. Robbins 
461f4ff850STim J. Robbins #include <limits.h>
471f4ff850STim J. Robbins #include <stdarg.h>
481f4ff850STim J. Robbins #include <stdio.h>
491f4ff850STim J. Robbins #include <stdlib.h>
501f4ff850STim J. Robbins #include <string.h>
511f4ff850STim J. Robbins #include <wchar.h>
521f4ff850STim J. Robbins #include "local.h"
531f4ff850STim J. Robbins 
541f4ff850STim J. Robbins static int	eofread(void *, char *, int);
551f4ff850STim J. Robbins 
561f4ff850STim J. Robbins static int
571f4ff850STim J. Robbins eofread(void *cookie, char *buf, int len)
581f4ff850STim J. Robbins {
591f4ff850STim J. Robbins 
601f4ff850STim J. Robbins 	return (0);
611f4ff850STim J. Robbins }
621f4ff850STim J. Robbins 
631f4ff850STim J. Robbins int
641f4ff850STim J. Robbins vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
651f4ff850STim J. Robbins     va_list ap)
661f4ff850STim J. Robbins {
671f4ff850STim J. Robbins 	FILE f;
681f4ff850STim J. Robbins 	mbstate_t mbs;
691f4ff850STim J. Robbins 	struct __sFILEX ext;
701f4ff850STim J. Robbins 	char *mbstr;
711f4ff850STim J. Robbins 	size_t mlen;
721f4ff850STim J. Robbins 	int r;
731f4ff850STim J. Robbins 
741f4ff850STim J. Robbins 	/*
751f4ff850STim J. Robbins 	 * XXX Convert the wide character string to multibyte, which
761f4ff850STim J. Robbins 	 * __vfwscanf() will convert back to wide characters.
771f4ff850STim J. Robbins 	 */
781f4ff850STim J. Robbins 	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
791f4ff850STim J. Robbins 		return (EOF);
801f4ff850STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
811f4ff850STim J. Robbins 	if ((mlen = wcsrtombs(mbstr, &str, SIZE_T_MAX, &mbs)) == (size_t)-1) {
821f4ff850STim J. Robbins 		free(mbstr);
831f4ff850STim J. Robbins 		return (EOF);
841f4ff850STim J. Robbins 	}
851f4ff850STim J. Robbins 	f._file = -1;
861f4ff850STim J. Robbins 	f._flags = __SRD;
871f4ff850STim J. Robbins 	f._bf._base = f._p = (unsigned char *)mbstr;
881f4ff850STim J. Robbins 	f._bf._size = f._r = mlen;
891f4ff850STim J. Robbins 	f._read = eofread;
901f4ff850STim J. Robbins 	f._ub._base = NULL;
911f4ff850STim J. Robbins 	f._lb._base = NULL;
921f4ff850STim J. Robbins 	f._extra = &ext;
931f4ff850STim J. Robbins 	INITEXTRA(&f);
941f4ff850STim J. Robbins 	r = __vfwscanf(&f, fmt, ap);
951f4ff850STim J. Robbins 	free(mbstr);
961f4ff850STim J. Robbins 
971f4ff850STim J. Robbins 	return (r);
981f4ff850STim J. Robbins }
99