1*84d9c625SLionel Sambuc /* $NetBSD: vswprintf.c,v 1.6 2013/05/19 21:45:00 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. The name of the author may not be used to endorse or promote products
162fe8fb19SBen Gras * derived from this software without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
192fe8fb19SBen Gras * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
202fe8fb19SBen Gras * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
212fe8fb19SBen Gras * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
222fe8fb19SBen Gras * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
232fe8fb19SBen Gras * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
242fe8fb19SBen Gras * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
252fe8fb19SBen Gras * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
262fe8fb19SBen Gras * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
272fe8fb19SBen Gras * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282fe8fb19SBen Gras */
292fe8fb19SBen Gras
302fe8fb19SBen Gras #include <sys/cdefs.h>
312fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/libc/stdio/vswprintf.c,v 1.6 2005/02/21 19:41:44 fjoe Exp $");
342fe8fb19SBen Gras #else
35*84d9c625SLionel Sambuc __RCSID("$NetBSD: vswprintf.c,v 1.6 2013/05/19 21:45:00 christos Exp $");
362fe8fb19SBen Gras #endif
372fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
382fe8fb19SBen Gras
39*84d9c625SLionel Sambuc #include "namespace.h"
402fe8fb19SBen Gras #include <errno.h>
41*84d9c625SLionel Sambuc #include <locale.h>
422fe8fb19SBen Gras #include <stdio.h>
432fe8fb19SBen Gras #include <stdlib.h>
442fe8fb19SBen Gras #include <wchar.h>
452fe8fb19SBen Gras #include <stdarg.h>
46*84d9c625SLionel Sambuc
472fe8fb19SBen Gras #include "reentrant.h"
48*84d9c625SLionel Sambuc #include "setlocale_local.h"
492fe8fb19SBen Gras #include "local.h"
502fe8fb19SBen Gras
__weak_alias(vswprintf_l,_vswprintf_l)51*84d9c625SLionel Sambuc __weak_alias(vswprintf_l, _vswprintf_l)
52*84d9c625SLionel Sambuc
532fe8fb19SBen Gras int
54*84d9c625SLionel Sambuc vswprintf_l(wchar_t * __restrict s, size_t n, locale_t loc,
55*84d9c625SLionel Sambuc const wchar_t * __restrict fmt, va_list ap)
562fe8fb19SBen Gras {
572fe8fb19SBen Gras static const mbstate_t initial;
582fe8fb19SBen Gras mbstate_t mbs;
592fe8fb19SBen Gras FILE f;
602fe8fb19SBen Gras char *mbp;
612fe8fb19SBen Gras int ret, sverrno;
622fe8fb19SBen Gras size_t nwc;
632fe8fb19SBen Gras struct __sfileext fext;
642fe8fb19SBen Gras
652fe8fb19SBen Gras if (n == 0) {
662fe8fb19SBen Gras errno = EINVAL;
67f14fb602SLionel Sambuc return -1;
682fe8fb19SBen Gras }
692fe8fb19SBen Gras
702fe8fb19SBen Gras _FILEEXT_SETUP(&f, &fext);
712fe8fb19SBen Gras f._file = -1;
722fe8fb19SBen Gras f._flags = __SWR | __SSTR | __SALC;
73*84d9c625SLionel Sambuc f._bf._base = f._p = malloc(128);
742fe8fb19SBen Gras if (f._bf._base == NULL) {
752fe8fb19SBen Gras errno = ENOMEM;
76f14fb602SLionel Sambuc return -1;
772fe8fb19SBen Gras }
782fe8fb19SBen Gras f._bf._size = f._w = 127; /* Leave room for the NUL */
79*84d9c625SLionel Sambuc ret = __vfwprintf_unlocked_l(&f, loc, fmt, ap);
802fe8fb19SBen Gras if (ret < 0) {
812fe8fb19SBen Gras sverrno = errno;
822fe8fb19SBen Gras free(f._bf._base);
832fe8fb19SBen Gras errno = sverrno;
84f14fb602SLionel Sambuc return -1;
852fe8fb19SBen Gras }
862fe8fb19SBen Gras *f._p = '\0';
872fe8fb19SBen Gras mbp = (char *)f._bf._base;
882fe8fb19SBen Gras /*
892fe8fb19SBen Gras * XXX Undo the conversion from wide characters to multibyte that
902fe8fb19SBen Gras * fputwc() did in __vfwprintf().
912fe8fb19SBen Gras */
922fe8fb19SBen Gras mbs = initial;
93*84d9c625SLionel Sambuc nwc = mbsrtowcs_l(s, (void *)&mbp, n, &mbs, loc);
942fe8fb19SBen Gras free(f._bf._base);
952fe8fb19SBen Gras if (nwc == (size_t)-1) {
962fe8fb19SBen Gras errno = EILSEQ;
97f14fb602SLionel Sambuc return -1;
982fe8fb19SBen Gras }
992fe8fb19SBen Gras if (nwc == n) {
1002fe8fb19SBen Gras s[n - 1] = L'\0';
1012fe8fb19SBen Gras errno = EOVERFLOW;
102f14fb602SLionel Sambuc return -1;
1032fe8fb19SBen Gras }
1042fe8fb19SBen Gras
105f14fb602SLionel Sambuc return ret;
1062fe8fb19SBen Gras }
107*84d9c625SLionel Sambuc
108*84d9c625SLionel Sambuc int
vswprintf(wchar_t * __restrict s,size_t n,const wchar_t * __restrict fmt,va_list ap)109*84d9c625SLionel Sambuc vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
110*84d9c625SLionel Sambuc va_list ap)
111*84d9c625SLionel Sambuc {
112*84d9c625SLionel Sambuc return vswprintf_l(s, n, _current_locale(), fmt, ap);
113*84d9c625SLionel Sambuc }
114