Home | History | Annotate | Line # | Download | only in stdio
vswscanf.c revision 1.3.28.1
      1  1.3.28.1  christos /*	$NetBSD: vswscanf.c,v 1.3.28.1 2009/01/04 17:02:20 christos 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.3.28.1  christos __RCSID("$NetBSD: vswscanf.c,v 1.3.28.1 2009/01/04 17:02:20 christos Exp $");
     46       1.1  christos #endif
     47       1.1  christos #endif /* LIBC_SCCS and not lint */
     48       1.1  christos 
     49       1.1  christos #include <limits.h>
     50       1.1  christos #include <stdarg.h>
     51       1.1  christos #include <stdio.h>
     52       1.1  christos #include <stdlib.h>
     53       1.1  christos #include <string.h>
     54       1.1  christos #include <wchar.h>
     55       1.1  christos #include "reentrant.h"
     56       1.1  christos #include "local.h"
     57       1.1  christos 
     58       1.1  christos static int	eofread(void *, char *, int);
     59       1.1  christos 
     60       1.1  christos static int
     61       1.1  christos /*ARGSUSED*/
     62       1.1  christos eofread(void *cookie, char *buf, int len)
     63       1.1  christos {
     64       1.1  christos 
     65       1.1  christos 	return (0);
     66       1.1  christos }
     67       1.1  christos 
     68       1.1  christos int
     69       1.1  christos vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
     70       1.1  christos     va_list ap)
     71       1.1  christos {
     72       1.1  christos 	static const mbstate_t initial;
     73       1.1  christos 	mbstate_t mbs;
     74       1.1  christos 	FILE f;
     75       1.1  christos 	char *mbstr;
     76       1.1  christos 	size_t mlen;
     77       1.1  christos 	int r;
     78       1.2  christos 	const wchar_t *rstr = str;
     79       1.1  christos 	struct __sfileext fext;
     80       1.1  christos 
     81       1.1  christos 	/*
     82       1.1  christos 	 * XXX Convert the wide character string to multibyte, which
     83       1.1  christos 	 * __vfwscanf() will convert back to wide characters.
     84       1.1  christos 	 */
     85       1.1  christos 	if ((mbstr = malloc(wcslen(str) * MB_CUR_MAX + 1)) == NULL)
     86       1.1  christos 		return (EOF);
     87       1.1  christos 	mbs = initial;
     88       1.3      yamt 	if ((mlen = wcsrtombs(mbstr, &rstr, SIZE_T_MAX, &mbs)) == (size_t)-1) {
     89       1.1  christos 		free(mbstr);
     90       1.1  christos 		return (EOF);
     91       1.1  christos 	}
     92       1.1  christos 	_FILEEXT_SETUP(&f, &fext);
     93  1.3.28.1  christos 	(void)memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data));
     94       1.1  christos 	f._file = -1;
     95       1.1  christos 	f._flags = __SRD;
     96       1.1  christos 	f._bf._base = f._p = (unsigned char *)mbstr;
     97       1.1  christos 	f._bf._size = f._r = mlen;
     98       1.1  christos 	f._read = eofread;
     99       1.1  christos 	_UB(&f)._base = NULL;
    100       1.1  christos 	f._lb._base = NULL;
    101       1.1  christos 	r = __vfwscanf_unlocked(&f, fmt, ap);
    102       1.1  christos 	free(mbstr);
    103       1.1  christos 
    104       1.1  christos 	return (r);
    105       1.1  christos }
    106