- /* We reached the last digit, the deepest point of our recursion, and
- only now know how long the number to be printed actually is. Now we
- have to do the sign, prefix, width, and precision padding stuff
- before printing the numbers while we resurface from the recursion.
- */
- /* At worst, we need two prefix characters (hex prefix). */
- char preface[3] = "\0";
- size_t preidx = 0;
- if ( ( status->flags & E_alt ) && ( status->base == 16 || status->base == 8 ) )
- {
- /* Octal / hexadecimal prefix for "%#" conversions */
- preface[ preidx++ ] = '0';
- if ( status->base == 16 )
- {
- preface[ preidx++ ] = ( status->flags & E_lower ) ? 'x' : 'X';
- }
- }
- if ( value < 0 )
- {
- /* Negative sign for negative values - at all times. */
- preface[ preidx++ ] = '-';
- }
- else if ( ! ( status->flags & E_unsigned ) )
- {
- /* plus sign / extra space are only for unsigned conversions */
- if ( status->flags & E_plus )
- {
- preface[ preidx++ ] = '+';
- }
- else if ( status->flags & E_space )
- {
- preface[ preidx++ ] = ' ';
- }
- }
+ intmax_t signval = (intmax_t) value;
+ bool negative = signval < 0;
+ value = signval < 0 ? -signval : signval;
+
+ if ( negative )