2 * Copyright (c) 2005 Poul-Henning Kamp
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <namespace.h>
47 * Convert a wide character string argument for the %ls format to a multibyte
48 * string representation. If not -1, prec specifies the maximum number of
49 * bytes to output, and also means that we can't assume that the wide char.
50 * string ends is null-terminated.
53 __wcsconv(wchar_t *wcsarg, int prec)
55 static const mbstate_t initial;
62 /* Allocate space for the maximum number of bytes we could output. */
66 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
67 if (nbytes == (size_t)-1)
71 * Optimisation: if the output precision is small enough,
72 * just allocate enough memory for the maximum instead of
73 * scanning the string.
82 clen = wcrtomb(buf, *p++, &mbs);
83 if (clen == 0 || clen == (size_t)-1 ||
84 (int)(nbytes + clen) > prec)
90 if ((convbuf = malloc(nbytes + 1)) == NULL)
93 /* Fill the output buffer. */
96 if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
97 nbytes, &mbs)) == (size_t)-1) {
101 convbuf[nbytes] = '\0';
106 /* 's' ---------------------------------------------------------------*/
109 __printf_arginfo_str(const struct printf_info *pi, size_t n, int *argt)
113 if (pi->is_long || pi->spec == 'C')
114 argt[0] = PA_WSTRING;
121 __printf_render_str(struct __printf_io *io, const struct printf_info *pi, const void *const *arg)
128 if (pi->is_long || pi->spec == 'S') {
129 wcp = *((wint_t **)arg[0]);
131 return (__printf_out(io, pi, "(null)", 6));
132 convbuf = __wcsconv(wcp, pi->prec);
135 l = __printf_out(io, pi, convbuf, strlen(convbuf));
139 p = *((char **)arg[0]);
141 return (__printf_out(io, pi, "(null)", 6));
143 if (pi->prec >= 0 && pi->prec < l)
145 return (__printf_out(io, pi, p, l));
148 /* 'c' ---------------------------------------------------------------*/
151 __printf_arginfo_chr(const struct printf_info *pi, size_t n, int *argt)
155 if (pi->is_long || pi->spec == 'C')
163 __printf_render_chr(struct __printf_io *io, const struct printf_info *pi, const void *const *arg)
168 static const mbstate_t initial; /* XXX: this is bogus! */
171 char buf[MB_CUR_MAX];
173 if (pi->is_long || pi->spec == 'C') {
174 ii = *((wint_t *)arg[0]);
177 mbseqlen = wcrtomb(buf, (wchar_t)ii, &mbs);
178 if (mbseqlen == (size_t) -1)
180 return (__printf_out(io, pi, buf, mbseqlen));
182 i = *((int *)arg[0]);
184 i = __printf_out(io, pi, &c, 1);