Subversion Repositories HelenOS

Rev

Rev 3022 | Rev 4537 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2001-2004 Jakub Jermar
  3.  * Copyright (c) 2006 Josef Cejka
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. /** @addtogroup libc
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  * @brief   Printing functions.
  36.  */
  37.  
  38. #include <unistd.h>
  39. #include <stdio.h>
  40. #include <io/printf_core.h>
  41. #include <ctype.h>
  42. #include <string.h>
  43.  
  44. #define __PRINTF_FLAG_PREFIX        0x00000001  /**< show prefixes 0x or 0*/
  45. #define __PRINTF_FLAG_SIGNED        0x00000002  /**< signed / unsigned number */
  46. #define __PRINTF_FLAG_ZEROPADDED    0x00000004  /**< print leading zeroes */
  47. #define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /**< align to left */
  48. #define __PRINTF_FLAG_SHOWPLUS      0x00000020  /**< always show + sign */
  49. #define __PRINTF_FLAG_SPACESIGN     0x00000040  /**< print space instead of plus */
  50. #define __PRINTF_FLAG_BIGCHARS      0x00000080  /**< show big characters */
  51. #define __PRINTF_FLAG_NEGATIVE      0x00000100  /**< number has - sign */
  52.  
  53. #define PRINT_NUMBER_BUFFER_SIZE    (64+5)      /**< Buffer big enought for 64 bit number
  54.                              * printed in base 2, sign, prefix and
  55.                              * 0 to terminate string.. (last one is only for better testing
  56.                              * end of buffer by zero-filling subroutine)
  57.                              */
  58. /** Enumeration of possible arguments types.
  59.  */
  60. typedef enum {
  61.     PrintfQualifierByte = 0,
  62.     PrintfQualifierShort,
  63.     PrintfQualifierInt,
  64.     PrintfQualifierLong,
  65.     PrintfQualifierLongLong,
  66.     PrintfQualifierSizeT,
  67.     PrintfQualifierPointer
  68. } qualifier_t;
  69.  
  70. static char digits_small[] = "0123456789abcdef";    /**< Small hexadecimal characters */
  71. static char digits_big[] = "0123456789ABCDEF";      /**< Big hexadecimal characters */
  72.  
  73. /** Print count chars from buffer without adding newline
  74.  * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
  75.  * @param count
  76.  * @param ps output method and its data
  77.  * @return number of printed characters
  78.  */
  79. static int printf_putnchars(const char * buf, size_t count,
  80.     struct printf_spec *ps)
  81. {
  82.     return ps->write((void *)buf, count, ps->data);
  83. }
  84.  
  85. /** Print string without added newline
  86.  * @param str string to print
  87.  * @param ps write function specification and support data
  88.  * @return number of printed characters
  89.  */
  90. static int printf_putstr(const char * str, struct printf_spec *ps)
  91. {
  92.     size_t count;
  93.    
  94.     if (str == NULL)
  95.         return printf_putnchars("(NULL)", 6, ps);
  96.  
  97.     count = strlen(str);
  98.  
  99.     return ps->write((void *) str, count, ps->data);
  100. }
  101.  
  102. /** Print one character to output
  103.  * @param c one character
  104.  * @param ps output method
  105.  * @return number of printed characters
  106.  */
  107. static int printf_putchar(int c, struct printf_spec *ps)
  108. {
  109.     unsigned char ch = c;
  110.    
  111.     return ps->write((void *) &ch, 1, ps->data);
  112. }
  113.  
  114. /** Print one formatted character
  115.  * @param c character to print
  116.  * @param width
  117.  * @param flags
  118.  * @return number of printed characters
  119.  */
  120. static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps)
  121. {
  122.     int counter = 0;
  123.    
  124.     if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
  125.         /*
  126.          * One space is consumed by the character itself, hence the
  127.          * predecrement.
  128.          */
  129.         while (--width > 0) {
  130.             if (printf_putchar(' ', ps) > 0)   
  131.                 ++counter;
  132.         }
  133.     }
  134.    
  135.     if (printf_putchar(c, ps) > 0)
  136.         counter++;
  137.     /*
  138.      * One space is consumed by the character itself, hence the
  139.      * predecrement.
  140.      */
  141.     while (--width > 0) {
  142.         if (printf_putchar(' ', ps) > 0)
  143.             ++counter;
  144.     }
  145.    
  146.     return ++counter;
  147. }
  148.  
  149. /** Print one string
  150.  * @param s string
  151.  * @param width
  152.  * @param precision
  153.  * @param flags
  154.  * @return number of printed characters
  155.  */
  156. static int print_string(char *s, int width, int precision, uint64_t flags,
  157.     struct printf_spec *ps)
  158. {
  159.     int counter = 0;
  160.     size_t size;
  161.     int retval;
  162.  
  163.     if (s == NULL) {
  164.         return printf_putstr("(NULL)", ps);
  165.     }
  166.    
  167.     size = strlen(s);
  168.  
  169.     /* print leading spaces */
  170.  
  171.     if (precision == 0)
  172.         precision = size;
  173.  
  174.     width -= precision;
  175.    
  176.     if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
  177.         while (width-- > 0) {  
  178.             if (printf_putchar(' ', ps) == 1)  
  179.                 counter++;
  180.         }
  181.     }
  182.  
  183.     if ((retval = printf_putnchars(s, size < precision ? size : precision,
  184.         ps)) < 0) {
  185.         return -counter;
  186.     }
  187.  
  188.     counter += retval; 
  189.  
  190.     while (width-- > 0) {
  191.         if (printf_putchar(' ', ps) == 1)  
  192.             ++counter;
  193.     }
  194.    
  195.     return counter;
  196. }
  197.  
  198.  
  199. /** Print number in given base
  200.  *
  201.  * Print significant digits of a number in given
  202.  * base.
  203.  *
  204.  * @param num  Number to print.
  205.  * @param width
  206.  * @param precision
  207.  * @param base Base to print the number in (should
  208.  *             be in range 2 .. 16).
  209.  * @param flags output modifiers
  210.  * @return number of printed characters
  211.  *
  212.  */
  213. static int print_number(uint64_t num, int width, int precision, int base,
  214.     uint64_t flags, struct printf_spec *ps)
  215. {
  216.     char *digits = digits_small;
  217.     char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for
  218.                          * base == 2, prefix and sign */
  219.     char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
  220.     int size = 0; /* size of number with all prefixes and signs */
  221.     int number_size; /* size of plain number */
  222.     char sgn;
  223.     int retval;
  224.     int counter = 0;
  225.    
  226.     if (flags & __PRINTF_FLAG_BIGCHARS)
  227.         digits = digits_big;   
  228.    
  229.     *ptr-- = 0; /* Put zero at end of string */
  230.  
  231.     if (num == 0) {
  232.         *ptr-- = '0';
  233.         size++;
  234.     } else {
  235.         do {
  236.             *ptr-- = digits[num % base];
  237.             size++;
  238.         } while (num /= base);
  239.     }
  240.    
  241.     number_size = size;
  242.  
  243.     /*
  244.      * Collect sum of all prefixes/signs/... to calculate padding and
  245.      * leading zeroes
  246.      */
  247.     if (flags & __PRINTF_FLAG_PREFIX) {
  248.         switch(base) {
  249.         case 2: /* Binary formating is not standard, but usefull */
  250.             size += 2;
  251.             break;
  252.         case 8:
  253.             size++;
  254.             break;
  255.         case 16:
  256.             size += 2;
  257.             break;
  258.         }
  259.     }
  260.  
  261.     sgn = 0;
  262.     if (flags & __PRINTF_FLAG_SIGNED) {
  263.         if (flags & __PRINTF_FLAG_NEGATIVE) {
  264.             sgn = '-';
  265.             size++;
  266.         } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
  267.             sgn = '+';
  268.             size++;
  269.         } else if (flags & __PRINTF_FLAG_SPACESIGN) {
  270.             sgn = ' ';
  271.             size++;
  272.         }
  273.     }
  274.  
  275.     if (flags & __PRINTF_FLAG_LEFTALIGNED) {
  276.         flags &= ~__PRINTF_FLAG_ZEROPADDED;
  277.     }
  278.  
  279.     /*
  280.      * If number is leftaligned or precision is specified then zeropadding
  281.      * is ignored.
  282.      */
  283.     if (flags & __PRINTF_FLAG_ZEROPADDED) {
  284.         if ((precision == 0) && (width > size)) {
  285.             precision = width - size + number_size;
  286.         }
  287.     }
  288.  
  289.     /* print leading spaces */
  290.  
  291.     /* We must print whole number not only a part. */
  292.     if (number_size > precision)
  293.         precision = number_size;
  294.  
  295.     width -= precision + size - number_size;
  296.    
  297.     if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
  298.         while (width-- > 0) {  
  299.             if (printf_putchar(' ', ps) == 1)  
  300.                 counter++;
  301.         }
  302.     }
  303.    
  304.     /* print sign */
  305.     if (sgn) {
  306.         if (printf_putchar(sgn, ps) == 1)
  307.             counter++;
  308.     }
  309.    
  310.     /* print prefix */
  311.    
  312.     if (flags & __PRINTF_FLAG_PREFIX) {
  313.         switch(base) {
  314.         case 2: /* Binary formating is not standard, but usefull */
  315.             if (printf_putchar('0', ps) == 1)
  316.                 counter++;
  317.             if (flags & __PRINTF_FLAG_BIGCHARS) {
  318.                 if (printf_putchar('B', ps) == 1)
  319.                     counter++;
  320.             } else {
  321.                 if (printf_putchar('b', ps) == 1)
  322.                     counter++;
  323.             }
  324.             break;
  325.         case 8:
  326.             if (printf_putchar('o', ps) == 1)
  327.                 counter++;
  328.             break;
  329.         case 16:
  330.             if (printf_putchar('0', ps) == 1)
  331.                 counter++;
  332.             if (flags & __PRINTF_FLAG_BIGCHARS) {
  333.                 if (printf_putchar('X', ps) == 1)
  334.                     counter++;
  335.             } else {
  336.                 if (printf_putchar('x', ps) == 1)
  337.                     counter++;
  338.             }
  339.             break;
  340.         }
  341.     }
  342.  
  343.     /* print leading zeroes */
  344.     precision -= number_size;
  345.     while (precision-- > 0) {  
  346.         if (printf_putchar('0', ps) == 1)
  347.             counter++;
  348.     }
  349.  
  350.    
  351.     /* print number itself */
  352.  
  353.     if ((retval = printf_putstr(++ptr, ps)) > 0) {
  354.         counter += retval;
  355.     }
  356.    
  357.     /* print ending spaces */
  358.    
  359.     while (width-- > 0) {  
  360.         if (printf_putchar(' ', ps) == 1)  
  361.             counter++;
  362.     }
  363.  
  364.     return counter;
  365. }
  366.  
  367.  
  368. /** Print formatted string.
  369.  *
  370.  * Print string formatted according to the fmt parameter and variadic arguments.
  371.  * Each formatting directive must have the following form:
  372.  *
  373.  *  \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION
  374.  *
  375.  * FLAGS:@n
  376.  *  - "#" Force to print prefix.
  377.  *  For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and
  378.  *  0X and for conversion \%b the prefix is 0b.
  379.  *
  380.  *  - "-"   Align to left.
  381.  *
  382.  *  - "+"   Print positive sign just as negative.
  383.  *
  384.  *  - " "   If the printed number is positive and "+" flag is not set,
  385.  *      print space in place of sign.
  386.  *
  387.  *  - "0"   Print 0 as padding instead of spaces. Zeroes are placed between
  388.  *      sign and the rest of the number. This flag is ignored if "-"
  389.  *      flag is specified.
  390.  *
  391.  * WIDTH:@n
  392.  *  - Specify minimal width of printed argument. If it is bigger, width is
  393.  *    ignored. If width is specified with a "*" character instead of number,
  394.  *    width is taken from parameter list. And integer parameter is expected
  395.  *    before parameter for processed conversion specification. If this value
  396.  *    is negative its absolute value is taken and the "-" flag is set.
  397.  *
  398.  * PRECISION:@n
  399.  *  - Value precision. For numbers it specifies minimum valid numbers.
  400.  *    Smaller numbers are printed with leading zeroes. Bigger numbers are
  401.  *    not affected. Strings with more than precision characters are cut off.
  402.  *    Just as with width, an "*" can be used used instead of a number. An
  403.  *    integer value is then expected in parameters. When both width and
  404.  *    precision are specified using "*", the first parameter is used for
  405.  *    width and the second one for precision.
  406.  *
  407.  * TYPE:@n
  408.  *  - "hh"  Signed or unsigned char.@n
  409.  *  - "h"   Signed or usigned short.@n
  410.  *  - ""    Signed or usigned int (default value).@n
  411.  *  - "l"   Signed or usigned long int.@n
  412.  *  - "ll"  Signed or usigned long long int.@n
  413.  *  - "z"   Type size_t.@n
  414.  *
  415.  *
  416.  * CONVERSION:@n
  417.  *  - % Print percentile character itself.
  418.  *
  419.  *  - c Print single character.
  420.  *
  421.  *  - s Print zero terminated string. If a NULL value is passed as
  422.  *      value, "(NULL)" is printed instead.
  423.  *
  424.  *  - P, p  Print value of a pointer. Void * value is expected and it is
  425.  *      printed in hexadecimal notation with prefix (as with '\%#X' or
  426.  *      '\%#x' for 32bit or '\%#X' or '\%#x' for 64bit long pointers).
  427.  *
  428.  *  - b Print value as unsigned binary number. Prefix is not printed by
  429.  *      default. (Nonstandard extension.)
  430.  *
  431.  *  - o Print value as unsigned octal number. Prefix is not printed by
  432.  *      default.
  433.  *
  434.  *  - d, i  Print signed decimal number. There is no difference between d
  435.  *      and i conversion.
  436.  *
  437.  *  - u Print unsigned decimal number.
  438.  *
  439.  *  - X, x  Print hexadecimal number with upper- or lower-case. Prefix is
  440.  *      not printed by default.
  441.  *
  442.  * All other characters from fmt except the formatting directives are printed in
  443.  * verbatim.
  444.  *
  445.  * @param fmt Formatting NULL terminated string.
  446.  * @return Number of printed characters or negative value on failure.
  447.  */
  448. int printf_core(const char *fmt, struct printf_spec *ps, va_list ap)
  449. {
  450.     /* i is the index of the currently processed char from fmt */
  451.     int i = 0;
  452.     /* j is the index to the first not printed nonformating character */
  453.     int j = 0;
  454.  
  455.     int end;
  456.     int counter;    /* counter of printed characters */
  457.     int retval; /* used to store return values from called functions */
  458.     char c;
  459.     qualifier_t qualifier;  /* type of argument */
  460.     int base;   /* base in which will be a numeric parameter printed */
  461.     uint64_t number; /* argument value */
  462.     size_t  size;   /* byte size of integer parameter */
  463.     int width, precision;
  464.     uint64_t flags;
  465.    
  466.     counter = 0;
  467.    
  468.     while ((c = fmt[i])) {
  469.         /* control character */
  470.         if (c == '%' ) {
  471.             /* print common characters if any processed */ 
  472.             if (i > j) {
  473.                 if ((retval = printf_putnchars(&fmt[j],
  474.                     (size_t)(i - j), ps)) < 0) { /* error */
  475.                     goto minus_out;
  476.                 }
  477.                 counter += retval;
  478.             }
  479.        
  480.             j = i;
  481.             /* parse modifiers */
  482.             flags = 0;
  483.             end = 0;
  484.            
  485.             do {
  486.                 ++i;
  487.                 switch (c = fmt[i]) {
  488.                 case '#':
  489.                     flags |= __PRINTF_FLAG_PREFIX;
  490.                     break;
  491.                 case '-':
  492.                     flags |= __PRINTF_FLAG_LEFTALIGNED;
  493.                     break;
  494.                 case '+':
  495.                     flags |= __PRINTF_FLAG_SHOWPLUS;
  496.                     break;
  497.                 case ' ':
  498.                     flags |= __PRINTF_FLAG_SPACESIGN;
  499.                     break;
  500.                 case '0':
  501.                     flags |= __PRINTF_FLAG_ZEROPADDED;
  502.                     break;
  503.                 default:
  504.                     end = 1;
  505.                 }; 
  506.                
  507.             } while (end == 0);
  508.            
  509.             /* width & '*' operator */
  510.             width = 0;
  511.             if (isdigit(fmt[i])) {
  512.                 while (isdigit(fmt[i])) {
  513.                     width *= 10;
  514.                     width += fmt[i++] - '0';
  515.                 }
  516.             } else if (fmt[i] == '*') {
  517.                 /* get width value from argument list*/
  518.                 i++;
  519.                 width = (int)va_arg(ap, int);
  520.                 if (width < 0) {
  521.                     /* negative width sets '-' flag */
  522.                     width *= -1;
  523.                     flags |= __PRINTF_FLAG_LEFTALIGNED;
  524.                 }
  525.             }
  526.            
  527.             /* precision and '*' operator */   
  528.             precision = 0;
  529.             if (fmt[i] == '.') {
  530.                 ++i;
  531.                 if (isdigit(fmt[i])) {
  532.                     while (isdigit(fmt[i])) {
  533.                         precision *= 10;
  534.                         precision += fmt[i++] - '0';
  535.                     }
  536.                 } else if (fmt[i] == '*') {
  537.                     /* get precision value from argument */
  538.                     i++;
  539.                     precision = (int)va_arg(ap, int);
  540.                     if (precision < 0) {
  541.                         /* negative precision ignored */
  542.                         precision = 0;
  543.                     }
  544.                 }
  545.             }
  546.  
  547.             switch (fmt[i++]) {
  548.             /** @todo unimplemented qualifiers:
  549.              * t ptrdiff_t - ISO C 99
  550.              */
  551.             case 'h':   /* char or short */
  552.                 qualifier = PrintfQualifierShort;
  553.                 if (fmt[i] == 'h') {
  554.                     i++;
  555.                     qualifier = PrintfQualifierByte;
  556.                 }
  557.                 break;
  558.             case 'l':   /* long or long long*/
  559.                 qualifier = PrintfQualifierLong;
  560.                 if (fmt[i] == 'l') {
  561.                     i++;
  562.                     qualifier = PrintfQualifierLongLong;
  563.                 }
  564.                 break;
  565.             case 'z':   /* size_t */
  566.                 qualifier = PrintfQualifierSizeT;
  567.                 break;
  568.             default:
  569.                 /* set default type */
  570.                 qualifier = PrintfQualifierInt;
  571.                 --i;
  572.             }  
  573.            
  574.             base = 10;
  575.  
  576.             switch (c = fmt[i]) {
  577.  
  578.             /*
  579.              * String and character conversions.
  580.              */
  581.             case 's':
  582.                 if ((retval = print_string(va_arg(ap, char*),
  583.                     width, precision, flags, ps)) < 0) {
  584.                     goto minus_out;
  585.                 }
  586.                    
  587.                 counter += retval;
  588.                 j = i + 1;
  589.                 goto next_char;
  590.             case 'c':
  591.                 c = va_arg(ap, unsigned int);
  592.                 retval = print_char(c, width, flags, ps);
  593.                 if (retval < 0) {
  594.                     goto minus_out;
  595.                 }
  596.  
  597.                 counter += retval;
  598.                 j = i + 1;
  599.                 goto next_char;
  600.  
  601.             /*
  602.              * Integer values
  603.              */
  604.             case 'P': /* pointer */
  605.                     flags |= __PRINTF_FLAG_BIGCHARS;
  606.             case 'p':
  607.                 flags |= __PRINTF_FLAG_PREFIX;
  608.                 base = 16;
  609.                 qualifier = PrintfQualifierPointer;
  610.                 break; 
  611.             case 'b':
  612.                 base = 2;
  613.                 break;
  614.             case 'o':
  615.                 base = 8;
  616.                 break;
  617.             case 'd':
  618.             case 'i':
  619.                 flags |= __PRINTF_FLAG_SIGNED;  
  620.             case 'u':
  621.                 break;
  622.             case 'X':
  623.                 flags |= __PRINTF_FLAG_BIGCHARS;
  624.             case 'x':
  625.                 base = 16;
  626.                 break;
  627.             /* percentile itself */
  628.             case '%':
  629.                 j = i;
  630.                 goto next_char;
  631.             /*
  632.              * Bad formatting.
  633.              */
  634.             default:
  635.                 /*
  636.                  * Unknown format. Now, j is the index of '%',
  637.                  * so we will print the whole bad format
  638.                  * sequence.
  639.                  */
  640.                 goto next_char;    
  641.             }
  642.        
  643.        
  644.             /* Print integers */
  645.             switch (qualifier) {
  646.             case PrintfQualifierByte:
  647.                 size = sizeof(unsigned char);
  648.                 number = (uint64_t)va_arg(ap, unsigned int);
  649.                 break;
  650.             case PrintfQualifierShort:
  651.                 size = sizeof(unsigned short);
  652.                 number = (uint64_t)va_arg(ap, unsigned int);
  653.                 break;
  654.             case PrintfQualifierInt:
  655.                 size = sizeof(unsigned int);
  656.                 number = (uint64_t)va_arg(ap, unsigned int);
  657.                 break;
  658.             case PrintfQualifierLong:
  659.                 size = sizeof(unsigned long);
  660.                 number = (uint64_t)va_arg(ap, unsigned long);
  661.                 break;
  662.             case PrintfQualifierLongLong:
  663.                 size = sizeof(unsigned long long);
  664.                 number = (uint64_t)va_arg(ap,
  665.                     unsigned long long);
  666.                 break;
  667.             case PrintfQualifierPointer:
  668.                 size = sizeof(void *);
  669.                 number = (uint64_t)(unsigned long)va_arg(ap,
  670.                     void *);
  671.                 break;
  672.             case PrintfQualifierSizeT:
  673.                 size = sizeof(size_t);
  674.                 number = (uint64_t)va_arg(ap, size_t);
  675.                 break;
  676.             default: /* Unknown qualifier */
  677.                 goto minus_out;
  678.                    
  679.             }
  680.            
  681.             if (flags & __PRINTF_FLAG_SIGNED) {
  682.                 if (number & (0x1 << (size * 8 - 1))) {
  683.                     flags |= __PRINTF_FLAG_NEGATIVE;
  684.                
  685.                     if (size == sizeof(uint64_t)) {
  686.                         number = -((int64_t)number);
  687.                     } else {
  688.                         number = ~number;
  689.                         number &=
  690.                             ~(0xFFFFFFFFFFFFFFFFll <<
  691.                             (size * 8));
  692.                         number++;
  693.                     }
  694.                 }
  695.             }
  696.  
  697.             if ((retval = print_number(number, width, precision,
  698.                 base, flags, ps)) < 0 ) {
  699.                 goto minus_out;
  700.             }
  701.  
  702.             counter += retval;
  703.             j = i + 1;
  704.         }  
  705. next_char:
  706.            
  707.         ++i;
  708.     }
  709.    
  710.     if (i > j) {
  711.         retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps);
  712.         if (retval < 0) { /* error */
  713.             goto minus_out;
  714.         }
  715.         counter += retval;
  716.     }
  717.    
  718.     return counter;
  719. minus_out:
  720.     return -counter;
  721. }
  722.  
  723. /** @}
  724.  */
  725.