printf-pos.c (113142) | printf-pos.c (113146) |
---|---|
1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 24 unchanged lines hidden (view full) --- 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 39#endif /* LIBC_SCCS and not lint */ 40#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 24 unchanged lines hidden (view full) --- 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 39#endif /* LIBC_SCCS and not lint */ 40#include <sys/cdefs.h> |
41__FBSDID("$FreeBSD: head/lib/libc/stdio/vfprintf.c 113142 2003-04-05 22:03:43Z das $"); | 41__FBSDID("$FreeBSD: head/lib/libc/stdio/vfprintf.c 113146 2003-04-05 22:11:42Z das $"); |
42 43/* 44 * Actual printf innards. 45 * 46 * This code is large and complicated... 47 */ 48 49#include "namespace.h" --- 56 unchanged lines hidden (view full) --- 106 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG, 107 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET, 108 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR, 109 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR 110}; 111 112static int __sprint(FILE *, struct __suio *); 113static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0); | 42 43/* 44 * Actual printf innards. 45 * 46 * This code is large and complicated... 47 */ 48 49#include "namespace.h" --- 56 unchanged lines hidden (view full) --- 106 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG, 107 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET, 108 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR, 109 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR 110}; 111 112static int __sprint(FILE *, struct __suio *); 113static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0); |
114static char *__ujtoa(uintmax_t, char *, int, int, char *, int, char, | 114static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char, |
115 const char *); | 115 const char *); |
116static char *__ultoa(u_long, char *, int, int, char *, int, char, | 116static char *__ultoa(u_long, char *, int, int, const char *, int, char, |
117 const char *); 118static char *__wcsconv(wchar_t *, int); 119static void __find_arguments(const char *, va_list, union arg **); 120static void __grow_type_table(int, enum typeid **, int *); 121 122/* 123 * Flush out all the vectors defined by the given uio, 124 * then reset it so that it can be reused. --- 55 unchanged lines hidden (view full) --- 180 181/* 182 * Convert an unsigned long to ASCII for printf purposes, returning 183 * a pointer to the first character of the string representation. 184 * Octal numbers can be forced to have a leading zero; hex numbers 185 * use the given digits. 186 */ 187static char * | 117 const char *); 118static char *__wcsconv(wchar_t *, int); 119static void __find_arguments(const char *, va_list, union arg **); 120static void __grow_type_table(int, enum typeid **, int *); 121 122/* 123 * Flush out all the vectors defined by the given uio, 124 * then reset it so that it can be reused. --- 55 unchanged lines hidden (view full) --- 180 181/* 182 * Convert an unsigned long to ASCII for printf purposes, returning 183 * a pointer to the first character of the string representation. 184 * Octal numbers can be forced to have a leading zero; hex numbers 185 * use the given digits. 186 */ 187static char * |
188__ultoa(u_long val, char *endp, int base, int octzero, char *xdigs, | 188__ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs, |
189 int needgrp, char thousep, const char *grp) 190{ 191 char *cp = endp; 192 long sval; 193 int ndig; 194 195 /* 196 * Handle the three cases separately, in the hope of getting --- 60 unchanged lines hidden (view full) --- 257 default: /* oops */ 258 abort(); 259 } 260 return (cp); 261} 262 263/* Identical to __ultoa, but for intmax_t. */ 264static char * | 189 int needgrp, char thousep, const char *grp) 190{ 191 char *cp = endp; 192 long sval; 193 int ndig; 194 195 /* 196 * Handle the three cases separately, in the hope of getting --- 60 unchanged lines hidden (view full) --- 257 default: /* oops */ 258 abort(); 259 } 260 return (cp); 261} 262 263/* Identical to __ultoa, but for intmax_t. */ 264static char * |
265__ujtoa(uintmax_t val, char *endp, int base, int octzero, char *xdigs, | 265__ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs, |
266 int needgrp, char thousep, const char *grp) 267{ 268 char *cp = endp; 269 intmax_t sval; 270 int ndig; 271 272 /* quick test for small values; __ultoa is typically much faster */ 273 /* (perhaps instead we should run until small, then call __ultoa?) */ --- 128 unchanged lines hidden (view full) --- 402 403 FLOCKFILE(fp); 404 ret = __vfprintf(fp, fmt0, ap); 405 FUNLOCKFILE(fp); 406 return (ret); 407} 408 409#ifdef FLOATING_POINT | 266 int needgrp, char thousep, const char *grp) 267{ 268 char *cp = endp; 269 intmax_t sval; 270 int ndig; 271 272 /* quick test for small values; __ultoa is typically much faster */ 273 /* (perhaps instead we should run until small, then call __ultoa?) */ --- 128 unchanged lines hidden (view full) --- 402 403 FLOCKFILE(fp); 404 ret = __vfprintf(fp, fmt0, ap); 405 FUNLOCKFILE(fp); 406 return (ret); 407} 408 409#ifdef FLOATING_POINT |
410 411#define dtoa __dtoa 412#define freedtoa __freedtoa 413 414#include <float.h> |
|
410#include <math.h> 411#include "floatio.h" | 415#include <math.h> 416#include "floatio.h" |
417#include "gdtoa.h" |
|
412 413#define DEFPREC 6 414 | 418 419#define DEFPREC 6 420 |
415extern char *__dtoa(double, int, int, int *, int *, char **); 416extern void __freedtoa(char *s); 417 418static char *cvt(double, int, int, char *, int *, int, int *); | |
419static int exponent(char *, int, int); 420 421#endif /* FLOATING_POINT */ 422 423/* 424 * The size of the buffer we use as scratch space for integer 425 * conversions, among other things. Technically, we would need the 426 * most space for base 10 conversions with thousands' grouping 427 * characters between each pair of digits. 100 bytes is a 428 * conservative overestimate even for a 128-bit uintmax_t. 429 */ 430#define BUF 100 431 432#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 433 434/* 435 * Flags used during conversion. 436 */ 437#define ALT 0x001 /* alternate form */ | 421static int exponent(char *, int, int); 422 423#endif /* FLOATING_POINT */ 424 425/* 426 * The size of the buffer we use as scratch space for integer 427 * conversions, among other things. Technically, we would need the 428 * most space for base 10 conversions with thousands' grouping 429 * characters between each pair of digits. 100 bytes is a 430 * conservative overestimate even for a 128-bit uintmax_t. 431 */ 432#define BUF 100 433 434#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 435 436/* 437 * Flags used during conversion. 438 */ 439#define ALT 0x001 /* alternate form */ |
438#define HEXPREFIX 0x002 /* add 0x or 0X prefix */ | |
439#define LADJUST 0x004 /* left adjustment */ 440#define LONGDBL 0x008 /* long double */ 441#define LONGINT 0x010 /* long integer */ 442#define LLONGINT 0x020 /* long long integer */ 443#define SHORTINT 0x040 /* short integer */ 444#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 445#define FPT 0x100 /* Floating point number */ 446#define GROUPING 0x200 /* use grouping ("'" flag) */ --- 12 unchanged lines hidden (view full) --- 459 char *fmt; /* format string */ 460 int ch; /* character from fmt */ 461 int n, n2; /* handy integer (short term usage) */ 462 char *cp; /* handy char pointer (short term usage) */ 463 struct __siov *iovp; /* for PRINT macro */ 464 int flags; /* flags as above */ 465 int ret; /* return value accumulator */ 466 int width; /* width from format (%8d), or 0 */ | 440#define LADJUST 0x004 /* left adjustment */ 441#define LONGDBL 0x008 /* long double */ 442#define LONGINT 0x010 /* long integer */ 443#define LLONGINT 0x020 /* long long integer */ 444#define SHORTINT 0x040 /* short integer */ 445#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 446#define FPT 0x100 /* Floating point number */ 447#define GROUPING 0x200 /* use grouping ("'" flag) */ --- 12 unchanged lines hidden (view full) --- 460 char *fmt; /* format string */ 461 int ch; /* character from fmt */ 462 int n, n2; /* handy integer (short term usage) */ 463 char *cp; /* handy char pointer (short term usage) */ 464 struct __siov *iovp; /* for PRINT macro */ 465 int flags; /* flags as above */ 466 int ret; /* return value accumulator */ 467 int width; /* width from format (%8d), or 0 */ |
467 int prec; /* precision from format (%.3d), or -1 */ | 468 int prec; /* precision from format; <0 for N/A */ |
468 char sign; /* sign prefix (' ', '+', '-', or \0) */ 469 char thousands_sep; /* locale specific thousands separator */ 470 const char *grouping; /* locale specific numeric grouping rules */ 471#ifdef FLOATING_POINT | 469 char sign; /* sign prefix (' ', '+', '-', or \0) */ 470 char thousands_sep; /* locale specific thousands separator */ 471 const char *grouping; /* locale specific numeric grouping rules */ 472#ifdef FLOATING_POINT |
473 /* 474 * We can decompose the printed representation of floating 475 * point numbers into several parts, some of which may be empty: 476 * 477 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 478 * A B ---C--- D E F 479 * 480 * A: 'sign' holds this value if present; '\0' otherwise 481 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 482 * C: cp points to the string MMMNNN. Leading and trailing 483 * zeros are not in the string and must be added. 484 * D: expchar holds this character; '\0' if no exponent, e.g. %f 485 * F: at least two digits for decimal, at least one digit for hex 486 */ |
|
472 char *decimal_point; /* locale specific decimal point */ | 487 char *decimal_point; /* locale specific decimal point */ |
473 char softsign; /* temporary negative sign for floats */ 474 double _double; /* double precision arguments %[eEfgG] */ | 488 int signflag; /* true if float is negative */ 489 union { /* floating point arguments %[aAeEfFgG] */ 490 double dbl; 491 long double ldbl; 492 } fparg; |
475 int expt; /* integer value of exponent */ | 493 int expt; /* integer value of exponent */ |
494 char expchar; /* exponent character: [eEpP\0] */ 495 char *dtoaend; /* pointer to end of converted digits */ |
|
476 int expsize; /* character count for expstr */ | 496 int expsize; /* character count for expstr */ |
477 int ndig; /* actual number of digits returned by cvt */ 478 char expstr[MAXEXPDIG+2]; /* buffer for exponent string */ | 497 int lead; /* sig figs before decimal or group sep */ 498 int ndig; /* actual number of digits returned by dtoa */ 499 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ |
479 char *dtoaresult; /* buffer allocated by dtoa */ | 500 char *dtoaresult; /* buffer allocated by dtoa */ |
501 int nseps; /* number of group separators with ' */ 502 int nrepeats; /* number of repeats of the last group */ |
|
480#endif 481 u_long ulval; /* integer arguments %[diouxX] */ 482 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 483 int base; /* base for [diouxX] conversion */ 484 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 485 int realsz; /* field size expanded by dprec, sign, etc */ 486 int size; /* size of converted field or string */ 487 int prsize; /* max size of printed field */ | 503#endif 504 u_long ulval; /* integer arguments %[diouxX] */ 505 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 506 int base; /* base for [diouxX] conversion */ 507 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 508 int realsz; /* field size expanded by dprec, sign, etc */ 509 int size; /* size of converted field or string */ 510 int prsize; /* max size of printed field */ |
488 char *xdigs; /* digits for [xX] conversion */ | 511 const char *xdigs; /* digits for %[xX] conversion */ |
489#define NIOV 8 490 struct __suio uio; /* output information: summary */ 491 struct __siov iov[NIOV];/* ... and individual io vectors */ 492 char buf[BUF]; /* buffer with space for digits of uintmax_t */ | 512#define NIOV 8 513 struct __suio uio; /* output information: summary */ 514 struct __siov iov[NIOV];/* ... and individual io vectors */ 515 char buf[BUF]; /* buffer with space for digits of uintmax_t */ |
493 char ox[2]; /* space for 0x hex-prefix */ | 516 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ |
494 union arg *argtable; /* args, built due to positional arg */ 495 union arg statargtable [STATIC_ARG_TBL_SIZE]; 496 int nextarg; /* 1-based argument index */ 497 va_list orgap; /* original argument pointer */ 498 char *convbuf; /* wide to multibyte conversion result */ 499 500 /* 501 * Choose PADSIZE to trade efficiency vs. size. If larger printf 502 * fields occur frequently, increase PADSIZE and make the initialisers 503 * below longer. 504 */ 505#define PADSIZE 16 /* pad chunk size */ 506 static char blanks[PADSIZE] = 507 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 508 static char zeroes[PADSIZE] = 509 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 510 | 517 union arg *argtable; /* args, built due to positional arg */ 518 union arg statargtable [STATIC_ARG_TBL_SIZE]; 519 int nextarg; /* 1-based argument index */ 520 va_list orgap; /* original argument pointer */ 521 char *convbuf; /* wide to multibyte conversion result */ 522 523 /* 524 * Choose PADSIZE to trade efficiency vs. size. If larger printf 525 * fields occur frequently, increase PADSIZE and make the initialisers 526 * below longer. 527 */ 528#define PADSIZE 16 /* pad chunk size */ 529 static char blanks[PADSIZE] = 530 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 531 static char zeroes[PADSIZE] = 532 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 533 |
534 static const char xdigs_lower[16] = "0123456789abcdef"; 535 static const char xdigs_upper[16] = "0123456789ABCDEF"; 536 |
|
511 /* 512 * BEWARE, these `goto error' on error, and PAD uses `n'. 513 */ 514#define PRINT(ptr, len) { \ 515 iovp->iov_base = (ptr); \ 516 iovp->iov_len = (len); \ 517 uio.uio_resid += (len); \ 518 iovp++; \ --- 123 unchanged lines hidden (view full) --- 642 goto done; 643 fmt++; /* skip over '%' */ 644 645 flags = 0; 646 dprec = 0; 647 width = 0; 648 prec = -1; 649 sign = '\0'; | 537 /* 538 * BEWARE, these `goto error' on error, and PAD uses `n'. 539 */ 540#define PRINT(ptr, len) { \ 541 iovp->iov_base = (ptr); \ 542 iovp->iov_len = (len); \ 543 uio.uio_resid += (len); \ 544 iovp++; \ --- 123 unchanged lines hidden (view full) --- 668 goto done; 669 fmt++; /* skip over '%' */ 670 671 flags = 0; 672 dprec = 0; 673 width = 0; 674 prec = -1; 675 sign = '\0'; |
676 ox[1] = '\0'; |
|
650 651rflag: ch = *fmt++; 652reswitch: switch (ch) { 653 case ' ': 654 /*- 655 * ``If the space and + flags both appear, the space 656 * flag will be ignored.'' 657 * -- ANSI X3J11 --- 138 unchanged lines hidden (view full) --- 796 } 797 } 798 base = 10; 799 goto number; 800#ifdef FLOATING_POINT 801#ifdef HEXFLOAT 802 case 'a': 803 case 'A': | 677 678rflag: ch = *fmt++; 679reswitch: switch (ch) { 680 case ' ': 681 /*- 682 * ``If the space and + flags both appear, the space 683 * flag will be ignored.'' 684 * -- ANSI X3J11 --- 138 unchanged lines hidden (view full) --- 823 } 824 } 825 base = 10; 826 goto number; 827#ifdef FLOATING_POINT 828#ifdef HEXFLOAT 829 case 'a': 830 case 'A': |
831 if (ch == 'a') { 832 ox[1] = 'x'; 833 xdigs = xdigs_lower; 834 expchar = 'p'; 835 } else { 836 ox[1] = 'X'; 837 xdigs = xdigs_upper; 838 expchar = 'P'; 839 } 840 /* 841 * XXX We don't actually have a conversion 842 * XXX routine for this yet. 843 */ 844 if (flags & LONGDBL) { 845 fparg.ldbl = (double)GETARG(long double); 846 dtoaresult = cp = 847 __hldtoa(fparg.ldbl, xdigs, prec, 848 &expt, &signflag, &dtoaend); 849 } else { 850 fparg.dbl = GETARG(double); 851 dtoaresult = cp = 852 __hdtoa(fparg.dbl, xdigs, prec, 853 &expt, &signflag, &dtoaend); 854 } 855 goto fp_begin; |
|
804#endif 805 case 'e': 806 case 'E': | 856#endif 857 case 'e': 858 case 'E': |
807 /*- 808 * Grouping apply to %i, %d, %u, %f, %F, %g, %G 809 * conversion specifiers only. For other conversions 810 * behavior is undefined. 811 * -- POSIX 812 */ 813 flags &= ~GROUPING; 814 /*FALLTHROUGH*/ | 859 expchar = ch; 860 if (prec < 0) /* account for digit before decpt */ 861 prec = DEFPREC + 1; 862 else 863 prec++; 864 goto fp_begin; |
815 case 'f': 816 case 'F': | 865 case 'f': 866 case 'F': |
867 expchar = '\0'; |
|
817 goto fp_begin; 818 case 'g': 819 case 'G': | 868 goto fp_begin; 869 case 'g': 870 case 'G': |
871 expchar = ch - ('g' - 'e'); |
|
820 if (prec == 0) 821 prec = 1; | 872 if (prec == 0) 873 prec = 1; |
822fp_begin: if (prec == -1) | 874fp_begin: 875 if (prec < 0) |
823 prec = DEFPREC; | 876 prec = DEFPREC; |
824 if (flags & LONGDBL) 825 /* XXX this loses precision. */ 826 _double = (double)GETARG(long double); 827 else 828 _double = GETARG(double); 829 /* do this before tricky precision changes */ 830 if (isinf(_double)) { 831 if (_double < 0) 832 sign = '-'; 833 if (isupper(ch)) 834 cp = "INF"; 835 else 836 cp = "inf"; 837 size = 3; 838 break; | 877 if (dtoaresult != NULL) 878 freedtoa(dtoaresult); 879 if (flags & LONGDBL) { 880 fparg.ldbl = GETARG(long double); 881 dtoaresult = cp = 882 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 883 &expt, &signflag, &dtoaend); 884 } else { 885 fparg.dbl = GETARG(double); 886 dtoaresult = cp = 887 dtoa(fparg.dbl, expchar ? 2 : 3, prec, 888 &expt, &signflag, &dtoaend); 889 if (expt == 9999) 890 expt = INT_MAX; |
839 } | 891 } |
840 if (isnan(_double)) { 841 if (isupper(ch)) 842 cp = "NAN"; 843 else 844 cp = "nan"; | 892 if (signflag) 893 sign = '-'; 894 if (expt == INT_MAX) { /* inf or nan */ 895 if (*cp == 'N') { 896 cp = (ch >= 'a') ? "nan" : "NAN"; 897 sign = '\0'; 898 } else 899 cp = (ch >= 'a') ? "inf" : "INF"; |
845 size = 3; 846 break; 847 } 848 flags |= FPT; | 900 size = 3; 901 break; 902 } 903 flags |= FPT; |
849 if (dtoaresult != NULL) { 850 __freedtoa(dtoaresult); 851 dtoaresult = NULL; 852 } 853 dtoaresult = cp = cvt(_double, prec, flags, &softsign, 854 &expt, ch, &ndig); | 904 ndig = dtoaend - cp; |
855 if (ch == 'g' || ch == 'G') { | 905 if (ch == 'g' || ch == 'G') { |
856 if (expt <= -4 || expt > prec) 857 ch = (ch == 'g') ? 'e' : 'E'; 858 else 859 ch = 'g'; | 906 if (expt > -4 && expt <= prec) { 907 /* Make %[gG] smell like %[fF] */ 908 expchar = '\0'; 909 if (flags & ALT) 910 prec -= expt; 911 else 912 prec = ndig - expt; 913 if (prec < 0) 914 prec = 0; 915 } |
860 } | 916 } |
861 if (ch == 'e' || ch == 'E') { 862 --expt; 863 expsize = exponent(expstr, expt, ch); 864 size = expsize + ndig; 865 if (ndig > 1 || flags & ALT) | 917 if (expchar) { 918 expsize = exponent(expstr, expt - 1, expchar); 919 size = expsize + prec; 920 if (prec || flags & ALT) |
866 ++size; | 921 ++size; |
867 } else if (ch == 'f' || ch == 'F') { | 922 } else { |
868 if (expt > 0) { 869 size = expt; 870 if (prec || flags & ALT) 871 size += prec + 1; 872 } else /* "0.X" */ 873 size = prec + 2; | 923 if (expt > 0) { 924 size = expt; 925 if (prec || flags & ALT) 926 size += prec + 1; 927 } else /* "0.X" */ 928 size = prec + 2; |
874 } else if (expt >= ndig) { /* fixed g fmt */ 875 size = expt; 876 if (flags & ALT) 877 ++size; 878 } else 879 size = ndig + (expt > 0 ? 880 1 : 2 - expt); 881 882 if (softsign) 883 sign = '-'; | 929 if (grouping && expt > 0) { 930 /* space for thousands' grouping */ 931 nseps = nrepeats = 0; 932 lead = expt; 933 while (*grouping != CHAR_MAX) { 934 if (lead <= *grouping) 935 break; 936 lead -= *grouping; 937 if (*(grouping+1)) { 938 nseps++; 939 grouping++; 940 } else 941 nrepeats++; 942 } 943 size += nseps + nrepeats; 944 } else 945 lead = (expt < ndig) ? expt : ndig; 946 } |
884 break; 885#endif /* FLOATING_POINT */ 886 case 'n': 887 /* 888 * Assignment-like behavior is specified if the 889 * value overflows or is otherwise unrepresentable. 890 * C99 says to use `signed char' for %hhn conversions. 891 */ --- 29 unchanged lines hidden (view full) --- 921 * ``The argument shall be a pointer to void. The 922 * value of the pointer is converted to a sequence 923 * of printable characters, in an implementation- 924 * defined manner.'' 925 * -- ANSI X3J11 926 */ 927 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 928 base = 16; | 947 break; 948#endif /* FLOATING_POINT */ 949 case 'n': 950 /* 951 * Assignment-like behavior is specified if the 952 * value overflows or is otherwise unrepresentable. 953 * C99 says to use `signed char' for %hhn conversions. 954 */ --- 29 unchanged lines hidden (view full) --- 984 * ``The argument shall be a pointer to void. The 985 * value of the pointer is converted to a sequence 986 * of printable characters, in an implementation- 987 * defined manner.'' 988 * -- ANSI X3J11 989 */ 990 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 991 base = 16; |
929 xdigs = "0123456789abcdef"; 930 flags = flags | INTMAXT | HEXPREFIX; 931 ch = 'x'; | 992 xdigs = xdigs_lower; 993 flags = flags | INTMAXT; 994 ox[1] = 'x'; |
932 goto nosign; 933 case 'S': 934 flags |= LONGINT; 935 /*FALLTHROUGH*/ 936 case 's': 937 if (flags & LONGINT) { 938 wchar_t *wcp; 939 --- 35 unchanged lines hidden (view full) --- 975 case 'u': 976 if (flags & INTMAX_SIZE) 977 ujval = UJARG(); 978 else 979 ulval = UARG(); 980 base = 10; 981 goto nosign; 982 case 'X': | 995 goto nosign; 996 case 'S': 997 flags |= LONGINT; 998 /*FALLTHROUGH*/ 999 case 's': 1000 if (flags & LONGINT) { 1001 wchar_t *wcp; 1002 --- 35 unchanged lines hidden (view full) --- 1038 case 'u': 1039 if (flags & INTMAX_SIZE) 1040 ujval = UJARG(); 1041 else 1042 ulval = UARG(); 1043 base = 10; 1044 goto nosign; 1045 case 'X': |
983 xdigs = "0123456789ABCDEF"; | 1046 xdigs = xdigs_upper; |
984 goto hex; 985 case 'x': | 1047 goto hex; 1048 case 'x': |
986 xdigs = "0123456789abcdef"; | 1049 xdigs = xdigs_lower; |
987hex: 988 if (flags & INTMAX_SIZE) 989 ujval = UJARG(); 990 else 991 ulval = UARG(); 992 base = 16; 993 /* leading 0x/X only if non-zero */ 994 if (flags & ALT && 995 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) | 1050hex: 1051 if (flags & INTMAX_SIZE) 1052 ujval = UJARG(); 1053 else 1054 ulval = UARG(); 1055 base = 16; 1056 /* leading 0x/X only if non-zero */ 1057 if (flags & ALT && 1058 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) |
996 flags |= HEXPREFIX; | 1059 ox[1] = ch; |
997 998 flags &= ~GROUPING; 999 /* unsigned conversions */ 1000nosign: sign = '\0'; 1001 /*- 1002 * ``... diouXx conversions ... if a precision is 1003 * specified, the 0 flag will be ignored.'' 1004 * -- ANSI X3J11 --- 47 unchanged lines hidden (view full) --- 1052 * floating precision; finally, if LADJUST, pad with blanks. 1053 * 1054 * Compute actual size, so we know how much to pad. 1055 * size excludes decimal prec; realsz includes it. 1056 */ 1057 realsz = dprec > size ? dprec : size; 1058 if (sign) 1059 realsz++; | 1060 1061 flags &= ~GROUPING; 1062 /* unsigned conversions */ 1063nosign: sign = '\0'; 1064 /*- 1065 * ``... diouXx conversions ... if a precision is 1066 * specified, the 0 flag will be ignored.'' 1067 * -- ANSI X3J11 --- 47 unchanged lines hidden (view full) --- 1115 * floating precision; finally, if LADJUST, pad with blanks. 1116 * 1117 * Compute actual size, so we know how much to pad. 1118 * size excludes decimal prec; realsz includes it. 1119 */ 1120 realsz = dprec > size ? dprec : size; 1121 if (sign) 1122 realsz++; |
1060 else if (flags & HEXPREFIX) | 1123 else if (ox[1]) |
1061 realsz += 2; 1062 1063 prsize = width > realsz ? width : realsz; 1064 if ((unsigned)ret + prsize > INT_MAX) { 1065 ret = EOF; 1066 goto error; 1067 } 1068 1069 /* right-adjusting blank padding */ 1070 if ((flags & (LADJUST|ZEROPAD)) == 0) 1071 PAD(width - realsz, blanks); 1072 1073 /* prefix */ 1074 if (sign) { 1075 PRINT(&sign, 1); | 1124 realsz += 2; 1125 1126 prsize = width > realsz ? width : realsz; 1127 if ((unsigned)ret + prsize > INT_MAX) { 1128 ret = EOF; 1129 goto error; 1130 } 1131 1132 /* right-adjusting blank padding */ 1133 if ((flags & (LADJUST|ZEROPAD)) == 0) 1134 PAD(width - realsz, blanks); 1135 1136 /* prefix */ 1137 if (sign) { 1138 PRINT(&sign, 1); |
1076 } else if (flags & HEXPREFIX) { | 1139 } else if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
1077 ox[0] = '0'; | 1140 ox[0] = '0'; |
1078 ox[1] = ch; | |
1079 PRINT(ox, 2); 1080 } 1081 1082 /* right-adjusting zero padding */ 1083 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1084 PAD(width - realsz, zeroes); 1085 1086 /* leading zeroes from decimal precision */ 1087 PAD(dprec - size, zeroes); 1088 1089 /* the string or number proper */ 1090#ifdef FLOATING_POINT 1091 if ((flags & FPT) == 0) { 1092 PRINT(cp, size); 1093 } else { /* glue together f_p fragments */ | 1141 PRINT(ox, 2); 1142 } 1143 1144 /* right-adjusting zero padding */ 1145 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1146 PAD(width - realsz, zeroes); 1147 1148 /* leading zeroes from decimal precision */ 1149 PAD(dprec - size, zeroes); 1150 1151 /* the string or number proper */ 1152#ifdef FLOATING_POINT 1153 if ((flags & FPT) == 0) { 1154 PRINT(cp, size); 1155 } else { /* glue together f_p fragments */ |
1094 if (ch >= 'f') { /* 'f' or 'g' */ 1095 if (_double == 0) { 1096 /* kludge for __dtoa irregularity */ 1097 PRINT("0", 1); 1098 if (expt < ndig || (flags & ALT) != 0) { 1099 PRINT(decimal_point, 1); 1100 PAD(ndig - 1, zeroes); 1101 } 1102 } else if (expt <= 0) { 1103 PRINT("0", 1); 1104 PRINT(decimal_point, 1); | 1156 if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1157 if (expt <= 0) { 1158 buf[0] = '0'; 1159 buf[1] = *decimal_point; 1160 PRINT(buf, 2); |
1105 PAD(-expt, zeroes); | 1161 PAD(-expt, zeroes); |
1106 PRINT(cp, ndig); 1107 } else if (expt >= ndig) { 1108 PRINT(cp, ndig); 1109 PAD(expt - ndig, zeroes); 1110 if (flags & ALT) 1111 PRINT(decimal_point, 1); | 1162 if (ndig > 0) 1163 PRINT(cp, ndig); |
1112 } else { | 1164 } else { |
1113 PRINT(cp, expt); 1114 cp += expt; 1115 PRINT(decimal_point, 1); 1116 PRINT(cp, ndig-expt); | 1165 PRINT(cp, lead); 1166 cp += lead; 1167 if (grouping) { 1168 while (nseps>0 || nrepeats>0) { 1169 if (nrepeats > 0) 1170 nrepeats--; 1171 else { 1172 grouping--; 1173 nseps--; 1174 } 1175 PRINT(&thousands_sep, 1176 1); 1177 PRINT(cp, *grouping); 1178 cp += *grouping; 1179 } 1180 } else { 1181 PAD(expt - lead, zeroes); 1182 } 1183 if (prec || flags & ALT) 1184 PRINT(decimal_point,1); 1185 if (ndig > lead) 1186 PRINT(cp, ndig - lead); |
1117 } | 1187 } |
1118 } else { /* 'e' or 'E' */ 1119 if (ndig > 1 || flags & ALT) { 1120 ox[0] = *cp++; 1121 ox[1] = *decimal_point; 1122 PRINT(ox, 2); 1123 if (_double) { 1124 PRINT(cp, ndig-1); 1125 } else /* 0.[0..] */ 1126 /* __dtoa irregularity */ 1127 PAD(ndig - 1, zeroes); | 1188 PAD(prec - ndig + expt, zeroes); 1189 } else { /* %[eE] or sufficiently long %[gG] */ 1190 if (prec || flags & ALT) { 1191 buf[0] = *cp++; 1192 buf[1] = *decimal_point; 1193 PRINT(buf, 2); 1194 PRINT(cp, ndig-1); 1195 PAD(prec - ndig, zeroes); |
1128 } else /* XeYYY */ 1129 PRINT(cp, 1); | 1196 } else /* XeYYY */ 1197 PRINT(cp, 1); |
1198 |
|
1130 PRINT(expstr, expsize); 1131 } 1132 } 1133#else 1134 PRINT(cp, size); 1135#endif 1136 /* left-adjusting padding (always blank) */ 1137 if (flags & LADJUST) --- 4 unchanged lines hidden (view full) --- 1142 1143 FLUSH(); /* copy out the I/O vectors */ 1144 } 1145done: 1146 FLUSH(); 1147error: 1148#ifdef FLOATING_POINT 1149 if (dtoaresult != NULL) | 1199 PRINT(expstr, expsize); 1200 } 1201 } 1202#else 1203 PRINT(cp, size); 1204#endif 1205 /* left-adjusting padding (always blank) */ 1206 if (flags & LADJUST) --- 4 unchanged lines hidden (view full) --- 1211 1212 FLUSH(); /* copy out the I/O vectors */ 1213 } 1214done: 1215 FLUSH(); 1216error: 1217#ifdef FLOATING_POINT 1218 if (dtoaresult != NULL) |
1150 __freedtoa(dtoaresult); | 1219 freedtoa(dtoaresult); |
1151#endif 1152 if (convbuf != NULL) 1153 free(convbuf); 1154 if (__sferror(fp)) 1155 ret = EOF; 1156 if ((argtable != NULL) && (argtable != statargtable)) 1157 free (argtable); 1158 return (ret); --- 353 unchanged lines hidden (view full) --- 1512 1513 *typetable = newtable; 1514 *tablesize = newsize; 1515} 1516 1517 1518#ifdef FLOATING_POINT 1519 | 1220#endif 1221 if (convbuf != NULL) 1222 free(convbuf); 1223 if (__sferror(fp)) 1224 ret = EOF; 1225 if ((argtable != NULL) && (argtable != statargtable)) 1226 free (argtable); 1227 return (ret); --- 353 unchanged lines hidden (view full) --- 1581 1582 *typetable = newtable; 1583 *tablesize = newsize; 1584} 1585 1586 1587#ifdef FLOATING_POINT 1588 |
1520static char * 1521cvt(double value, int ndigits, int flags, char *sign, int *decpt, 1522 int ch, int *length) 1523{ 1524 int mode, dsgn; 1525 char *digits, *bp, *rve; 1526 1527 if (ch == 'f') 1528 mode = 3; /* ndigits after the decimal point */ 1529 else { 1530 /* 1531 * To obtain ndigits after the decimal point for the 'e' 1532 * and 'E' formats, round to ndigits + 1 significant 1533 * figures. 1534 */ 1535 if (ch == 'e' || ch == 'E') 1536 ndigits++; 1537 mode = 2; /* ndigits significant digits */ 1538 } 1539 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); 1540 *sign = dsgn != 0; 1541 if ((ch != 'g' && ch != 'G') || flags & ALT) { 1542 /* print trailing zeros */ 1543 bp = digits + ndigits; 1544 if (ch == 'f') { 1545 if ((*digits == '0' || *digits == '\0') && value) 1546 *decpt = -ndigits + 1; 1547 bp += *decpt; 1548 } 1549 if (value == 0) /* kludge for __dtoa irregularity */ 1550 rve = bp; 1551 while (rve < bp) 1552 *rve++ = '0'; 1553 } 1554 *length = rve - digits; 1555 return (digits); 1556} 1557 | |
1558static int 1559exponent(char *p0, int exp, int fmtch) 1560{ 1561 char *p, *t; 1562 char expbuf[MAXEXPDIG]; 1563 1564 p = p0; 1565 *p++ = fmtch; --- 7 unchanged lines hidden (view full) --- 1573 if (exp > 9) { 1574 do { 1575 *--t = to_char(exp % 10); 1576 } while ((exp /= 10) > 9); 1577 *--t = to_char(exp); 1578 for (; t < expbuf + MAXEXPDIG; *p++ = *t++); 1579 } 1580 else { | 1589static int 1590exponent(char *p0, int exp, int fmtch) 1591{ 1592 char *p, *t; 1593 char expbuf[MAXEXPDIG]; 1594 1595 p = p0; 1596 *p++ = fmtch; --- 7 unchanged lines hidden (view full) --- 1604 if (exp > 9) { 1605 do { 1606 *--t = to_char(exp % 10); 1607 } while ((exp /= 10) > 9); 1608 *--t = to_char(exp); 1609 for (; t < expbuf + MAXEXPDIG; *p++ = *t++); 1610 } 1611 else { |
1581 *p++ = '0'; | 1612 /* 1613 * Exponents for decimal floating point conversions 1614 * (%[eEgG]) must be at least two characters long, 1615 * whereas exponents for hexadecimal conversions can 1616 * be only one character long. 1617 */ 1618 if (fmtch == 'e' || fmtch == 'E') 1619 *p++ = '0'; |
1582 *p++ = to_char(exp); 1583 } 1584 return (p - p0); 1585} 1586#endif /* FLOATING_POINT */ | 1620 *p++ = to_char(exp); 1621 } 1622 return (p - p0); 1623} 1624#endif /* FLOATING_POINT */ |