1 1.12 joerg /* $NetBSD: vswscanf.c,v 1.12 2013/05/17 12:55:57 joerg 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.12 joerg __RCSID("$NetBSD: vswscanf.c,v 1.12 2013/05/17 12:55:57 joerg Exp $"); 46 1.1 christos #endif 47 1.1 christos #endif /* LIBC_SCCS and not lint */ 48 1.1 christos 49 1.10 joerg #include "namespace.h" 50 1.10 joerg 51 1.7 christos #include <assert.h> 52 1.1 christos #include <limits.h> 53 1.10 joerg #include <locale.h> 54 1.1 christos #include <stdarg.h> 55 1.1 christos #include <stdio.h> 56 1.1 christos #include <stdlib.h> 57 1.1 christos #include <string.h> 58 1.1 christos #include <wchar.h> 59 1.1 christos #include "reentrant.h" 60 1.10 joerg #include "setlocale_local.h" 61 1.1 christos #include "local.h" 62 1.1 christos 63 1.10 joerg __weak_alias(vswscanf_l, _vswscanf_l) 64 1.10 joerg 65 1.9 christos static ssize_t 66 1.1 christos /*ARGSUSED*/ 67 1.9 christos eofread(void *cookie, void *buf, size_t len) 68 1.1 christos { 69 1.1 christos 70 1.8 christos return 0; 71 1.1 christos } 72 1.1 christos 73 1.1 christos int 74 1.10 joerg vswscanf_l(const wchar_t * __restrict str, locale_t loc, 75 1.10 joerg const wchar_t * __restrict fmt, va_list ap) 76 1.1 christos { 77 1.1 christos static const mbstate_t initial; 78 1.1 christos mbstate_t mbs; 79 1.1 christos FILE f; 80 1.1 christos char *mbstr; 81 1.1 christos size_t mlen; 82 1.1 christos int r; 83 1.2 christos const wchar_t *rstr = str; 84 1.1 christos struct __sfileext fext; 85 1.1 christos 86 1.1 christos /* 87 1.1 christos * XXX Convert the wide character string to multibyte, which 88 1.1 christos * __vfwscanf() will convert back to wide characters. 89 1.1 christos */ 90 1.10 joerg mbstr = malloc(wcslen(str) * MB_CUR_MAX_L(loc) + 1); 91 1.10 joerg if (mbstr == NULL) 92 1.8 christos return EOF; 93 1.1 christos mbs = initial; 94 1.10 joerg if ((mlen = wcsrtombs_l(mbstr, &rstr, SIZE_T_MAX, &mbs, loc)) 95 1.10 joerg == (size_t)-1) { 96 1.1 christos free(mbstr); 97 1.8 christos return EOF; 98 1.1 christos } 99 1.1 christos _FILEEXT_SETUP(&f, &fext); 100 1.4 christos (void)memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data)); 101 1.1 christos f._file = -1; 102 1.1 christos f._flags = __SRD; 103 1.1 christos f._bf._base = f._p = (unsigned char *)mbstr; 104 1.7 christos _DIAGASSERT(__type_fit(int, mlen)); 105 1.7 christos f._bf._size = f._r = (int)mlen; 106 1.1 christos f._read = eofread; 107 1.1 christos _UB(&f)._base = NULL; 108 1.10 joerg r = __vfwscanf_unlocked_l(&f, loc, fmt, ap); 109 1.1 christos free(mbstr); 110 1.1 christos 111 1.8 christos return r; 112 1.1 christos } 113 1.10 joerg 114 1.10 joerg int 115 1.10 joerg vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt, 116 1.10 joerg va_list ap) 117 1.10 joerg { 118 1.12 joerg return vswscanf_l(str, _current_locale(), fmt, ap); 119 1.11 joerg } 120