Home | History | Annotate | Line # | Download | only in stdio
vswscanf.c revision 1.6.6.1
      1  1.6.6.1      yamt /*	$NetBSD: vswscanf.c,v 1.6.6.1 2012/04/17 00:05:25 yamt Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 1990, 1993
      5      1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * This code is derived from software contributed to Berkeley by
      8      1.1  christos  * Donn Seeley at UUNET Technologies, Inc.
      9      1.1  christos  *
     10      1.1  christos  * Redistribution and use in source and binary forms, with or without
     11      1.1  christos  * modification, are permitted provided that the following conditions
     12      1.1  christos  * are met:
     13      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  christos  *    documentation and/or other materials provided with the distribution.
     18      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19      1.1  christos  *    must display the following acknowledgement:
     20      1.1  christos  *	This product includes software developed by the University of
     21      1.1  christos  *	California, Berkeley and its contributors.
     22      1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     23      1.1  christos  *    may be used to endorse or promote products derived from this software
     24      1.1  christos  *    without specific prior written permission.
     25      1.1  christos  *
     26      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36      1.1  christos  * SUCH DAMAGE.
     37      1.1  christos  */
     38      1.1  christos 
     39      1.1  christos #include <sys/cdefs.h>
     40      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     41      1.1  christos #if 0
     42      1.1  christos static char sccsid[] = "@(#)vsscanf.c	8.1 (Berkeley) 6/4/93";
     43      1.1  christos __FBSDID("$FreeBSD: src/lib/libc/stdio/vswscanf.c,v 1.3 2004/04/07 09:55:05 tjr Exp $");
     44      1.1  christos #else
     45  1.6.6.1      yamt __RCSID("$NetBSD: vswscanf.c,v 1.6.6.1 2012/04/17 00:05:25 yamt Exp $");
     46      1.1  christos #endif
     47      1.1  christos #endif /* LIBC_SCCS and not lint */
     48      1.1  christos 
     49  1.6.6.1      yamt #include <assert.h>
     50      1.1  christos #include <limits.h>
     51      1.1  christos #include <stdarg.h>
     52      1.1  christos #include <stdio.h>
     53      1.1  christos #include <stdlib.h>
     54      1.1  christos #include <string.h>
     55      1.1  christos #include <wchar.h>
     56      1.1  christos #include "reentrant.h"
     57      1.1  christos #include "local.h"
     58      1.1  christos 
     59  1.6.6.1      yamt static ssize_t
     60      1.1  christos /*ARGSUSED*/
     61  1.6.6.1      yamt eofread(void *cookie, void *buf, size_t len)
     62      1.1  christos {
     63      1.1  christos 
     64  1.6.6.1      yamt 	return 0;
     65      1.1  christos }
     66      1.1  christos 
     67      1.1  christos int
     68      1.1  christos vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
     69      1.1  christos     va_list ap)
     70      1.1  christos {
     71      1.1  christos 	static const mbstate_t initial;
     72      1.1  christos 	mbstate_t mbs;
     73      1.1  christos 	FILE f;
     74      1.1  christos 	char *mbstr;
     75      1.1  christos 	size_t mlen;
     76      1.1  christos 	int r;
     77      1.2  christos 	const wchar_t *rstr = str;
     78      1.1  christos 	struct __sfileext fext;
     79      1.1  christos 
     80      1.1  christos 	/*
     81      1.1  christos 	 * XXX Convert the wide character string to multibyte, which
     82      1.1  christos 	 * __vfwscanf() will convert back to wide characters.
     83      1.1  christos 	 */
     84      1.1  christos 	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
     85  1.6.6.1      yamt 		return EOF;
     86      1.1  christos 	mbs = initial;
     87      1.3      yamt 	if ((mlen = wcsrtombs(mbstr, &rstr, SIZE_T_MAX, &mbs)) == (size_t)-1) {
     88      1.1  christos 		free(mbstr);
     89  1.6.6.1      yamt 		return EOF;
     90      1.1  christos 	}
     91      1.1  christos 	_FILEEXT_SETUP(&f, &fext);
     92      1.4  christos 	(void)memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data));
     93      1.1  christos 	f._file = -1;
     94      1.1  christos 	f._flags = __SRD;
     95      1.1  christos 	f._bf._base = f._p = (unsigned char *)mbstr;
     96  1.6.6.1      yamt 	_DIAGASSERT(__type_fit(int, mlen));
     97  1.6.6.1      yamt 	f._bf._size = f._r = (int)mlen;
     98      1.1  christos 	f._read = eofread;
     99      1.1  christos 	_UB(&f)._base = NULL;
    100      1.1  christos 	r = __vfwscanf_unlocked(&f, fmt, ap);
    101      1.1  christos 	free(mbstr);
    102      1.1  christos 
    103  1.6.6.1      yamt 	return r;
    104      1.1  christos }
    105