]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /*- |
2 | * Copyright (c) 1990, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Chris Torek. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
9385eb3d A |
16 | * 4. Neither the name of the University nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
9385eb3d A |
33 | #if 0 |
34 | #if defined(LIBC_SCCS) && !defined(lint) | |
35 | static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; | |
36 | #endif /* LIBC_SCCS and not lint */ | |
9385eb3d | 37 | #endif |
59e0d9fe | 38 | #include <sys/cdefs.h> |
1f2f436a | 39 | __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.42 2009/11/25 04:27:55 wollman Exp $"); |
9385eb3d | 40 | |
ad3c9f2a A |
41 | #include "xlocale_private.h" |
42 | ||
9385eb3d A |
43 | /* |
44 | * Actual wprintf innards. | |
45 | * | |
46 | * Avoid making gratuitous changes to this source file; it should be kept | |
47 | * as close as possible to vfprintf.c for ease of maintenance. | |
48 | */ | |
49 | ||
50 | #include "namespace.h" | |
51 | #include <sys/types.h> | |
52 | ||
53 | #include <ctype.h> | |
54 | #include <limits.h> | |
55 | #include <locale.h> | |
56 | #include <stdarg.h> | |
57 | #include <stddef.h> | |
58 | #include <stdint.h> | |
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <string.h> | |
62 | #include <wchar.h> | |
63 | #include <wctype.h> | |
ad3c9f2a | 64 | #include <errno.h> |
9385eb3d A |
65 | #include "un-namespace.h" |
66 | ||
67 | #include "libc_private.h" | |
68 | #include "local.h" | |
69 | #include "fvwrite.h" | |
1f2f436a | 70 | #include "printflocal.h" |
9385eb3d | 71 | |
ad3c9f2a A |
72 | static int __sprint(FILE *, locale_t, struct __suio *); |
73 | static int __sbprintf(FILE *, locale_t, const wchar_t *, va_list); | |
74 | static wint_t __xfputwc(wchar_t, FILE *, locale_t); | |
75 | static wchar_t *__mbsconv(char *, int, locale_t); | |
76 | __private_extern__ const char *__fix_nogrouping(const char *); | |
1f2f436a A |
77 | |
78 | #define CHAR wchar_t | |
79 | #include "printfcommon.h" | |
80 | ||
81 | struct grouping_state { | |
82 | wchar_t thousands_sep; /* locale-specific thousands separator */ | |
83 | const char *grouping; /* locale-specific numeric grouping rules */ | |
84 | int lead; /* sig figs before decimal or group sep */ | |
85 | int nseps; /* number of group separators with ' */ | |
86 | int nrepeats; /* number of repeats of the last group */ | |
9385eb3d A |
87 | }; |
88 | ||
1f2f436a A |
89 | static const mbstate_t initial_mbs; |
90 | ||
91 | static inline wchar_t | |
ad3c9f2a | 92 | get_decpt(locale_t loc) |
1f2f436a A |
93 | { |
94 | mbstate_t mbs; | |
95 | wchar_t decpt; | |
96 | int nconv; | |
97 | ||
98 | mbs = initial_mbs; | |
ad3c9f2a | 99 | nconv = mbrtowc_l(&decpt, localeconv_l(loc)->decimal_point, MB_CUR_MAX_L(loc), &mbs, loc); |
1f2f436a A |
100 | if (nconv == (size_t)-1 || nconv == (size_t)-2) |
101 | decpt = '.'; /* failsafe */ | |
102 | return (decpt); | |
103 | } | |
104 | ||
105 | static inline wchar_t | |
ad3c9f2a | 106 | get_thousep(locale_t loc) |
1f2f436a A |
107 | { |
108 | mbstate_t mbs; | |
109 | wchar_t thousep; | |
110 | int nconv; | |
111 | ||
112 | mbs = initial_mbs; | |
ad3c9f2a A |
113 | nconv = mbrtowc_l(&thousep, localeconv_l(loc)->thousands_sep, |
114 | MB_CUR_MAX_L(loc), &mbs, loc); | |
1f2f436a A |
115 | if (nconv == (size_t)-1 || nconv == (size_t)-2) |
116 | thousep = '\0'; /* failsafe */ | |
117 | return (thousep); | |
118 | } | |
119 | ||
9385eb3d | 120 | /* |
1f2f436a A |
121 | * Initialize the thousands' grouping state in preparation to print a |
122 | * number with ndigits digits. This routine returns the total number | |
123 | * of wide characters that will be printed. | |
9385eb3d | 124 | */ |
1f2f436a | 125 | static int |
ad3c9f2a | 126 | grouping_init(struct grouping_state *gs, int ndigits, locale_t loc) |
1f2f436a | 127 | { |
9385eb3d | 128 | |
ad3c9f2a A |
129 | gs->grouping = __fix_nogrouping(localeconv_l(loc)->grouping); |
130 | gs->thousands_sep = get_thousep(loc); | |
1f2f436a A |
131 | |
132 | gs->nseps = gs->nrepeats = 0; | |
133 | gs->lead = ndigits; | |
134 | while (*gs->grouping != CHAR_MAX) { | |
135 | if (gs->lead <= *gs->grouping) | |
136 | break; | |
137 | gs->lead -= *gs->grouping; | |
138 | if (*(gs->grouping+1)) { | |
139 | gs->nseps++; | |
140 | gs->grouping++; | |
141 | } else | |
142 | gs->nrepeats++; | |
143 | } | |
144 | return (gs->nseps + gs->nrepeats); | |
145 | } | |
146 | ||
147 | /* | |
148 | * Print a number with thousands' separators. | |
149 | */ | |
150 | static int | |
151 | grouping_print(struct grouping_state *gs, struct io_state *iop, | |
ad3c9f2a | 152 | const CHAR *cp, const CHAR *ep, locale_t loc) |
1f2f436a A |
153 | { |
154 | const CHAR *cp0 = cp; | |
155 | ||
ad3c9f2a | 156 | if (io_printandpad(iop, cp, ep, gs->lead, zeroes, loc)) |
1f2f436a A |
157 | return (-1); |
158 | cp += gs->lead; | |
159 | while (gs->nseps > 0 || gs->nrepeats > 0) { | |
160 | if (gs->nrepeats > 0) | |
161 | gs->nrepeats--; | |
162 | else { | |
163 | gs->grouping--; | |
164 | gs->nseps--; | |
165 | } | |
ad3c9f2a | 166 | if (io_print(iop, &gs->thousands_sep, 1, loc)) |
1f2f436a | 167 | return (-1); |
ad3c9f2a | 168 | if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, loc)) |
1f2f436a A |
169 | return (-1); |
170 | cp += *gs->grouping; | |
171 | } | |
172 | if (cp > ep) | |
173 | cp = ep; | |
174 | return (cp - cp0); | |
175 | } | |
176 | ||
177 | ||
178 | /* | |
179 | * Flush out all the vectors defined by the given uio, | |
180 | * then reset it so that it can be reused. | |
181 | * | |
182 | * XXX The fact that we do this a character at a time and convert to a | |
183 | * multibyte character sequence even if the destination is a wide | |
184 | * string eclipses the benefits of buffering. | |
185 | */ | |
186 | static int | |
ad3c9f2a | 187 | __sprint(FILE *fp, locale_t loc, struct __suio *uio) |
1f2f436a A |
188 | { |
189 | struct __siov *iov; | |
190 | wchar_t *p; | |
191 | int i, len; | |
192 | ||
193 | iov = uio->uio_iov; | |
194 | for (; uio->uio_resid != 0; uio->uio_resid -= len, iov++) { | |
195 | p = (wchar_t *)iov->iov_base; | |
196 | len = iov->iov_len; | |
197 | for (i = 0; i < len; i++) { | |
ad3c9f2a | 198 | if (__xfputwc(p[i], fp, loc) == WEOF) |
1f2f436a A |
199 | return (-1); |
200 | } | |
201 | } | |
202 | uio->uio_iovcnt = 0; | |
203 | return (0); | |
204 | } | |
9385eb3d A |
205 | |
206 | /* | |
207 | * Helper function for `fprintf to unbuffered unix file': creates a | |
208 | * temporary buffer. We only work on write-only files; this avoids | |
209 | * worries about ungetc buffers and so forth. | |
210 | */ | |
211 | static int | |
ad3c9f2a | 212 | __sbprintf(FILE *fp, locale_t loc, const wchar_t *fmt, va_list ap) |
9385eb3d A |
213 | { |
214 | int ret; | |
215 | FILE fake; | |
216 | unsigned char buf[BUFSIZ]; | |
ad3c9f2a A |
217 | struct __sFILEX ext; |
218 | fake._extra = &ext; | |
219 | INITEXTRA(&fake); | |
9385eb3d | 220 | |
1f2f436a A |
221 | /* XXX This is probably not needed. */ |
222 | if (prepwrite(fp) != 0) | |
223 | return (EOF); | |
224 | ||
9385eb3d A |
225 | /* copy the important variables */ |
226 | fake._flags = fp->_flags & ~__SNBF; | |
227 | fake._file = fp->_file; | |
228 | fake._cookie = fp->_cookie; | |
229 | fake._write = fp->_write; | |
1f2f436a A |
230 | fake._orientation = fp->_orientation; |
231 | fake._mbstate = fp->_mbstate; | |
9385eb3d A |
232 | |
233 | /* set up the buffer */ | |
234 | fake._bf._base = fake._p = buf; | |
235 | fake._bf._size = fake._w = sizeof(buf); | |
236 | fake._lbfsize = 0; /* not actually used, but Just In Case */ | |
237 | ||
238 | /* do the work, then copy any error status */ | |
ad3c9f2a | 239 | ret = __vfwprintf(&fake, loc, fmt, ap); |
9385eb3d A |
240 | if (ret >= 0 && __fflush(&fake)) |
241 | ret = WEOF; | |
242 | if (fake._flags & __SERR) | |
243 | fp->_flags |= __SERR; | |
244 | return (ret); | |
245 | } | |
246 | ||
51631861 A |
247 | /* |
248 | * Like __fputwc, but handles fake string (__SSTR) files properly. | |
249 | * File must already be locked. | |
250 | */ | |
251 | static wint_t | |
ad3c9f2a | 252 | __xfputwc(wchar_t wc, FILE *fp, locale_t loc) |
51631861 | 253 | { |
59e0d9fe | 254 | mbstate_t mbs; |
51631861 A |
255 | char buf[MB_LEN_MAX]; |
256 | struct __suio uio; | |
257 | struct __siov iov; | |
59e0d9fe | 258 | size_t len; |
51631861 A |
259 | |
260 | if ((fp->_flags & __SSTR) == 0) | |
ad3c9f2a | 261 | return (__fputwc(wc, fp, loc)); |
51631861 | 262 | |
1f2f436a | 263 | mbs = initial_mbs; |
ad3c9f2a | 264 | if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) { |
51631861 A |
265 | fp->_flags |= __SERR; |
266 | return (WEOF); | |
267 | } | |
268 | uio.uio_iov = &iov; | |
269 | uio.uio_resid = len; | |
270 | uio.uio_iovcnt = 1; | |
271 | iov.iov_base = buf; | |
272 | iov.iov_len = len; | |
273 | return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF); | |
274 | } | |
275 | ||
9385eb3d A |
276 | /* |
277 | * Convert a multibyte character string argument for the %s format to a wide | |
278 | * string representation. ``prec'' specifies the maximum number of bytes | |
279 | * to output. If ``prec'' is greater than or equal to zero, we can't assume | |
280 | * that the multibyte char. string ends in a null character. | |
281 | */ | |
282 | static wchar_t * | |
ad3c9f2a | 283 | __mbsconv(char *mbsarg, int prec, locale_t loc) |
9385eb3d | 284 | { |
59e0d9fe | 285 | mbstate_t mbs; |
9385eb3d A |
286 | wchar_t *convbuf, *wcp; |
287 | const char *p; | |
ad3c9f2a A |
288 | size_t insize, nchars, nconv = 0; |
289 | int mb_cur_max = MB_CUR_MAX_L(loc); | |
9385eb3d A |
290 | |
291 | if (mbsarg == NULL) | |
292 | return (NULL); | |
293 | ||
294 | /* | |
295 | * Supplied argument is a multibyte string; convert it to wide | |
296 | * characters first. | |
297 | */ | |
298 | if (prec >= 0) { | |
299 | /* | |
300 | * String is not guaranteed to be NUL-terminated. Find the | |
301 | * number of characters to print. | |
302 | */ | |
9385eb3d | 303 | p = mbsarg; |
1f2f436a A |
304 | insize = nchars = nconv = 0; |
305 | mbs = initial_mbs; | |
9385eb3d | 306 | while (nchars != (size_t)prec) { |
ad3c9f2a | 307 | nconv = mbrlen_l(p, mb_cur_max, &mbs, loc); |
9385eb3d A |
308 | if (nconv == 0 || nconv == (size_t)-1 || |
309 | nconv == (size_t)-2) | |
310 | break; | |
311 | p += nconv; | |
312 | nchars++; | |
313 | insize += nconv; | |
314 | } | |
315 | if (nconv == (size_t)-1 || nconv == (size_t)-2) | |
316 | return (NULL); | |
1f2f436a | 317 | } else { |
9385eb3d | 318 | insize = strlen(mbsarg); |
1f2f436a A |
319 | nconv = 0; |
320 | } | |
9385eb3d A |
321 | |
322 | /* | |
323 | * Allocate buffer for the result and perform the conversion, | |
324 | * converting at most `size' bytes of the input multibyte string to | |
325 | * wide characters for printing. | |
326 | */ | |
327 | convbuf = malloc((insize + 1) * sizeof(*convbuf)); | |
328 | if (convbuf == NULL) | |
329 | return (NULL); | |
330 | wcp = convbuf; | |
331 | p = mbsarg; | |
1f2f436a | 332 | mbs = initial_mbs; |
9385eb3d | 333 | while (insize != 0) { |
ad3c9f2a | 334 | nconv = mbrtowc_l(wcp, p, insize, &mbs, loc); |
9385eb3d A |
335 | if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) |
336 | break; | |
337 | wcp++; | |
338 | p += nconv; | |
339 | insize -= nconv; | |
340 | } | |
341 | if (nconv == (size_t)-1 || nconv == (size_t)-2) { | |
342 | free(convbuf); | |
343 | return (NULL); | |
344 | } | |
345 | *wcp = L'\0'; | |
346 | ||
347 | return (convbuf); | |
348 | } | |
349 | ||
350 | /* | |
351 | * MT-safe version | |
352 | */ | |
353 | int | |
ad3c9f2a | 354 | vfwprintf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt0, va_list ap) |
9385eb3d A |
355 | { |
356 | int ret; | |
357 | ||
ad3c9f2a | 358 | NORMALIZE_LOCALE(loc); |
9385eb3d | 359 | FLOCKFILE(fp); |
1f2f436a A |
360 | /* optimise fprintf(stderr) (and other unbuffered Unix files) */ |
361 | if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && | |
362 | fp->_file >= 0) | |
ad3c9f2a | 363 | ret = __sbprintf(fp, loc, fmt0, ap); |
1f2f436a | 364 | else |
ad3c9f2a | 365 | ret = __vfwprintf(fp, loc, fmt0, ap); |
9385eb3d A |
366 | FUNLOCKFILE(fp); |
367 | return (ret); | |
368 | } | |
369 | ||
ad3c9f2a A |
370 | int |
371 | vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap) | |
372 | { | |
373 | return vfwprintf_l(fp, __current_locale(), fmt0, ap); | |
374 | } | |
375 | ||
9385eb3d A |
376 | /* |
377 | * The size of the buffer we use as scratch space for integer | |
1f2f436a A |
378 | * conversions, among other things. We need enough space to |
379 | * write a uintmax_t in octal (plus one byte). | |
9385eb3d | 380 | */ |
1f2f436a A |
381 | #if UINTMAX_MAX <= UINT64_MAX |
382 | #define BUF 32 | |
383 | #else | |
384 | #error "BUF must be large enough to format a uintmax_t" | |
385 | #endif | |
9385eb3d A |
386 | |
387 | /* | |
388 | * Non-MT-safe version | |
389 | */ | |
ad3c9f2a A |
390 | __private_extern__ int |
391 | __vfwprintf(FILE *fp, locale_t loc, const wchar_t *fmt0, va_list ap) | |
9385eb3d A |
392 | { |
393 | wchar_t *fmt; /* format string */ | |
394 | wchar_t ch; /* character from fmt */ | |
1f2f436a | 395 | int n, n2; /* handy integer (short term usage) */ |
9385eb3d A |
396 | wchar_t *cp; /* handy char pointer (short term usage) */ |
397 | int flags; /* flags as above */ | |
398 | int ret; /* return value accumulator */ | |
399 | int width; /* width from format (%8d), or 0 */ | |
400 | int prec; /* precision from format; <0 for N/A */ | |
401 | wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */ | |
1f2f436a | 402 | struct grouping_state gs; /* thousands' grouping info */ |
59e0d9fe | 403 | #ifndef NO_FLOATING_POINT |
9385eb3d A |
404 | /* |
405 | * We can decompose the printed representation of floating | |
406 | * point numbers into several parts, some of which may be empty: | |
407 | * | |
408 | * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ | |
409 | * A B ---C--- D E F | |
410 | * | |
411 | * A: 'sign' holds this value if present; '\0' otherwise | |
412 | * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal | |
413 | * C: cp points to the string MMMNNN. Leading and trailing | |
414 | * zeros are not in the string and must be added. | |
415 | * D: expchar holds this character; '\0' if no exponent, e.g. %f | |
416 | * F: at least two digits for decimal, at least one digit for hex | |
417 | */ | |
1f2f436a | 418 | wchar_t decimal_point; /* locale specific decimal point */ |
9385eb3d A |
419 | int signflag; /* true if float is negative */ |
420 | union { /* floating point arguments %[aAeEfFgG] */ | |
421 | double dbl; | |
422 | long double ldbl; | |
423 | } fparg; | |
424 | int expt; /* integer value of exponent */ | |
425 | char expchar; /* exponent character: [eEpP\0] */ | |
426 | char *dtoaend; /* pointer to end of converted digits */ | |
427 | int expsize; /* character count for expstr */ | |
9385eb3d A |
428 | int ndig; /* actual number of digits returned by dtoa */ |
429 | wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ | |
430 | char *dtoaresult; /* buffer allocated by dtoa */ | |
ad3c9f2a A |
431 | #endif |
432 | #ifdef VECTORS | |
433 | union arg vval; /* Vector argument. */ | |
434 | wchar_t *pct; /* Pointer to '%' at beginning of specifier. */ | |
435 | wchar_t vsep; /* Vector separator character. */ | |
9385eb3d A |
436 | #endif |
437 | u_long ulval; /* integer arguments %[diouxX] */ | |
438 | uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ | |
439 | int base; /* base for [diouxX] conversion */ | |
440 | int dprec; /* a copy of prec if [diouxX], 0 otherwise */ | |
441 | int realsz; /* field size expanded by dprec, sign, etc */ | |
442 | int size; /* size of converted field or string */ | |
443 | int prsize; /* max size of printed field */ | |
59e0d9fe | 444 | const char *xdigs; /* digits for [xX] conversion */ |
1f2f436a | 445 | struct io_state io; /* I/O buffering state */ |
9385eb3d A |
446 | wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */ |
447 | wchar_t ox[2]; /* space for 0x hex-prefix */ | |
448 | union arg *argtable; /* args, built due to positional arg */ | |
449 | union arg statargtable [STATIC_ARG_TBL_SIZE]; | |
450 | int nextarg; /* 1-based argument index */ | |
451 | va_list orgap; /* original argument pointer */ | |
452 | wchar_t *convbuf; /* multibyte to wide conversion result */ | |
453 | ||
59e0d9fe A |
454 | static const char xdigs_lower[16] = "0123456789abcdef"; |
455 | static const char xdigs_upper[16] = "0123456789ABCDEF"; | |
9385eb3d | 456 | |
1f2f436a | 457 | /* BEWARE, these `goto error' on error. */ |
9385eb3d | 458 | #define PRINT(ptr, len) do { \ |
ad3c9f2a | 459 | if (io_print(&io, (ptr), (len), loc)) \ |
1f2f436a | 460 | goto error; \ |
9385eb3d | 461 | } while (0) |
1f2f436a | 462 | #define PAD(howmany, with) { \ |
ad3c9f2a | 463 | if (io_pad(&io, (howmany), (with), loc)) \ |
1f2f436a A |
464 | goto error; \ |
465 | } | |
466 | #define PRINTANDPAD(p, ep, len, with) { \ | |
ad3c9f2a | 467 | if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \ |
1f2f436a A |
468 | goto error; \ |
469 | } | |
470 | #define FLUSH() { \ | |
ad3c9f2a | 471 | if (io_flush(&io, loc)) \ |
1f2f436a A |
472 | goto error; \ |
473 | } | |
9385eb3d A |
474 | |
475 | /* | |
476 | * Get the argument indexed by nextarg. If the argument table is | |
477 | * built, use it to get the argument. If its not, get the next | |
478 | * argument (and arguments must be gotten sequentially). | |
479 | */ | |
480 | #define GETARG(type) \ | |
481 | ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ | |
482 | (nextarg++, va_arg(ap, type))) | |
483 | ||
484 | /* | |
485 | * To extend shorts properly, we need both signed and unsigned | |
486 | * argument extraction methods. | |
487 | */ | |
488 | #define SARG() \ | |
489 | (flags&LONGINT ? GETARG(long) : \ | |
490 | flags&SHORTINT ? (long)(short)GETARG(int) : \ | |
491 | flags&CHARINT ? (long)(signed char)GETARG(int) : \ | |
492 | (long)GETARG(int)) | |
493 | #define UARG() \ | |
494 | (flags&LONGINT ? GETARG(u_long) : \ | |
495 | flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ | |
496 | flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ | |
497 | (u_long)GETARG(u_int)) | |
498 | #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) | |
499 | #define SJARG() \ | |
500 | (flags&INTMAXT ? GETARG(intmax_t) : \ | |
1f2f436a | 501 | flags&SIZET ? (intmax_t)GETARG(ssize_t) : \ |
9385eb3d A |
502 | flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ |
503 | (intmax_t)GETARG(long long)) | |
504 | #define UJARG() \ | |
505 | (flags&INTMAXT ? GETARG(uintmax_t) : \ | |
506 | flags&SIZET ? (uintmax_t)GETARG(size_t) : \ | |
ad3c9f2a | 507 | flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \ |
9385eb3d A |
508 | (uintmax_t)GETARG(unsigned long long)) |
509 | ||
510 | /* | |
511 | * Get * arguments, including the form *nn$. Preserve the nextarg | |
512 | * that the argument can be gotten once the type is determined. | |
513 | */ | |
514 | #define GETASTER(val) \ | |
515 | n2 = 0; \ | |
516 | cp = fmt; \ | |
517 | while (is_digit(*cp)) { \ | |
518 | n2 = 10 * n2 + to_digit(*cp); \ | |
519 | cp++; \ | |
520 | } \ | |
521 | if (*cp == '$') { \ | |
522 | int hold = nextarg; \ | |
523 | if (argtable == NULL) { \ | |
524 | argtable = statargtable; \ | |
1f2f436a A |
525 | if (__find_warguments (fmt0, orgap, &argtable)) { \ |
526 | ret = EOF; \ | |
527 | goto error; \ | |
528 | } \ | |
9385eb3d A |
529 | } \ |
530 | nextarg = n2; \ | |
531 | val = GETARG (int); \ | |
532 | nextarg = hold; \ | |
533 | fmt = ++cp; \ | |
534 | } else { \ | |
535 | val = GETARG (int); \ | |
536 | } | |
537 | ||
538 | ||
9385eb3d | 539 | /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */ |
ad3c9f2a A |
540 | if (prepwrite(fp) != 0) { |
541 | errno = EBADF; | |
9385eb3d | 542 | return (EOF); |
ad3c9f2a A |
543 | } |
544 | ORIENT(fp, 1); | |
9385eb3d | 545 | |
1f2f436a | 546 | convbuf = NULL; |
9385eb3d A |
547 | fmt = (wchar_t *)fmt0; |
548 | argtable = NULL; | |
549 | nextarg = 1; | |
550 | va_copy(orgap, ap); | |
1f2f436a | 551 | io_init(&io, fp); |
9385eb3d | 552 | ret = 0; |
1f2f436a | 553 | #ifndef NO_FLOATING_POINT |
ad3c9f2a | 554 | decimal_point = get_decpt(loc); |
1f2f436a | 555 | #endif |
9385eb3d A |
556 | |
557 | /* | |
558 | * Scan the format for conversions (`%' character). | |
559 | */ | |
560 | for (;;) { | |
561 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) | |
562 | /* void */; | |
563 | if ((n = fmt - cp) != 0) { | |
564 | if ((unsigned)ret + n > INT_MAX) { | |
565 | ret = EOF; | |
566 | goto error; | |
567 | } | |
568 | PRINT(cp, n); | |
569 | ret += n; | |
570 | } | |
571 | if (ch == '\0') | |
572 | goto done; | |
ad3c9f2a A |
573 | #ifdef VECTORS |
574 | pct = fmt; | |
575 | #endif /* VECTORS */ | |
9385eb3d A |
576 | fmt++; /* skip over '%' */ |
577 | ||
578 | flags = 0; | |
579 | dprec = 0; | |
580 | width = 0; | |
581 | prec = -1; | |
1f2f436a | 582 | gs.grouping = NULL; |
9385eb3d A |
583 | sign = '\0'; |
584 | ox[1] = '\0'; | |
ad3c9f2a A |
585 | #ifdef VECTORS |
586 | vsep = 'X'; /* Illegal value, changed to defaults later. */ | |
587 | #endif /* VECTORS */ | |
9385eb3d A |
588 | |
589 | rflag: ch = *fmt++; | |
590 | reswitch: switch (ch) { | |
591 | case ' ': | |
592 | /*- | |
593 | * ``If the space and + flags both appear, the space | |
594 | * flag will be ignored.'' | |
595 | * -- ANSI X3J11 | |
596 | */ | |
597 | if (!sign) | |
598 | sign = ' '; | |
599 | goto rflag; | |
600 | case '#': | |
601 | flags |= ALT; | |
602 | goto rflag; | |
ad3c9f2a A |
603 | #ifdef VECTORS |
604 | case ',': case ';': case ':': case '_': | |
605 | vsep = ch; | |
606 | goto rflag; | |
607 | #endif /* VECTORS */ | |
9385eb3d A |
608 | case '*': |
609 | /*- | |
610 | * ``A negative field width argument is taken as a | |
611 | * - flag followed by a positive field width.'' | |
612 | * -- ANSI X3J11 | |
613 | * They don't exclude field widths read from args. | |
614 | */ | |
615 | GETASTER (width); | |
616 | if (width >= 0) | |
617 | goto rflag; | |
618 | width = -width; | |
619 | /* FALLTHROUGH */ | |
620 | case '-': | |
621 | flags |= LADJUST; | |
622 | goto rflag; | |
623 | case '+': | |
624 | sign = '+'; | |
625 | goto rflag; | |
626 | case '\'': | |
627 | flags |= GROUPING; | |
9385eb3d A |
628 | goto rflag; |
629 | case '.': | |
630 | if ((ch = *fmt++) == '*') { | |
631 | GETASTER (prec); | |
632 | goto rflag; | |
633 | } | |
634 | prec = 0; | |
635 | while (is_digit(ch)) { | |
636 | prec = 10 * prec + to_digit(ch); | |
637 | ch = *fmt++; | |
638 | } | |
639 | goto reswitch; | |
640 | case '0': | |
641 | /*- | |
642 | * ``Note that 0 is taken as a flag, not as the | |
643 | * beginning of a field width.'' | |
644 | * -- ANSI X3J11 | |
645 | */ | |
646 | flags |= ZEROPAD; | |
647 | goto rflag; | |
648 | case '1': case '2': case '3': case '4': | |
649 | case '5': case '6': case '7': case '8': case '9': | |
650 | n = 0; | |
651 | do { | |
652 | n = 10 * n + to_digit(ch); | |
653 | ch = *fmt++; | |
654 | } while (is_digit(ch)); | |
655 | if (ch == '$') { | |
656 | nextarg = n; | |
657 | if (argtable == NULL) { | |
658 | argtable = statargtable; | |
1f2f436a A |
659 | if (__find_warguments (fmt0, orgap, |
660 | &argtable)) { | |
661 | ret = EOF; | |
662 | goto error; | |
663 | } | |
9385eb3d A |
664 | } |
665 | goto rflag; | |
666 | } | |
667 | width = n; | |
668 | goto reswitch; | |
59e0d9fe | 669 | #ifndef NO_FLOATING_POINT |
9385eb3d A |
670 | case 'L': |
671 | flags |= LONGDBL; | |
672 | goto rflag; | |
673 | #endif | |
674 | case 'h': | |
675 | if (flags & SHORTINT) { | |
676 | flags &= ~SHORTINT; | |
677 | flags |= CHARINT; | |
678 | } else | |
679 | flags |= SHORTINT; | |
680 | goto rflag; | |
681 | case 'j': | |
682 | flags |= INTMAXT; | |
683 | goto rflag; | |
684 | case 'l': | |
685 | if (flags & LONGINT) { | |
686 | flags &= ~LONGINT; | |
687 | flags |= LLONGINT; | |
688 | } else | |
689 | flags |= LONGINT; | |
690 | goto rflag; | |
691 | case 'q': | |
692 | flags |= LLONGINT; /* not necessarily */ | |
693 | goto rflag; | |
694 | case 't': | |
695 | flags |= PTRDIFFT; | |
696 | goto rflag; | |
697 | case 'z': | |
698 | flags |= SIZET; | |
699 | goto rflag; | |
700 | case 'C': | |
701 | flags |= LONGINT; | |
702 | /*FALLTHROUGH*/ | |
703 | case 'c': | |
ad3c9f2a A |
704 | #ifdef VECTORS |
705 | if (flags & VECTOR) | |
706 | break; | |
707 | #endif /* VECTORS */ | |
9385eb3d A |
708 | if (flags & LONGINT) |
709 | *(cp = buf) = (wchar_t)GETARG(wint_t); | |
710 | else | |
ad3c9f2a | 711 | *(cp = buf) = (wchar_t)btowc_l(GETARG(int), loc); |
9385eb3d A |
712 | size = 1; |
713 | sign = '\0'; | |
714 | break; | |
715 | case 'D': | |
716 | flags |= LONGINT; | |
717 | /*FALLTHROUGH*/ | |
718 | case 'd': | |
719 | case 'i': | |
ad3c9f2a A |
720 | #ifdef VECTORS |
721 | if (flags & VECTOR) | |
722 | break; | |
723 | #endif /* VECTORS */ | |
9385eb3d A |
724 | if (flags & INTMAX_SIZE) { |
725 | ujval = SJARG(); | |
726 | if ((intmax_t)ujval < 0) { | |
727 | ujval = -ujval; | |
728 | sign = '-'; | |
729 | } | |
730 | } else { | |
731 | ulval = SARG(); | |
732 | if ((long)ulval < 0) { | |
733 | ulval = -ulval; | |
734 | sign = '-'; | |
735 | } | |
736 | } | |
737 | base = 10; | |
738 | goto number; | |
59e0d9fe | 739 | #ifndef NO_FLOATING_POINT |
9385eb3d A |
740 | case 'a': |
741 | case 'A': | |
ad3c9f2a A |
742 | #ifdef VECTORS |
743 | if (flags & VECTOR) { | |
744 | flags |= FPT; | |
745 | break; | |
746 | } | |
747 | #endif /* VECTORS */ | |
9385eb3d A |
748 | if (ch == 'a') { |
749 | ox[1] = 'x'; | |
750 | xdigs = xdigs_lower; | |
751 | expchar = 'p'; | |
752 | } else { | |
753 | ox[1] = 'X'; | |
754 | xdigs = xdigs_upper; | |
755 | expchar = 'P'; | |
756 | } | |
59e0d9fe A |
757 | if (prec >= 0) |
758 | prec++; | |
9385eb3d | 759 | if (flags & LONGDBL) { |
59e0d9fe | 760 | fparg.ldbl = GETARG(long double); |
9385eb3d A |
761 | dtoaresult = |
762 | __hldtoa(fparg.ldbl, xdigs, prec, | |
763 | &expt, &signflag, &dtoaend); | |
764 | } else { | |
765 | fparg.dbl = GETARG(double); | |
766 | dtoaresult = | |
767 | __hdtoa(fparg.dbl, xdigs, prec, | |
768 | &expt, &signflag, &dtoaend); | |
769 | } | |
59e0d9fe A |
770 | if (prec < 0) |
771 | prec = dtoaend - dtoaresult; | |
772 | if (expt == INT_MAX) | |
773 | ox[1] = '\0'; | |
9385eb3d A |
774 | if (convbuf != NULL) |
775 | free(convbuf); | |
59e0d9fe | 776 | ndig = dtoaend - dtoaresult; |
ad3c9f2a | 777 | cp = convbuf = __mbsconv(dtoaresult, -1, loc); |
9385eb3d | 778 | freedtoa(dtoaresult); |
59e0d9fe | 779 | goto fp_common; |
9385eb3d A |
780 | case 'e': |
781 | case 'E': | |
ad3c9f2a A |
782 | #ifdef VECTORS |
783 | if (flags & VECTOR) { | |
784 | flags |= FPT; | |
785 | break; | |
786 | } | |
787 | #endif /* VECTORS */ | |
9385eb3d A |
788 | expchar = ch; |
789 | if (prec < 0) /* account for digit before decpt */ | |
790 | prec = DEFPREC + 1; | |
791 | else | |
792 | prec++; | |
793 | goto fp_begin; | |
794 | case 'f': | |
795 | case 'F': | |
ad3c9f2a A |
796 | #ifdef VECTORS |
797 | if (flags & VECTOR) { | |
798 | flags |= FPT; | |
799 | break; | |
800 | } | |
801 | #endif /* VECTORS */ | |
9385eb3d A |
802 | expchar = '\0'; |
803 | goto fp_begin; | |
804 | case 'g': | |
805 | case 'G': | |
ad3c9f2a A |
806 | #ifdef VECTORS |
807 | if (flags & VECTOR) { | |
808 | flags |= FPT; | |
809 | break; | |
810 | } | |
811 | #endif /* VECTORS */ | |
9385eb3d A |
812 | expchar = ch - ('g' - 'e'); |
813 | if (prec == 0) | |
814 | prec = 1; | |
815 | fp_begin: | |
816 | if (prec < 0) | |
817 | prec = DEFPREC; | |
818 | if (convbuf != NULL) | |
819 | free(convbuf); | |
820 | if (flags & LONGDBL) { | |
821 | fparg.ldbl = GETARG(long double); | |
822 | dtoaresult = | |
823 | __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, | |
824 | &expt, &signflag, &dtoaend); | |
825 | } else { | |
826 | fparg.dbl = GETARG(double); | |
827 | dtoaresult = | |
828 | dtoa(fparg.dbl, expchar ? 2 : 3, prec, | |
829 | &expt, &signflag, &dtoaend); | |
830 | if (expt == 9999) | |
831 | expt = INT_MAX; | |
832 | } | |
833 | ndig = dtoaend - dtoaresult; | |
ad3c9f2a | 834 | cp = convbuf = __mbsconv(dtoaresult, -1, loc); |
9385eb3d | 835 | freedtoa(dtoaresult); |
59e0d9fe | 836 | fp_common: |
9385eb3d A |
837 | if (signflag) |
838 | sign = '-'; | |
839 | if (expt == INT_MAX) { /* inf or nan */ | |
840 | if (*cp == 'N') { | |
841 | cp = (ch >= 'a') ? L"nan" : L"NAN"; | |
842 | sign = '\0'; | |
843 | } else | |
844 | cp = (ch >= 'a') ? L"inf" : L"INF"; | |
845 | size = 3; | |
1f2f436a | 846 | flags &= ~ZEROPAD; |
9385eb3d A |
847 | break; |
848 | } | |
849 | flags |= FPT; | |
850 | if (ch == 'g' || ch == 'G') { | |
851 | if (expt > -4 && expt <= prec) { | |
852 | /* Make %[gG] smell like %[fF] */ | |
853 | expchar = '\0'; | |
854 | if (flags & ALT) | |
855 | prec -= expt; | |
856 | else | |
857 | prec = ndig - expt; | |
858 | if (prec < 0) | |
859 | prec = 0; | |
860 | } else { | |
861 | /* | |
862 | * Make %[gG] smell like %[eE], but | |
863 | * trim trailing zeroes if no # flag. | |
864 | */ | |
865 | if (!(flags & ALT)) | |
866 | prec = ndig; | |
867 | } | |
868 | } | |
869 | if (expchar) { | |
870 | expsize = exponent(expstr, expt - 1, expchar); | |
871 | size = expsize + prec; | |
872 | if (prec > 1 || flags & ALT) | |
873 | ++size; | |
874 | } else { | |
875 | /* space for digits before decimal point */ | |
876 | if (expt > 0) | |
877 | size = expt; | |
878 | else /* "0" */ | |
879 | size = 1; | |
880 | /* space for decimal pt and following digits */ | |
881 | if (prec || flags & ALT) | |
882 | size += prec + 1; | |
1f2f436a | 883 | if ((flags & GROUPING) && expt > 0) |
ad3c9f2a | 884 | size += grouping_init(&gs, expt, loc); |
9385eb3d A |
885 | } |
886 | break; | |
59e0d9fe | 887 | #endif /* !NO_FLOATING_POINT */ |
9385eb3d | 888 | case 'n': |
ad3c9f2a | 889 | { |
9385eb3d A |
890 | /* |
891 | * Assignment-like behavior is specified if the | |
892 | * value overflows or is otherwise unrepresentable. | |
893 | * C99 says to use `signed char' for %hhn conversions. | |
894 | */ | |
ad3c9f2a A |
895 | void *ptr = GETARG(void *); |
896 | if (ptr == NULL) | |
897 | continue; | |
898 | else if (flags & LLONGINT) | |
899 | *(long long *)ptr = ret; | |
9385eb3d | 900 | else if (flags & SIZET) |
ad3c9f2a | 901 | *(ssize_t *)ptr = (ssize_t)ret; |
9385eb3d | 902 | else if (flags & PTRDIFFT) |
ad3c9f2a | 903 | *(ptrdiff_t *)ptr = ret; |
9385eb3d | 904 | else if (flags & INTMAXT) |
ad3c9f2a | 905 | *(intmax_t *)ptr = ret; |
9385eb3d | 906 | else if (flags & LONGINT) |
ad3c9f2a | 907 | *(long *)ptr = ret; |
9385eb3d | 908 | else if (flags & SHORTINT) |
ad3c9f2a | 909 | *(short *)ptr = ret; |
9385eb3d | 910 | else if (flags & CHARINT) |
ad3c9f2a | 911 | *(signed char *)ptr = ret; |
9385eb3d | 912 | else |
ad3c9f2a | 913 | *(int *)ptr = ret; |
9385eb3d | 914 | continue; /* no output */ |
ad3c9f2a | 915 | } |
9385eb3d A |
916 | case 'O': |
917 | flags |= LONGINT; | |
918 | /*FALLTHROUGH*/ | |
919 | case 'o': | |
ad3c9f2a A |
920 | #ifdef VECTORS |
921 | if (flags & VECTOR) | |
922 | break; | |
923 | #endif /* VECTORS */ | |
9385eb3d A |
924 | if (flags & INTMAX_SIZE) |
925 | ujval = UJARG(); | |
926 | else | |
927 | ulval = UARG(); | |
928 | base = 8; | |
929 | goto nosign; | |
930 | case 'p': | |
931 | /*- | |
932 | * ``The argument shall be a pointer to void. The | |
933 | * value of the pointer is converted to a sequence | |
934 | * of printable characters, in an implementation- | |
935 | * defined manner.'' | |
936 | * -- ANSI X3J11 | |
937 | */ | |
ad3c9f2a A |
938 | #ifdef VECTORS |
939 | if (flags & VECTOR) | |
940 | break; | |
941 | #endif /* VECTORS */ | |
9385eb3d A |
942 | ujval = (uintmax_t)(uintptr_t)GETARG(void *); |
943 | base = 16; | |
944 | xdigs = xdigs_lower; | |
945 | flags = flags | INTMAXT; | |
946 | ox[1] = 'x'; | |
947 | goto nosign; | |
948 | case 'S': | |
949 | flags |= LONGINT; | |
950 | /*FALLTHROUGH*/ | |
951 | case 's': | |
952 | if (flags & LONGINT) { | |
953 | if ((cp = GETARG(wchar_t *)) == NULL) | |
954 | cp = L"(null)"; | |
955 | } else { | |
956 | char *mbp; | |
957 | ||
958 | if (convbuf != NULL) | |
959 | free(convbuf); | |
960 | if ((mbp = GETARG(char *)) == NULL) | |
961 | cp = L"(null)"; | |
962 | else { | |
ad3c9f2a | 963 | convbuf = __mbsconv(mbp, prec, loc); |
9385eb3d A |
964 | if (convbuf == NULL) { |
965 | fp->_flags |= __SERR; | |
966 | goto error; | |
967 | } | |
968 | cp = convbuf; | |
969 | } | |
970 | } | |
ad3c9f2a | 971 | #if 0 // wcsnlen needs API review first |
1f2f436a | 972 | size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp); |
ad3c9f2a A |
973 | #else |
974 | size = wcslen(cp); | |
975 | if(prec >= 0 && prec < size) | |
976 | size = prec; | |
977 | #endif | |
9385eb3d A |
978 | sign = '\0'; |
979 | break; | |
980 | case 'U': | |
981 | flags |= LONGINT; | |
982 | /*FALLTHROUGH*/ | |
983 | case 'u': | |
ad3c9f2a A |
984 | #ifdef VECTORS |
985 | if (flags & VECTOR) | |
986 | break; | |
987 | #endif /* VECTORS */ | |
9385eb3d A |
988 | if (flags & INTMAX_SIZE) |
989 | ujval = UJARG(); | |
990 | else | |
991 | ulval = UARG(); | |
992 | base = 10; | |
993 | goto nosign; | |
994 | case 'X': | |
995 | xdigs = xdigs_upper; | |
996 | goto hex; | |
997 | case 'x': | |
998 | xdigs = xdigs_lower; | |
999 | hex: | |
ad3c9f2a A |
1000 | #ifdef VECTORS |
1001 | if (flags & VECTOR) | |
1002 | break; | |
1003 | #endif /* VECTORS */ | |
9385eb3d A |
1004 | if (flags & INTMAX_SIZE) |
1005 | ujval = UJARG(); | |
1006 | else | |
1007 | ulval = UARG(); | |
1008 | base = 16; | |
1009 | /* leading 0x/X only if non-zero */ | |
1010 | if (flags & ALT && | |
1011 | (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) | |
1012 | ox[1] = ch; | |
1013 | ||
1014 | flags &= ~GROUPING; | |
1015 | /* unsigned conversions */ | |
1016 | nosign: sign = '\0'; | |
1017 | /*- | |
1018 | * ``... diouXx conversions ... if a precision is | |
1019 | * specified, the 0 flag will be ignored.'' | |
1020 | * -- ANSI X3J11 | |
ad3c9f2a | 1021 | * except for %#.0o and zero value |
9385eb3d A |
1022 | */ |
1023 | number: if ((dprec = prec) >= 0) | |
1024 | flags &= ~ZEROPAD; | |
1025 | ||
1026 | /*- | |
1027 | * ``The result of converting a zero value with an | |
1028 | * explicit precision of zero is no characters.'' | |
1029 | * -- ANSI X3J11 | |
1f2f436a A |
1030 | * |
1031 | * ``The C Standard is clear enough as is. The call | |
1032 | * printf("%#.0o", 0) should print 0.'' | |
1033 | * -- Defect Report #151 | |
9385eb3d A |
1034 | */ |
1035 | cp = buf + BUF; | |
1036 | if (flags & INTMAX_SIZE) { | |
1f2f436a A |
1037 | if (ujval != 0 || prec != 0 || |
1038 | (flags & ALT && base == 8)) | |
9385eb3d | 1039 | cp = __ujtoa(ujval, cp, base, |
1f2f436a | 1040 | flags & ALT, xdigs); |
9385eb3d | 1041 | } else { |
1f2f436a A |
1042 | if (ulval != 0 || prec != 0 || |
1043 | (flags & ALT && base == 8)) | |
9385eb3d | 1044 | cp = __ultoa(ulval, cp, base, |
1f2f436a | 1045 | flags & ALT, xdigs); |
9385eb3d A |
1046 | } |
1047 | size = buf + BUF - cp; | |
1048 | if (size > BUF) /* should never happen */ | |
ad3c9f2a | 1049 | LIBC_ABORT("size (%d) > BUF (%d)", size, BUF); |
1f2f436a | 1050 | if ((flags & GROUPING) && size != 0) |
ad3c9f2a | 1051 | size += grouping_init(&gs, size, loc); |
9385eb3d | 1052 | break; |
ad3c9f2a A |
1053 | #ifdef VECTORS |
1054 | case 'v': | |
1055 | flags |= VECTOR; | |
1056 | goto rflag; | |
1057 | #endif /* VECTORS */ | |
9385eb3d A |
1058 | default: /* "%?" prints ?, unless ? is NUL */ |
1059 | if (ch == '\0') | |
1060 | goto done; | |
1061 | /* pretend it was %c with argument ch */ | |
1062 | cp = buf; | |
1063 | *cp = ch; | |
1064 | size = 1; | |
1065 | sign = '\0'; | |
1066 | break; | |
1067 | } | |
1068 | ||
ad3c9f2a A |
1069 | #ifdef VECTORS |
1070 | if (flags & VECTOR) { | |
1071 | /* | |
1072 | * Do the minimum amount of work necessary to construct | |
1073 | * a format specifier that can be used to recursively | |
1074 | * call vfprintf() for each element in the vector. | |
1075 | */ | |
1076 | int i, j; /* Counter. */ | |
1077 | int vcnt; /* Number of elements in vector. */ | |
1078 | char *vfmt; /* Pointer to format specifier. */ | |
1079 | #define EXTRAHH 2 | |
1080 | char vfmt_buf[32 + EXTRAHH]; /* Static buffer for format spec. */ | |
1081 | int vwidth = 0; /* Width specified via '*'. */ | |
1082 | int vprec = 0; /* Precision specified via '*'. */ | |
1083 | char *vstr; /* Used for asprintf(). */ | |
1084 | int vlen; /* Length returned by asprintf(). */ | |
1085 | enum { | |
1086 | V_CHAR, V_SHORT, V_INT, | |
1087 | V_PCHAR, V_PSHORT, V_PINT, | |
1088 | V_FLOAT, | |
1089 | #ifdef V64TYPE | |
1090 | V_LONGLONG, V_PLONGLONG, | |
1091 | V_DOUBLE, | |
1092 | #endif /* V64TYPE */ | |
1093 | } vtype; | |
1094 | ||
1095 | vval.vectorarg = GETARG(VECTORTYPE); | |
1096 | /* | |
1097 | * Set vfmt. If vfmt_buf may not be big enough, | |
1098 | * malloc() space, taking care to free it later. | |
1099 | * (EXTRAHH is for possible extra "hh") | |
1100 | */ | |
1101 | if (&fmt[-1] - pct + EXTRAHH < sizeof(vfmt_buf)) | |
1102 | vfmt = vfmt_buf; | |
1103 | else | |
1104 | vfmt = (char *)malloc(&fmt[-1] - pct + EXTRAHH + 1); | |
1105 | ||
1106 | /* Set the separator character, if not specified. */ | |
1107 | if (vsep == 'X') { | |
1108 | if (ch == 'c') | |
1109 | vsep = '\0'; | |
1110 | else | |
1111 | vsep = ' '; | |
1112 | } | |
1113 | ||
1114 | /* Create the format specifier. */ | |
1115 | for (i = j = 0; i < &fmt[-1] - pct; i++) { | |
1116 | switch (pct[i]) { | |
1117 | case ',': case ';': case ':': case '_': | |
1118 | case 'v': case 'h': case 'l': | |
1119 | /* Ignore. */ | |
1120 | break; | |
1121 | case '*': | |
1122 | if (pct[i - 1] != '.') | |
1123 | vwidth = 1; | |
1124 | else | |
1125 | vprec = 1; | |
1126 | /* FALLTHROUGH */ | |
1127 | default: | |
1128 | vfmt[j++] = pct[i]; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | /* | |
1133 | * Determine the number of elements in the vector and | |
1134 | * finish up the format specifier. | |
1135 | */ | |
1136 | if (flags & SHORTINT) { | |
1137 | switch (ch) { | |
1138 | case 'c': | |
1139 | vtype = V_SHORT; | |
1140 | break; | |
1141 | case 'p': | |
1142 | vtype = V_PSHORT; | |
1143 | break; | |
1144 | default: | |
1145 | vfmt[j++] = 'h'; | |
1146 | vtype = V_SHORT; | |
1147 | break; | |
1148 | } | |
1149 | vcnt = 8; | |
1150 | } else if (flags & LONGINT) { | |
1151 | vcnt = 4; | |
1152 | vtype = (ch == 'p') ? V_PINT : V_INT; | |
1153 | #ifdef V64TYPE | |
1154 | } else if (flags & LLONGINT) { | |
1155 | switch (ch) { | |
1156 | case 'a': | |
1157 | case 'A': | |
1158 | case 'e': | |
1159 | case 'E': | |
1160 | case 'f': | |
1161 | case 'g': | |
1162 | case 'G': | |
1163 | vcnt = 2; | |
1164 | vtype = V_DOUBLE; | |
1165 | break; | |
1166 | case 'd': | |
1167 | case 'i': | |
1168 | case 'u': | |
1169 | case 'o': | |
1170 | case 'p': | |
1171 | case 'x': | |
1172 | case 'X': | |
1173 | vfmt[j++] = 'l'; | |
1174 | vfmt[j++] = 'l'; | |
1175 | vcnt = 2; | |
1176 | vtype = (ch == 'p') ? V_PLONGLONG : V_LONGLONG; | |
1177 | break; | |
1178 | default: | |
1179 | /* | |
1180 | * The default case should never | |
1181 | * happen. | |
1182 | */ | |
1183 | case 'c': | |
1184 | vcnt = 16; | |
1185 | vtype = V_CHAR; | |
1186 | } | |
1187 | #endif /* V64TYPE */ | |
1188 | } else { | |
1189 | switch (ch) { | |
1190 | case 'a': | |
1191 | case 'A': | |
1192 | case 'e': | |
1193 | case 'E': | |
1194 | case 'f': | |
1195 | case 'g': | |
1196 | case 'G': | |
1197 | vcnt = 4; | |
1198 | vtype = V_FLOAT; | |
1199 | break; | |
1200 | default: | |
1201 | /* | |
1202 | * The default case should never | |
1203 | * happen. | |
1204 | */ | |
1205 | case 'd': | |
1206 | case 'i': | |
1207 | case 'u': | |
1208 | case 'o': | |
1209 | case 'x': | |
1210 | case 'X': | |
1211 | vfmt[j++] = 'h'; | |
1212 | vfmt[j++] = 'h'; | |
1213 | /* drop through */ | |
1214 | case 'p': | |
1215 | case 'c': | |
1216 | vcnt = 16; | |
1217 | vtype = (ch == 'p') ? V_PCHAR : V_CHAR; | |
1218 | } | |
1219 | } | |
1220 | vfmt[j++] = ch; | |
1221 | vfmt[j++] = '\0'; | |
1222 | ||
1223 | /* Get a vector element. */ | |
1224 | #ifdef V64TYPE | |
1225 | #define VPRINT(type, ind, args...) do { \ | |
1226 | switch (type) { \ | |
1227 | case V_CHAR: \ | |
1228 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \ | |
1229 | break; \ | |
1230 | case V_PCHAR: \ | |
1231 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \ | |
1232 | break; \ | |
1233 | case V_SHORT: \ | |
1234 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \ | |
1235 | break; \ | |
1236 | case V_PSHORT: \ | |
1237 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \ | |
1238 | break; \ | |
1239 | case V_INT: \ | |
1240 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \ | |
1241 | break; \ | |
1242 | case V_PINT: \ | |
1243 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \ | |
1244 | break; \ | |
1245 | case V_LONGLONG: \ | |
1246 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \ | |
1247 | break; \ | |
1248 | case V_PLONGLONG: \ | |
1249 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \ | |
1250 | break; \ | |
1251 | case V_FLOAT: \ | |
1252 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \ | |
1253 | break; \ | |
1254 | case V_DOUBLE: \ | |
1255 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \ | |
1256 | break; \ | |
1257 | } \ | |
1258 | ret += vlen; \ | |
1259 | PRINT(vstr, vlen); \ | |
1260 | free(vstr); \ | |
1261 | } while (0) | |
1262 | #else /* !V64TYPE */ | |
1263 | #define VPRINT(type, ind, args...) do { \ | |
1264 | switch (type) { \ | |
1265 | case V_CHAR: \ | |
1266 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \ | |
1267 | break; \ | |
1268 | case V_PCHAR: \ | |
1269 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \ | |
1270 | break; \ | |
1271 | case V_SHORT: \ | |
1272 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \ | |
1273 | break; \ | |
1274 | case V_PSHORT: \ | |
1275 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \ | |
1276 | break; \ | |
1277 | case V_INT: \ | |
1278 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \ | |
1279 | break; \ | |
1280 | case V_PINT: \ | |
1281 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \ | |
1282 | break; \ | |
1283 | case V_FLOAT: \ | |
1284 | vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \ | |
1285 | break; \ | |
1286 | } \ | |
1287 | ret += vlen; \ | |
1288 | PRINT(vstr, vlen); \ | |
1289 | free(vstr); \ | |
1290 | } while (0) | |
1291 | #endif /* V64TYPE */ | |
1292 | ||
1293 | /* Actually print. */ | |
1294 | if (vwidth == 0) { | |
1295 | if (vprec == 0) { | |
1296 | /* First element. */ | |
1297 | VPRINT(vtype, 0); | |
1298 | for (i = 1; i < vcnt; i++) { | |
1299 | /* Separator. */ | |
1300 | if(vsep) | |
1301 | PRINT(&vsep, 1); | |
1302 | ||
1303 | /* Element. */ | |
1304 | VPRINT(vtype, i); | |
1305 | } | |
1306 | } else { | |
1307 | /* First element. */ | |
1308 | VPRINT(vtype, 0, prec); | |
1309 | for (i = 1; i < vcnt; i++) { | |
1310 | /* Separator. */ | |
1311 | if(vsep) | |
1312 | PRINT(&vsep, 1); | |
1313 | ||
1314 | /* Element. */ | |
1315 | VPRINT(vtype, i, prec); | |
1316 | } | |
1317 | } | |
1318 | } else { | |
1319 | if (vprec == 0) { | |
1320 | /* First element. */ | |
1321 | VPRINT(vtype, 0, width); | |
1322 | for (i = 1; i < vcnt; i++) { | |
1323 | /* Separator. */ | |
1324 | if(vsep) | |
1325 | PRINT(&vsep, 1); | |
1326 | ||
1327 | /* Element. */ | |
1328 | VPRINT(vtype, i, width); | |
1329 | } | |
1330 | } else { | |
1331 | /* First element. */ | |
1332 | VPRINT(vtype, 0, width, prec); | |
1333 | for (i = 1; i < vcnt; i++) { | |
1334 | /* Separator. */ | |
1335 | if(vsep) | |
1336 | PRINT(&vsep, 1); | |
1337 | ||
1338 | /* Element. */ | |
1339 | VPRINT(vtype, i, width, prec); | |
1340 | } | |
1341 | } | |
1342 | } | |
1343 | #undef VPRINT | |
1344 | ||
1345 | if (vfmt != vfmt_buf) | |
1346 | free(vfmt); | |
1347 | ||
1348 | continue; | |
1349 | } | |
1350 | #endif /* VECTORS */ | |
9385eb3d A |
1351 | /* |
1352 | * All reasonable formats wind up here. At this point, `cp' | |
1353 | * points to a string which (if not flags&LADJUST) should be | |
1354 | * padded out to `width' places. If flags&ZEROPAD, it should | |
1355 | * first be prefixed by any sign or other prefix; otherwise, | |
1356 | * it should be blank padded before the prefix is emitted. | |
1357 | * After any left-hand padding and prefixing, emit zeroes | |
1358 | * required by a decimal [diouxX] precision, then print the | |
1359 | * string proper, then emit zeroes required by any leftover | |
1360 | * floating precision; finally, if LADJUST, pad with blanks. | |
1361 | * | |
1362 | * Compute actual size, so we know how much to pad. | |
1363 | * size excludes decimal prec; realsz includes it. | |
1364 | */ | |
1365 | realsz = dprec > size ? dprec : size; | |
1366 | if (sign) | |
1367 | realsz++; | |
59e0d9fe | 1368 | if (ox[1]) |
9385eb3d A |
1369 | realsz += 2; |
1370 | ||
1371 | prsize = width > realsz ? width : realsz; | |
1372 | if ((unsigned)ret + prsize > INT_MAX) { | |
1373 | ret = EOF; | |
1374 | goto error; | |
1375 | } | |
1376 | ||
1377 | /* right-adjusting blank padding */ | |
1378 | if ((flags & (LADJUST|ZEROPAD)) == 0) | |
1379 | PAD(width - realsz, blanks); | |
1380 | ||
1381 | /* prefix */ | |
59e0d9fe | 1382 | if (sign) |
9385eb3d | 1383 | PRINT(&sign, 1); |
59e0d9fe A |
1384 | |
1385 | if (ox[1]) { /* ox[1] is either x, X, or \0 */ | |
9385eb3d A |
1386 | ox[0] = '0'; |
1387 | PRINT(ox, 2); | |
1388 | } | |
1389 | ||
1390 | /* right-adjusting zero padding */ | |
1391 | if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) | |
1392 | PAD(width - realsz, zeroes); | |
1393 | ||
9385eb3d | 1394 | /* the string or number proper */ |
59e0d9fe | 1395 | #ifndef NO_FLOATING_POINT |
9385eb3d | 1396 | if ((flags & FPT) == 0) { |
1f2f436a A |
1397 | #endif |
1398 | /* leading zeroes from decimal precision */ | |
1399 | PAD(dprec - size, zeroes); | |
1400 | if (gs.grouping) { | |
ad3c9f2a | 1401 | if (grouping_print(&gs, &io, cp, buf+BUF, loc) < 0) |
1f2f436a A |
1402 | goto error; |
1403 | } else { | |
1404 | PRINT(cp, size); | |
1405 | } | |
1406 | #ifndef NO_FLOATING_POINT | |
9385eb3d A |
1407 | } else { /* glue together f_p fragments */ |
1408 | if (!expchar) { /* %[fF] or sufficiently short %[gG] */ | |
1409 | if (expt <= 0) { | |
1410 | PRINT(zeroes, 1); | |
1411 | if (prec || flags & ALT) | |
1f2f436a | 1412 | PRINT(&decimal_point, 1); |
9385eb3d A |
1413 | PAD(-expt, zeroes); |
1414 | /* already handled initial 0's */ | |
1415 | prec += expt; | |
1416 | } else { | |
1f2f436a A |
1417 | if (gs.grouping) { |
1418 | n = grouping_print(&gs, &io, | |
ad3c9f2a | 1419 | cp, convbuf + ndig, loc); |
1f2f436a A |
1420 | if (n < 0) |
1421 | goto error; | |
1422 | cp += n; | |
1423 | } else { | |
1424 | PRINTANDPAD(cp, convbuf + ndig, | |
1425 | expt, zeroes); | |
1426 | cp += expt; | |
9385eb3d | 1427 | } |
1f2f436a A |
1428 | if (prec || flags & ALT) |
1429 | PRINT(&decimal_point, 1); | |
9385eb3d A |
1430 | } |
1431 | PRINTANDPAD(cp, convbuf + ndig, prec, zeroes); | |
1432 | } else { /* %[eE] or sufficiently long %[gG] */ | |
1433 | if (prec > 1 || flags & ALT) { | |
1434 | buf[0] = *cp++; | |
1f2f436a | 1435 | buf[1] = decimal_point; |
9385eb3d A |
1436 | PRINT(buf, 2); |
1437 | PRINT(cp, ndig-1); | |
1438 | PAD(prec - ndig, zeroes); | |
1439 | } else /* XeYYY */ | |
1440 | PRINT(cp, 1); | |
1441 | PRINT(expstr, expsize); | |
1442 | } | |
1443 | } | |
9385eb3d A |
1444 | #endif |
1445 | /* left-adjusting padding (always blank) */ | |
1446 | if (flags & LADJUST) | |
1447 | PAD(width - realsz, blanks); | |
1448 | ||
1449 | /* finally, adjust ret */ | |
1450 | ret += prsize; | |
1f2f436a A |
1451 | |
1452 | FLUSH(); /* copy out the I/O vectors */ | |
9385eb3d A |
1453 | } |
1454 | done: | |
1f2f436a | 1455 | FLUSH(); |
9385eb3d | 1456 | error: |
3d9156a7 | 1457 | va_end(orgap); |
9385eb3d A |
1458 | if (convbuf != NULL) |
1459 | free(convbuf); | |
1460 | if (__sferror(fp)) | |
1461 | ret = EOF; | |
1462 | if ((argtable != NULL) && (argtable != statargtable)) | |
1463 | free (argtable); | |
1464 | return (ret); | |
1465 | /* NOTREACHED */ | |
1466 | } |