Subversion Repositories HelenOS-historic

Compare Revisions

Ignore whitespace Rev 1604 → Rev 1603

/kernel/trunk/generic/src/printf/vsnprintf.c
50,34 → 50,31
*/
int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
{
size_t i;
size_t i,j;
i = data->size - data->len;
j = count;
 
if ((count == 0) || (i == 0)) {
return 0;
if (i > 0) {
if (i <= j) {
if (i == 1) {
/* We have only one free byte left in buffer => write there trailing zero */
data->string[data->size - 1] = 0;
data->len = data->size;
} else {
/* We have not enought space for whole string with the trailing zero => print only a part of string */
memcpy((void *)(data->string + data->len), (void *)str, i - 1);
data->string[data->size - 1] = 0;
data->len = data->size;
}
} else {
/* Buffer is big enought to print whole string */
memcpy((void *)(data->string + data->len), (void *)str, j);
data->len += j;
/* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
data->string[data->len] = 0;
}
}
if (i == 1) {
/* We have only one free byte left in buffer => write there trailing zero */
data->string[data->size - 1] = 0;
data->len = data->size;
return 1;
}
if (i <= count) {
/* We have not enought space for whole string with the trailing zero => print only a part of string */
memcpy((void *)(data->string + data->len), (void *)str, i - 1);
data->string[data->size - 1] = 0;
data->len = data->size;
return i;
}
/* Buffer is big enought to print whole string */
memcpy((void *)(data->string + data->len), (void *)str, count);
data->len += count;
/* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
data->string[data->len] = 0;
 
return count;
}
 
/kernel/trunk/generic/src/printf/printf_core.c
100,17 → 100,21
* @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
* @param count
* @param ps output method and its data
* @return number or printed characters
* @return 0 on success, EOF on fail
*/
static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps)
{
return ps->write((void *)buf, count, ps->data);
if (ps->write((void *)buf, count, ps->data) == count) {
return 0;
}
return EOF;
}
 
/** Print string without added newline
* @param str string to print
* @param ps write function specification and support data
* @return number or printed characters
* @return 0 on success or EOF on fail
*/
static int printf_putstr(const char * str, struct printf_spec *ps)
{
122,19 → 126,27
 
count = strlen(str);
 
return ps->write((void *) str, count, ps->data);
if (ps->write((void *) str, count, ps->data) == count) {
return 0;
}
return EOF;
}
 
/** Print one character to output
* @param c one character
* @param ps output method
* @return number or printed characters
* @return printed character or EOF
*/
static int printf_putchar(int c, struct printf_spec *ps)
{
unsigned char ch = c;
return ps->write((void *) &ch, 1, ps->data);
if (ps->write((void *) &ch, 1, ps->data) == 1) {
return c;
}
return EOF;
}
 
/** Print one formatted character
141,7 → 153,7
* @param c character to print
* @param width
* @param flags
* @return number of printed characters, negative value on fail
* @return number of printed characters or EOF
*/
static int print_char(char c, int width, __u64 flags, struct printf_spec *ps)
{
150,17 → 162,18
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
while (--width > 0) { /* one space is consumed by character itself hence predecrement */
/* FIXME: painful slow */
if (printf_putchar(' ', ps) > 0)
++counter;
printf_putchar(' ', ps);
++counter;
}
}
if (printf_putchar(c, ps) > 0)
counter++;
if (printf_putchar(c, ps) == EOF) {
return EOF;
}
 
while (--width > 0) { /* one space is consumed by character itself hence predecrement */
if (printf_putchar(' ', ps) > 0)
++counter;
printf_putchar(' ', ps);
++counter;
}
return ++counter;
171,7 → 184,7
* @param width
* @param precision
* @param flags
* @return number of printed characters or negative value on fail
* @return number of printed characters or EOF
*/
static int print_string(char *s, int width, int precision, __u64 flags, struct printf_spec *ps)
178,7 → 191,6
{
int counter = 0;
size_t size;
int retval;
 
if (s == NULL) {
return printf_putstr("(NULL)", ps);
195,28 → 207,29
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
while (width-- > 0) {
if (printf_putchar(' ', ps) == 1)
counter++;
printf_putchar(' ', ps);
counter++;
}
}
 
while (precision > size) {
precision--;
if (printf_putchar(' ', ps) == 1)
++counter;
printf_putchar(' ', ps);
++counter;
}
if ((retval = printf_putnchars(s, precision, ps)) < 0) {
return -counter;
if (printf_putnchars(s, precision, ps) == EOF) {
return EOF;
}
counter += retval;
 
counter += precision;
 
while (width-- > 0) {
if (printf_putchar(' ', ps) == 1)
++counter;
printf_putchar(' ', ps);
++counter;
}
return counter;
return ++counter;
}
 
 
241,9 → 254,8
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;
310,16 → 322,15
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
while (width-- > 0) {
if (printf_putchar(' ', ps) == 1)
counter++;
printf_putchar(' ', ps);
written++;
}
}
/* print sign */
if (sgn) {
if (printf_putchar(sgn, ps) == 1)
counter++;
printf_putchar(sgn, ps);
written++;
}
/* print prefix */
327,30 → 338,26
if (flags & __PRINTF_FLAG_PREFIX) {
switch(base) {
case 2: /* Binary formating is not standard, but usefull */
if (printf_putchar('0', ps) == 1)
counter++;
printf_putchar('0', ps);
if (flags & __PRINTF_FLAG_BIGCHARS) {
if (printf_putchar('B', ps) == 1)
counter++;
printf_putchar('B', ps);
} else {
if (printf_putchar('b', ps) == 1)
counter++;
printf_putchar('b', ps);
}
written += 2;
break;
case 8:
if (printf_putchar('o', ps) == 1)
counter++;
printf_putchar('o', ps);
written++;
break;
case 16:
if (printf_putchar('0', ps) == 1)
counter++;
printf_putchar('0', ps);
if (flags & __PRINTF_FLAG_BIGCHARS) {
if (printf_putchar('X', ps) == 1)
counter++;
printf_putchar('X', ps);
} else {
if (printf_putchar('x', ps) == 1)
counter++;
printf_putchar('x', ps);
}
written += 2;
break;
}
}
358,25 → 365,23
/* print leading zeroes */
precision -= number_size;
while (precision-- > 0) {
if (printf_putchar('0', ps) == 1)
counter++;
printf_putchar('0', ps);
written++;
}
 
/* print number itself */
 
if ((retval = printf_putstr(++ptr, ps)) > 0) {
counter += retval;
}
written += printf_putstr(++ptr, ps);
/* print ending spaces */
while (width-- > 0) {
if (printf_putchar(' ', ps) == 1)
counter++;
printf_putchar(' ', ps);
written++;
}
 
return counter;
return written;
}
 
 
479,7 → 484,7
if (c == '%' ) {
/* print common characters if any processed */
if (i > j) {
if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */
if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
counter = -counter;
goto out;
}
576,7 → 581,7
* String and character conversions.
*/
case 's':
if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) {
if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) {
counter = -counter;
goto out;
};
586,7 → 591,7
goto next_char;
case 'c':
c = va_arg(ap, unsigned int);
if ((retval = print_char(c, width, flags, ps)) < 0) {
if ((retval = print_char(c, width, flags, ps)) == EOF) {
counter = -counter;
goto out;
};
687,7 → 692,7
}
}
 
if ((retval = print_number(number, width, precision, base, flags, ps)) < 0) {
if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) {
counter = -counter;
goto out;
};
701,7 → 706,7
}
if (i > j) {
if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) < 0) { /* error */
if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) == EOF) { /* error */
counter = -counter;
goto out;
/kernel/trunk/test/print/print1/test.c
32,10 → 32,9
 
void test(void)
{
int retval;
__native nat = 0x12345678u;
char buffer[BUFFER_SIZE];
unsigned char buffer[BUFFER_SIZE];
printf(" Printf test \n");
53,19 → 52,13
printf(" Print to NULL '%s'\n",NULL);
 
retval = snprintf(buffer, BUFFER_SIZE, "Short text without parameters.");
printf("Result is: '%s', retval = %d\n", buffer, retval);
 
retval = snprintf(buffer, BUFFER_SIZE, "Very very very long text without parameters.");
printf("Result is: '%s', retval = %d\n", buffer, retval);
printf("Print short text to %d char long buffer via snprintf.\n", BUFFER_SIZE);
retval = snprintf(buffer, BUFFER_SIZE, "Short %s", "text");
printf("Result is: '%s', retval = %d\n", buffer, retval);
snprintf(buffer, BUFFER_SIZE, "Short %s", "text");
printf("Result is: '%s'\n", buffer);
printf("Print long text to %d char long buffer via snprintf.\n", BUFFER_SIZE);
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);
printf("Result is: '%s', retval = %d\n", buffer, 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);
printf("Result is: '%s'\n", buffer);
return;
}