Changeset f1249e1 in mainline for libc/generic/io/printf_core.c


Ignore:
Timestamp:
2006-06-06T16:45:43Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f4b961f
Parents:
0d11fc1c
Message:

Printf return value fix ported from kernel to uspace.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libc/generic/io/printf_core.c

    r0d11fc1c rf1249e1  
    7474 * @param count
    7575 * @param ps output method and its data
    76  * @return 0 on success, EOF on fail
     76 * @return number of printed characters
    7777 */
    7878static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps)
    7979{
    80         if (ps->write((void *)buf, count, ps->data) == count) {
    81                 return 0;
    82         }
    83        
    84         return EOF;
     80        return ps->write((void *)buf, count, ps->data);
    8581}
    8682
     
    8884 * @param str string to print
    8985 * @param ps write function specification and support data
    90  * @return 0 on success or EOF on fail
     86 * @return number of printed characters
    9187 */
    9288static int printf_putstr(const char * str, struct printf_spec *ps)
     
    110106 * @param c one character
    111107 * @param ps output method
    112  * @return printed character or EOF
     108 * @return number of printed characters
    113109 */
    114110static int printf_putchar(int c, struct printf_spec *ps)
     
    116112        unsigned char ch = c;
    117113       
    118         if (ps->write((void *) &ch, 1, ps->data) == 1) {
    119                 return c;
    120         }
    121        
    122         return EOF;
     114        return ps->write((void *) &ch, 1, ps->data);
    123115}
    124116
     
    127119 * @param width
    128120 * @param flags
    129  * @return number of printed characters or EOF
     121 * @return number of printed characters
    130122 */
    131123static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps)
     
    135127        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    136128                while (--width > 0) {   /* one space is consumed by character itself hence predecrement */
    137                         /* FIXME: painful slow */
    138                         printf_putchar(' ', ps);       
     129                        if (printf_putchar(' ', ps) > 0)       
     130                                ++counter;
     131                }
     132        }
     133       
     134        if (printf_putchar(c, ps) > 0)
     135                counter++;
     136       
     137        while (--width > 0) { /* one space is consumed by character itself hence predecrement */
     138                if (printf_putchar(' ', ps) > 0)
    139139                        ++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;
    150140        }
    151141       
     
    158148 * @param precision
    159149 * @param flags
    160  * @return number of printed characters or EOF
     150 * @return number of printed characters
    161151 */
    162152                                               
     
    165155        int counter = 0;
    166156        size_t size;
     157        int retval;
    167158
    168159        if (s == NULL) {
     
    181172        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    182173                while (width-- > 0) {   
    183                         printf_putchar(' ', ps);       
    184                         counter++;
     174                        if (printf_putchar(' ', ps) == 1)       
     175                                counter++;
    185176                }
    186177        }
     
    188179        while (precision > size) {
    189180                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;
     181                if (printf_putchar(' ', ps) == 1)       
     182                        ++counter;
     183        }
     184       
     185        if ((retval = printf_putnchars(s, precision, ps)) < 0) {
     186                return -counter;
     187        }
     188
     189        counter += retval;     
    199190
    200191        while (width-- > 0) {
    201                 printf_putchar(' ', ps);       
    202                 ++counter;
    203         }
    204        
    205         return ++counter;
     192                if (printf_putchar(' ', ps) == 1)       
     193                        ++counter;
     194        }
     195       
     196        return counter;
    206197}
    207198
     
    218209 *             be in range 2 .. 16).
    219210 * @param flags output modifiers
    220  * @return number of written characters or EOF
     211 * @return number of printed characters
    221212 *
    222213 */
     
    228219        int size = 0; /* size of number with all prefixes and signs */
    229220        int number_size; /* size of plain number */
    230         int written = 0;
    231221        char sgn;
     222        int retval;
     223        int counter = 0;
    232224       
    233225        if (flags & __PRINTF_FLAG_BIGCHARS)
     
    296288        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    297289                while (width-- > 0) {   
    298                         printf_putchar(' ', ps);       
    299                         written++;
     290                        if (printf_putchar(' ', ps) == 1)       
     291                                counter++;
    300292                }
    301293        }
     
    303295        /* print sign */
    304296        if (sgn) {
    305                 printf_putchar(sgn, ps);
    306                 written++;
     297                if (printf_putchar(sgn, ps) == 1)
     298                        counter++;
    307299        }
    308300       
     
    312304                switch(base) {
    313305                        case 2: /* Binary formating is not standard, but usefull */
    314                                 printf_putchar('0', ps);
     306                                if (printf_putchar('0', ps) == 1)
     307                                        counter++;
    315308                                if (flags & __PRINTF_FLAG_BIGCHARS) {
    316                                         printf_putchar('B', ps);
     309                                        if (printf_putchar('B', ps) == 1)
     310                                                counter++;
    317311                                } else {
    318                                         printf_putchar('b', ps);
    319                                 }
    320                                 written += 2;
     312                                        if (printf_putchar('b', ps) == 1)
     313                                                counter++;
     314                                }
    321315                                break;
    322316                        case 8:
    323                                 printf_putchar('o', ps);
    324                                 written++;
     317                                if (printf_putchar('o', ps) == 1)
     318                                        counter++;
    325319                                break;
    326320                        case 16:
    327                                 printf_putchar('0', ps);
     321                                if (printf_putchar('0', ps) == 1)
     322                                        counter++;
    328323                                if (flags & __PRINTF_FLAG_BIGCHARS) {
    329                                         printf_putchar('X', ps);
     324                                        if (printf_putchar('X', ps) == 1)
     325                                                counter++;
    330326                                } else {
    331                                         printf_putchar('x', ps);
    332                                 }
    333                                 written += 2;
     327                                        if (printf_putchar('x', ps) == 1)
     328                                                counter++;
     329                                }
    334330                                break;
    335331                }
     
    339335        precision -= number_size;
    340336        while (precision-- > 0) {       
    341                 printf_putchar('0', ps);       
    342                 written++;
     337                if (printf_putchar('0', ps) == 1)
     338                        counter++;
    343339        }
    344340
     
    346342        /* print number itself */
    347343
    348         written += printf_putstr(++ptr, ps);
     344        if ((retval = printf_putstr(++ptr, ps)) > 0) {
     345                counter += retval;
     346        }
    349347       
    350348        /* print ending spaces */
    351349       
    352350        while (width-- > 0) {   
    353                 printf_putchar(' ', ps);       
    354                 written++;
    355         }
    356 
    357         return written;
     351                if (printf_putchar(' ', ps) == 1)       
     352                        counter++;
     353        }
     354
     355        return counter;
    358356}
    359357
     
    457455                        /* print common characters if any processed */ 
    458456                        if (i > j) {
    459                                 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
     457                                if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */
    460458                                        goto minus_out;
    461459                                }
     
    553551                                */
    554552                                case 's':
    555                                         if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) {
     553                                        if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) {
    556554                                                goto minus_out;
    557555                                        };
     
    562560                                case 'c':
    563561                                        c = va_arg(ap, unsigned int);
    564                                         if ((retval = print_char(c, width, flags, ps)) == EOF) {
     562                                        if ((retval = print_char(c, width, flags, ps)) < 0) {
    565563                                                goto minus_out;
    566564                                        };
     
    662660                        }
    663661
    664                         if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) {
     662                        if ((retval = print_number(number, width, precision, base, flags, ps)) < 0 ) {
    665663                                goto minus_out;
    666664                        };
     
    675673       
    676674        if (i > j) {
    677                 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
     675                if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */
    678676                        goto minus_out;
    679677                }
Note: See TracChangeset for help on using the changeset viewer.