vswprintf.c revision 234529
1272343Sngie/*	$OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $	*/
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Copyright (c) 2011 The FreeBSD Foundation
8272343Sngie * All rights reserved.
9272343Sngie * Portions of this software were developed by David Chisnall
10272343Sngie * under sponsorship from the FreeBSD Foundation.
11272343Sngie *
12272343Sngie * Redistribution and use in source and binary forms, with or without
13272343Sngie * modification, are permitted provided that the following conditions
14272343Sngie * are met:
15272343Sngie * 1. Redistributions of source code must retain the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer.
17272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
18272343Sngie *    notice, this list of conditions and the following disclaimer in the
19272343Sngie *    documentation and/or other materials provided with the distribution.
20272343Sngie * 3. The name of the author may not be used to endorse or promote products
21272343Sngie *    derived from this software without specific prior written permission.
22272343Sngie *
23272343Sngie * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25272343Sngie * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26272343Sngie * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27272343Sngie * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28272343Sngie * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29272343Sngie * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30272343Sngie * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32272343Sngie * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33272343Sngie */
34272343Sngie
35272343Sngie#include <sys/cdefs.h>
36272343Sngie#if 0
37272343Sngie__FBSDID("FreeBSD: src/lib/libc/stdio/vasprintf.c,v 1.16 2002/08/21 16:19:57 mike Exp ");
38272343Sngie#endif
39272343Sngie__FBSDID("$FreeBSD: head/lib/libc/stdio/vswprintf.c 234529 2012-04-21 06:08:29Z das $");
40272343Sngie
41272343Sngie#include <errno.h>
42272343Sngie#include <stdio.h>
43272343Sngie#include <stdlib.h>
44272343Sngie#include <wchar.h>
45272343Sngie#include "local.h"
46272343Sngie#include "xlocale_private.h"
47272343Sngie
48272343Sngieint
49272343Sngievswprintf_l(wchar_t * __restrict s, size_t n, locale_t locale,
50272343Sngie		const wchar_t * __restrict fmt, __va_list ap)
51272343Sngie{
52272343Sngie	static const mbstate_t initial;
53272343Sngie	mbstate_t mbs;
54272343Sngie	FILE f = FAKE_FILE;
55272343Sngie	char *mbp;
56272343Sngie	int ret, sverrno;
57272343Sngie	size_t nwc;
58272343Sngie	FIX_LOCALE(locale);
59272343Sngie
60272343Sngie	if (n == 0) {
61272343Sngie		errno = EINVAL;
62272343Sngie		return (-1);
63272343Sngie	}
64272343Sngie
65272343Sngie	f._flags = __SWR | __SSTR | __SALC;
66272343Sngie	f._bf._base = f._p = (unsigned char *)malloc(128);
67272343Sngie	if (f._bf._base == NULL) {
68272343Sngie		errno = ENOMEM;
69272343Sngie		*s = L'\0';
70272343Sngie		return (-1);
71272343Sngie	}
72272343Sngie	f._bf._size = f._w = 127;		/* Leave room for the NUL */
73272343Sngie	ret = __vfwprintf(&f, locale, fmt, ap);
74272343Sngie	if (ret < 0) {
75272343Sngie		sverrno = errno;
76272343Sngie		free(f._bf._base);
77272343Sngie		errno = sverrno;
78272343Sngie		*s = L'\0';
79272343Sngie		return (-1);
80272343Sngie	}
81272343Sngie	*f._p = '\0';
82272343Sngie	mbp = f._bf._base;
83272343Sngie	/*
84272343Sngie	 * XXX Undo the conversion from wide characters to multibyte that
85272343Sngie	 * fputwc() did in __vfwprintf().
86272343Sngie	 */
87272343Sngie	mbs = initial;
88272343Sngie	nwc = mbsrtowcs_l(s, (const char **)&mbp, n, &mbs, locale);
89272343Sngie	free(f._bf._base);
90272343Sngie	if (nwc == (size_t)-1) {
91272343Sngie		errno = EILSEQ;
92272343Sngie		*s = L'\0';
93		return (-1);
94	}
95	if (nwc == n) {
96		s[n - 1] = L'\0';
97		errno = EOVERFLOW;
98		return (-1);
99	}
100
101	return (ret);
102}
103int
104vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
105    __va_list ap)
106{
107	return vswprintf_l(s, n, __get_locale(), fmt, ap);
108}
109