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