blob: efc8fd00279c6e028ab6d39b6b01760778ff6aba [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $OpenBSD: vfprintf.c,v 1.37 2006/01/13 17:56:18 millert 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 */
33
34/*
35 * Actual printf innards.
36 *
37 * This code is large and complicated...
38 */
39
40#include <sys/types.h>
41#include <sys/mman.h>
42
43#include <errno.h>
44#include <stdarg.h>
45#include <stddef.h>
46#include <stdio.h>
47#include <stdint.h>
48#include <stdlib.h>
49#include <string.h>
50
51#include "local.h"
52#include "fvwrite.h"
53
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040054union arg {
55 int intarg;
56 unsigned int uintarg;
57 long longarg;
58 unsigned long ulongarg;
59 long long longlongarg;
60 unsigned long long ulonglongarg;
61 ptrdiff_t ptrdiffarg;
62 size_t sizearg;
63 size_t ssizearg;
64 intmax_t intmaxarg;
65 uintmax_t uintmaxarg;
66 void *pvoidarg;
67 char *pchararg;
68 signed char *pschararg;
69 short *pshortarg;
70 int *pintarg;
71 long *plongarg;
72 long long *plonglongarg;
73 ptrdiff_t *pptrdiffarg;
74 size_t *pssizearg;
75 intmax_t *pintmaxarg;
76#ifdef FLOATING_POINT
77 double doublearg;
78 long double longdoublearg;
79#endif
80};
81
82static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083 size_t *argtablesiz);
84static int __grow_type_table(unsigned char **typetable, int *tablesize);
85
86/*
87 * Flush out all the vectors defined by the given uio,
88 * then reset it so that it can be reused.
89 */
90static int
91__sprint(FILE *fp, struct __suio *uio)
92{
93 int err;
94
95 if (uio->uio_resid == 0) {
96 uio->uio_iovcnt = 0;
97 return (0);
98 }
99 err = __sfvwrite(fp, uio);
100 uio->uio_resid = 0;
101 uio->uio_iovcnt = 0;
102 return (err);
103}
104
105/*
106 * Helper function for `fprintf to unbuffered unix file': creates a
107 * temporary buffer. We only work on write-only files; this avoids
108 * worries about ungetc buffers and so forth.
109 */
110static int
111__sbprintf(FILE *fp, const char *fmt, va_list ap)
112{
113 int ret;
114 FILE fake;
115 struct __sfileext fakeext;
116 unsigned char buf[BUFSIZ];
117
118 _FILEEXT_SETUP(&fake, &fakeext);
119 /* copy the important variables */
120 fake._flags = fp->_flags & ~__SNBF;
121 fake._file = fp->_file;
122 fake._cookie = fp->_cookie;
123 fake._write = fp->_write;
124
125 /* set up the buffer */
126 fake._bf._base = fake._p = buf;
127 fake._bf._size = fake._w = sizeof(buf);
128 fake._lbfsize = 0; /* not actually used, but Just In Case */
129
130 /* do the work, then copy any error status */
Kenny Rootf5823402011-02-12 07:13:44 -0800131 ret = __vfprintf(&fake, fmt, ap);
132 if (ret >= 0 && __sflush(&fake))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133 ret = EOF;
134 if (fake._flags & __SERR)
135 fp->_flags |= __SERR;
136 return (ret);
137}
138
139
140#ifdef FLOATING_POINT
141#include <locale.h>
142#include <math.h>
143#include "floatio.h"
144
145#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
146#define DEFPREC 6
147
148static char *cvt(double, int, int, char *, int *, int, int *);
149static int exponent(char *, int, int);
150#else /* no FLOATING_POINT */
151#define BUF 40
152#endif /* FLOATING_POINT */
153
154#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
155
156/* BIONIC: do not link libm for only two rather simple functions */
157#ifdef FLOATING_POINT
158static int _my_isinf(double);
159static int _my_isnan(double);
160#endif
161
162/*
163 * Macros for converting digits to letters and vice versa
164 */
165#define to_digit(c) ((c) - '0')
166#define is_digit(c) ((unsigned)to_digit(c) <= 9)
167#define to_char(n) ((n) + '0')
168
169/*
170 * Flags used during conversion.
171 */
172#define ALT 0x0001 /* alternate form */
173#define HEXPREFIX 0x0002 /* add 0x or 0X prefix */
174#define LADJUST 0x0004 /* left adjustment */
175#define LONGDBL 0x0008 /* long double; unimplemented */
176#define LONGINT 0x0010 /* long integer */
177#define LLONGINT 0x0020 /* long long integer */
178#define SHORTINT 0x0040 /* short integer */
179#define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */
180#define FPT 0x0100 /* Floating point number */
181#define PTRINT 0x0200 /* (unsigned) ptrdiff_t */
182#define SIZEINT 0x0400 /* (signed) size_t */
183#define CHARINT 0x0800 /* 8 bit integer */
184#define MAXINT 0x1000 /* largest integer size (intmax_t) */
185
186int
187vfprintf(FILE *fp, const char *fmt0, __va_list ap)
188{
Kenny Rootf5823402011-02-12 07:13:44 -0800189 int ret;
190
191 FLOCKFILE(fp);
192 ret = __vfprintf(fp, fmt0, ap);
193 FUNLOCKFILE(fp);
194 return (ret);
195}
196
197int
198__vfprintf(FILE *fp, const char *fmt0, __va_list ap)
199{
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400200 char *fmt; /* format string */
201 int ch; /* character from fmt */
202 int n, m, n2; /* handy integers (short term usage) */
203 char *cp; /* handy char pointer (short term usage) */
204 char *cp_free = NULL; /* BIONIC: copy of cp to be freed after usage */
205 struct __siov *iovp; /* for PRINT macro */
206 int flags; /* flags as above */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207 int ret; /* return value accumulator */
208 int width; /* width from format (%8d), or 0 */
209 int prec; /* precision from format (%.3d), or -1 */
210 char sign; /* sign prefix (' ', '+', '-', or \0) */
211 wchar_t wc;
212 void* ps;
213#ifdef FLOATING_POINT
214 char *decimal_point = ".";
215 char softsign; /* temporary negative sign for floats */
216 double _double = 0.; /* double precision arguments %[eEfgG] */
217 int expt; /* integer value of exponent */
218 int expsize = 0; /* character count for expstr */
219 int ndig; /* actual number of digits returned by cvt */
220 char expstr[7]; /* buffer for exponent string */
221#endif
222
223 uintmax_t _umax; /* integer arguments %[diouxX] */
224 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
225 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
226 int realsz; /* field size expanded by dprec */
227 int size; /* size of converted field or string */
228 char* xdigs = NULL; /* digits for [xX] conversion */
229#define NIOV 8
230 struct __suio uio; /* output information: summary */
231 struct __siov iov[NIOV];/* ... and individual io vectors */
232 char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */
233 char ox[2]; /* space for 0x hex-prefix */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400234 union arg *argtable; /* args, built due to positional arg */
235 union arg statargtable[STATIC_ARG_TBL_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236 size_t argtablesiz;
237 int nextarg; /* 1-based argument index */
238 va_list orgap; /* original argument pointer */
239 /*
240 * Choose PADSIZE to trade efficiency vs. size. If larger printf
241 * fields occur frequently, increase PADSIZE and make the initialisers
242 * below longer.
243 */
244#define PADSIZE 16 /* pad chunk size */
Glenn Kasten0946b1f2011-01-09 11:28:22 -0800245 static const char blanks[PADSIZE] =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
Glenn Kasten0946b1f2011-01-09 11:28:22 -0800247 static const char zeroes[PADSIZE] =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
249
250 /*
251 * BEWARE, these `goto error' on error, and PAD uses `n'.
252 */
253#define PRINT(ptr, len) do { \
254 iovp->iov_base = (ptr); \
255 iovp->iov_len = (len); \
256 uio.uio_resid += (len); \
257 iovp++; \
258 if (++uio.uio_iovcnt >= NIOV) { \
259 if (__sprint(fp, &uio)) \
260 goto error; \
261 iovp = iov; \
262 } \
263} while (0)
264#define PAD(howmany, with) do { \
265 if ((n = (howmany)) > 0) { \
266 while (n > PADSIZE) { \
267 PRINT(with, PADSIZE); \
268 n -= PADSIZE; \
269 } \
270 PRINT(with, n); \
271 } \
272} while (0)
273#define FLUSH() do { \
274 if (uio.uio_resid && __sprint(fp, &uio)) \
275 goto error; \
276 uio.uio_iovcnt = 0; \
277 iovp = iov; \
278} while (0)
279
280 /*
281 * To extend shorts properly, we need both signed and unsigned
282 * argument extraction methods.
283 */
284#define SARG() \
285 ((intmax_t)(flags&MAXINT ? GETARG(intmax_t) : \
286 flags&LLONGINT ? GETARG(long long) : \
287 flags&LONGINT ? GETARG(long) : \
288 flags&PTRINT ? GETARG(ptrdiff_t) : \
289 flags&SIZEINT ? GETARG(ssize_t) : \
290 flags&SHORTINT ? (short)GETARG(int) : \
291 flags&CHARINT ? (__signed char)GETARG(int) : \
292 GETARG(int)))
293#define UARG() \
294 ((uintmax_t)(flags&MAXINT ? GETARG(uintmax_t) : \
295 flags&LLONGINT ? GETARG(unsigned long long) : \
296 flags&LONGINT ? GETARG(unsigned long) : \
297 flags&PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
298 flags&SIZEINT ? GETARG(size_t) : \
299 flags&SHORTINT ? (unsigned short)GETARG(int) : \
300 flags&CHARINT ? (unsigned char)GETARG(int) : \
301 GETARG(unsigned int)))
302
303 /*
304 * Get * arguments, including the form *nn$. Preserve the nextarg
305 * that the argument can be gotten once the type is determined.
306 */
307#define GETASTER(val) \
308 n2 = 0; \
309 cp = fmt; \
310 while (is_digit(*cp)) { \
311 n2 = 10 * n2 + to_digit(*cp); \
312 cp++; \
313 } \
314 if (*cp == '$') { \
315 int hold = nextarg; \
316 if (argtable == NULL) { \
317 argtable = statargtable; \
318 __find_arguments(fmt0, orgap, &argtable, &argtablesiz); \
319 } \
320 nextarg = n2; \
321 val = GETARG(int); \
322 nextarg = hold; \
323 fmt = ++cp; \
324 } else { \
325 val = GETARG(int); \
326 }
327
328/*
329* Get the argument indexed by nextarg. If the argument table is
330* built, use it to get the argument. If its not, get the next
331* argument (and arguments must be gotten sequentially).
332*/
333#define GETARG(type) \
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400334 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
335 (nextarg++, va_arg(ap, type)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
337 _SET_ORIENTATION(fp, -1);
338 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
339 if (cantwrite(fp)) {
340 errno = EBADF;
341 return (EOF);
342 }
343
344 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
345 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
346 fp->_file >= 0)
347 return (__sbprintf(fp, fmt0, ap));
348
349 fmt = (char *)fmt0;
350 argtable = NULL;
351 nextarg = 1;
352 va_copy(orgap, ap);
353 uio.uio_iov = iovp = iov;
354 uio.uio_resid = 0;
355 uio.uio_iovcnt = 0;
356 ret = 0;
357
358 memset(&ps, 0, sizeof(ps));
359 /*
360 * Scan the format for conversions (`%' character).
361 */
362 for (;;) {
363 cp = fmt;
364#if 1 /* BIONIC */
365 n = -1;
366 while ( (wc = *fmt) != 0 ) {
367 if (wc == '%') {
368 n = 1;
369 break;
370 }
371 fmt++;
372 }
373#else
374 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
375 fmt += n;
376 if (wc == '%') {
377 fmt--;
378 break;
379 }
380 }
381#endif
382 if ((m = fmt - cp) != 0) {
383 PRINT(cp, m);
384 ret += m;
385 }
386 if (n <= 0)
387 goto done;
388 fmt++; /* skip over '%' */
389
390 flags = 0;
391 dprec = 0;
392 width = 0;
393 prec = -1;
394 sign = '\0';
395
396rflag: ch = *fmt++;
397reswitch: switch (ch) {
398 case ' ':
399 /*
400 * ``If the space and + flags both appear, the space
401 * flag will be ignored.''
402 * -- ANSI X3J11
403 */
404 if (!sign)
405 sign = ' ';
406 goto rflag;
407 case '#':
408 flags |= ALT;
409 goto rflag;
410 case '*':
411 /*
412 * ``A negative field width argument is taken as a
413 * - flag followed by a positive field width.''
414 * -- ANSI X3J11
415 * They don't exclude field widths read from args.
416 */
417 GETASTER(width);
418 if (width >= 0)
419 goto rflag;
420 width = -width;
421 /* FALLTHROUGH */
422 case '-':
423 flags |= LADJUST;
424 goto rflag;
425 case '+':
426 sign = '+';
427 goto rflag;
428 case '.':
429 if ((ch = *fmt++) == '*') {
430 GETASTER(n);
431 prec = n < 0 ? -1 : n;
432 goto rflag;
433 }
434 n = 0;
435 while (is_digit(ch)) {
436 n = 10 * n + to_digit(ch);
437 ch = *fmt++;
438 }
439 if (ch == '$') {
440 nextarg = n;
441 if (argtable == NULL) {
442 argtable = statargtable;
443 __find_arguments(fmt0, orgap,
444 &argtable, &argtablesiz);
445 }
446 goto rflag;
447 }
448 prec = n < 0 ? -1 : n;
449 goto reswitch;
450 case '0':
451 /*
452 * ``Note that 0 is taken as a flag, not as the
453 * beginning of a field width.''
454 * -- ANSI X3J11
455 */
456 flags |= ZEROPAD;
457 goto rflag;
458 case '1': case '2': case '3': case '4':
459 case '5': case '6': case '7': case '8': case '9':
460 n = 0;
461 do {
462 n = 10 * n + to_digit(ch);
463 ch = *fmt++;
464 } while (is_digit(ch));
465 if (ch == '$') {
466 nextarg = n;
467 if (argtable == NULL) {
468 argtable = statargtable;
469 __find_arguments(fmt0, orgap,
470 &argtable, &argtablesiz);
471 }
472 goto rflag;
473 }
474 width = n;
475 goto reswitch;
476#ifdef FLOATING_POINT
477 case 'L':
478 flags |= LONGDBL;
479 goto rflag;
480#endif
481 case 'h':
Elliott Hughes1d13c642013-09-23 16:02:39 -0700482 if (*fmt == 'h') {
483 fmt++;
484 flags |= CHARINT;
485 } else {
486 flags |= SHORTINT;
487 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800488 goto rflag;
489 case 'j':
490 flags |= MAXINT;
491 goto rflag;
492 case 'l':
493 if (*fmt == 'l') {
494 fmt++;
495 flags |= LLONGINT;
496 } else {
497 flags |= LONGINT;
498 }
499 goto rflag;
500 case 'q':
501 flags |= LLONGINT;
502 goto rflag;
503 case 't':
504 flags |= PTRINT;
505 goto rflag;
506 case 'z':
507 flags |= SIZEINT;
508 goto rflag;
509 case 'c':
510 *(cp = buf) = GETARG(int);
511 size = 1;
512 sign = '\0';
513 break;
514 case 'D':
515 flags |= LONGINT;
516 /*FALLTHROUGH*/
517 case 'd':
518 case 'i':
519 _umax = SARG();
520 if ((intmax_t)_umax < 0) {
521 _umax = -_umax;
522 sign = '-';
523 }
524 base = DEC;
525 goto number;
526#ifdef FLOATING_POINT
527 case 'e':
528 case 'E':
529 case 'f':
530 case 'g':
531 case 'G':
532 if (prec == -1) {
533 prec = DEFPREC;
534 } else if ((ch == 'g' || ch == 'G') && prec == 0) {
535 prec = 1;
536 }
537
538 if (flags & LONGDBL) {
539 _double = (double) GETARG(long double);
540 } else {
541 _double = GETARG(double);
542 }
543
544 /* do this before tricky precision changes */
545 if (_my_isinf(_double)) {
546 if (_double < 0)
547 sign = '-';
548 cp = "Inf";
549 size = 3;
550 break;
551 }
552 if (_my_isnan(_double)) {
553 cp = "NaN";
554 size = 3;
555 break;
556 }
557
558 flags |= FPT;
559 cp = cvt(_double, prec, flags, &softsign,
560 &expt, ch, &ndig);
561 cp_free = cp;
562 if (ch == 'g' || ch == 'G') {
563 if (expt <= -4 || expt > prec)
564 ch = (ch == 'g') ? 'e' : 'E';
565 else
566 ch = 'g';
567 }
568 if (ch <= 'e') { /* 'e' or 'E' fmt */
569 --expt;
570 expsize = exponent(expstr, expt, ch);
571 size = expsize + ndig;
572 if (ndig > 1 || flags & ALT)
573 ++size;
574 } else if (ch == 'f') { /* f fmt */
575 if (expt > 0) {
576 size = expt;
577 if (prec || flags & ALT)
578 size += prec + 1;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400579 } else { /* "0.X" */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800580 size = prec + 2;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400581 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800582 } else if (expt >= ndig) { /* fixed g fmt */
583 size = expt;
584 if (flags & ALT)
585 ++size;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400586 } else {
587 size = ndig + (expt > 0 ? 1 : 2 - expt);
588 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589
590 if (softsign)
591 sign = '-';
592 break;
593#endif /* FLOATING_POINT */
594/* the Android security team suggests removing support for %n
595 * since it has no real practical value, and could lead to
Nick Kralevich9145ad32012-07-25 16:01:38 -0700596 * running malicious code (for really buggy programs that
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800597 * send to printf() user-generated formatting strings).
598 */
599#if 0
600 case 'n':
601 if (flags & LLONGINT)
602 *GETARG(long long *) = ret;
603 else if (flags & LONGINT)
604 *GETARG(long *) = ret;
605 else if (flags & SHORTINT)
606 *GETARG(short *) = ret;
607 else if (flags & CHARINT)
608 *GETARG(__signed char *) = ret;
609 else if (flags & PTRINT)
610 *GETARG(ptrdiff_t *) = ret;
611 else if (flags & SIZEINT)
612 *GETARG(ssize_t *) = ret;
613 else if (flags & MAXINT)
614 *GETARG(intmax_t *) = ret;
615 else
616 *GETARG(int *) = ret;
617 continue; /* no output */
618#endif
619 case 'O':
620 flags |= LONGINT;
621 /*FALLTHROUGH*/
622 case 'o':
623 _umax = UARG();
624 base = OCT;
625 goto nosign;
626 case 'p':
627 /*
628 * ``The argument shall be a pointer to void. The
629 * value of the pointer is converted to a sequence
630 * of printable characters, in an implementation-
631 * defined manner.''
632 * -- ANSI X3J11
633 */
634 /* NOSTRICT */
635 _umax = (u_long)GETARG(void *);
636 base = HEX;
637 xdigs = "0123456789abcdef";
638 flags |= HEXPREFIX;
639 ch = 'x';
640 goto nosign;
641 case 's':
642 if ((cp = GETARG(char *)) == NULL)
643 cp = "(null)";
644 if (prec >= 0) {
645 /*
646 * can't use strlen; can only look for the
647 * NUL in the first `prec' characters, and
648 * strlen() will go further.
649 */
650 char *p = memchr(cp, 0, prec);
651
652 if (p != NULL) {
653 size = p - cp;
654 if (size > prec)
655 size = prec;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400656 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657 size = prec;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400658 }
659 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660 size = strlen(cp);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400661 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662 sign = '\0';
663 break;
664 case 'U':
665 flags |= LONGINT;
666 /*FALLTHROUGH*/
667 case 'u':
668 _umax = UARG();
669 base = DEC;
670 goto nosign;
671 case 'X':
672 xdigs = "0123456789ABCDEF";
673 goto hex;
674 case 'x':
675 xdigs = "0123456789abcdef";
676hex: _umax = UARG();
677 base = HEX;
678 /* leading 0x/X only if non-zero */
679 if (flags & ALT && _umax != 0)
680 flags |= HEXPREFIX;
681
682 /* unsigned conversions */
683nosign: sign = '\0';
684 /*
685 * ``... diouXx conversions ... if a precision is
686 * specified, the 0 flag will be ignored.''
687 * -- ANSI X3J11
688 */
689number: if ((dprec = prec) >= 0)
690 flags &= ~ZEROPAD;
691
692 /*
693 * ``The result of converting a zero value with an
694 * explicit precision of zero is no characters.''
695 * -- ANSI X3J11
696 */
697 cp = buf + BUF;
698 if (_umax != 0 || prec != 0) {
699 /*
700 * Unsigned mod is hard, and unsigned mod
701 * by a constant is easier than that by
702 * a variable; hence this switch.
703 */
704 switch (base) {
705 case OCT:
706 do {
707 *--cp = to_char(_umax & 7);
708 _umax >>= 3;
709 } while (_umax);
710 /* handle octal leading 0 */
711 if (flags & ALT && *cp != '0')
712 *--cp = '0';
713 break;
714
715 case DEC:
716 /* many numbers are 1 digit */
717 while (_umax >= 10) {
718 *--cp = to_char(_umax % 10);
719 _umax /= 10;
720 }
721 *--cp = to_char(_umax);
722 break;
723
724 case HEX:
725 do {
726 *--cp = xdigs[_umax & 15];
727 _umax >>= 4;
728 } while (_umax);
729 break;
730
731 default:
732 cp = "bug in vfprintf: bad base";
733 size = strlen(cp);
734 goto skipsize;
735 }
736 }
737 size = buf + BUF - cp;
738 skipsize:
739 break;
740 default: /* "%?" prints ?, unless ? is NUL */
741 if (ch == '\0')
742 goto done;
743 /* pretend it was %c with argument ch */
744 cp = buf;
745 *cp = ch;
746 size = 1;
747 sign = '\0';
748 break;
749 }
750
751 /*
752 * All reasonable formats wind up here. At this point, `cp'
753 * points to a string which (if not flags&LADJUST) should be
754 * padded out to `width' places. If flags&ZEROPAD, it should
755 * first be prefixed by any sign or other prefix; otherwise,
756 * it should be blank padded before the prefix is emitted.
757 * After any left-hand padding and prefixing, emit zeroes
758 * required by a decimal [diouxX] precision, then print the
759 * string proper, then emit zeroes required by any leftover
760 * floating precision; finally, if LADJUST, pad with blanks.
761 *
762 * Compute actual size, so we know how much to pad.
763 * size excludes decimal prec; realsz includes it.
764 */
765 realsz = dprec > size ? dprec : size;
766 if (sign)
767 realsz++;
768 else if (flags & HEXPREFIX)
769 realsz+= 2;
770
771 /* right-adjusting blank padding */
772 if ((flags & (LADJUST|ZEROPAD)) == 0)
773 PAD(width - realsz, blanks);
774
775 /* prefix */
776 if (sign) {
777 PRINT(&sign, 1);
778 } else if (flags & HEXPREFIX) {
779 ox[0] = '0';
780 ox[1] = ch;
781 PRINT(ox, 2);
782 }
783
784 /* right-adjusting zero padding */
785 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
786 PAD(width - realsz, zeroes);
787
788 /* leading zeroes from decimal precision */
789 PAD(dprec - size, zeroes);
790
791 /* the string or number proper */
792#ifdef FLOATING_POINT
793 if ((flags & FPT) == 0) {
794 PRINT(cp, size);
795 } else { /* glue together f_p fragments */
796 if (ch >= 'f') { /* 'f' or 'g' */
797 if (_double == 0) {
798 /* kludge for __dtoa irregularity */
799 PRINT("0", 1);
800 if (expt < ndig || (flags & ALT) != 0) {
801 PRINT(decimal_point, 1);
802 PAD(ndig - 1, zeroes);
803 }
804 } else if (expt <= 0) {
805 PRINT("0", 1);
806 PRINT(decimal_point, 1);
807 PAD(-expt, zeroes);
808 PRINT(cp, ndig);
809 } else if (expt >= ndig) {
810 PRINT(cp, ndig);
811 PAD(expt - ndig, zeroes);
812 if (flags & ALT)
813 PRINT(".", 1);
814 } else {
815 PRINT(cp, expt);
816 cp += expt;
817 PRINT(".", 1);
818 PRINT(cp, ndig-expt);
819 }
820 } else { /* 'e' or 'E' */
821 if (ndig > 1 || flags & ALT) {
822 ox[0] = *cp++;
823 ox[1] = '.';
824 PRINT(ox, 2);
825 if (_double) {
826 PRINT(cp, ndig-1);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400827 } else {/* 0.[0..] */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828 /* __dtoa irregularity */
829 PAD(ndig - 1, zeroes);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400830 }
831 } else { /* XeYYY */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800832 PRINT(cp, 1);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400833 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800834 PRINT(expstr, expsize);
835 }
836 }
837#else
838 PRINT(cp, size);
839#endif
840 /* left-adjusting padding (always blank) */
841 if (flags & LADJUST)
842 PAD(width - realsz, blanks);
843
844 /* finally, adjust ret */
845 ret += width > realsz ? width : realsz;
846
847 FLUSH(); /* copy out the I/O vectors */
848#if 1 /* BIONIC: remove memory leak when printing doubles */
849 if (cp_free) {
850 free(cp_free);
851 cp_free = NULL;
852 }
853#endif
854 }
855done:
856 FLUSH();
857error:
858#if 1 /* BIONIC: remove memory leak when printing doubles */
859 if (cp_free) {
860 free(cp_free);
861 cp_free = NULL;
862 }
863#endif
864 if (argtable != NULL && argtable != statargtable) {
865 munmap(argtable, argtablesiz);
866 argtable = NULL;
867 }
Yaroslav Miroshnychenkoc7dcd672012-06-14 12:41:54 +0200868 va_end(orgap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800869 return (__sferror(fp) ? EOF : ret);
870 /* NOTREACHED */
871}
872
873/*
874 * Type ids for argument type table.
875 */
876#define T_UNUSED 0
877#define T_SHORT 1
878#define T_U_SHORT 2
879#define TP_SHORT 3
880#define T_INT 4
881#define T_U_INT 5
882#define TP_INT 6
883#define T_LONG 7
884#define T_U_LONG 8
885#define TP_LONG 9
886#define T_LLONG 10
887#define T_U_LLONG 11
888#define TP_LLONG 12
889#define T_DOUBLE 13
890#define T_LONG_DOUBLE 14
891#define TP_CHAR 15
892#define TP_VOID 16
893#define T_PTRINT 17
894#define TP_PTRINT 18
895#define T_SIZEINT 19
896#define T_SSIZEINT 20
897#define TP_SSIZEINT 21
898#define T_MAXINT 22
899#define T_MAXUINT 23
900#define TP_MAXINT 24
901
902/*
903 * Find all arguments when a positional parameter is encountered. Returns a
904 * table, indexed by argument number, of pointers to each arguments. The
905 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
906 * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
907 * used since we are attempting to make snprintf thread safe, and alloca is
908 * problematic since we have nested functions..)
909 */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400910static int
911__find_arguments(const char *fmt0, va_list ap, union arg **argtable,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800912 size_t *argtablesiz)
913{
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400914 char *fmt; /* format string */
915 int ch; /* character from fmt */
916 int n, n2; /* handy integer (short term usage) */
917 char *cp; /* handy char pointer (short term usage) */
918 int flags; /* flags as above */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800919 unsigned char *typetable; /* table of types */
920 unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
921 int tablesize; /* current size of type table */
922 int tablemax; /* largest used index in table */
923 int nextarg; /* 1-based argument index */
924 wchar_t wc;
925 void* ps;
926
927 /*
928 * Add an argument type to the table, expanding if necessary.
929 */
930#define ADDTYPE(type) \
931 ((nextarg >= tablesize) ? \
932 __grow_type_table(&typetable, &tablesize) : 0, \
933 (nextarg > tablemax) ? tablemax = nextarg : 0, \
934 typetable[nextarg++] = type)
935
936#define ADDSARG() \
937 ((flags&MAXINT) ? ADDTYPE(T_MAXINT) : \
938 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
939 ((flags&SIZEINT) ? ADDTYPE(T_SSIZEINT) : \
940 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
941 ((flags&LONGINT) ? ADDTYPE(T_LONG) : \
942 ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : ADDTYPE(T_INT)))))))
943
944#define ADDUARG() \
945 ((flags&MAXINT) ? ADDTYPE(T_MAXUINT) : \
946 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
947 ((flags&SIZEINT) ? ADDTYPE(T_SIZEINT) : \
948 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
949 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \
950 ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : ADDTYPE(T_U_INT)))))))
951
952 /*
953 * Add * arguments to the type array.
954 */
955#define ADDASTER() \
956 n2 = 0; \
957 cp = fmt; \
958 while (is_digit(*cp)) { \
959 n2 = 10 * n2 + to_digit(*cp); \
960 cp++; \
961 } \
962 if (*cp == '$') { \
963 int hold = nextarg; \
964 nextarg = n2; \
965 ADDTYPE(T_INT); \
966 nextarg = hold; \
967 fmt = ++cp; \
968 } else { \
969 ADDTYPE(T_INT); \
970 }
971 fmt = (char *)fmt0;
972 typetable = stattypetable;
973 tablesize = STATIC_ARG_TBL_SIZE;
974 tablemax = 0;
975 nextarg = 1;
976 memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
977 memset(&ps, 0, sizeof(ps));
978
979 /*
980 * Scan the format for conversions (`%' character).
981 */
982 for (;;) {
983 cp = fmt;
984#if 1 /* BIONIC */
985 n = -1;
986 while ((wc = *fmt) != 0) {
987 if (wc == '%') {
988 n = 1;
989 break;
990 }
991 fmt++;
992 }
993#else
994 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
995 fmt += n;
996 if (wc == '%') {
997 fmt--;
998 break;
999 }
1000 }
1001#endif
1002 if (n <= 0)
1003 goto done;
1004 fmt++; /* skip over '%' */
1005
1006 flags = 0;
1007
1008rflag: ch = *fmt++;
1009reswitch: switch (ch) {
1010 case ' ':
1011 case '#':
1012 goto rflag;
1013 case '*':
1014 ADDASTER();
1015 goto rflag;
1016 case '-':
1017 case '+':
1018 goto rflag;
1019 case '.':
1020 if ((ch = *fmt++) == '*') {
1021 ADDASTER();
1022 goto rflag;
1023 }
1024 while (is_digit(ch)) {
1025 ch = *fmt++;
1026 }
1027 goto reswitch;
1028 case '0':
1029 goto rflag;
1030 case '1': case '2': case '3': case '4':
1031 case '5': case '6': case '7': case '8': case '9':
1032 n = 0;
1033 do {
1034 n = 10 * n + to_digit(ch);
1035 ch = *fmt++;
1036 } while (is_digit(ch));
1037 if (ch == '$') {
1038 nextarg = n;
1039 goto rflag;
1040 }
1041 goto reswitch;
1042#ifdef FLOATING_POINT
1043 case 'L':
1044 flags |= LONGDBL;
1045 goto rflag;
1046#endif
1047 case 'h':
1048 if (*fmt == 'h') {
1049 fmt++;
1050 flags |= CHARINT;
1051 } else {
1052 flags |= SHORTINT;
1053 }
1054 goto rflag;
1055 case 'l':
1056 if (*fmt == 'l') {
1057 fmt++;
1058 flags |= LLONGINT;
1059 } else {
1060 flags |= LONGINT;
1061 }
1062 goto rflag;
1063 case 'q':
1064 flags |= LLONGINT;
1065 goto rflag;
1066 case 't':
1067 flags |= PTRINT;
1068 goto rflag;
1069 case 'z':
1070 flags |= SIZEINT;
1071 goto rflag;
1072 case 'c':
1073 ADDTYPE(T_INT);
1074 break;
1075 case 'D':
1076 flags |= LONGINT;
1077 /*FALLTHROUGH*/
1078 case 'd':
1079 case 'i':
1080 ADDSARG();
1081 break;
1082#ifdef FLOATING_POINT
1083 case 'e':
1084 case 'E':
1085 case 'f':
1086 case 'g':
1087 case 'G':
1088 if (flags & LONGDBL)
1089 ADDTYPE(T_LONG_DOUBLE);
1090 else
1091 ADDTYPE(T_DOUBLE);
1092 break;
1093#endif /* FLOATING_POINT */
1094 case 'n':
1095 if (flags & LLONGINT)
1096 ADDTYPE(TP_LLONG);
1097 else if (flags & LONGINT)
1098 ADDTYPE(TP_LONG);
1099 else if (flags & SHORTINT)
1100 ADDTYPE(TP_SHORT);
1101 else if (flags & PTRINT)
1102 ADDTYPE(TP_PTRINT);
1103 else if (flags & SIZEINT)
1104 ADDTYPE(TP_SSIZEINT);
1105 else if (flags & MAXINT)
1106 ADDTYPE(TP_MAXINT);
1107 else
1108 ADDTYPE(TP_INT);
1109 continue; /* no output */
1110 case 'O':
1111 flags |= LONGINT;
1112 /*FALLTHROUGH*/
1113 case 'o':
1114 ADDUARG();
1115 break;
1116 case 'p':
1117 ADDTYPE(TP_VOID);
1118 break;
1119 case 's':
1120 ADDTYPE(TP_CHAR);
1121 break;
1122 case 'U':
1123 flags |= LONGINT;
1124 /*FALLTHROUGH*/
1125 case 'u':
1126 case 'X':
1127 case 'x':
1128 ADDUARG();
1129 break;
1130 default: /* "%?" prints ?, unless ? is NUL */
1131 if (ch == '\0')
1132 goto done;
1133 break;
1134 }
1135 }
1136done:
1137 /*
1138 * Build the argument table.
1139 */
1140 if (tablemax >= STATIC_ARG_TBL_SIZE) {
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001141 *argtablesiz = sizeof(union arg) * (tablemax + 1);
1142 *argtable = mmap(NULL, *argtablesiz,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001143 PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001144 if (*argtable == MAP_FAILED)
1145 return (-1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001146 }
1147
1148#if 0
1149 /* XXX is this required? */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001150 (*argtable)[0].intarg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001151#endif
1152 for (n = 1; n <= tablemax; n++) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001153 switch (typetable[n]) {
1154 case T_UNUSED:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001155 (*argtable)[n].intarg = va_arg(ap, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001156 break;
1157 case T_SHORT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001158 (*argtable)[n].intarg = va_arg(ap, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159 break;
1160 case T_U_SHORT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001161 (*argtable)[n].intarg = va_arg(ap, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001162 break;
1163 case TP_SHORT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001164 (*argtable)[n].pshortarg = va_arg(ap, short *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001165 break;
1166 case T_INT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001167 (*argtable)[n].intarg = va_arg(ap, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001168 break;
1169 case T_U_INT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001170 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001171 break;
1172 case TP_INT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001173 (*argtable)[n].pintarg = va_arg(ap, int *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001174 break;
1175 case T_LONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001176 (*argtable)[n].longarg = va_arg(ap, long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001177 break;
1178 case T_U_LONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001179 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001180 break;
1181 case TP_LONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001182 (*argtable)[n].plongarg = va_arg(ap, long *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001183 break;
1184 case T_LLONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001185 (*argtable)[n].longlongarg = va_arg(ap, long long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001186 break;
1187 case T_U_LLONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001188 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189 break;
1190 case TP_LLONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001191 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001192 break;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001193#ifdef FLOATING_POINT
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194 case T_DOUBLE:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001195 (*argtable)[n].doublearg = va_arg(ap, double);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196 break;
1197 case T_LONG_DOUBLE:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001198 (*argtable)[n].longdoublearg = va_arg(ap, long double);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199 break;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001200#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001201 case TP_CHAR:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001202 (*argtable)[n].pchararg = va_arg(ap, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 break;
1204 case TP_VOID:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001205 (*argtable)[n].pvoidarg = va_arg(ap, void *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206 break;
1207 case T_PTRINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001208 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209 break;
1210 case TP_PTRINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001211 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001212 break;
1213 case T_SIZEINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001214 (*argtable)[n].sizearg = va_arg(ap, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001215 break;
1216 case T_SSIZEINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001217 (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001218 break;
1219 case TP_SSIZEINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001220 (*argtable)[n].pssizearg = va_arg(ap, ssize_t *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001221 break;
1222 case TP_MAXINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001223 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001224 break;
1225 }
1226 }
1227
1228 if (typetable != NULL && typetable != stattypetable) {
1229 munmap(typetable, *argtablesiz);
1230 typetable = NULL;
1231 }
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001232 return (0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001233}
1234
1235/*
1236 * Increase the size of the type table.
1237 */
1238static int
1239__grow_type_table(unsigned char **typetable, int *tablesize)
1240{
1241 unsigned char *oldtable = *typetable;
1242 int newsize = *tablesize * 2;
1243
1244 if (*tablesize == STATIC_ARG_TBL_SIZE) {
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001245 *typetable = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001246 MAP_ANON|MAP_PRIVATE, -1, 0);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001247 if (*typetable == MAP_FAILED)
1248 return (-1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001249 memcpy( *typetable, oldtable, *tablesize);
1250 } else {
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001251 unsigned char *new = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252 MAP_ANON|MAP_PRIVATE, -1, 0);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001253 if (new == MAP_FAILED)
1254 return (-1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001255 memmove(new, *typetable, *tablesize);
1256 munmap(*typetable, *tablesize);
1257 *typetable = new;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001258 }
1259 memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
1260
1261 *tablesize = newsize;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001262 return (0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001263}
1264
1265
1266#ifdef FLOATING_POINT
1267
1268extern char *__dtoa(double, int, int, int *, int *, char **);
1269
1270static char *
1271cvt(double value, int ndigits, int flags, char *sign, int *decpt, int ch,
1272 int *length)
1273{
1274 int mode, dsgn;
1275 char *digits, *bp, *rve;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001276
1277 if (ch == 'f') {
1278 mode = 3; /* ndigits after the decimal point */
1279 } else {
1280 /* To obtain ndigits after the decimal point for the 'e'
1281 * and 'E' formats, round to ndigits + 1 significant
1282 * figures.
1283 */
1284 if (ch == 'e' || ch == 'E') {
1285 ndigits++;
1286 }
1287 mode = 2; /* ndigits significant digits */
1288 }
1289
1290 if (value < 0) {
1291 value = -value;
1292 *sign = '-';
1293 } else
1294 *sign = '\000';
1295 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001296 if ((ch != 'g' && ch != 'G') || flags & ALT) {/* Print trailing zeros */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001297 bp = digits + ndigits;
1298 if (ch == 'f') {
1299 if (*digits == '0' && value)
1300 *decpt = -ndigits + 1;
1301 bp += *decpt;
1302 }
1303 if (value == 0) /* kludge for __dtoa irregularity */
1304 rve = bp;
1305 while (rve < bp)
1306 *rve++ = '0';
1307 }
1308 *length = rve - digits;
1309 return (digits);
1310}
1311
1312static int
1313exponent(char *p0, int exp, int fmtch)
1314{
1315 char *p, *t;
1316 char expbuf[MAXEXP];
1317
1318 p = p0;
1319 *p++ = fmtch;
1320 if (exp < 0) {
1321 exp = -exp;
1322 *p++ = '-';
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001323 } else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001324 *p++ = '+';
1325 t = expbuf + MAXEXP;
1326 if (exp > 9) {
1327 do {
1328 *--t = to_char(exp % 10);
1329 } while ((exp /= 10) > 9);
1330 *--t = to_char(exp);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001331 for (; t < expbuf + MAXEXP; *p++ = *t++)
1332 /* nothing */;
1333 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001334 *p++ = '0';
1335 *p++ = to_char(exp);
1336 }
1337 return (p - p0);
1338}
1339
1340
1341/* BIONIC */
1342#include <machine/ieee.h>
1343typedef union {
1344 double d;
1345 struct ieee_double i;
1346} ieee_u;
1347
1348static int
1349_my_isinf (double value)
1350{
1351 ieee_u u;
1352
1353 u.d = value;
1354 return (u.i.dbl_exp == 2047 && u.i.dbl_frach == 0 && u.i.dbl_fracl == 0);
1355}
1356
1357static int
1358_my_isnan (double value)
1359{
1360 ieee_u u;
1361
1362 u.d = value;
1363 return (u.i.dbl_exp == 2047 && (u.i.dbl_frach != 0 || u.i.dbl_fracl != 0));
1364}
1365#endif /* FLOATING_POINT */