| 664 | } |
| 665 | |
| 666 | static void |
| 667 | fmtint(char *str, size_t *len, size_t size, INTMAX_T value, int base, int width, |
| 668 | int precision, int flags) |
| 669 | { |
| 670 | UINTMAX_T uvalue; |
| 671 | char iconvert[MAX_CONVERT_LENGTH]; |
| 672 | char sign = 0; |
| 673 | char hexprefix = 0; |
| 674 | int spadlen = 0; /* Amount to space pad. */ |
| 675 | int zpadlen = 0; /* Amount to zero pad. */ |
| 676 | int pos; |
| 677 | int separators = (flags & PRINT_F_QUOTE); |
| 678 | int noprecision = (precision == -1); |
| 679 | |
| 680 | if (flags & PRINT_F_UNSIGNED) |
| 681 | uvalue = value; |
| 682 | else { |
| 683 | uvalue = (value >= 0) ? value : -value; |
| 684 | if (value < 0) |
| 685 | sign = '-'; |
| 686 | else if (flags & PRINT_F_PLUS) /* Do a sign. */ |
| 687 | sign = '+'; |
| 688 | else if (flags & PRINT_F_SPACE) |
| 689 | sign = ' '; |
| 690 | } |
| 691 | |
| 692 | pos = convert(uvalue, iconvert, sizeof(iconvert), base, |
| 693 | flags & PRINT_F_UP); |
| 694 | |
| 695 | if (flags & PRINT_F_NUM && uvalue != 0) { |
| 696 | /* |
| 697 | * C99 says: "The result is converted to an `alternative form'. |
| 698 | * For `o' conversion, it increases the precision, if and only |
| 699 | * if necessary, to force the first digit of the result to be a |
| 700 | * zero (if the value and precision are both 0, a single 0 is |
| 701 | * printed). For `x' (or `X') conversion, a nonzero result has |
| 702 | * `0x' (or `0X') prefixed to it." (7.19.6.1, 6) |
| 703 | */ |
| 704 | switch (base) { |
| 705 | case 8: |
| 706 | if (precision <= pos) |
| 707 | precision = pos + 1; |
| 708 | break; |
| 709 | case 16: |
| 710 | hexprefix = (flags & PRINT_F_UP) ? 'X' : 'x'; |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (separators) /* Get the number of group separators we'll print. */ |
| 716 | separators = getnumsep(pos); |
| 717 | |
| 718 | zpadlen = precision - pos - separators; |
| 719 | spadlen = width /* Minimum field width. */ |
| 720 | - separators /* Number of separators. */ |
| 721 | - MAX(precision, pos) /* Number of integer digits. */ |
| 722 | - (sign ? 1 : 0) /* Will we print a sign? */ |
| 723 | - (hexprefix ? 2 : 0); /* Will we print a prefix? */ |