Changeset 4ddeace in mainline


Ignore:
Timestamp:
2006-06-06T11:45:42Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
99f3249
Parents:
3247f0a
Message:

Fixed printf return value.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • generic/src/printf/printf_core.c

    r3247f0a r4ddeace  
    101101 * @param count
    102102 * @param ps output method and its data
    103  * @return 0 on success, EOF on fail
     103 * @return number or printed characters
    104104 */
    105105static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps)
    106106{
    107         if (ps->write((void *)buf, count, ps->data) == count) {
    108                 return 0;
    109         }
    110        
    111         return EOF;
     107        return ps->write((void *)buf, count, ps->data);
    112108}
    113109
     
    115111 * @param str string to print
    116112 * @param ps write function specification and support data
    117  * @return 0 on success or EOF on fail
     113 * @return number or printed characters
    118114 */
    119115static int printf_putstr(const char * str, struct printf_spec *ps)
     
    127123        count = strlen(str);
    128124
    129         if (ps->write((void *) str, count, ps->data) == count) {
    130                 return 0;
    131         }
    132        
    133         return EOF;
     125        return ps->write((void *) str, count, ps->data);
    134126}
    135127
     
    137129 * @param c one character
    138130 * @param ps output method
    139  * @return printed character or EOF
     131 * @return number or printed characters
    140132 */
    141133static int printf_putchar(int c, struct printf_spec *ps)
     
    143135        unsigned char ch = c;
    144136       
    145         if (ps->write((void *) &ch, 1, ps->data) == 1) {
    146                 return c;
    147         }
    148        
    149         return EOF;
     137        return ps->write((void *) &ch, 1, ps->data);
    150138}
    151139
     
    154142 * @param width
    155143 * @param flags
    156  * @return number of printed characters or EOF
     144 * @return number of printed characters, negative value on fail
    157145 */
    158146static int print_char(char c, int width, __u64 flags, struct printf_spec *ps)
     
    163151                while (--width > 0) {   /* one space is consumed by character itself hence predecrement */
    164152                        /* FIXME: painful slow */
    165                         printf_putchar(' ', ps);       
     153                        if (printf_putchar(' ', ps) > 0)       
     154                                ++counter;
     155                }
     156        }
     157       
     158        if (printf_putchar(c, ps) > 0)
     159                counter++;
     160
     161        while (--width > 0) { /* one space is consumed by character itself hence predecrement */
     162                if (printf_putchar(' ', ps) > 0)
    166163                        ++counter;
    167                 }
    168         }
    169        
    170         if (printf_putchar(c, ps) == EOF) {
    171                 return EOF;
    172         }
    173 
    174         while (--width > 0) { /* one space is consumed by character itself hence predecrement */
    175                 printf_putchar(' ', ps);
    176                 ++counter;
    177164        }
    178165       
     
    185172 * @param precision
    186173 * @param flags
    187  * @return number of printed characters or EOF
     174 * @return number of printed characters or negative value on fail
    188175 */
    189176                                               
     
    192179        int counter = 0;
    193180        size_t size;
     181        int retval;
    194182
    195183        if (s == NULL) {
     
    208196        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    209197                while (width-- > 0) {   
    210                         printf_putchar(' ', ps);       
    211                         counter++;
     198                        if (printf_putchar(' ', ps) == 1)       
     199                                counter++;
    212200                }
    213201        }
     
    215203        while (precision > size) {
    216204                precision--;
    217                 printf_putchar(' ', ps);       
    218                 ++counter;
    219         }
    220        
    221         if (printf_putnchars(s, precision, ps) == EOF) {
    222                 return EOF;
    223         }
    224 
    225         counter += precision;
     205                if (printf_putchar(' ', ps) == 1)       
     206                        ++counter;
     207        }
     208       
     209        if ((retval = printf_putnchars(s, precision, ps)) < 0) {
     210                return -counter;
     211        }
     212        counter += retval;     
    226213
    227214        while (width-- > 0) {
    228                 printf_putchar(' ', ps);       
    229                 ++counter;
    230         }
    231        
    232         return ++counter;
     215                if (printf_putchar(' ', ps) == 1)       
     216                        ++counter;
     217        }
     218       
     219        return counter;
    233220}
    234221
     
    255242        int size = 0; /* size of number with all prefixes and signs */
    256243        int number_size; /* size of plain number */
    257         int written = 0;
    258244        char sgn;
     245        int retval;
     246        int counter = 0;
    259247       
    260248        if (flags & __PRINTF_FLAG_BIGCHARS)
     
    323311        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    324312                while (width-- > 0) {   
    325                         printf_putchar(' ', ps);       
    326                         written++;
     313                        if (printf_putchar(' ', ps) == 1)       
     314                                counter++;
    327315                }
    328316        }
     317       
    329318       
    330319        /* print sign */
    331320        if (sgn) {
    332                 printf_putchar(sgn, ps);
    333                 written++;
     321                if (printf_putchar(sgn, ps) == 1)
     322                        counter++;
    334323        }
    335324       
     
    339328                switch(base) {
    340329                        case 2: /* Binary formating is not standard, but usefull */
    341                                 printf_putchar('0', ps);
     330                                if (printf_putchar('0', ps) == 1)
     331                                        counter++;
    342332                                if (flags & __PRINTF_FLAG_BIGCHARS) {
    343                                         printf_putchar('B', ps);
     333                                        if (printf_putchar('B', ps) == 1)
     334                                                counter++;
    344335                                } else {
    345                                         printf_putchar('b', ps);
     336                                        if (printf_putchar('b', ps) == 1)
     337                                                counter++;
    346338                                }
    347                                 written += 2;
    348339                                break;
    349340                        case 8:
    350                                 printf_putchar('o', ps);
    351                                 written++;
     341                                if (printf_putchar('o', ps) == 1)
     342                                        counter++;
    352343                                break;
    353344                        case 16:
    354                                 printf_putchar('0', ps);
     345                                if (printf_putchar('0', ps) == 1)
     346                                        counter++;
    355347                                if (flags & __PRINTF_FLAG_BIGCHARS) {
    356                                         printf_putchar('X', ps);
     348                                        if (printf_putchar('X', ps) == 1)
     349                                                counter++;
    357350                                } else {
    358                                         printf_putchar('x', ps);
     351                                        if (printf_putchar('x', ps) == 1)
     352                                                counter++;
    359353                                }
    360                                 written += 2;
    361354                                break;
    362355                }
     
    366359        precision -= number_size;
    367360        while (precision-- > 0) {       
    368                 printf_putchar('0', ps);       
    369                 written++;
     361                if (printf_putchar('0', ps) == 1)
     362                        counter++;
    370363        }
    371364
     
    373366        /* print number itself */
    374367
    375         written += printf_putstr(++ptr, ps);
     368        if ((retval = printf_putstr(++ptr, ps)) > 0) {
     369                counter += retval;
     370        }
    376371       
    377372        /* print ending spaces */
    378373       
    379374        while (width-- > 0) {   
    380                 printf_putchar(' ', ps);       
    381                 written++;
    382         }
    383 
    384         return written;
     375                if (printf_putchar(' ', ps) == 1)       
     376                        counter++;
     377        }
     378
     379        return counter;
    385380}
    386381
     
    485480                        /* print common characters if any processed */ 
    486481                        if (i > j) {
    487                                 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
     482                                if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */
    488483                                        counter = -counter;
    489484                                        goto out;
     
    582577                                */
    583578                                case 's':
    584                                         if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) {
     579                                        if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) {
    585580                                                counter = -counter;
    586581                                                goto out;
     
    592587                                case 'c':
    593588                                        c = va_arg(ap, unsigned int);
    594                                         if ((retval = print_char(c, width, flags, ps)) == EOF) {
     589                                        if ((retval = print_char(c, width, flags, ps)) < 0) {
    595590                                                counter = -counter;
    596591                                                goto out;
     
    693688                        }
    694689
    695                         if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) {
     690                        if ((retval = print_number(number, width, precision, base, flags, ps)) < 0) {
    696691                                counter = -counter;
    697692                                goto out;
     
    707702       
    708703        if (i > j) {
    709                 if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) == EOF) { /* error */
     704                if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) < 0) { /* error */
    710705                        counter = -counter;
    711706                        goto out;
  • generic/src/printf/vsnprintf.c

    r3247f0a r4ddeace  
    5151int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
    5252{
    53         size_t i,j;
     53        size_t i;
    5454        i = data->size - data->len;
    55         j = count;
    5655
    57         if (i > 0) {
    58                 if (i <= j) {
    59                         if (i == 1) {
    60                                 /* We have only one free byte left in buffer => write there trailing zero */
    61                                 data->string[data->size - 1] = 0;
    62                                 data->len = data->size;
    63                         } else {
    64                                 /* We have not enought space for whole string with the trailing zero => print only a part of string */
    65                                 memcpy((void *)(data->string + data->len), (void *)str, i - 1);
    66                                 data->string[data->size - 1] = 0;
    67                                 data->len = data->size;
    68                         }
    69                 } else {
    70                         /* Buffer is big enought to print whole string */
    71                         memcpy((void *)(data->string + data->len), (void *)str, j);
    72                         data->len += j;
    73                         /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
    74                         data->string[data->len] = 0;
    75                 }
     56        if ((count == 0) || (i == 0)) {
     57                return 0;
    7658        }
    7759       
     60        if (i == 1) {
     61                /* We have only one free byte left in buffer => write there trailing zero */
     62                data->string[data->size - 1] = 0;
     63                data->len = data->size;
     64                return 1;
     65        }
     66       
     67        if (i <= count) {
     68                /* We have not enought space for whole string with the trailing zero => print only a part of string */
     69                        memcpy((void *)(data->string + data->len), (void *)str, i - 1);
     70                        data->string[data->size - 1] = 0;
     71                        data->len = data->size;
     72                        return i;
     73        }
     74       
     75        /* Buffer is big enought to print whole string */
     76        memcpy((void *)(data->string + data->len), (void *)str, count);
     77        data->len += count;
     78        /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
     79        data->string[data->len] = 0;
     80
    7881        return count;   
    7982}
  • test/print/print1/test.c

    r3247f0a r4ddeace  
    3333void test(void)
    3434{
     35        int retval;
    3536        __native nat = 0x12345678u;
    3637       
    37         unsigned char buffer[BUFFER_SIZE];
     38        char buffer[BUFFER_SIZE];
    3839       
    3940        printf(" Printf test \n");
     
    5354        printf(" Print to NULL '%s'\n",NULL);
    5455
     56        retval = snprintf(buffer, BUFFER_SIZE, "Short text without parameters.");
     57        printf("Result is: '%s', retval = %d\n", buffer, retval);
     58
     59        retval = snprintf(buffer, BUFFER_SIZE, "Very very very long text without parameters.");
     60        printf("Result is: '%s', retval = %d\n", buffer, retval);
     61       
    5562        printf("Print short text to %d char long buffer via snprintf.\n", BUFFER_SIZE);
    56         snprintf(buffer, BUFFER_SIZE, "Short %s", "text");
    57         printf("Result is: '%s'\n", buffer);
     63        retval = snprintf(buffer, BUFFER_SIZE, "Short %s", "text");
     64        printf("Result is: '%s', retval = %d\n", buffer, retval);
    5865       
    5966        printf("Print long text to %d char long buffer via snprintf.\n", BUFFER_SIZE);
    60         snprintf(buffer, BUFFER_SIZE, "Very long %s. This text`s length is more than %d. We are interested in the result.", "text" , BUFFER_SIZE);
    61         printf("Result is: '%s'\n", buffer);
     67        retval = snprintf(buffer, BUFFER_SIZE, "Very long %s. This text`s length is more than %d. We are interested in the result.", "text" , BUFFER_SIZE);
     68        printf("Result is: '%s', retval = %d\n", buffer, retval);
    6269       
    6370        return;
Note: See TracChangeset for help on using the changeset viewer.