Changeset 3214a20 in mainline
- Timestamp:
- 2006-04-24T21:05:59Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 11fa83a
- Parents:
- 4309741
- Location:
- libc
- Files:
-
- 7 added
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
libc/Makefile
r4309741 r3214a20 51 51 generic/futex.c \ 52 52 generic/io/io.c \ 53 generic/io/print.c \ 53 generic/io/printf.c \ 54 generic/io/sprintf.c \ 55 generic/io/snprintf.c \ 56 generic/io/vprintf.c \ 57 generic/io/vsprintf.c \ 58 generic/io/vsnprintf.c \ 59 generic/io/printf_core.c \ 54 60 malloc/malloc.c \ 55 61 generic/psthread.c -
libc/generic/io/printf_core.c
r4309741 r3214a20 28 28 */ 29 29 30 #include <unistd.h> 30 31 #include <stdio.h> 31 #include <unistd.h> 32 #include <io/io.h> 33 #include <stdarg.h> 32 #include <io/printf_core.h> 34 33 #include <ctype.h> 35 34 #include <string.h> … … 62 61 static char digits_big[] = "0123456789ABCDEF"; /* Big hexadecimal characters */ 63 62 63 /** Print count chars from buffer without adding newline 64 * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! 65 * @param count 66 * @param ps output method and its data 67 * @return 0 on success, EOF on fail 68 */ 69 static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) 70 { 71 if (ps->write((void *)buf, count, ps->data) == count) { 72 return 0; 73 } 74 75 return EOF; 76 } 77 78 /** Print string without added newline 79 * @param str string to print 80 * @param ps write function specification and support data 81 * @return 0 on success or EOF on fail 82 */ 83 static int printf_putstr(const char * str, struct printf_spec *ps) 84 { 85 size_t count; 86 87 if (str == NULL) { 88 return printf_putnchars("(NULL)", 6, ps); 89 } 90 91 for (count = 0; str[count] != 0; count++); 92 93 if (ps->write((void *) str, count, ps->data) == count) { 94 return 0; 95 } 96 97 return EOF; 98 } 99 100 /** Print one character to output 101 * @param c one character 102 * @param ps output method 103 * @return printed character or EOF 104 */ 105 static int printf_putchar(int c, struct printf_spec *ps) 106 { 107 unsigned char ch = c; 108 109 if (ps->write((void *) &ch, 1, ps->data) == 1) { 110 return c; 111 } 112 113 return EOF; 114 } 64 115 65 116 /** Print one formatted character … … 69 120 * @return number of printed characters or EOF 70 121 */ 71 static int print_char(char c, int width, uint64_t flags )122 static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps) 72 123 { 73 124 int counter = 0; … … 76 127 while (--width > 0) { /* one space is consumed by character itself hence predecrement */ 77 128 /* FIXME: painful slow */ 78 p utchar(' ');129 printf_putchar(' ', ps); 79 130 ++counter; 80 131 } 81 132 } 82 133 83 if (p utchar(c) == EOF) {134 if (printf_putchar(c, ps) == EOF) { 84 135 return EOF; 85 136 } 86 137 87 138 while (--width > 0) { /* one space is consumed by character itself hence predecrement */ 88 p utchar(' ');139 printf_putchar(' ', ps); 89 140 ++counter; 90 141 } … … 101 152 */ 102 153 103 static int print_string(char *s, int width, int precision, uint64_t flags )154 static int print_string(char *s, int width, int precision, uint64_t flags, struct printf_spec *ps) 104 155 { 105 156 int counter = 0; … … 107 158 108 159 if (s == NULL) { 109 return p utstr("(NULL)");160 return printf_putstr("(NULL)", ps); 110 161 } 111 162 … … 121 172 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { 122 173 while (width-- > 0) { 123 p utchar(' ');174 printf_putchar(' ', ps); 124 175 counter++; 125 176 } … … 128 179 while (precision > size) { 129 180 precision--; 130 p utchar(' ');181 printf_putchar(' ', ps); 131 182 ++counter; 132 183 } 133 184 134 if (p utnchars(s, precision) == EOF) {185 if (printf_putnchars(s, precision, ps) == EOF) { 135 186 return EOF; 136 187 } … … 139 190 140 191 while (width-- > 0) { 141 p utchar(' ');192 printf_putchar(' ', ps); 142 193 ++counter; 143 194 } … … 161 212 * 162 213 */ 163 static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags )214 static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags, struct printf_spec *ps) 164 215 { 165 216 char *digits = digits_small; … … 187 238 188 239 number_size = size; 189 240 190 241 /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */ 191 242 if (flags & __PRINTF_FLAG_PREFIX) { … … 236 287 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { 237 288 while (width-- > 0) { 238 p utchar(' ');289 printf_putchar(' ', ps); 239 290 written++; 240 291 } … … 243 294 /* print sign */ 244 295 if (sgn) { 245 p utchar(sgn);296 printf_putchar(sgn, ps); 246 297 written++; 247 298 } … … 252 303 switch(base) { 253 304 case 2: /* Binary formating is not standard, but usefull */ 254 p utchar('0');305 printf_putchar('0', ps); 255 306 if (flags & __PRINTF_FLAG_BIGCHARS) { 256 p utchar('B');307 printf_putchar('B', ps); 257 308 } else { 258 p utchar('b');309 printf_putchar('b', ps); 259 310 } 260 311 written += 2; 261 312 break; 262 313 case 8: 263 p utchar('o');314 printf_putchar('o', ps); 264 315 written++; 265 316 break; 266 317 case 16: 267 p utchar('0');318 printf_putchar('0', ps); 268 319 if (flags & __PRINTF_FLAG_BIGCHARS) { 269 p utchar('X');320 printf_putchar('X', ps); 270 321 } else { 271 p utchar('x');322 printf_putchar('x', ps); 272 323 } 273 324 written += 2; … … 279 330 precision -= number_size; 280 331 while (precision-- > 0) { 281 p utchar('0');332 printf_putchar('0', ps); 282 333 written++; 283 334 } … … 286 337 /* print number itself */ 287 338 288 written += p utstr(++ptr);339 written += printf_putstr(++ptr, ps); 289 340 290 341 /* print ending spaces */ 291 342 292 343 while (width-- > 0) { 293 p utchar(' ');344 printf_putchar(' ', ps); 294 345 written++; 295 346 } … … 363 414 * @return count of printed characters or negative value on fail. 364 415 */ 365 int printf (const char *fmt, ...)416 int printf_core(const char *fmt, struct printf_spec *ps, va_list ap) 366 417 { 367 418 int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */ … … 369 420 int counter; /* counter of printed characters */ 370 421 int retval; /* used to store return values from called functions */ 371 va_list ap;372 422 char c; 373 423 qualifier_t qualifier; /* type of argument */ … … 379 429 380 430 counter = 0; 381 va_start(ap, fmt);382 431 383 432 while ((c = fmt[i])) { … … 386 435 /* print common characters if any processed */ 387 436 if (i > j) { 388 if ((retval = p utnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */437 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */ 389 438 return -counter; 390 439 } … … 482 531 */ 483 532 case 's': 484 if ((retval = print_string(va_arg(ap, char*), width, precision, flags )) == EOF) {533 if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) { 485 534 return -counter; 486 535 }; … … 491 540 case 'c': 492 541 c = va_arg(ap, unsigned int); 493 if ((retval = print_char(c, width, flags 542 if ((retval = print_char(c, width, flags, ps)) == EOF) { 494 543 return -counter; 495 544 }; … … 591 640 } 592 641 593 if ((retval = print_number(number, width, precision, base, flags )) == EOF ) {642 if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) { 594 643 return -counter; 595 644 }; … … 604 653 605 654 if (i > j) { 606 if ((retval = p utnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */655 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */ 607 656 return -counter; 608 657 } … … 610 659 } 611 660 612 va_end(ap);613 661 return counter; 614 662 } -
libc/include/io/io.h
r4309741 r3214a20 28 28 29 29 #ifndef __LIBC__IO_H__ 30 #define __LIBC__IO_ IO_H__30 #define __LIBC__IO_H__ 31 31 32 32 #include <libarch/types.h> -
libc/include/stdio.h
r4309741 r3214a20 31 31 32 32 #include <types.h> 33 #include <stdarg.h> 33 34 34 35 #define EOF (-1) … … 37 38 38 39 extern int printf(const char *fmt, ...); 40 extern int sprintf(char *str, const char *fmt, ...); 41 extern int snprintf(char *str, size_t size, const char *fmt, ...); 42 43 extern int vprintf(const char *fmt, va_list ap); 44 extern int vsprintf(char *str, const char *fmt, va_list ap); 45 extern int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); 46 39 47 #define fprintf(f, fmt, ...) printf(fmt, ##__VA_ARGS__) 40 48
Note:
See TracChangeset
for help on using the changeset viewer.