Subversion Repositories HelenOS

Rev

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