1 /* $OpenBSD: vfprintf.c,v 1.63 2013/03/02 19:40:08 guenther Exp $ */
2 /*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
34 /*
35 * Actual printf innards.
36 *
37 * This code is large and complicated...
38 */
40 #include <sys/types.h>
41 #include <sys/mman.h>
43 #include <errno.h>
44 #include <langinfo.h>
45 #include <limits.h>
46 #include <stdarg.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdint.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <wchar.h>
55 #include "local.h"
56 #include "fvwrite.h"
58 union arg {
59 int intarg;
60 unsigned int uintarg;
61 long longarg;
62 unsigned long ulongarg;
63 long long longlongarg;
64 unsigned long long ulonglongarg;
65 ptrdiff_t ptrdiffarg;
66 size_t sizearg;
67 ssize_t ssizearg;
68 intmax_t intmaxarg;
69 uintmax_t uintmaxarg;
70 void *pvoidarg;
71 char *pchararg;
72 signed char *pschararg;
73 short *pshortarg;
74 int *pintarg;
75 long *plongarg;
76 long long *plonglongarg;
77 ptrdiff_t *pptrdiffarg;
78 ssize_t *pssizearg;
79 intmax_t *pintmaxarg;
80 #ifdef FLOATING_POINT
81 double doublearg;
82 long double longdoublearg;
83 #endif
84 #ifdef PRINTF_WIDE_CHAR
85 wint_t wintarg;
86 wchar_t *pwchararg;
87 #endif
88 };
90 static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
91 size_t *argtablesiz);
92 static int __grow_type_table(unsigned char **typetable, int *tablesize);
94 /*
95 * Flush out all the vectors defined by the given uio,
96 * then reset it so that it can be reused.
97 */
98 static int
99 __sprint(FILE *fp, struct __suio *uio)
100 {
101 int err;
103 if (uio->uio_resid == 0) {
104 uio->uio_iovcnt = 0;
105 return (0);
106 }
107 err = __sfvwrite(fp, uio);
108 uio->uio_resid = 0;
109 uio->uio_iovcnt = 0;
110 return (err);
111 }
113 /*
114 * Helper function for `fprintf to unbuffered unix file': creates a
115 * temporary buffer. We only work on write-only files; this avoids
116 * worries about ungetc buffers and so forth.
117 */
118 static int
119 __sbprintf(FILE *fp, const char *fmt, va_list ap)
120 {
121 int ret;
122 FILE fake;
123 struct __sfileext fakeext;
124 unsigned char buf[BUFSIZ];
126 _FILEEXT_SETUP(&fake, &fakeext);
127 /* copy the important variables */
128 fake._flags = fp->_flags & ~__SNBF;
129 fake._file = fp->_file;
130 fake._cookie = fp->_cookie;
131 fake._write = fp->_write;
133 /* set up the buffer */
134 fake._bf._base = fake._p = buf;
135 fake._bf._size = fake._w = sizeof(buf);
136 fake._lbfsize = 0; /* not actually used, but Just In Case */
138 /* do the work, then copy any error status */
139 ret = __vfprintf(&fake, fmt, ap);
140 if (ret >= 0 && __sflush(&fake))
141 ret = EOF;
142 if (fake._flags & __SERR)
143 fp->_flags |= __SERR;
144 return (ret);
145 }
147 #ifdef PRINTF_WIDE_CHAR
148 /*
149 * Convert a wide character string argument for the %ls format to a multibyte
150 * string representation. If not -1, prec specifies the maximum number of
151 * bytes to output, and also means that we can't assume that the wide char
152 * string is null-terminated.
153 */
154 static char *
155 __wcsconv(wchar_t *wcsarg, int prec)
156 {
157 mbstate_t mbs;
158 char buf[MB_LEN_MAX];
159 wchar_t *p;
160 char *convbuf;
161 size_t clen, nbytes;
163 /* Allocate space for the maximum number of bytes we could output. */
164 if (prec < 0) {
165 memset(&mbs, 0, sizeof(mbs));
166 p = wcsarg;
167 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
168 if (nbytes == (size_t)-1) {
169 errno = EILSEQ;
170 return (NULL);
171 }
172 } else {
173 /*
174 * Optimisation: if the output precision is small enough,
175 * just allocate enough memory for the maximum instead of
176 * scanning the string.
177 */
178 if (prec < 128)
179 nbytes = prec;
180 else {
181 nbytes = 0;
182 p = wcsarg;
183 memset(&mbs, 0, sizeof(mbs));
184 for (;;) {
185 clen = wcrtomb(buf, *p++, &mbs);
186 if (clen == 0 || clen == (size_t)-1 ||
187 nbytes + clen > (size_t)prec)
188 break;
189 nbytes += clen;
190 }
191 if (clen == (size_t)-1) {
192 errno = EILSEQ;
193 return (NULL);
194 }
195 }
196 }
197 if ((convbuf = malloc(nbytes + 1)) == NULL)
198 return (NULL);
200 /* Fill the output buffer. */
201 p = wcsarg;
202 memset(&mbs, 0, sizeof(mbs));
203 if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
204 nbytes, &mbs)) == (size_t)-1) {
205 free(convbuf);
206 errno = EILSEQ;
207 return (NULL);
208 }
209 convbuf[nbytes] = '\0';
210 return (convbuf);
211 }
212 #endif
214 #ifdef FLOATING_POINT
215 #include <float.h>
216 #include <locale.h>
217 #include <math.h>
218 #include "floatio.h"
220 #define DEFPREC 6
222 extern char *__dtoa(double, int, int, int *, int *, char **);
223 extern void __freedtoa(char *);
224 static int exponent(char *, int, int);
225 #endif /* FLOATING_POINT */
227 /*
228 * The size of the buffer we use as scratch space for integer
229 * conversions, among other things. Technically, we would need the
230 * most space for base 10 conversions with thousands' grouping
231 * characters between each pair of digits. 100 bytes is a
232 * conservative overestimate even for a 128-bit uintmax_t.
233 */
234 #define BUF 100
236 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
239 /*
240 * Macros for converting digits to letters and vice versa
241 */
242 #define to_digit(c) ((c) - '0')
243 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
244 #define to_char(n) ((n) + '0')
246 /*
247 * Flags used during conversion.
248 */
249 #define ALT 0x0001 /* alternate form */
250 #define LADJUST 0x0004 /* left adjustment */
251 #define LONGDBL 0x0008 /* long double */
252 #define LONGINT 0x0010 /* long integer */
253 #define LLONGINT 0x0020 /* long long integer */
254 #define SHORTINT 0x0040 /* short integer */
255 #define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */
256 #define FPT 0x0100 /* Floating point number */
257 #define PTRINT 0x0200 /* (unsigned) ptrdiff_t */
258 #define SIZEINT 0x0400 /* (signed) size_t */
259 #define CHARINT 0x0800 /* 8 bit integer */
260 #define MAXINT 0x1000 /* largest integer size (intmax_t) */
262 int
263 vfprintf(FILE *fp, const char *fmt0, __va_list ap)
264 {
265 int ret;
267 FLOCKFILE(fp);
268 ret = __vfprintf(fp, fmt0, ap);
269 FUNLOCKFILE(fp);
270 return (ret);
271 }
273 int
274 __vfprintf(FILE *fp, const char *fmt0, __va_list ap)
275 {
276 char *fmt; /* format string */
277 int ch; /* character from fmt */
278 int n, n2; /* handy integers (short term usage) */
279 char *cp; /* handy char pointer (short term usage) */
280 struct __siov *iovp; /* for PRINT macro */
281 int flags; /* flags as above */
282 int ret; /* return value accumulator */
283 int width; /* width from format (%8d), or 0 */
284 int prec; /* precision from format; <0 for N/A */
285 char sign; /* sign prefix (' ', '+', '-', or \0) */
286 wchar_t wc;
287 mbstate_t ps;
288 #ifdef FLOATING_POINT
289 /*
290 * We can decompose the printed representation of floating
291 * point numbers into several parts, some of which may be empty:
292 *
293 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
294 * A B ---C--- D E F
295 *
296 * A: 'sign' holds this value if present; '\0' otherwise
297 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
298 * C: cp points to the string MMMNNN. Leading and trailing
299 * zeros are not in the string and must be added.
300 * D: expchar holds this character; '\0' if no exponent, e.g. %f
301 * F: at least two digits for decimal, at least one digit for hex
302 */
303 char *decimal_point = NULL;
304 int signflag; /* true if float is negative */
305 union { /* floating point arguments %[aAeEfFgG] */
306 double dbl;
307 long double ldbl;
308 } fparg;
309 int expt; /* integer value of exponent */
310 char expchar; /* exponent character: [eEpP\0] */
311 char *dtoaend; /* pointer to end of converted digits */
312 int expsize; /* character count for expstr */
313 int lead; /* sig figs before decimal or group sep */
314 int ndig; /* actual number of digits returned by dtoa */
315 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
316 char *dtoaresult = NULL;
317 #endif
319 uintmax_t _umax; /* integer arguments %[diouxX] */
320 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
321 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
322 int realsz; /* field size expanded by dprec */
323 int size; /* size of converted field or string */
324 const char *xdigs; /* digits for %[xX] conversion */
325 #define NIOV 8
326 struct __suio uio; /* output information: summary */
327 struct __siov iov[NIOV];/* ... and individual io vectors */
328 char buf[BUF]; /* buffer with space for digits of uintmax_t */
329 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
330 union arg *argtable; /* args, built due to positional arg */
331 union arg statargtable[STATIC_ARG_TBL_SIZE];
332 size_t argtablesiz;
333 int nextarg; /* 1-based argument index */
334 va_list orgap; /* original argument pointer */
335 #ifdef PRINTF_WIDE_CHAR
336 char *convbuf; /* buffer for wide to multi-byte conversion */
337 #endif
339 /*
340 * Choose PADSIZE to trade efficiency vs. size. If larger printf
341 * fields occur frequently, increase PADSIZE and make the initialisers
342 * below longer.
343 */
344 #define PADSIZE 16 /* pad chunk size */
345 static char blanks[PADSIZE] =
346 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
347 static char zeroes[PADSIZE] =
348 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
350 static const char xdigs_lower[16] = "0123456789abcdef";
351 static const char xdigs_upper[16] = "0123456789ABCDEF";
353 /*
354 * BEWARE, these `goto error' on error, and PAD uses `n'.
355 */
356 #define PRINT(ptr, len) do { \
357 iovp->iov_base = (ptr); \
358 iovp->iov_len = (len); \
359 uio.uio_resid += (len); \
360 iovp++; \
361 if (++uio.uio_iovcnt >= NIOV) { \
362 if (__sprint(fp, &uio)) \
363 goto error; \
364 iovp = iov; \
365 } \
366 } while (0)
367 #define PAD(howmany, with) do { \
368 if ((n = (howmany)) > 0) { \
369 while (n > PADSIZE) { \
370 PRINT(with, PADSIZE); \
371 n -= PADSIZE; \
372 } \
373 PRINT(with, n); \
374 } \
375 } while (0)
376 #define PRINTANDPAD(p, ep, len, with) do { \
377 n2 = (ep) - (p); \
378 if (n2 > (len)) \
379 n2 = (len); \
380 if (n2 > 0) \
381 PRINT((p), n2); \
382 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
383 } while(0)
384 #define FLUSH() do { \
385 if (uio.uio_resid && __sprint(fp, &uio)) \
386 goto error; \
387 uio.uio_iovcnt = 0; \
388 iovp = iov; \
389 } while (0)
391 /*
392 * To extend shorts properly, we need both signed and unsigned
393 * argument extraction methods.
394 */
395 #define SARG() \
396 ((intmax_t)(flags&MAXINT ? GETARG(intmax_t) : \
397 flags&LLONGINT ? GETARG(long long) : \
398 flags&LONGINT ? GETARG(long) : \
399 flags&PTRINT ? GETARG(ptrdiff_t) : \
400 flags&SIZEINT ? GETARG(ssize_t) : \
401 flags&SHORTINT ? (short)GETARG(int) : \
402 flags&CHARINT ? (__signed char)GETARG(int) : \
403 GETARG(int)))
404 #define UARG() \
405 ((uintmax_t)(flags&MAXINT ? GETARG(uintmax_t) : \
406 flags&LLONGINT ? GETARG(unsigned long long) : \
407 flags&LONGINT ? GETARG(unsigned long) : \
408 flags&PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
409 flags&SIZEINT ? GETARG(size_t) : \
410 flags&SHORTINT ? (unsigned short)GETARG(int) : \
411 flags&CHARINT ? (unsigned char)GETARG(int) : \
412 GETARG(unsigned int)))
414 /*
415 * Append a digit to a value and check for overflow.
416 */
417 #define APPEND_DIGIT(val, dig) do { \
418 if ((val) > INT_MAX / 10) \
419 goto overflow; \
420 (val) *= 10; \
421 if ((val) > INT_MAX - to_digit((dig))) \
422 goto overflow; \
423 (val) += to_digit((dig)); \
424 } while (0)
426 /*
427 * Get * arguments, including the form *nn$. Preserve the nextarg
428 * that the argument can be gotten once the type is determined.
429 */
430 #define GETASTER(val) \
431 n2 = 0; \
432 cp = fmt; \
433 while (is_digit(*cp)) { \
434 APPEND_DIGIT(n2, *cp); \
435 cp++; \
436 } \
437 if (*cp == '$') { \
438 int hold = nextarg; \
439 if (argtable == NULL) { \
440 argtable = statargtable; \
441 __find_arguments(fmt0, orgap, &argtable, &argtablesiz); \
442 } \
443 nextarg = n2; \
444 val = GETARG(int); \
445 nextarg = hold; \
446 fmt = ++cp; \
447 } else { \
448 val = GETARG(int); \
449 }
451 /*
452 * Get the argument indexed by nextarg. If the argument table is
453 * built, use it to get the argument. If its not, get the next
454 * argument (and arguments must be gotten sequentially).
455 */
456 #define GETARG(type) \
457 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
458 (nextarg++, va_arg(ap, type)))
460 _SET_ORIENTATION(fp, -1);
461 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
462 if (cantwrite(fp)) {
463 errno = EBADF;
464 return (EOF);
465 }
467 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
468 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
469 fp->_file >= 0)
470 return (__sbprintf(fp, fmt0, ap));
472 fmt = (char *)fmt0;
473 argtable = NULL;
474 nextarg = 1;
475 va_copy(orgap, ap);
476 uio.uio_iov = iovp = iov;
477 uio.uio_resid = 0;
478 uio.uio_iovcnt = 0;
479 ret = 0;
480 #ifdef PRINTF_WIDE_CHAR
481 convbuf = NULL;
482 #endif
484 memset(&ps, 0, sizeof(ps));
485 /*
486 * Scan the format for conversions (`%' character).
487 */
488 for (;;) {
489 cp = fmt;
490 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
491 fmt += n;
492 if (wc == '%') {
493 fmt--;
494 break;
495 }
496 }
497 if (fmt != cp) {
498 ptrdiff_t m = fmt - cp;
499 if (m < 0 || m > INT_MAX - ret)
500 goto overflow;
501 PRINT(cp, m);
502 ret += m;
503 }
504 if (n <= 0)
505 goto done;
506 fmt++; /* skip over '%' */
508 flags = 0;
509 dprec = 0;
510 width = 0;
511 prec = -1;
512 sign = '\0';
513 ox[1] = '\0';
515 rflag: ch = *fmt++;
516 reswitch: switch (ch) {
517 case ' ':
518 /*
519 * ``If the space and + flags both appear, the space
520 * flag will be ignored.''
521 * -- ANSI X3J11
522 */
523 if (!sign)
524 sign = ' ';
525 goto rflag;
526 case '#':
527 flags |= ALT;
528 goto rflag;
529 case '\'':
530 /* grouping not implemented */
531 goto rflag;
532 case '*':
533 /*
534 * ``A negative field width argument is taken as a
535 * - flag followed by a positive field width.''
536 * -- ANSI X3J11
537 * They don't exclude field widths read from args.
538 */
539 GETASTER(width);
540 if (width >= 0)
541 goto rflag;
542 if (width == INT_MIN)
543 goto overflow;
544 width = -width;
545 /* FALLTHROUGH */
546 case '-':
547 flags |= LADJUST;
548 goto rflag;
549 case '+':
550 sign = '+';
551 goto rflag;
552 case '.':
553 if ((ch = *fmt++) == '*') {
554 GETASTER(n);
555 prec = n < 0 ? -1 : n;
556 goto rflag;
557 }
558 n = 0;
559 while (is_digit(ch)) {
560 APPEND_DIGIT(n, ch);
561 ch = *fmt++;
562 }
563 if (ch == '$') {
564 nextarg = n;
565 if (argtable == NULL) {
566 argtable = statargtable;
567 __find_arguments(fmt0, orgap,
568 &argtable, &argtablesiz);
569 }
570 goto rflag;
571 }
572 prec = n;
573 goto reswitch;
574 case '0':
575 /*
576 * ``Note that 0 is taken as a flag, not as the
577 * beginning of a field width.''
578 * -- ANSI X3J11
579 */
580 flags |= ZEROPAD;
581 goto rflag;
582 case '1': case '2': case '3': case '4':
583 case '5': case '6': case '7': case '8': case '9':
584 n = 0;
585 do {
586 APPEND_DIGIT(n, ch);
587 ch = *fmt++;
588 } while (is_digit(ch));
589 if (ch == '$') {
590 nextarg = n;
591 if (argtable == NULL) {
592 argtable = statargtable;
593 __find_arguments(fmt0, orgap,
594 &argtable, &argtablesiz);
595 }
596 goto rflag;
597 }
598 width = n;
599 goto reswitch;
600 #ifdef FLOATING_POINT
601 case 'L':
602 flags |= LONGDBL;
603 goto rflag;
604 #endif
605 case 'h':
606 if (*fmt == 'h') {
607 fmt++;
608 flags |= CHARINT;
609 } else {
610 flags |= SHORTINT;
611 }
612 goto rflag;
613 case 'j':
614 flags |= MAXINT;
615 goto rflag;
616 case 'l':
617 if (*fmt == 'l') {
618 fmt++;
619 flags |= LLONGINT;
620 } else {
621 flags |= LONGINT;
622 }
623 goto rflag;
624 case 'q':
625 flags |= LLONGINT;
626 goto rflag;
627 case 't':
628 flags |= PTRINT;
629 goto rflag;
630 case 'z':
631 flags |= SIZEINT;
632 goto rflag;
633 case 'c':
634 #ifdef PRINTF_WIDE_CHAR
635 if (flags & LONGINT) {
636 mbstate_t mbs;
637 size_t mbseqlen;
639 memset(&mbs, 0, sizeof(mbs));
640 mbseqlen = wcrtomb(buf,
641 (wchar_t)GETARG(wint_t), &mbs);
642 if (mbseqlen == (size_t)-1) {
643 fp->_flags |= __SERR;
644 errno = EILSEQ;
645 goto error;
646 }
647 cp = buf;
648 size = (int)mbseqlen;
649 } else {
650 #endif
651 *(cp = buf) = GETARG(int);
652 size = 1;
653 #ifdef PRINTF_WIDE_CHAR
654 }
655 #endif
656 sign = '\0';
657 break;
658 case 'D':
659 flags |= LONGINT;
660 /*FALLTHROUGH*/
661 case 'd':
662 case 'i':
663 _umax = SARG();
664 if ((intmax_t)_umax < 0) {
665 _umax = -_umax;
666 sign = '-';
667 }
668 base = DEC;
669 goto number;
670 #ifdef FLOATING_POINT
671 case 'a':
672 case 'A':
673 if (ch == 'a') {
674 ox[1] = 'x';
675 xdigs = xdigs_lower;
676 expchar = 'p';
677 } else {
678 ox[1] = 'X';
679 xdigs = xdigs_upper;
680 expchar = 'P';
681 }
682 if (prec >= 0)
683 prec++;
684 if (dtoaresult)
685 __freedtoa(dtoaresult);
686 if (flags & LONGDBL) {
687 fparg.ldbl = GETARG(long double);
688 dtoaresult = cp =
689 __hldtoa(fparg.ldbl, xdigs, prec,
690 &expt, &signflag, &dtoaend);
691 if (dtoaresult == NULL) {
692 errno = ENOMEM;
693 goto error;
694 }
695 } else {
696 fparg.dbl = GETARG(double);
697 dtoaresult = cp =
698 __hdtoa(fparg.dbl, xdigs, prec,
699 &expt, &signflag, &dtoaend);
700 if (dtoaresult == NULL) {
701 errno = ENOMEM;
702 goto error;
703 }
704 }
705 if (prec < 0)
706 prec = dtoaend - cp;
707 if (expt == INT_MAX)
708 ox[1] = '\0';
709 goto fp_common;
710 case 'e':
711 case 'E':
712 expchar = ch;
713 if (prec < 0) /* account for digit before decpt */
714 prec = DEFPREC + 1;
715 else
716 prec++;
717 goto fp_begin;
718 case 'f':
719 case 'F':
720 expchar = '\0';
721 goto fp_begin;
722 case 'g':
723 case 'G':
724 expchar = ch - ('g' - 'e');
725 if (prec == 0)
726 prec = 1;
727 fp_begin:
728 if (prec < 0)
729 prec = DEFPREC;
730 if (dtoaresult)
731 __freedtoa(dtoaresult);
732 if (flags & LONGDBL) {
733 fparg.ldbl = GETARG(long double);
734 dtoaresult = cp =
735 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
736 &expt, &signflag, &dtoaend);
737 if (dtoaresult == NULL) {
738 errno = ENOMEM;
739 goto error;
740 }
741 } else {
742 fparg.dbl = GETARG(double);
743 dtoaresult = cp =
744 __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
745 &expt, &signflag, &dtoaend);
746 if (dtoaresult == NULL) {
747 errno = ENOMEM;
748 goto error;
749 }
750 if (expt == 9999)
751 expt = INT_MAX;
752 }
753 fp_common:
754 if (signflag)
755 sign = '-';
756 if (expt == INT_MAX) { /* inf or nan */
757 if (*cp == 'N') {
758 cp = (ch >= 'a') ? "nan" : "NAN";
759 sign = '\0';
760 } else
761 cp = (ch >= 'a') ? "inf" : "INF";
762 size = 3;
763 flags &= ~ZEROPAD;
764 break;
765 }
766 flags |= FPT;
767 ndig = dtoaend - cp;
768 if (ch == 'g' || ch == 'G') {
769 if (expt > -4 && expt <= prec) {
770 /* Make %[gG] smell like %[fF] */
771 expchar = '\0';
772 if (flags & ALT)
773 prec -= expt;
774 else
775 prec = ndig - expt;
776 if (prec < 0)
777 prec = 0;
778 } else {
779 /*
780 * Make %[gG] smell like %[eE], but
781 * trim trailing zeroes if no # flag.
782 */
783 if (!(flags & ALT))
784 prec = ndig;
785 }
786 }
787 if (expchar) {
788 expsize = exponent(expstr, expt - 1, expchar);
789 size = expsize + prec;
790 if (prec > 1 || flags & ALT)
791 ++size;
792 } else {
793 /* space for digits before decimal point */
794 if (expt > 0)
795 size = expt;
796 else /* "0" */
797 size = 1;
798 /* space for decimal pt and following digits */
799 if (prec || flags & ALT)
800 size += prec + 1;
801 lead = expt;
802 }
803 break;
804 #endif /* FLOATING_POINT */
805 case 'n':
806 if (flags & LLONGINT)
807 *GETARG(long long *) = ret;
808 else if (flags & LONGINT)
809 *GETARG(long *) = ret;
810 else if (flags & SHORTINT)
811 *GETARG(short *) = ret;
812 else if (flags & CHARINT)
813 *GETARG(__signed char *) = ret;
814 else if (flags & PTRINT)
815 *GETARG(ptrdiff_t *) = ret;
816 else if (flags & SIZEINT)
817 *GETARG(ssize_t *) = ret;
818 else if (flags & MAXINT)
819 *GETARG(intmax_t *) = ret;
820 else
821 *GETARG(int *) = ret;
822 continue; /* no output */
823 case 'O':
824 flags |= LONGINT;
825 /*FALLTHROUGH*/
826 case 'o':
827 _umax = UARG();
828 base = OCT;
829 goto nosign;
830 case 'p':
831 /*
832 * ``The argument shall be a pointer to void. The
833 * value of the pointer is converted to a sequence
834 * of printable characters, in an implementation-
835 * defined manner.''
836 * -- ANSI X3J11
837 */
838 /* NOSTRICT */
839 _umax = (u_long)GETARG(void *);
840 base = HEX;
841 xdigs = xdigs_lower;
842 ox[1] = 'x';
843 goto nosign;
844 case 's':
845 #ifdef PRINTF_WIDE_CHAR
846 if (flags & LONGINT) {
847 wchar_t *wcp;
849 if (convbuf != NULL) {
850 free(convbuf);
851 convbuf = NULL;
852 }
853 if ((wcp = GETARG(wchar_t *)) == NULL) {
854 cp = "(null)";
855 } else {
856 convbuf = __wcsconv(wcp, prec);
857 if (convbuf == NULL) {
858 fp->_flags = __SERR;
859 goto error;
860 }
861 cp = convbuf;
862 }
863 } else
864 #endif /* PRINTF_WIDE_CHAR */
865 if ((cp = GETARG(char *)) == NULL)
866 cp = "(null)";
867 if (prec >= 0) {
868 /*
869 * can't use strlen; can only look for the
870 * NUL in the first `prec' characters, and
871 * strlen() will go further.
872 */
873 char *p = memchr(cp, 0, prec);
875 size = p ? (p - cp) : prec;
876 } else {
877 size_t len;
879 if ((len = strlen(cp)) > INT_MAX)
880 goto overflow;
881 size = (int)len;
882 }
883 sign = '\0';
884 break;
885 case 'U':
886 flags |= LONGINT;
887 /*FALLTHROUGH*/
888 case 'u':
889 _umax = UARG();
890 base = DEC;
891 goto nosign;
892 case 'X':
893 xdigs = xdigs_upper;
894 goto hex;
895 case 'x':
896 xdigs = xdigs_lower;
897 hex: _umax = UARG();
898 base = HEX;
899 /* leading 0x/X only if non-zero */
900 if (flags & ALT && _umax != 0)
901 ox[1] = ch;
903 /* unsigned conversions */
904 nosign: sign = '\0';
905 /*
906 * ``... diouXx conversions ... if a precision is
907 * specified, the 0 flag will be ignored.''
908 * -- ANSI X3J11
909 */
910 number: if ((dprec = prec) >= 0)
911 flags &= ~ZEROPAD;
913 /*
914 * ``The result of converting a zero value with an
915 * explicit precision of zero is no characters.''
916 * -- ANSI X3J11
917 */
918 cp = buf + BUF;
919 if (_umax != 0 || prec != 0) {
920 /*
921 * Unsigned mod is hard, and unsigned mod
922 * by a constant is easier than that by
923 * a variable; hence this switch.
924 */
925 switch (base) {
926 case OCT:
927 do {
928 *--cp = to_char(_umax & 7);
929 _umax >>= 3;
930 } while (_umax);
931 /* handle octal leading 0 */
932 if (flags & ALT && *cp != '0')
933 *--cp = '0';
934 break;
936 case DEC:
937 /* many numbers are 1 digit */
938 while (_umax >= 10) {
939 *--cp = to_char(_umax % 10);
940 _umax /= 10;
941 }
942 *--cp = to_char(_umax);
943 break;
945 case HEX:
946 do {
947 *--cp = xdigs[_umax & 15];
948 _umax >>= 4;
949 } while (_umax);
950 break;
952 default:
953 cp = "bug in vfprintf: bad base";
954 size = strlen(cp);
955 goto skipsize;
956 }
957 }
958 size = buf + BUF - cp;
959 if (size > BUF) /* should never happen */
960 abort();
961 skipsize:
962 break;
963 default: /* "%?" prints ?, unless ? is NUL */
964 if (ch == '\0')
965 goto done;
966 /* pretend it was %c with argument ch */
967 cp = buf;
968 *cp = ch;
969 size = 1;
970 sign = '\0';
971 break;
972 }
974 /*
975 * All reasonable formats wind up here. At this point, `cp'
976 * points to a string which (if not flags&LADJUST) should be
977 * padded out to `width' places. If flags&ZEROPAD, it should
978 * first be prefixed by any sign or other prefix; otherwise,
979 * it should be blank padded before the prefix is emitted.
980 * After any left-hand padding and prefixing, emit zeroes
981 * required by a decimal %[diouxX] precision, then print the
982 * string proper, then emit zeroes required by any leftover
983 * floating precision; finally, if LADJUST, pad with blanks.
984 *
985 * Compute actual size, so we know how much to pad.
986 * size excludes decimal prec; realsz includes it.
987 */
988 realsz = dprec > size ? dprec : size;
989 if (sign)
990 realsz++;
991 if (ox[1])
992 realsz+= 2;
994 /* right-adjusting blank padding */
995 if ((flags & (LADJUST|ZEROPAD)) == 0)
996 PAD(width - realsz, blanks);
998 /* prefix */
999 if (sign)
1000 PRINT(&sign, 1);
1001 if (ox[1]) { /* ox[1] is either x, X, or \0 */
1002 ox[0] = '0';
1003 PRINT(ox, 2);
1004 }
1006 /* right-adjusting zero padding */
1007 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1008 PAD(width - realsz, zeroes);
1010 /* leading zeroes from decimal precision */
1011 PAD(dprec - size, zeroes);
1013 /* the string or number proper */
1014 #ifdef FLOATING_POINT
1015 if ((flags & FPT) == 0) {
1016 PRINT(cp, size);
1017 } else { /* glue together f_p fragments */
1018 if (decimal_point == NULL)
1019 decimal_point = nl_langinfo(RADIXCHAR);
1020 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1021 if (expt <= 0) {
1022 PRINT(zeroes, 1);
1023 if (prec || flags & ALT)
1024 PRINT(decimal_point, 1);
1025 PAD(-expt, zeroes);
1026 /* already handled initial 0's */
1027 prec += expt;
1028 } else {
1029 PRINTANDPAD(cp, dtoaend, lead, zeroes);
1030 cp += lead;
1031 if (prec || flags & ALT)
1032 PRINT(decimal_point, 1);
1033 }
1034 PRINTANDPAD(cp, dtoaend, prec, zeroes);
1035 } else { /* %[eE] or sufficiently long %[gG] */
1036 if (prec > 1 || flags & ALT) {
1037 buf[0] = *cp++;
1038 buf[1] = *decimal_point;
1039 PRINT(buf, 2);
1040 PRINT(cp, ndig-1);
1041 PAD(prec - ndig, zeroes);
1042 } else { /* XeYYY */
1043 PRINT(cp, 1);
1044 }
1045 PRINT(expstr, expsize);
1046 }
1047 }
1048 #else
1049 PRINT(cp, size);
1050 #endif
1051 /* left-adjusting padding (always blank) */
1052 if (flags & LADJUST)
1053 PAD(width - realsz, blanks);
1055 /* finally, adjust ret */
1056 if (width < realsz)
1057 width = realsz;
1058 if (width > INT_MAX - ret)
1059 goto overflow;
1060 ret += width;
1062 FLUSH(); /* copy out the I/O vectors */
1063 }
1064 done:
1065 FLUSH();
1066 error:
1067 va_end(orgap);
1068 if (__sferror(fp))
1069 ret = -1;
1070 goto finish;
1072 overflow:
1073 errno = ENOMEM;
1074 ret = -1;
1076 finish:
1077 #ifdef PRINTF_WIDE_CHAR
1078 if (convbuf)
1079 free(convbuf);
1080 #endif
1081 #ifdef FLOATING_POINT
1082 if (dtoaresult)
1083 __freedtoa(dtoaresult);
1084 #endif
1085 if (argtable != NULL && argtable != statargtable) {
1086 munmap(argtable, argtablesiz);
1087 argtable = NULL;
1088 }
1089 return (ret);
1090 }
1092 /*
1093 * Type ids for argument type table.
1094 */
1095 #define T_UNUSED 0
1096 #define T_SHORT 1
1097 #define T_U_SHORT 2
1098 #define TP_SHORT 3
1099 #define T_INT 4
1100 #define T_U_INT 5
1101 #define TP_INT 6
1102 #define T_LONG 7
1103 #define T_U_LONG 8
1104 #define TP_LONG 9
1105 #define T_LLONG 10
1106 #define T_U_LLONG 11
1107 #define TP_LLONG 12
1108 #define T_DOUBLE 13
1109 #define T_LONG_DOUBLE 14
1110 #define TP_CHAR 15
1111 #define TP_VOID 16
1112 #define T_PTRINT 17
1113 #define TP_PTRINT 18
1114 #define T_SIZEINT 19
1115 #define T_SSIZEINT 20
1116 #define TP_SSIZEINT 21
1117 #define T_MAXINT 22
1118 #define T_MAXUINT 23
1119 #define TP_MAXINT 24
1120 #define T_CHAR 25
1121 #define T_U_CHAR 26
1122 #define T_WINT 27
1123 #define TP_WCHAR 28
1125 /*
1126 * Find all arguments when a positional parameter is encountered. Returns a
1127 * table, indexed by argument number, of pointers to each arguments. The
1128 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1129 * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
1130 * used since we are attempting to make snprintf thread safe, and alloca is
1131 * problematic since we have nested functions..)
1132 */
1133 static int
1134 __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
1135 size_t *argtablesiz)
1136 {
1137 char *fmt; /* format string */
1138 int ch; /* character from fmt */
1139 int n, n2; /* handy integer (short term usage) */
1140 char *cp; /* handy char pointer (short term usage) */
1141 int flags; /* flags as above */
1142 unsigned char *typetable; /* table of types */
1143 unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
1144 int tablesize; /* current size of type table */
1145 int tablemax; /* largest used index in table */
1146 int nextarg; /* 1-based argument index */
1147 int ret = 0; /* return value */
1148 wchar_t wc;
1149 mbstate_t ps;
1151 /*
1152 * Add an argument type to the table, expanding if necessary.
1153 */
1154 #define ADDTYPE(type) \
1155 ((nextarg >= tablesize) ? \
1156 __grow_type_table(&typetable, &tablesize) : 0, \
1157 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1158 typetable[nextarg++] = type)
1160 #define ADDSARG() \
1161 ((flags&MAXINT) ? ADDTYPE(T_MAXINT) : \
1162 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
1163 ((flags&SIZEINT) ? ADDTYPE(T_SSIZEINT) : \
1164 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1165 ((flags&LONGINT) ? ADDTYPE(T_LONG) : \
1166 ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : \
1167 ((flags&CHARINT) ? ADDTYPE(T_CHAR) : ADDTYPE(T_INT))))))))
1169 #define ADDUARG() \
1170 ((flags&MAXINT) ? ADDTYPE(T_MAXUINT) : \
1171 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
1172 ((flags&SIZEINT) ? ADDTYPE(T_SIZEINT) : \
1173 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1174 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \
1175 ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : \
1176 ((flags&CHARINT) ? ADDTYPE(T_U_CHAR) : ADDTYPE(T_U_INT))))))))
1178 /*
1179 * Add * arguments to the type array.
1180 */
1181 #define ADDASTER() \
1182 n2 = 0; \
1183 cp = fmt; \
1184 while (is_digit(*cp)) { \
1185 APPEND_DIGIT(n2, *cp); \
1186 cp++; \
1187 } \
1188 if (*cp == '$') { \
1189 int hold = nextarg; \
1190 nextarg = n2; \
1191 ADDTYPE(T_INT); \
1192 nextarg = hold; \
1193 fmt = ++cp; \
1194 } else { \
1195 ADDTYPE(T_INT); \
1196 }
1197 fmt = (char *)fmt0;
1198 typetable = stattypetable;
1199 tablesize = STATIC_ARG_TBL_SIZE;
1200 tablemax = 0;
1201 nextarg = 1;
1202 memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1203 memset(&ps, 0, sizeof(ps));
1205 /*
1206 * Scan the format for conversions (`%' character).
1207 */
1208 for (;;) {
1209 cp = fmt;
1210 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
1211 fmt += n;
1212 if (wc == '%') {
1213 fmt--;
1214 break;
1215 }
1216 }
1217 if (n <= 0)
1218 goto done;
1219 fmt++; /* skip over '%' */
1221 flags = 0;
1223 rflag: ch = *fmt++;
1224 reswitch: switch (ch) {
1225 case ' ':
1226 case '#':
1227 case '\'':
1228 goto rflag;
1229 case '*':
1230 ADDASTER();
1231 goto rflag;
1232 case '-':
1233 case '+':
1234 goto rflag;
1235 case '.':
1236 if ((ch = *fmt++) == '*') {
1237 ADDASTER();
1238 goto rflag;
1239 }
1240 while (is_digit(ch)) {
1241 ch = *fmt++;
1242 }
1243 goto reswitch;
1244 case '0':
1245 goto rflag;
1246 case '1': case '2': case '3': case '4':
1247 case '5': case '6': case '7': case '8': case '9':
1248 n = 0;
1249 do {
1250 APPEND_DIGIT(n ,ch);
1251 ch = *fmt++;
1252 } while (is_digit(ch));
1253 if (ch == '$') {
1254 nextarg = n;
1255 goto rflag;
1256 }
1257 goto reswitch;
1258 #ifdef FLOATING_POINT
1259 case 'L':
1260 flags |= LONGDBL;
1261 goto rflag;
1262 #endif
1263 case 'h':
1264 if (*fmt == 'h') {
1265 fmt++;
1266 flags |= CHARINT;
1267 } else {
1268 flags |= SHORTINT;
1269 }
1270 goto rflag;
1271 case 'j':
1272 flags |= MAXINT;
1273 goto rflag;
1274 case 'l':
1275 if (*fmt == 'l') {
1276 fmt++;
1277 flags |= LLONGINT;
1278 } else {
1279 flags |= LONGINT;
1280 }
1281 goto rflag;
1282 case 'q':
1283 flags |= LLONGINT;
1284 goto rflag;
1285 case 't':
1286 flags |= PTRINT;
1287 goto rflag;
1288 case 'z':
1289 flags |= SIZEINT;
1290 goto rflag;
1291 case 'c':
1292 #ifdef PRINTF_WIDE_CHAR
1293 if (flags & LONGINT)
1294 ADDTYPE(T_WINT);
1295 else
1296 #endif
1297 ADDTYPE(T_INT);
1298 break;
1299 case 'D':
1300 flags |= LONGINT;
1301 /*FALLTHROUGH*/
1302 case 'd':
1303 case 'i':
1304 ADDSARG();
1305 break;
1306 #ifdef FLOATING_POINT
1307 case 'a':
1308 case 'A':
1309 case 'e':
1310 case 'E':
1311 case 'f':
1312 case 'F':
1313 case 'g':
1314 case 'G':
1315 if (flags & LONGDBL)
1316 ADDTYPE(T_LONG_DOUBLE);
1317 else
1318 ADDTYPE(T_DOUBLE);
1319 break;
1320 #endif /* FLOATING_POINT */
1321 case 'n':
1322 if (flags & LLONGINT)
1323 ADDTYPE(TP_LLONG);
1324 else if (flags & LONGINT)
1325 ADDTYPE(TP_LONG);
1326 else if (flags & SHORTINT)
1327 ADDTYPE(TP_SHORT);
1328 else if (flags & PTRINT)
1329 ADDTYPE(TP_PTRINT);
1330 else if (flags & SIZEINT)
1331 ADDTYPE(TP_SSIZEINT);
1332 else if (flags & MAXINT)
1333 ADDTYPE(TP_MAXINT);
1334 else
1335 ADDTYPE(TP_INT);
1336 continue; /* no output */
1337 case 'O':
1338 flags |= LONGINT;
1339 /*FALLTHROUGH*/
1340 case 'o':
1341 ADDUARG();
1342 break;
1343 case 'p':
1344 ADDTYPE(TP_VOID);
1345 break;
1346 case 's':
1347 #ifdef PRINTF_WIDE_CHAR
1348 if (flags & LONGINT)
1349 ADDTYPE(TP_WCHAR);
1350 else
1351 #endif
1352 ADDTYPE(TP_CHAR);
1353 break;
1354 case 'U':
1355 flags |= LONGINT;
1356 /*FALLTHROUGH*/
1357 case 'u':
1358 case 'X':
1359 case 'x':
1360 ADDUARG();
1361 break;
1362 default: /* "%?" prints ?, unless ? is NUL */
1363 if (ch == '\0')
1364 goto done;
1365 break;
1366 }
1367 }
1368 done:
1369 /*
1370 * Build the argument table.
1371 */
1372 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1373 *argtablesiz = sizeof(union arg) * (tablemax + 1);
1374 *argtable = mmap(NULL, *argtablesiz,
1375 PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0);
1376 if (*argtable == MAP_FAILED)
1377 return (-1);
1378 }
1380 #if 0
1381 /* XXX is this required? */
1382 (*argtable)[0].intarg = 0;
1383 #endif
1384 for (n = 1; n <= tablemax; n++) {
1385 switch (typetable[n]) {
1386 case T_UNUSED:
1387 case T_CHAR:
1388 case T_U_CHAR:
1389 case T_SHORT:
1390 case T_U_SHORT:
1391 case T_INT:
1392 (*argtable)[n].intarg = va_arg(ap, int);
1393 break;
1394 case TP_SHORT:
1395 (*argtable)[n].pshortarg = va_arg(ap, short *);
1396 break;
1397 case T_U_INT:
1398 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1399 break;
1400 case TP_INT:
1401 (*argtable)[n].pintarg = va_arg(ap, int *);
1402 break;
1403 case T_LONG:
1404 (*argtable)[n].longarg = va_arg(ap, long);
1405 break;
1406 case T_U_LONG:
1407 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1408 break;
1409 case TP_LONG:
1410 (*argtable)[n].plongarg = va_arg(ap, long *);
1411 break;
1412 case T_LLONG:
1413 (*argtable)[n].longlongarg = va_arg(ap, long long);
1414 break;
1415 case T_U_LLONG:
1416 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1417 break;
1418 case TP_LLONG:
1419 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
1420 break;
1421 #ifdef FLOATING_POINT
1422 case T_DOUBLE:
1423 (*argtable)[n].doublearg = va_arg(ap, double);
1424 break;
1425 case T_LONG_DOUBLE:
1426 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1427 break;
1428 #endif
1429 case TP_CHAR:
1430 (*argtable)[n].pchararg = va_arg(ap, char *);
1431 break;
1432 case TP_VOID:
1433 (*argtable)[n].pvoidarg = va_arg(ap, void *);
1434 break;
1435 case T_PTRINT:
1436 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1437 break;
1438 case TP_PTRINT:
1439 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
1440 break;
1441 case T_SIZEINT:
1442 (*argtable)[n].sizearg = va_arg(ap, size_t);
1443 break;
1444 case T_SSIZEINT:
1445 (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
1446 break;
1447 case TP_SSIZEINT:
1448 (*argtable)[n].pssizearg = va_arg(ap, ssize_t *);
1449 break;
1450 case T_MAXINT:
1451 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1452 break;
1453 case T_MAXUINT:
1454 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1455 break;
1456 case TP_MAXINT:
1457 (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t *);
1458 break;
1459 #ifdef PRINTF_WIDE_CHAR
1460 case T_WINT:
1461 (*argtable)[n].wintarg = va_arg(ap, wint_t);
1462 break;
1463 case TP_WCHAR:
1464 (*argtable)[n].pwchararg = va_arg(ap, wchar_t *);
1465 break;
1466 #endif
1467 }
1468 }
1469 goto finish;
1471 overflow:
1472 errno = ENOMEM;
1473 ret = -1;
1475 finish:
1476 if (typetable != NULL && typetable != stattypetable) {
1477 munmap(typetable, *argtablesiz);
1478 typetable = NULL;
1479 }
1480 return (ret);
1481 }
1483 /*
1484 * Increase the size of the type table.
1485 */
1486 static int
1487 __grow_type_table(unsigned char **typetable, int *tablesize)
1488 {
1489 unsigned char *oldtable = *typetable;
1490 int newsize = *tablesize * 2;
1492 if (newsize < getpagesize())
1493 newsize = getpagesize();
1495 if (*tablesize == STATIC_ARG_TBL_SIZE) {
1496 *typetable = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
1497 MAP_ANON|MAP_PRIVATE, -1, 0);
1498 if (*typetable == MAP_FAILED)
1499 return (-1);
1500 bcopy(oldtable, *typetable, *tablesize);
1501 } else {
1502 unsigned char *new = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
1503 MAP_ANON|MAP_PRIVATE, -1, 0);
1504 if (new == MAP_FAILED)
1505 return (-1);
1506 memmove(new, *typetable, *tablesize);
1507 munmap(*typetable, *tablesize);
1508 *typetable = new;
1509 }
1510 memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
1512 *tablesize = newsize;
1513 return (0);
1514 }
1517 #ifdef FLOATING_POINT
1518 static int
1519 exponent(char *p0, int exp, int fmtch)
1520 {
1521 char *p, *t;
1522 char expbuf[MAXEXPDIG];
1524 p = p0;
1525 *p++ = fmtch;
1526 if (exp < 0) {
1527 exp = -exp;
1528 *p++ = '-';
1529 } else
1530 *p++ = '+';
1531 t = expbuf + MAXEXPDIG;
1532 if (exp > 9) {
1533 do {
1534 *--t = to_char(exp % 10);
1535 } while ((exp /= 10) > 9);
1536 *--t = to_char(exp);
1537 for (; t < expbuf + MAXEXPDIG; *p++ = *t++)
1538 /* nothing */;
1539 } else {
1540 /*
1541 * Exponents for decimal floating point conversions
1542 * (%[eEgG]) must be at least two characters long,
1543 * whereas exponents for hexadecimal conversions can
1544 * be only one character long.
1545 */
1546 if (fmtch == 'e' || fmtch == 'E')
1547 *p++ = '0';
1548 *p++ = to_char(exp);
1549 }
1550 return (p - p0);
1551 }
1552 #endif /* FLOATING_POINT */