1 1.6 christos /* $NetBSD: vswprintf.c,v 1.6 2013/05/19 21:45:00 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com> 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 3. The name of the author may not be used to endorse or promote products 16 1.1 christos * derived from this software without specific prior written permission. 17 1.1 christos * 18 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19 1.1 christos * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20 1.1 christos * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 1.1 christos * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 1.1 christos * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 1.1 christos * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 1.1 christos * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 1.1 christos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 1.1 christos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 1.1 christos * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 christos */ 29 1.1 christos 30 1.1 christos #include <sys/cdefs.h> 31 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 32 1.1 christos #if 0 33 1.1 christos __FBSDID("$FreeBSD: src/lib/libc/stdio/vswprintf.c,v 1.6 2005/02/21 19:41:44 fjoe Exp $"); 34 1.1 christos #else 35 1.6 christos __RCSID("$NetBSD: vswprintf.c,v 1.6 2013/05/19 21:45:00 christos Exp $"); 36 1.1 christos #endif 37 1.1 christos #endif /* LIBC_SCCS and not lint */ 38 1.1 christos 39 1.4 joerg #include "namespace.h" 40 1.1 christos #include <errno.h> 41 1.4 joerg #include <locale.h> 42 1.1 christos #include <stdio.h> 43 1.1 christos #include <stdlib.h> 44 1.1 christos #include <wchar.h> 45 1.1 christos #include <stdarg.h> 46 1.4 joerg 47 1.1 christos #include "reentrant.h" 48 1.4 joerg #include "setlocale_local.h" 49 1.1 christos #include "local.h" 50 1.1 christos 51 1.4 joerg __weak_alias(vswprintf_l, _vswprintf_l) 52 1.4 joerg 53 1.1 christos int 54 1.4 joerg vswprintf_l(wchar_t * __restrict s, size_t n, locale_t loc, 55 1.4 joerg const wchar_t * __restrict fmt, va_list ap) 56 1.1 christos { 57 1.1 christos static const mbstate_t initial; 58 1.1 christos mbstate_t mbs; 59 1.1 christos FILE f; 60 1.1 christos char *mbp; 61 1.1 christos int ret, sverrno; 62 1.1 christos size_t nwc; 63 1.1 christos struct __sfileext fext; 64 1.1 christos 65 1.1 christos if (n == 0) { 66 1.1 christos errno = EINVAL; 67 1.3 christos return -1; 68 1.1 christos } 69 1.1 christos 70 1.1 christos _FILEEXT_SETUP(&f, &fext); 71 1.1 christos f._file = -1; 72 1.1 christos f._flags = __SWR | __SSTR | __SALC; 73 1.6 christos f._bf._base = f._p = malloc(128); 74 1.1 christos if (f._bf._base == NULL) { 75 1.1 christos errno = ENOMEM; 76 1.3 christos return -1; 77 1.1 christos } 78 1.1 christos f._bf._size = f._w = 127; /* Leave room for the NUL */ 79 1.4 joerg ret = __vfwprintf_unlocked_l(&f, loc, fmt, ap); 80 1.1 christos if (ret < 0) { 81 1.1 christos sverrno = errno; 82 1.1 christos free(f._bf._base); 83 1.1 christos errno = sverrno; 84 1.3 christos return -1; 85 1.1 christos } 86 1.1 christos *f._p = '\0'; 87 1.1 christos mbp = (char *)f._bf._base; 88 1.1 christos /* 89 1.1 christos * XXX Undo the conversion from wide characters to multibyte that 90 1.1 christos * fputwc() did in __vfwprintf(). 91 1.1 christos */ 92 1.1 christos mbs = initial; 93 1.4 joerg nwc = mbsrtowcs_l(s, (void *)&mbp, n, &mbs, loc); 94 1.1 christos free(f._bf._base); 95 1.1 christos if (nwc == (size_t)-1) { 96 1.1 christos errno = EILSEQ; 97 1.3 christos return -1; 98 1.1 christos } 99 1.1 christos if (nwc == n) { 100 1.1 christos s[n - 1] = L'\0'; 101 1.1 christos errno = EOVERFLOW; 102 1.3 christos return -1; 103 1.1 christos } 104 1.1 christos 105 1.3 christos return ret; 106 1.1 christos } 107 1.4 joerg 108 1.4 joerg int 109 1.4 joerg vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt, 110 1.4 joerg va_list ap) 111 1.4 joerg { 112 1.5 joerg return vswprintf_l(s, n, _current_locale(), fmt, ap); 113 1.4 joerg } 114