100,21 → 100,17 |
* @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
* @param count |
* @param ps output method and its data |
* @return 0 on success, EOF on fail |
* @return number or printed characters |
*/ |
static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) |
{ |
if (ps->write((void *)buf, count, ps->data) == count) { |
return 0; |
} |
|
return EOF; |
return ps->write((void *)buf, count, ps->data); |
} |
|
/** Print string without added newline |
* @param str string to print |
* @param ps write function specification and support data |
* @return 0 on success or EOF on fail |
* @return number or printed characters |
*/ |
static int printf_putstr(const char * str, struct printf_spec *ps) |
{ |
126,27 → 122,19 |
|
count = strlen(str); |
|
if (ps->write((void *) str, count, ps->data) == count) { |
return 0; |
} |
|
return EOF; |
return ps->write((void *) str, count, ps->data); |
} |
|
/** Print one character to output |
* @param c one character |
* @param ps output method |
* @return printed character or EOF |
* @return number or printed characters |
*/ |
static int printf_putchar(int c, struct printf_spec *ps) |
{ |
unsigned char ch = c; |
|
if (ps->write((void *) &ch, 1, ps->data) == 1) { |
return c; |
} |
|
return EOF; |
return ps->write((void *) &ch, 1, ps->data); |
} |
|
/** Print one formatted character |
153,7 → 141,7 |
* @param c character to print |
* @param width |
* @param flags |
* @return number of printed characters or EOF |
* @return number of printed characters, negative value on fail |
*/ |
static int print_char(char c, int width, __u64 flags, struct printf_spec *ps) |
{ |
162,18 → 150,17 |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
/* FIXME: painful slow */ |
printf_putchar(' ', ps); |
++counter; |
if (printf_putchar(' ', ps) > 0) |
++counter; |
} |
} |
|
if (printf_putchar(c, ps) == EOF) { |
return EOF; |
} |
if (printf_putchar(c, ps) > 0) |
counter++; |
|
while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
printf_putchar(' ', ps); |
++counter; |
if (printf_putchar(' ', ps) > 0) |
++counter; |
} |
|
return ++counter; |
184,7 → 171,7 |
* @param width |
* @param precision |
* @param flags |
* @return number of printed characters or EOF |
* @return number of printed characters or negative value on fail |
*/ |
|
static int print_string(char *s, int width, int precision, __u64 flags, struct printf_spec *ps) |
191,6 → 178,7 |
{ |
int counter = 0; |
size_t size; |
int retval; |
|
if (s == NULL) { |
return printf_putstr("(NULL)", ps); |
207,29 → 195,28 |
|
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (width-- > 0) { |
printf_putchar(' ', ps); |
counter++; |
if (printf_putchar(' ', ps) == 1) |
counter++; |
} |
} |
|
while (precision > size) { |
precision--; |
printf_putchar(' ', ps); |
++counter; |
if (printf_putchar(' ', ps) == 1) |
++counter; |
} |
|
if (printf_putnchars(s, precision, ps) == EOF) { |
return EOF; |
if ((retval = printf_putnchars(s, precision, ps)) < 0) { |
return -counter; |
} |
counter += retval; |
|
counter += precision; |
|
while (width-- > 0) { |
printf_putchar(' ', ps); |
++counter; |
if (printf_putchar(' ', ps) == 1) |
++counter; |
} |
|
return ++counter; |
return counter; |
} |
|
|
254,8 → 241,9 |
char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
int size = 0; /* size of number with all prefixes and signs */ |
int number_size; /* size of plain number */ |
int written = 0; |
char sgn; |
int retval; |
int counter = 0; |
|
if (flags & __PRINTF_FLAG_BIGCHARS) |
digits = digits_big; |
322,15 → 310,16 |
|
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
while (width-- > 0) { |
printf_putchar(' ', ps); |
written++; |
if (printf_putchar(' ', ps) == 1) |
counter++; |
} |
} |
|
|
/* print sign */ |
if (sgn) { |
printf_putchar(sgn, ps); |
written++; |
if (printf_putchar(sgn, ps) == 1) |
counter++; |
} |
|
/* print prefix */ |
338,26 → 327,30 |
if (flags & __PRINTF_FLAG_PREFIX) { |
switch(base) { |
case 2: /* Binary formating is not standard, but usefull */ |
printf_putchar('0', ps); |
if (printf_putchar('0', ps) == 1) |
counter++; |
if (flags & __PRINTF_FLAG_BIGCHARS) { |
printf_putchar('B', ps); |
if (printf_putchar('B', ps) == 1) |
counter++; |
} else { |
printf_putchar('b', ps); |
if (printf_putchar('b', ps) == 1) |
counter++; |
} |
written += 2; |
break; |
case 8: |
printf_putchar('o', ps); |
written++; |
if (printf_putchar('o', ps) == 1) |
counter++; |
break; |
case 16: |
printf_putchar('0', ps); |
if (printf_putchar('0', ps) == 1) |
counter++; |
if (flags & __PRINTF_FLAG_BIGCHARS) { |
printf_putchar('X', ps); |
if (printf_putchar('X', ps) == 1) |
counter++; |
} else { |
printf_putchar('x', ps); |
if (printf_putchar('x', ps) == 1) |
counter++; |
} |
written += 2; |
break; |
} |
} |
365,23 → 358,25 |
/* print leading zeroes */ |
precision -= number_size; |
while (precision-- > 0) { |
printf_putchar('0', ps); |
written++; |
if (printf_putchar('0', ps) == 1) |
counter++; |
} |
|
|
/* print number itself */ |
|
written += printf_putstr(++ptr, ps); |
if ((retval = printf_putstr(++ptr, ps)) > 0) { |
counter += retval; |
} |
|
/* print ending spaces */ |
|
while (width-- > 0) { |
printf_putchar(' ', ps); |
written++; |
if (printf_putchar(' ', ps) == 1) |
counter++; |
} |
|
return written; |
return counter; |
} |
|
|
484,7 → 479,7 |
if (c == '%' ) { |
/* print common characters if any processed */ |
if (i > j) { |
if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */ |
if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */ |
counter = -counter; |
goto out; |
} |
581,7 → 576,7 |
* String and character conversions. |
*/ |
case 's': |
if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) { |
if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) { |
counter = -counter; |
goto out; |
}; |
591,7 → 586,7 |
goto next_char; |
case 'c': |
c = va_arg(ap, unsigned int); |
if ((retval = print_char(c, width, flags, ps)) == EOF) { |
if ((retval = print_char(c, width, flags, ps)) < 0) { |
counter = -counter; |
goto out; |
}; |
692,7 → 687,7 |
} |
} |
|
if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) { |
if ((retval = print_number(number, width, precision, base, flags, ps)) < 0) { |
counter = -counter; |
goto out; |
}; |
706,7 → 701,7 |
} |
|
if (i > j) { |
if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) == EOF) { /* error */ |
if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) < 0) { /* error */ |
counter = -counter; |
goto out; |
|