1 /* $OpenBSD: vfprintf.c,v 1.65 2014/03/19 05:17:01 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"
219 #include "gdtoa.h"
221 #define DEFPREC 6
223 static int exponent(char *, int, int);
224 #endif /* FLOATING_POINT */
226 /*
227 * The size of the buffer we use as scratch space for integer
228 * conversions, among other things. Technically, we would need the
229 * most space for base 10 conversions with thousands' grouping
230 * characters between each pair of digits. 100 bytes is a
231 * conservative overestimate even for a 128-bit uintmax_t.
232 */
233 #define BUF 100
235 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
238 /*
239 * Macros for converting digits to letters and vice versa
240 */
241 #define to_digit(c) ((c) - '0')
242 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
243 #define to_char(n) ((n) + '0')
245 /*
246 * Flags used during conversion.
247 */
248 #define ALT 0x0001 /* alternate form */
249 #define LADJUST 0x0004 /* left adjustment */
250 #define LONGDBL 0x0008 /* long double */
251 #define LONGINT 0x0010 /* long integer */
252 #define LLONGINT 0x0020 /* long long integer */
253 #define SHORTINT 0x0040 /* short integer */
254 #define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */
255 #define FPT 0x0100 /* Floating point number */
256 #define PTRINT 0x0200 /* (unsigned) ptrdiff_t */
257 #define SIZEINT 0x0400 /* (signed) size_t */
258 #define CHARINT 0x0800 /* 8 bit integer */
259 #define MAXINT 0x1000 /* largest integer size (intmax_t) */
261 int
262 vfprintf(FILE *fp, const char *fmt0, __va_list ap)
263 {
264 int ret;
266 FLOCKFILE(fp);
267 ret = __vfprintf(fp, fmt0, ap);
268 FUNLOCKFILE(fp);
269 return (ret);
270 }
272 int
273 __vfprintf(FILE *fp, const char *fmt0, __va_list ap)
274 {
275 char *fmt; /* format string */
276 int ch; /* character from fmt */
277 int n, n2; /* handy integers (short term usage) */
278 char *cp; /* handy char pointer (short term usage) */
279 struct __siov *iovp; /* for PRINT macro */
280 int flags; /* flags as above */
281 int ret; /* return value accumulator */
282 int width; /* width from format (%8d), or 0 */
283 int prec; /* precision from format; <0 for N/A */
284 char sign; /* sign prefix (' ', '+', '-', or \0) */
285 wchar_t wc;
286 mbstate_t ps;
287 #ifdef FLOATING_POINT
288 /*
289 * We can decompose the printed representation of floating
290 * point numbers into several parts, some of which may be empty:
291 *
292 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
293 * A B ---C--- D E F
294 *
295 * A: 'sign' holds this value if present; '\0' otherwise
296 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
297 * C: cp points to the string MMMNNN. Leading and trailing
298 * zeros are not in the string and must be added.
299 * D: expchar holds this character; '\0' if no exponent, e.g. %f
300 * F: at least two digits for decimal, at least one digit for hex
301 */
302 char *decimal_point = NULL;
303 int signflag; /* true if float is negative */
304 union { /* floating point arguments %[aAeEfFgG] */
305 double dbl;
306 long double ldbl;
307 } fparg;
308 int expt; /* integer value of exponent */
309 char expchar; /* exponent character: [eEpP\0] */
310 char *dtoaend; /* pointer to end of converted digits */
311 int expsize; /* character count for expstr */
312 int lead; /* sig figs before decimal or group sep */
313 int ndig; /* actual number of digits returned by dtoa */
314 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
315 char *dtoaresult = NULL;
316 #endif
318 uintmax_t _umax; /* integer arguments %[diouxX] */
319 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
320 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
321 int realsz; /* field size expanded by dprec */
322 int size; /* size of converted field or string */
323 const char *xdigs; /* digits for %[xX] conversion */
324 #define NIOV 8
325 struct __suio uio; /* output information: summary */
326 struct __siov iov[NIOV];/* ... and individual io vectors */
327 char buf[BUF]; /* buffer with space for digits of uintmax_t */
328 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
329 union arg *argtable; /* args, built due to positional arg */
330 union arg statargtable[STATIC_ARG_TBL_SIZE];
331 size_t argtablesiz;
332 int nextarg; /* 1-based argument index */
333 va_list orgap; /* original argument pointer */
334 #ifdef PRINTF_WIDE_CHAR
335 char *convbuf; /* buffer for wide to multi-byte conversion */
336 #endif
338 /*
339 * Choose PADSIZE to trade efficiency vs. size. If larger printf
340 * fields occur frequently, increase PADSIZE and make the initialisers
341 * below longer.
342 */
343 #define PADSIZE 16 /* pad chunk size */
344 static char blanks[PADSIZE] =
345 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
346 static char zeroes[PADSIZE] =
347 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
349 static const char xdigs_lower[16] = "0123456789abcdef";
350 static const char xdigs_upper[16] = "0123456789ABCDEF";
352 /*
353 * BEWARE, these `goto error' on error, and PAD uses `n'.
354 */
355 #define PRINT(ptr, len) do { \
356 iovp->iov_base = (ptr); \
357 iovp->iov_len = (len); \
358 uio.uio_resid += (len); \
359 iovp++; \
360 if (++uio.uio_iovcnt >= NIOV) { \
361 if (__sprint(fp, &uio)) \
362 goto error; \
363 iovp = iov; \
364 } \
365 } while (0)
366 #define PAD(howmany, with) do { \
367 if ((n = (howmany)) > 0) { \
368 while (n > PADSIZE) { \
369 PRINT(with, PADSIZE); \
370 n -= PADSIZE; \
371 } \
372 PRINT(with, n); \
373 } \
374 } while (0)
375 #define PRINTANDPAD(p, ep, len, with) do { \
376 n2 = (ep) - (p); \
377 if (n2 > (len)) \
378 n2 = (len); \
379 if (n2 > 0) \
380 PRINT((p), n2); \
381 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
382 } while(0)
383 #define FLUSH() do { \
384 if (uio.uio_resid && __sprint(fp, &uio)) \
385 goto error; \
386 uio.uio_iovcnt = 0; \
387 iovp = iov; \
388 } while (0)
390 /*
391 * To extend shorts properly, we need both signed and unsigned
392 * argument extraction methods.
393 */
394 #define SARG() \
395 ((intmax_t)(flags&MAXINT ? GETARG(intmax_t) : \
396 flags&LLONGINT ? GETARG(long long) : \
397 flags&LONGINT ? GETARG(long) : \
398 flags&PTRINT ? GETARG(ptrdiff_t) : \
399 flags&SIZEINT ? GETARG(ssize_t) : \
400 flags&SHORTINT ? (short)GETARG(int) : \
401 flags&CHARINT ? (signed char)GETARG(int) : \
402 GETARG(int)))
403 #define UARG() \
404 ((uintmax_t)(flags&MAXINT ? GETARG(uintmax_t) : \
405 flags&LLONGINT ? GETARG(unsigned long long) : \
406 flags&LONGINT ? GETARG(unsigned long) : \
407 flags&PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
408 flags&SIZEINT ? GETARG(size_t) : \
409 flags&SHORTINT ? (unsigned short)GETARG(int) : \
410 flags&CHARINT ? (unsigned char)GETARG(int) : \
411 GETARG(unsigned int)))
413 /*
414 * Append a digit to a value and check for overflow.
415 */
416 #define APPEND_DIGIT(val, dig) do { \
417 if ((val) > INT_MAX / 10) \
418 goto overflow; \
419 (val) *= 10; \
420 if ((val) > INT_MAX - to_digit((dig))) \
421 goto overflow; \
422 (val) += to_digit((dig)); \
423 } while (0)
425 /*
426 * Get * arguments, including the form *nn$. Preserve the nextarg
427 * that the argument can be gotten once the type is determined.
428 */
429 #define GETASTER(val) \
430 n2 = 0; \
431 cp = fmt; \
432 while (is_digit(*cp)) { \
433 APPEND_DIGIT(n2, *cp); \
434 cp++; \
435 } \
436 if (*cp == '$') { \
437 int hold = nextarg; \
438 if (argtable == NULL) { \
439 argtable = statargtable; \
440 __find_arguments(fmt0, orgap, &argtable, &argtablesiz); \
441 } \
442 nextarg = n2; \
443 val = GETARG(int); \
444 nextarg = hold; \
445 fmt = ++cp; \
446 } else { \
447 val = GETARG(int); \
448 }
450 /*
451 * Get the argument indexed by nextarg. If the argument table is
452 * built, use it to get the argument. If its not, get the next
453 * argument (and arguments must be gotten sequentially).
454 */
455 #define GETARG(type) \
456 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
457 (nextarg++, va_arg(ap, type)))
459 _SET_ORIENTATION(fp, -1);
460 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
461 if (cantwrite(fp)) {
462 errno = EBADF;
463 return (EOF);
464 }
466 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
467 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
468 fp->_file >= 0)
469 return (__sbprintf(fp, fmt0, ap));
471 fmt = (char *)fmt0;
472 argtable = NULL;
473 nextarg = 1;
474 va_copy(orgap, ap);
475 uio.uio_iov = iovp = iov;
476 uio.uio_resid = 0;
477 uio.uio_iovcnt = 0;
478 ret = 0;
479 #ifdef PRINTF_WIDE_CHAR
480 convbuf = NULL;
481 #endif
483 memset(&ps, 0, sizeof(ps));
484 /*
485 * Scan the format for conversions (`%' character).
486 */
487 for (;;) {
488 cp = fmt;
489 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
490 fmt += n;
491 if (wc == '%') {
492 fmt--;
493 break;
494 }
495 }
496 if (fmt != cp) {
497 ptrdiff_t m = fmt - cp;
498 if (m < 0 || m > INT_MAX - ret)
499 goto overflow;
500 PRINT(cp, m);
501 ret += m;
502 }
503 if (n <= 0)
504 goto done;
505 fmt++; /* skip over '%' */
507 flags = 0;
508 dprec = 0;
509 width = 0;
510 prec = -1;
511 sign = '\0';
512 ox[1] = '\0';
514 rflag: ch = *fmt++;
515 reswitch: switch (ch) {
516 case ' ':
517 /*
518 * ``If the space and + flags both appear, the space
519 * flag will be ignored.''
520 * -- ANSI X3J11
521 */
522 if (!sign)
523 sign = ' ';
524 goto rflag;
525 case '#':
526 flags |= ALT;
527 goto rflag;
528 case '\'':
529 /* grouping not implemented */
530 goto rflag;
531 case '*':
532 /*
533 * ``A negative field width argument is taken as a
534 * - flag followed by a positive field width.''
535 * -- ANSI X3J11
536 * They don't exclude field widths read from args.
537 */
538 GETASTER(width);
539 if (width >= 0)
540 goto rflag;
541 if (width == INT_MIN)
542 goto overflow;
543 width = -width;
544 /* FALLTHROUGH */
545 case '-':
546 flags |= LADJUST;
547 goto rflag;
548 case '+':
549 sign = '+';
550 goto rflag;
551 case '.':
552 if ((ch = *fmt++) == '*') {
553 GETASTER(n);
554 prec = n < 0 ? -1 : n;
555 goto rflag;
556 }
557 n = 0;
558 while (is_digit(ch)) {
559 APPEND_DIGIT(n, ch);
560 ch = *fmt++;
561 }
562 if (ch == '$') {
563 nextarg = n;
564 if (argtable == NULL) {
565 argtable = statargtable;
566 __find_arguments(fmt0, orgap,
567 &argtable, &argtablesiz);
568 }
569 goto rflag;
570 }
571 prec = n;
572 goto reswitch;
573 case '0':
574 /*
575 * ``Note that 0 is taken as a flag, not as the
576 * beginning of a field width.''
577 * -- ANSI X3J11
578 */
579 flags |= ZEROPAD;
580 goto rflag;
581 case '1': case '2': case '3': case '4':
582 case '5': case '6': case '7': case '8': case '9':
583 n = 0;
584 do {
585 APPEND_DIGIT(n, ch);
586 ch = *fmt++;
587 } while (is_digit(ch));
588 if (ch == '$') {
589 nextarg = n;
590 if (argtable == NULL) {
591 argtable = statargtable;
592 __find_arguments(fmt0, orgap,
593 &argtable, &argtablesiz);
594 }
595 goto rflag;
596 }
597 width = n;
598 goto reswitch;
599 #ifdef FLOATING_POINT
600 case 'L':
601 flags |= LONGDBL;
602 goto rflag;
603 #endif
604 case 'h':
605 if (*fmt == 'h') {
606 fmt++;
607 flags |= CHARINT;
608 } else {
609 flags |= SHORTINT;
610 }
611 goto rflag;
612 case 'j':
613 flags |= MAXINT;
614 goto rflag;
615 case 'l':
616 if (*fmt == 'l') {
617 fmt++;
618 flags |= LLONGINT;
619 } else {
620 flags |= LONGINT;
621 }
622 goto rflag;
623 case 'q':
624 flags |= LLONGINT;
625 goto rflag;
626 case 't':
627 flags |= PTRINT;
628 goto rflag;
629 case 'z':
630 flags |= SIZEINT;
631 goto rflag;
632 case 'c':
633 #ifdef PRINTF_WIDE_CHAR
634 if (flags & LONGINT) {
635 mbstate_t mbs;
636 size_t mbseqlen;
638 memset(&mbs, 0, sizeof(mbs));
639 mbseqlen = wcrtomb(buf,
640 (wchar_t)GETARG(wint_t), &mbs);
641 if (mbseqlen == (size_t)-1) {
642 fp->_flags |= __SERR;
643 errno = EILSEQ;
644 goto error;
645 }
646 cp = buf;
647 size = (int)mbseqlen;
648 } else {
649 #endif
650 *(cp = buf) = GETARG(int);
651 size = 1;
652 #ifdef PRINTF_WIDE_CHAR
653 }
654 #endif
655 sign = '\0';
656 break;
657 case 'D':
658 flags |= LONGINT;
659 /*FALLTHROUGH*/
660 case 'd':
661 case 'i':
662 _umax = SARG();
663 if ((intmax_t)_umax < 0) {
664 _umax = -_umax;
665 sign = '-';
666 }
667 base = DEC;
668 goto number;
669 #ifdef FLOATING_POINT
670 case 'a':
671 case 'A':
672 if (ch == 'a') {
673 ox[1] = 'x';
674 xdigs = xdigs_lower;
675 expchar = 'p';
676 } else {
677 ox[1] = 'X';
678 xdigs = xdigs_upper;
679 expchar = 'P';
680 }
681 if (prec >= 0)
682 prec++;
683 if (dtoaresult)
684 __freedtoa(dtoaresult);
685 if (flags & LONGDBL) {
686 fparg.ldbl = GETARG(long double);
687 dtoaresult = cp =
688 __hldtoa(fparg.ldbl, xdigs, prec,
689 &expt, &signflag, &dtoaend);
690 if (dtoaresult == NULL) {
691 errno = ENOMEM;
692 goto error;
693 }
694 } else {
695 fparg.dbl = GETARG(double);
696 dtoaresult = cp =
697 __hdtoa(fparg.dbl, xdigs, prec,
698 &expt, &signflag, &dtoaend);
699 if (dtoaresult == NULL) {
700 errno = ENOMEM;
701 goto error;
702 }
703 }
704 if (prec < 0)
705 prec = dtoaend - cp;
706 if (expt == INT_MAX)
707 ox[1] = '\0';
708 goto fp_common;
709 case 'e':
710 case 'E':
711 expchar = ch;
712 if (prec < 0) /* account for digit before decpt */
713 prec = DEFPREC + 1;
714 else
715 prec++;
716 goto fp_begin;
717 case 'f':
718 case 'F':
719 expchar = '\0';
720 goto fp_begin;
721 case 'g':
722 case 'G':
723 expchar = ch - ('g' - 'e');
724 if (prec == 0)
725 prec = 1;
726 fp_begin:
727 if (prec < 0)
728 prec = DEFPREC;
729 if (dtoaresult)
730 __freedtoa(dtoaresult);
731 if (flags & LONGDBL) {
732 fparg.ldbl = GETARG(long double);
733 dtoaresult = cp =
734 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
735 &expt, &signflag, &dtoaend);
736 if (dtoaresult == NULL) {
737 errno = ENOMEM;
738 goto error;
739 }
740 } else {
741 fparg.dbl = GETARG(double);
742 dtoaresult = cp =
743 __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
744 &expt, &signflag, &dtoaend);
745 if (dtoaresult == NULL) {
746 errno = ENOMEM;
747 goto error;
748 }
749 if (expt == 9999)
750 expt = INT_MAX;
751 }
752 fp_common:
753 if (signflag)
754 sign = '-';
755 if (expt == INT_MAX) { /* inf or nan */
756 if (*cp == 'N') {
757 cp = (ch >= 'a') ? "nan" : "NAN";
758 sign = '\0';
759 } else
760 cp = (ch >= 'a') ? "inf" : "INF";
761 size = 3;
762 flags &= ~ZEROPAD;
763 break;
764 }
765 flags |= FPT;
766 ndig = dtoaend - cp;
767 if (ch == 'g' || ch == 'G') {
768 if (expt > -4 && expt <= prec) {
769 /* Make %[gG] smell like %[fF] */
770 expchar = '\0';
771 if (flags & ALT)
772 prec -= expt;
773 else
774 prec = ndig - expt;
775 if (prec < 0)
776 prec = 0;
777 } else {
778 /*
779 * Make %[gG] smell like %[eE], but
780 * trim trailing zeroes if no # flag.
781 */
782 if (!(flags & ALT))
783 prec = ndig;
784 }
785 }
786 if (expchar) {
787 expsize = exponent(expstr, expt - 1, expchar);
788 size = expsize + prec;
789 if (prec > 1 || flags & ALT)
790 ++size;
791 } else {
792 /* space for digits before decimal point */
793 if (expt > 0)
794 size = expt;
795 else /* "0" */
796 size = 1;
797 /* space for decimal pt and following digits */
798 if (prec || flags & ALT)
799 size += prec + 1;
800 lead = expt;
801 }
802 break;
803 #endif /* FLOATING_POINT */
804 case 'n':
805 if (flags & LLONGINT)
806 *GETARG(long long *) = ret;
807 else if (flags & LONGINT)
808 *GETARG(long *) = ret;
809 else if (flags & SHORTINT)
810 *GETARG(short *) = ret;
811 else if (flags & CHARINT)
812 *GETARG(signed char *) = ret;
813 else if (flags & PTRINT)
814 *GETARG(ptrdiff_t *) = ret;
815 else if (flags & SIZEINT)
816 *GETARG(ssize_t *) = ret;
817 else if (flags & MAXINT)
818 *GETARG(intmax_t *) = ret;
819 else
820 *GETARG(int *) = ret;
821 continue; /* no output */
822 case 'O':
823 flags |= LONGINT;
824 /*FALLTHROUGH*/
825 case 'o':
826 _umax = UARG();
827 base = OCT;
828 goto nosign;
829 case 'p':
830 /*
831 * ``The argument shall be a pointer to void. The
832 * value of the pointer is converted to a sequence
833 * of printable characters, in an implementation-
834 * defined manner.''
835 * -- ANSI X3J11
836 */
837 /* NOSTRICT */
838 _umax = (u_long)GETARG(void *);
839 base = HEX;
840 xdigs = xdigs_lower;
841 ox[1] = 'x';
842 goto nosign;
843 case 's':
844 #ifdef PRINTF_WIDE_CHAR
845 if (flags & LONGINT) {
846 wchar_t *wcp;
848 if (convbuf != NULL) {
849 free(convbuf);
850 convbuf = NULL;
851 }
852 if ((wcp = GETARG(wchar_t *)) == NULL) {
853 cp = "(null)";
854 } else {
855 convbuf = __wcsconv(wcp, prec);
856 if (convbuf == NULL) {
857 fp->_flags = __SERR;
858 goto error;
859 }
860 cp = convbuf;
861 }
862 } else
863 #endif /* PRINTF_WIDE_CHAR */
864 if ((cp = GETARG(char *)) == NULL)
865 cp = "(null)";
866 if (prec >= 0) {
867 /*
868 * can't use strlen; can only look for the
869 * NUL in the first `prec' characters, and
870 * strlen() will go further.
871 */
872 char *p = memchr(cp, 0, prec);
874 size = p ? (p - cp) : prec;
875 } else {
876 size_t len;
878 if ((len = strlen(cp)) > INT_MAX)
879 goto overflow;
880 size = (int)len;
881 }
882 sign = '\0';
883 break;
884 case 'U':
885 flags |= LONGINT;
886 /*FALLTHROUGH*/
887 case 'u':
888 _umax = UARG();
889 base = DEC;
890 goto nosign;
891 case 'X':
892 xdigs = xdigs_upper;
893 goto hex;
894 case 'x':
895 xdigs = xdigs_lower;
896 hex: _umax = UARG();
897 base = HEX;
898 /* leading 0x/X only if non-zero */
899 if (flags & ALT && _umax != 0)
900 ox[1] = ch;
902 /* unsigned conversions */
903 nosign: sign = '\0';
904 /*
905 * ``... diouXx conversions ... if a precision is
906 * specified, the 0 flag will be ignored.''
907 * -- ANSI X3J11
908 */
909 number: if ((dprec = prec) >= 0)
910 flags &= ~ZEROPAD;
912 /*
913 * ``The result of converting a zero value with an
914 * explicit precision of zero is no characters.''
915 * -- ANSI X3J11
916 */
917 cp = buf + BUF;
918 if (_umax != 0 || prec != 0) {
919 /*
920 * Unsigned mod is hard, and unsigned mod
921 * by a constant is easier than that by
922 * a variable; hence this switch.
923 */
924 switch (base) {
925 case OCT:
926 do {
927 *--cp = to_char(_umax & 7);
928 _umax >>= 3;
929 } while (_umax);
930 /* handle octal leading 0 */
931 if (flags & ALT && *cp != '0')
932 *--cp = '0';
933 break;
935 case DEC:
936 /* many numbers are 1 digit */
937 while (_umax >= 10) {
938 *--cp = to_char(_umax % 10);
939 _umax /= 10;
940 }
941 *--cp = to_char(_umax);
942 break;
944 case HEX:
945 do {
946 *--cp = xdigs[_umax & 15];
947 _umax >>= 4;
948 } while (_umax);
949 break;
951 default:
952 cp = "bug in vfprintf: bad base";
953 size = strlen(cp);
954 goto skipsize;
955 }
956 }
957 size = buf + BUF - cp;
958 if (size > BUF) /* should never happen */
959 abort();
960 skipsize:
961 break;
962 default: /* "%?" prints ?, unless ? is NUL */
963 if (ch == '\0')
964 goto done;
965 /* pretend it was %c with argument ch */
966 cp = buf;
967 *cp = ch;
968 size = 1;
969 sign = '\0';
970 break;
971 }
973 /*
974 * All reasonable formats wind up here. At this point, `cp'
975 * points to a string which (if not flags&LADJUST) should be
976 * padded out to `width' places. If flags&ZEROPAD, it should
977 * first be prefixed by any sign or other prefix; otherwise,
978 * it should be blank padded before the prefix is emitted.
979 * After any left-hand padding and prefixing, emit zeroes
980 * required by a decimal %[diouxX] precision, then print the
981 * string proper, then emit zeroes required by any leftover
982 * floating precision; finally, if LADJUST, pad with blanks.
983 *
984 * Compute actual size, so we know how much to pad.
985 * size excludes decimal prec; realsz includes it.
986 */
987 realsz = dprec > size ? dprec : size;
988 if (sign)
989 realsz++;
990 if (ox[1])
991 realsz+= 2;
993 /* right-adjusting blank padding */
994 if ((flags & (LADJUST|ZEROPAD)) == 0)
995 PAD(width - realsz, blanks);
997 /* prefix */
998 if (sign)
999 PRINT(&sign, 1);
1000 if (ox[1]) { /* ox[1] is either x, X, or \0 */
1001 ox[0] = '0';
1002 PRINT(ox, 2);
1003 }
1005 /* right-adjusting zero padding */
1006 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1007 PAD(width - realsz, zeroes);
1009 /* leading zeroes from decimal precision */
1010 PAD(dprec - size, zeroes);
1012 /* the string or number proper */
1013 #ifdef FLOATING_POINT
1014 if ((flags & FPT) == 0) {
1015 PRINT(cp, size);
1016 } else { /* glue together f_p fragments */
1017 if (decimal_point == NULL)
1018 decimal_point = nl_langinfo(RADIXCHAR);
1019 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1020 if (expt <= 0) {
1021 PRINT(zeroes, 1);
1022 if (prec || flags & ALT)
1023 PRINT(decimal_point, 1);
1024 PAD(-expt, zeroes);
1025 /* already handled initial 0's */
1026 prec += expt;
1027 } else {
1028 PRINTANDPAD(cp, dtoaend, lead, zeroes);
1029 cp += lead;
1030 if (prec || flags & ALT)
1031 PRINT(decimal_point, 1);
1032 }
1033 PRINTANDPAD(cp, dtoaend, prec, zeroes);
1034 } else { /* %[eE] or sufficiently long %[gG] */
1035 if (prec > 1 || flags & ALT) {
1036 buf[0] = *cp++;
1037 buf[1] = *decimal_point;
1038 PRINT(buf, 2);
1039 PRINT(cp, ndig-1);
1040 PAD(prec - ndig, zeroes);
1041 } else { /* XeYYY */
1042 PRINT(cp, 1);
1043 }
1044 PRINT(expstr, expsize);
1045 }
1046 }
1047 #else
1048 PRINT(cp, size);
1049 #endif
1050 /* left-adjusting padding (always blank) */
1051 if (flags & LADJUST)
1052 PAD(width - realsz, blanks);
1054 /* finally, adjust ret */
1055 if (width < realsz)
1056 width = realsz;
1057 if (width > INT_MAX - ret)
1058 goto overflow;
1059 ret += width;
1061 FLUSH(); /* copy out the I/O vectors */
1062 }
1063 done:
1064 FLUSH();
1065 error:
1066 va_end(orgap);
1067 if (__sferror(fp))
1068 ret = -1;
1069 goto finish;
1071 overflow:
1072 errno = ENOMEM;
1073 ret = -1;
1075 finish:
1076 #ifdef PRINTF_WIDE_CHAR
1077 if (convbuf)
1078 free(convbuf);
1079 #endif
1080 #ifdef FLOATING_POINT
1081 if (dtoaresult)
1082 __freedtoa(dtoaresult);
1083 #endif
1084 if (argtable != NULL && argtable != statargtable) {
1085 munmap(argtable, argtablesiz);
1086 argtable = NULL;
1087 }
1088 return (ret);
1089 }
1091 /*
1092 * Type ids for argument type table.
1093 */
1094 #define T_UNUSED 0
1095 #define T_SHORT 1
1096 #define T_U_SHORT 2
1097 #define TP_SHORT 3
1098 #define T_INT 4
1099 #define T_U_INT 5
1100 #define TP_INT 6
1101 #define T_LONG 7
1102 #define T_U_LONG 8
1103 #define TP_LONG 9
1104 #define T_LLONG 10
1105 #define T_U_LLONG 11
1106 #define TP_LLONG 12
1107 #define T_DOUBLE 13
1108 #define T_LONG_DOUBLE 14
1109 #define TP_CHAR 15
1110 #define TP_VOID 16
1111 #define T_PTRINT 17
1112 #define TP_PTRINT 18
1113 #define T_SIZEINT 19
1114 #define T_SSIZEINT 20
1115 #define TP_SSIZEINT 21
1116 #define T_MAXINT 22
1117 #define T_MAXUINT 23
1118 #define TP_MAXINT 24
1119 #define T_CHAR 25
1120 #define T_U_CHAR 26
1121 #define T_WINT 27
1122 #define TP_WCHAR 28
1124 /*
1125 * Find all arguments when a positional parameter is encountered. Returns a
1126 * table, indexed by argument number, of pointers to each arguments. The
1127 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1128 * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
1129 * used since we are attempting to make snprintf thread safe, and alloca is
1130 * problematic since we have nested functions..)
1131 */
1132 static int
1133 __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
1134 size_t *argtablesiz)
1135 {
1136 char *fmt; /* format string */
1137 int ch; /* character from fmt */
1138 int n, n2; /* handy integer (short term usage) */
1139 char *cp; /* handy char pointer (short term usage) */
1140 int flags; /* flags as above */
1141 unsigned char *typetable; /* table of types */
1142 unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
1143 int tablesize; /* current size of type table */
1144 int tablemax; /* largest used index in table */
1145 int nextarg; /* 1-based argument index */
1146 int ret = 0; /* return value */
1147 wchar_t wc;
1148 mbstate_t ps;
1150 /*
1151 * Add an argument type to the table, expanding if necessary.
1152 */
1153 #define ADDTYPE(type) \
1154 ((nextarg >= tablesize) ? \
1155 __grow_type_table(&typetable, &tablesize) : 0, \
1156 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1157 typetable[nextarg++] = type)
1159 #define ADDSARG() \
1160 ((flags&MAXINT) ? ADDTYPE(T_MAXINT) : \
1161 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
1162 ((flags&SIZEINT) ? ADDTYPE(T_SSIZEINT) : \
1163 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1164 ((flags&LONGINT) ? ADDTYPE(T_LONG) : \
1165 ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : \
1166 ((flags&CHARINT) ? ADDTYPE(T_CHAR) : ADDTYPE(T_INT))))))))
1168 #define ADDUARG() \
1169 ((flags&MAXINT) ? ADDTYPE(T_MAXUINT) : \
1170 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
1171 ((flags&SIZEINT) ? ADDTYPE(T_SIZEINT) : \
1172 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1173 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \
1174 ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : \
1175 ((flags&CHARINT) ? ADDTYPE(T_U_CHAR) : ADDTYPE(T_U_INT))))))))
1177 /*
1178 * Add * arguments to the type array.
1179 */
1180 #define ADDASTER() \
1181 n2 = 0; \
1182 cp = fmt; \
1183 while (is_digit(*cp)) { \
1184 APPEND_DIGIT(n2, *cp); \
1185 cp++; \
1186 } \
1187 if (*cp == '$') { \
1188 int hold = nextarg; \
1189 nextarg = n2; \
1190 ADDTYPE(T_INT); \
1191 nextarg = hold; \
1192 fmt = ++cp; \
1193 } else { \
1194 ADDTYPE(T_INT); \
1195 }
1196 fmt = (char *)fmt0;
1197 typetable = stattypetable;
1198 tablesize = STATIC_ARG_TBL_SIZE;
1199 tablemax = 0;
1200 nextarg = 1;
1201 memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1202 memset(&ps, 0, sizeof(ps));
1204 /*
1205 * Scan the format for conversions (`%' character).
1206 */
1207 for (;;) {
1208 cp = fmt;
1209 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
1210 fmt += n;
1211 if (wc == '%') {
1212 fmt--;
1213 break;
1214 }
1215 }
1216 if (n <= 0)
1217 goto done;
1218 fmt++; /* skip over '%' */
1220 flags = 0;
1222 rflag: ch = *fmt++;
1223 reswitch: switch (ch) {
1224 case ' ':
1225 case '#':
1226 case '\'':
1227 goto rflag;
1228 case '*':
1229 ADDASTER();
1230 goto rflag;
1231 case '-':
1232 case '+':
1233 goto rflag;
1234 case '.':
1235 if ((ch = *fmt++) == '*') {
1236 ADDASTER();
1237 goto rflag;
1238 }
1239 while (is_digit(ch)) {
1240 ch = *fmt++;
1241 }
1242 goto reswitch;
1243 case '0':
1244 goto rflag;
1245 case '1': case '2': case '3': case '4':
1246 case '5': case '6': case '7': case '8': case '9':
1247 n = 0;
1248 do {
1249 APPEND_DIGIT(n ,ch);
1250 ch = *fmt++;
1251 } while (is_digit(ch));
1252 if (ch == '$') {
1253 nextarg = n;
1254 goto rflag;
1255 }
1256 goto reswitch;
1257 #ifdef FLOATING_POINT
1258 case 'L':
1259 flags |= LONGDBL;
1260 goto rflag;
1261 #endif
1262 case 'h':
1263 if (*fmt == 'h') {
1264 fmt++;
1265 flags |= CHARINT;
1266 } else {
1267 flags |= SHORTINT;
1268 }
1269 goto rflag;
1270 case 'j':
1271 flags |= MAXINT;
1272 goto rflag;
1273 case 'l':
1274 if (*fmt == 'l') {
1275 fmt++;
1276 flags |= LLONGINT;
1277 } else {
1278 flags |= LONGINT;
1279 }
1280 goto rflag;
1281 case 'q':
1282 flags |= LLONGINT;
1283 goto rflag;
1284 case 't':
1285 flags |= PTRINT;
1286 goto rflag;
1287 case 'z':
1288 flags |= SIZEINT;
1289 goto rflag;
1290 case 'c':
1291 #ifdef PRINTF_WIDE_CHAR
1292 if (flags & LONGINT)
1293 ADDTYPE(T_WINT);
1294 else
1295 #endif
1296 ADDTYPE(T_INT);
1297 break;
1298 case 'D':
1299 flags |= LONGINT;
1300 /*FALLTHROUGH*/
1301 case 'd':
1302 case 'i':
1303 ADDSARG();
1304 break;
1305 #ifdef FLOATING_POINT
1306 case 'a':
1307 case 'A':
1308 case 'e':
1309 case 'E':
1310 case 'f':
1311 case 'F':
1312 case 'g':
1313 case 'G':
1314 if (flags & LONGDBL)
1315 ADDTYPE(T_LONG_DOUBLE);
1316 else
1317 ADDTYPE(T_DOUBLE);
1318 break;
1319 #endif /* FLOATING_POINT */
1320 case 'n':
1321 if (flags & LLONGINT)
1322 ADDTYPE(TP_LLONG);
1323 else if (flags & LONGINT)
1324 ADDTYPE(TP_LONG);
1325 else if (flags & SHORTINT)
1326 ADDTYPE(TP_SHORT);
1327 else if (flags & PTRINT)
1328 ADDTYPE(TP_PTRINT);
1329 else if (flags & SIZEINT)
1330 ADDTYPE(TP_SSIZEINT);
1331 else if (flags & MAXINT)
1332 ADDTYPE(TP_MAXINT);
1333 else
1334 ADDTYPE(TP_INT);
1335 continue; /* no output */
1336 case 'O':
1337 flags |= LONGINT;
1338 /*FALLTHROUGH*/
1339 case 'o':
1340 ADDUARG();
1341 break;
1342 case 'p':
1343 ADDTYPE(TP_VOID);
1344 break;
1345 case 's':
1346 #ifdef PRINTF_WIDE_CHAR
1347 if (flags & LONGINT)
1348 ADDTYPE(TP_WCHAR);
1349 else
1350 #endif
1351 ADDTYPE(TP_CHAR);
1352 break;
1353 case 'U':
1354 flags |= LONGINT;
1355 /*FALLTHROUGH*/
1356 case 'u':
1357 case 'X':
1358 case 'x':
1359 ADDUARG();
1360 break;
1361 default: /* "%?" prints ?, unless ? is NUL */
1362 if (ch == '\0')
1363 goto done;
1364 break;
1365 }
1366 }
1367 done:
1368 /*
1369 * Build the argument table.
1370 */
1371 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1372 *argtablesiz = sizeof(union arg) * (tablemax + 1);
1373 *argtable = mmap(NULL, *argtablesiz,
1374 PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0);
1375 if (*argtable == MAP_FAILED)
1376 return (-1);
1377 }
1379 #if 0
1380 /* XXX is this required? */
1381 (*argtable)[0].intarg = 0;
1382 #endif
1383 for (n = 1; n <= tablemax; n++) {
1384 switch (typetable[n]) {
1385 case T_UNUSED:
1386 case T_CHAR:
1387 case T_U_CHAR:
1388 case T_SHORT:
1389 case T_U_SHORT:
1390 case T_INT:
1391 (*argtable)[n].intarg = va_arg(ap, int);
1392 break;
1393 case TP_SHORT:
1394 (*argtable)[n].pshortarg = va_arg(ap, short *);
1395 break;
1396 case T_U_INT:
1397 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1398 break;
1399 case TP_INT:
1400 (*argtable)[n].pintarg = va_arg(ap, int *);
1401 break;
1402 case T_LONG:
1403 (*argtable)[n].longarg = va_arg(ap, long);
1404 break;
1405 case T_U_LONG:
1406 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1407 break;
1408 case TP_LONG:
1409 (*argtable)[n].plongarg = va_arg(ap, long *);
1410 break;
1411 case T_LLONG:
1412 (*argtable)[n].longlongarg = va_arg(ap, long long);
1413 break;
1414 case T_U_LLONG:
1415 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1416 break;
1417 case TP_LLONG:
1418 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
1419 break;
1420 #ifdef FLOATING_POINT
1421 case T_DOUBLE:
1422 (*argtable)[n].doublearg = va_arg(ap, double);
1423 break;
1424 case T_LONG_DOUBLE:
1425 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1426 break;
1427 #endif
1428 case TP_CHAR:
1429 (*argtable)[n].pchararg = va_arg(ap, char *);
1430 break;
1431 case TP_VOID:
1432 (*argtable)[n].pvoidarg = va_arg(ap, void *);
1433 break;
1434 case T_PTRINT:
1435 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1436 break;
1437 case TP_PTRINT:
1438 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
1439 break;
1440 case T_SIZEINT:
1441 (*argtable)[n].sizearg = va_arg(ap, size_t);
1442 break;
1443 case T_SSIZEINT:
1444 (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
1445 break;
1446 case TP_SSIZEINT:
1447 (*argtable)[n].pssizearg = va_arg(ap, ssize_t *);
1448 break;
1449 case T_MAXINT:
1450 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1451 break;
1452 case T_MAXUINT:
1453 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1454 break;
1455 case TP_MAXINT:
1456 (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t *);
1457 break;
1458 #ifdef PRINTF_WIDE_CHAR
1459 case T_WINT:
1460 (*argtable)[n].wintarg = va_arg(ap, wint_t);
1461 break;
1462 case TP_WCHAR:
1463 (*argtable)[n].pwchararg = va_arg(ap, wchar_t *);
1464 break;
1465 #endif
1466 }
1467 }
1468 goto finish;
1470 overflow:
1471 errno = ENOMEM;
1472 ret = -1;
1474 finish:
1475 if (typetable != NULL && typetable != stattypetable) {
1476 munmap(typetable, *argtablesiz);
1477 typetable = NULL;
1478 }
1479 return (ret);
1480 }
1482 /*
1483 * Increase the size of the type table.
1484 */
1485 static int
1486 __grow_type_table(unsigned char **typetable, int *tablesize)
1487 {
1488 unsigned char *oldtable = *typetable;
1489 int newsize = *tablesize * 2;
1491 if (newsize < getpagesize())
1492 newsize = getpagesize();
1494 if (*tablesize == STATIC_ARG_TBL_SIZE) {
1495 *typetable = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
1496 MAP_ANON|MAP_PRIVATE, -1, 0);
1497 if (*typetable == MAP_FAILED)
1498 return (-1);
1499 bcopy(oldtable, *typetable, *tablesize);
1500 } else {
1501 unsigned char *new = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
1502 MAP_ANON|MAP_PRIVATE, -1, 0);
1503 if (new == MAP_FAILED)
1504 return (-1);
1505 memmove(new, *typetable, *tablesize);
1506 munmap(*typetable, *tablesize);
1507 *typetable = new;
1508 }
1509 memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
1511 *tablesize = newsize;
1512 return (0);
1513 }
1516 #ifdef FLOATING_POINT
1517 static int
1518 exponent(char *p0, int exp, int fmtch)
1519 {
1520 char *p, *t;
1521 char expbuf[MAXEXPDIG];
1523 p = p0;
1524 *p++ = fmtch;
1525 if (exp < 0) {
1526 exp = -exp;
1527 *p++ = '-';
1528 } else
1529 *p++ = '+';
1530 t = expbuf + MAXEXPDIG;
1531 if (exp > 9) {
1532 do {
1533 *--t = to_char(exp % 10);
1534 } while ((exp /= 10) > 9);
1535 *--t = to_char(exp);
1536 for (; t < expbuf + MAXEXPDIG; *p++ = *t++)
1537 /* nothing */;
1538 } else {
1539 /*
1540 * Exponents for decimal floating point conversions
1541 * (%[eEgG]) must be at least two characters long,
1542 * whereas exponents for hexadecimal conversions can
1543 * be only one character long.
1544 */
1545 if (fmtch == 'e' || fmtch == 'E')
1546 *p++ = '0';
1547 *p++ = to_char(exp);
1548 }
1549 return (p - p0);
1550 }
1551 #endif /* FLOATING_POINT */