Rev 1273 | Rev 1702 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 1273 | Rev 1604 | ||
|---|---|---|---|
| Line 98... | Line 98... | ||
| 98 | 98 | ||
| 99 | /** Print count chars from buffer without adding newline |
99 | /** Print count chars from buffer without adding newline |
| 100 | * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
100 | * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
| 101 | * @param count |
101 | * @param count |
| 102 | * @param ps output method and its data |
102 | * @param ps output method and its data |
| 103 | * @return 0 on success, EOF on fail |
103 | * @return number or printed characters |
| 104 | */ |
104 | */ |
| 105 | static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) |
105 | static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) |
| 106 | { |
106 | { |
| 107 | if (ps->write((void *)buf, count, ps->data) == count) { |
107 | return ps->write((void *)buf, count, ps->data); |
| 108 | return 0; |
- | |
| 109 | } |
- | |
| 110 | - | ||
| 111 | return EOF; |
- | |
| 112 | } |
108 | } |
| 113 | 109 | ||
| 114 | /** Print string without added newline |
110 | /** Print string without added newline |
| 115 | * @param str string to print |
111 | * @param str string to print |
| 116 | * @param ps write function specification and support data |
112 | * @param ps write function specification and support data |
| 117 | * @return 0 on success or EOF on fail |
113 | * @return number or printed characters |
| 118 | */ |
114 | */ |
| 119 | static int printf_putstr(const char * str, struct printf_spec *ps) |
115 | static int printf_putstr(const char * str, struct printf_spec *ps) |
| 120 | { |
116 | { |
| 121 | size_t count; |
117 | size_t count; |
| 122 | 118 | ||
| Line 124... | Line 120... | ||
| 124 | return printf_putnchars("(NULL)", 6, ps); |
120 | return printf_putnchars("(NULL)", 6, ps); |
| 125 | } |
121 | } |
| 126 | 122 | ||
| 127 | count = strlen(str); |
123 | count = strlen(str); |
| 128 | 124 | ||
| 129 | if (ps->write((void *) str, count, ps->data) == count) { |
125 | return ps->write((void *) str, count, ps->data); |
| 130 | return 0; |
- | |
| 131 | } |
- | |
| 132 | - | ||
| 133 | return EOF; |
- | |
| 134 | } |
126 | } |
| 135 | 127 | ||
| 136 | /** Print one character to output |
128 | /** Print one character to output |
| 137 | * @param c one character |
129 | * @param c one character |
| 138 | * @param ps output method |
130 | * @param ps output method |
| 139 | * @return printed character or EOF |
131 | * @return number or printed characters |
| 140 | */ |
132 | */ |
| 141 | static int printf_putchar(int c, struct printf_spec *ps) |
133 | static int printf_putchar(int c, struct printf_spec *ps) |
| 142 | { |
134 | { |
| 143 | unsigned char ch = c; |
135 | unsigned char ch = c; |
| 144 | 136 | ||
| 145 | if (ps->write((void *) &ch, 1, ps->data) == 1) { |
137 | return ps->write((void *) &ch, 1, ps->data); |
| 146 | return c; |
- | |
| 147 | } |
- | |
| 148 | - | ||
| 149 | return EOF; |
- | |
| 150 | } |
138 | } |
| 151 | 139 | ||
| 152 | /** Print one formatted character |
140 | /** Print one formatted character |
| 153 | * @param c character to print |
141 | * @param c character to print |
| 154 | * @param width |
142 | * @param width |
| 155 | * @param flags |
143 | * @param flags |
| 156 | * @return number of printed characters or EOF |
144 | * @return number of printed characters, negative value on fail |
| 157 | */ |
145 | */ |
| 158 | static int print_char(char c, int width, __u64 flags, struct printf_spec *ps) |
146 | static int print_char(char c, int width, __u64 flags, struct printf_spec *ps) |
| 159 | { |
147 | { |
| 160 | int counter = 0; |
148 | int counter = 0; |
| 161 | 149 | ||
| 162 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
150 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
| 163 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
151 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
| 164 | /* FIXME: painful slow */ |
152 | /* FIXME: painful slow */ |
| 165 | printf_putchar(' ', ps); |
153 | if (printf_putchar(' ', ps) > 0) |
| 166 | ++counter; |
154 | ++counter; |
| 167 | } |
155 | } |
| 168 | } |
156 | } |
| 169 | 157 | ||
| 170 | if (printf_putchar(c, ps) == EOF) { |
158 | if (printf_putchar(c, ps) > 0) |
| 171 | return EOF; |
159 | counter++; |
| 172 | } |
- | |
| 173 | 160 | ||
| 174 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
161 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
| 175 | printf_putchar(' ', ps); |
162 | if (printf_putchar(' ', ps) > 0) |
| 176 | ++counter; |
163 | ++counter; |
| 177 | } |
164 | } |
| 178 | 165 | ||
| 179 | return ++counter; |
166 | return ++counter; |
| 180 | } |
167 | } |
| 181 | 168 | ||
| 182 | /** Print one string |
169 | /** Print one string |
| 183 | * @param s string |
170 | * @param s string |
| 184 | * @param width |
171 | * @param width |
| 185 | * @param precision |
172 | * @param precision |
| 186 | * @param flags |
173 | * @param flags |
| 187 | * @return number of printed characters or EOF |
174 | * @return number of printed characters or negative value on fail |
| 188 | */ |
175 | */ |
| 189 | 176 | ||
| 190 | static int print_string(char *s, int width, int precision, __u64 flags, struct printf_spec *ps) |
177 | static int print_string(char *s, int width, int precision, __u64 flags, struct printf_spec *ps) |
| 191 | { |
178 | { |
| 192 | int counter = 0; |
179 | int counter = 0; |
| 193 | size_t size; |
180 | size_t size; |
| - | 181 | int retval; |
|
| 194 | 182 | ||
| 195 | if (s == NULL) { |
183 | if (s == NULL) { |
| 196 | return printf_putstr("(NULL)", ps); |
184 | return printf_putstr("(NULL)", ps); |
| 197 | } |
185 | } |
| 198 | 186 | ||
| Line 205... | Line 193... | ||
| 205 | 193 | ||
| 206 | width -= precision; |
194 | width -= precision; |
| 207 | 195 | ||
| 208 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
196 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
| 209 | while (width-- > 0) { |
197 | while (width-- > 0) { |
| 210 | printf_putchar(' ', ps); |
198 | if (printf_putchar(' ', ps) == 1) |
| 211 | counter++; |
199 | counter++; |
| 212 | } |
200 | } |
| 213 | } |
201 | } |
| 214 | 202 | ||
| 215 | while (precision > size) { |
203 | while (precision > size) { |
| 216 | precision--; |
204 | precision--; |
| 217 | printf_putchar(' ', ps); |
205 | if (printf_putchar(' ', ps) == 1) |
| 218 | ++counter; |
206 | ++counter; |
| 219 | } |
207 | } |
| 220 | 208 | ||
| 221 | if (printf_putnchars(s, precision, ps) == EOF) { |
209 | if ((retval = printf_putnchars(s, precision, ps)) < 0) { |
| 222 | return EOF; |
210 | return -counter; |
| 223 | } |
211 | } |
| 224 | - | ||
| 225 | counter += precision; |
212 | counter += retval; |
| 226 | 213 | ||
| 227 | while (width-- > 0) { |
214 | while (width-- > 0) { |
| 228 | printf_putchar(' ', ps); |
215 | if (printf_putchar(' ', ps) == 1) |
| 229 | ++counter; |
216 | ++counter; |
| 230 | } |
217 | } |
| 231 | 218 | ||
| 232 | return ++counter; |
219 | return counter; |
| 233 | } |
220 | } |
| 234 | 221 | ||
| 235 | 222 | ||
| 236 | /** Print number in given base |
223 | /** Print number in given base |
| 237 | * |
224 | * |
| Line 252... | Line 239... | ||
| 252 | char *digits = digits_small; |
239 | char *digits = digits_small; |
| 253 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */ |
240 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */ |
| 254 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
241 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
| 255 | int size = 0; /* size of number with all prefixes and signs */ |
242 | int size = 0; /* size of number with all prefixes and signs */ |
| 256 | int number_size; /* size of plain number */ |
243 | int number_size; /* size of plain number */ |
| 257 | int written = 0; |
- | |
| 258 | char sgn; |
244 | char sgn; |
| - | 245 | int retval; |
|
| - | 246 | int counter = 0; |
|
| 259 | 247 | ||
| 260 | if (flags & __PRINTF_FLAG_BIGCHARS) |
248 | if (flags & __PRINTF_FLAG_BIGCHARS) |
| 261 | digits = digits_big; |
249 | digits = digits_big; |
| 262 | 250 | ||
| 263 | *ptr-- = 0; /* Put zero at end of string */ |
251 | *ptr-- = 0; /* Put zero at end of string */ |
| Line 320... | Line 308... | ||
| 320 | 308 | ||
| 321 | width -= precision + size - number_size; |
309 | width -= precision + size - number_size; |
| 322 | 310 | ||
| 323 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
311 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
| 324 | while (width-- > 0) { |
312 | while (width-- > 0) { |
| 325 | printf_putchar(' ', ps); |
313 | if (printf_putchar(' ', ps) == 1) |
| 326 | written++; |
314 | counter++; |
| 327 | } |
315 | } |
| 328 | } |
316 | } |
| 329 | 317 | ||
| - | 318 | ||
| 330 | /* print sign */ |
319 | /* print sign */ |
| 331 | if (sgn) { |
320 | if (sgn) { |
| 332 | printf_putchar(sgn, ps); |
321 | if (printf_putchar(sgn, ps) == 1) |
| 333 | written++; |
322 | counter++; |
| 334 | } |
323 | } |
| 335 | 324 | ||
| 336 | /* print prefix */ |
325 | /* print prefix */ |
| 337 | 326 | ||
| 338 | if (flags & __PRINTF_FLAG_PREFIX) { |
327 | if (flags & __PRINTF_FLAG_PREFIX) { |
| 339 | switch(base) { |
328 | switch(base) { |
| 340 | case 2: /* Binary formating is not standard, but usefull */ |
329 | case 2: /* Binary formating is not standard, but usefull */ |
| 341 | printf_putchar('0', ps); |
330 | if (printf_putchar('0', ps) == 1) |
| - | 331 | counter++; |
|
| 342 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
332 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
| 343 | printf_putchar('B', ps); |
333 | if (printf_putchar('B', ps) == 1) |
| - | 334 | counter++; |
|
| 344 | } else { |
335 | } else { |
| 345 | printf_putchar('b', ps); |
336 | if (printf_putchar('b', ps) == 1) |
| - | 337 | counter++; |
|
| 346 | } |
338 | } |
| 347 | written += 2; |
- | |
| 348 | break; |
339 | break; |
| 349 | case 8: |
340 | case 8: |
| 350 | printf_putchar('o', ps); |
341 | if (printf_putchar('o', ps) == 1) |
| 351 | written++; |
342 | counter++; |
| 352 | break; |
343 | break; |
| 353 | case 16: |
344 | case 16: |
| 354 | printf_putchar('0', ps); |
345 | if (printf_putchar('0', ps) == 1) |
| - | 346 | counter++; |
|
| 355 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
347 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
| 356 | printf_putchar('X', ps); |
348 | if (printf_putchar('X', ps) == 1) |
| - | 349 | counter++; |
|
| 357 | } else { |
350 | } else { |
| 358 | printf_putchar('x', ps); |
351 | if (printf_putchar('x', ps) == 1) |
| - | 352 | counter++; |
|
| 359 | } |
353 | } |
| 360 | written += 2; |
- | |
| 361 | break; |
354 | break; |
| 362 | } |
355 | } |
| 363 | } |
356 | } |
| 364 | 357 | ||
| 365 | /* print leading zeroes */ |
358 | /* print leading zeroes */ |
| 366 | precision -= number_size; |
359 | precision -= number_size; |
| 367 | while (precision-- > 0) { |
360 | while (precision-- > 0) { |
| 368 | printf_putchar('0', ps); |
361 | if (printf_putchar('0', ps) == 1) |
| 369 | written++; |
362 | counter++; |
| 370 | } |
363 | } |
| 371 | 364 | ||
| 372 | 365 | ||
| 373 | /* print number itself */ |
366 | /* print number itself */ |
| 374 | 367 | ||
| 375 | written += printf_putstr(++ptr, ps); |
368 | if ((retval = printf_putstr(++ptr, ps)) > 0) { |
| - | 369 | counter += retval; |
|
| - | 370 | } |
|
| 376 | 371 | ||
| 377 | /* print ending spaces */ |
372 | /* print ending spaces */ |
| 378 | 373 | ||
| 379 | while (width-- > 0) { |
374 | while (width-- > 0) { |
| 380 | printf_putchar(' ', ps); |
375 | if (printf_putchar(' ', ps) == 1) |
| 381 | written++; |
376 | counter++; |
| 382 | } |
377 | } |
| 383 | 378 | ||
| 384 | return written; |
379 | return counter; |
| 385 | } |
380 | } |
| 386 | 381 | ||
| 387 | 382 | ||
| 388 | /** Print formatted string. |
383 | /** Print formatted string. |
| 389 | * |
384 | * |
| Line 482... | Line 477... | ||
| 482 | while ((c = fmt[i])) { |
477 | while ((c = fmt[i])) { |
| 483 | /* control character */ |
478 | /* control character */ |
| 484 | if (c == '%' ) { |
479 | if (c == '%' ) { |
| 485 | /* print common characters if any processed */ |
480 | /* print common characters if any processed */ |
| 486 | if (i > j) { |
481 | 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 */ |
| 488 | counter = -counter; |
483 | counter = -counter; |
| 489 | goto out; |
484 | goto out; |
| 490 | } |
485 | } |
| 491 | counter += retval; |
486 | counter += retval; |
| 492 | } |
487 | } |
| Line 579... | Line 574... | ||
| 579 | 574 | ||
| 580 | /* |
575 | /* |
| 581 | * String and character conversions. |
576 | * String and character conversions. |
| 582 | */ |
577 | */ |
| 583 | case 's': |
578 | 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) { |
| 585 | counter = -counter; |
580 | counter = -counter; |
| 586 | goto out; |
581 | goto out; |
| 587 | }; |
582 | }; |
| 588 | 583 | ||
| 589 | counter += retval; |
584 | counter += retval; |
| 590 | j = i + 1; |
585 | j = i + 1; |
| 591 | goto next_char; |
586 | goto next_char; |
| 592 | case 'c': |
587 | case 'c': |
| 593 | c = va_arg(ap, unsigned int); |
588 | 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) { |
| 595 | counter = -counter; |
590 | counter = -counter; |
| 596 | goto out; |
591 | goto out; |
| 597 | }; |
592 | }; |
| 598 | 593 | ||
| 599 | counter += retval; |
594 | counter += retval; |
| Line 690... | Line 685... | ||
| 690 | number++; |
685 | number++; |
| 691 | } |
686 | } |
| 692 | } |
687 | } |
| 693 | } |
688 | } |
| 694 | 689 | ||
| 695 | if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) { |
690 | if ((retval = print_number(number, width, precision, base, flags, ps)) < 0) { |
| 696 | counter = -counter; |
691 | counter = -counter; |
| 697 | goto out; |
692 | goto out; |
| 698 | }; |
693 | }; |
| 699 | 694 | ||
| 700 | counter += retval; |
695 | counter += retval; |
| Line 704... | Line 699... | ||
| 704 | 699 | ||
| 705 | ++i; |
700 | ++i; |
| 706 | } |
701 | } |
| 707 | 702 | ||
| 708 | if (i > j) { |
703 | 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 */ |
| 710 | counter = -counter; |
705 | counter = -counter; |
| 711 | goto out; |
706 | goto out; |
| 712 | 707 | ||
| 713 | } |
708 | } |
| 714 | counter += retval; |
709 | counter += retval; |