2 * This file based on printf.c from 'Dlibs' on the atari ST (RdeBath)
5 * Dale Schumacher 399 Beacon Ave.
6 * (alias: Dalnefre') St. Paul, MN 55104
7 * dal@syntel.UUCP United States of America
8 * "It's not reality that's important, but how you perceive things."
11 /* Altered to use stdarg, made the core function vfnprintf.
12 * Hooked into the stdio package using 'inside information'
13 * Altered sizeof() assumptions, now assumes all integers except chars
15 * sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short)
21 * Manuel Novoa III Dec 2000
23 * The previous vfnprintf routine was almost completely rewritten with the
24 * goal of fixing some shortcomings and reducing object size.
26 * The summary of changes:
28 * Converted print conversion specification parsing from one big switch
29 * to a method using string tables. This new method verifies that the
30 * conversion flags, field width, precision, qualifier, and specifier
31 * appear in the correct order. Many questionable specifications were
32 * accepted by the previous code. This new method also resulted in a
33 * substantial reduction in object size of about 330 bytes (20%) from
34 * the old version (1627 bytes) on i386, even with the following
37 * Implemented %n specifier as required by the standards.
38 * Implemented proper handling of precision for int types.
39 * Implemented # for hex and pointer, fixed error for octal rep of 0.
40 * Implemented return of -1 on stream error.
42 * Added optional support for the GNU extension %m which prints the string
43 * corresponding the errno.
45 * Added optional support for long long ints and unsigned long long ints
46 * using the conversion qualifiers "ll", "L", or "q" (like glibc).
48 * Added optional support for doubles in a very limited form. None of
49 * the formating options are obeyed. The string returned by __dtostr
50 * is printed directly.
52 * Converted to use my (un)signed long (long) to string routines, which are
53 * smaller than the previous functions and don't require static buffers.
55 * Other Modifications:
56 * Modified sprintf, snprintf, vsprintf, vsnprintf to share on fake-file.
60 * Manuel Novoa III Jan 2000
62 * Removed fake file from *s*printf functions because of possible problems
63 * if called recursively. Instead, have sprintf, snprintf, and vsprintf
64 * call vsnprintf which allocates a fake file on the stack.
65 * Removed WANT_FPUTC option. Always use standard putc macro to avoid
66 * problems with the fake file used by the *s*printf functions.
67 * Fixed bug parsing flags -- did not restart scan.
68 * Added function asprintf.
69 * Fixed 0-pad prefixing bug.
70 * Converted sizeof(int) == sizeof(long) tests to compile time vs run time.
71 * This saves 112 bytes of code on i386.
72 * Fixed precision bug -- when negative set to default.
73 * Added function fnprintf to support __dtostr.
74 * Added floating point support for doubles. Yeah!
76 * TODO: long double support
79 /*****************************************************************************/
81 /*****************************************************************************/
82 /* The optional support for long longs and doubles comes in two forms.
84 * 1) Normal (or partial for doubles) output support. Set to 1 to turn on.
85 * Adds about 130 bytes for doubles, about 220 bytes for long longs,
86 * and about 275 for both to the base code size of 1163 on i386.
89 /* These are now set in uClibc_config.h based on Config. */
91 #define __UCLIBC_HAS_LONG_LONG__ 1
92 #define __UCLIBC_HAS_FLOATS__ 1
95 /* 2) An error message is inserted into the stream, an arg of the
96 * appropriate size is removed from the arglist, and processing
97 * continues. This is adds less code and may be useful in some
98 * cases. Set to 1 to turn on. Adds about 50 bytes for doubles,
99 * about 140 bytes for long longs, and about 175 bytes for both
100 * to the base code size of 1163 on i386.
103 #define WANT_LONG_LONG_ERROR 0
104 #define WANT_FLOAT_ERROR 0
107 * Set to support GNU extension of %m to print string corresponding to errno.
109 * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
110 * strerror and the corresponding string table which together are about 3.8k.
113 #define WANT_GNU_ERRNO 0
115 /**************************************************************************/
117 #include <sys/types.h>
130 #define va_strt va_start
133 #define va_strt(p,i) va_start(p)
138 extern int vfnprintf(FILE * op, size_t max_size,
139 register __const char *fmt, register va_list ap);
142 int printf(const char *fmt, ...)
148 rv = vfnprintf(stdout, -1, fmt, ptr);
155 int asprintf(char **app, const char *fmt, ...)
159 char *p; /* unitialized warning is ok here */
161 * First iteration - find out size of buffer required and allocate it.
162 * Second iteration - actually produce output.
165 for (i=0 ; i<2 ; i++) {
167 rv = vsnprintf(p, rv, fmt, ptr);
170 if (i==0) { /* first time through so */
171 p = malloc(++rv); /* allocate the buffer */
183 int sprintf(char *sp, const char *fmt, ...)
189 rv = vsnprintf(sp, -1, fmt, ptr);
197 int snprintf(char *sp, size_t size, const char *fmt, ...)
203 rv = vsnprintf(sp, size, fmt, ptr);
210 int fprintf(FILE * fp, const char *fmt, ...)
216 rv = vfnprintf(fp, -1, fmt, ptr);
223 int fnprintf(FILE * fp, size_t size, const char *fmt, ...)
229 rv = vfnprintf(fp, size, fmt, ptr);
236 int vprintf(const char *fmt, va_list ap)
238 return vfprintf(stdout, fmt, ap);
244 int vfprintf(FILE * op, register __const char *fmt, register va_list ap)
246 return vfnprintf(op, -1, fmt, ap);
252 int vsprintf(char *sp, __const char *fmt, va_list ap)
254 return vsnprintf(sp, -1, fmt, ap);
259 int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap)
265 * As we're only using the putc macro in vfnprintf, we don't need to
266 * initialize all FILE f's fields.
268 f.bufwrite = (char *) ((unsigned) -1);
272 rv = vfnprintf(&f, size, fmt, ap);
273 if (size) { /* If this is going to a buffer, */
274 *(f.bufpos) = 0; /* don't forget to nul-terminate. */
282 * Note: If fd has an associated buffered FILE, bad things happen.
284 extern int vdprintf(int fd, const char *fmt, va_list ap)
287 FILE f = {buf, 0, buf+sizeof(buf), buf, buf+sizeof(buf), 0, fd, _IOFBF};
290 rv = vfnprintf(&f, -1, fmt, ap);
302 extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
303 extern char *__ltostr(char *buf, long val, int base, int uppercase);
304 extern char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase);
305 extern char *__lltostr(char *buf, long long val, int base, int uppercase);
306 extern int __dtostr(FILE * fp, size_t size, long double x,
307 char flag[], int width, int preci, char mode);
318 static const char spec[] = "+-#0 ";
320 #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
321 static const char qual[] = "hlLq";
323 static const char qual[] = "hl";
326 #if !defined(__UCLIBC_HAS_LONG_LONG__) && WANT_LONG_LONG_ERROR
327 static const char ll_err[] = "<LONG-LONG>";
330 #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
331 static const char dbl_err[] = "<DOUBLE>";
334 #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
335 /* layout 012345678901234567 */
336 static const char u_spec[] = "%nbopxXudicsfgGeEaA";
338 /* layout 0123456789012 */
339 static const char u_spec[] = "%nbopxXudics";
342 /* WARNING: u_spec and u_radix need to stay in agreement!!! */
343 /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
344 static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
346 int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap)
354 int radix, dpoint /*, upcase*/;
355 #if defined(__UCLIBC_HAS_LONG_LONG__)
360 char flag[sizeof(spec)];
364 /* This speeds things up a bit for line unbuffered */
365 buffer_mode = (op->mode & __MODE_BUF);
366 op->mode &= (~__MODE_BUF);
370 fmt0 = fmt; /* save our position in case of bad format */
372 width = -1; /* min field width */
373 preci = -5; /* max string width or mininum digits */
374 radix = 10; /* number base */
375 dpoint = 0; /* found decimal point */
376 #if INT_MAX != LONG_MAX
377 lval = 0; /* sizeof(int) != sizeof(long) */
379 lval = 1; /* sizeof(int) == sizeof(long) */
382 tmp[1] = 0; /* set things up for %c -- better done here */
385 for (p =(char *) spec ; *p ; p++) {
388 flag[FLAG_0_PAD] = ' ';
390 /* process optional flags */
391 for (p = (char *)spec ; *p ; ) {
393 flag[p-spec] = *fmt++;
394 p = (char *)spec; /* restart scan */
400 if (!flag[FLAG_PLUS]) {
401 flag[FLAG_PLUS] = flag[FLAG_SPACE];
404 /* process optional width and precision */
410 if (*fmt == '*') { /* parameter width value */
414 for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
415 i = (i * 10) + (*fmt - '0');
428 flag[FLAG_MINUS_LJUSTIFY] = 1;
431 } while ((*fmt == '.') && !dpoint );
433 /* process optional qualifier */
434 for (p = (char *) qual ; *p ; p++) {
438 #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
439 if ((*p == 'l') && (*fmt == *p)) {
443 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
449 flag[FLAG_PLUS] = '\0';
450 flag[FLAG_0_PAD] = ' ';
456 /* process format specifier */
457 for (p = (char *) u_spec ; *p ; p++) {
458 if (*fmt != *p) continue;
459 if (p-u_spec < 1) { /* print a % */
462 if (p-u_spec < 2) { /* store output count in int ptr */
463 *(va_arg(ap, int *)) = cnt;
466 if (p-u_spec < 8) { /* unsigned conversion */
467 radix = u_radix[p-u_spec-2];
468 upcase = ((int)'x') - *p;
470 lval = (sizeof(char *) == sizeof(long));
473 #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
475 #if defined(__UCLIBC_HAS_LONG_LONG__)
476 p = __ulltostr(tmp + sizeof(tmp) - 1,
477 va_arg(ap, unsigned long long),
480 (void) va_arg(ap, unsigned long long); /* cary on */
482 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) */
484 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
485 #if UINT_MAX != ULONG_MAX
486 /* sizeof(unsigned int) != sizeof(unsigned long) */
487 p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
489 ? va_arg(ap, unsigned long)
490 : va_arg(ap, unsigned int)),
493 /* sizeof(unsigned int) == sizeof(unsigned long) */
494 p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
495 va_arg(ap, unsigned long),
498 #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
500 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
501 flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
502 if (flag[FLAG_HASH] && (*p != '0')) { /* non-zero */
504 *--p = '0'; /* add leadding zero */
505 } else if (radix != 10) { /* either 2 or 16 */
506 flag[FLAG_PLUS] = '0';
516 } else if (p-u_spec < 10) { /* signed conversion */
517 #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
519 #if defined(__UCLIBC_HAS_LONG_LONG__)
520 p = __lltostr(tmp + sizeof(tmp) - 1,
521 va_arg(ap, long long), 10, 0);
523 (void) va_arg(ap, long long); /* carry on */
525 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) */
527 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
528 #if INT_MAX != LONG_MAX
529 /* sizeof(int) != sizeof(long) */
530 p = __ltostr(tmp + sizeof(tmp) - 1, (long)
533 : va_arg(ap, int)), 10, 0);
535 /* sizeof(int) == sizeof(long) */
536 p = __ltostr(tmp + sizeof(tmp) - 1, (long)
537 va_arg(ap, long), 10, 0);
539 #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
541 #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
542 } else if (p-u_spec < 12) { /* character or string */
543 flag[FLAG_PLUS] = '\0';
544 flag[FLAG_0_PAD] = ' ';
545 if (*p == 'c') { /* character */
547 *p = va_arg(ap, int);
548 } else { /* string */
549 p = va_arg(ap, char *);
554 #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
555 } else if (p-u_spec < 27) { /* floating point */
556 #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
557 #if defined(__UCLIBC_HAS_FLOATS__)
562 (max_size > cnt ? max_size - cnt : 0),
563 (long double) ((lval > 1)
564 ? va_arg(ap, long double)
565 : va_arg(ap, double)),
566 flag, width, preci, *fmt);
568 #elif WANT_FLOAT_ERROR
569 (void) ((lval > 1) ? va_arg(ap, long double)
570 : va_arg(ap, double)); /* carry on */
571 p = (char *) dbl_err;
572 #endif /* defined(__UCLIBC_HAS_FLOATS__) */
578 { /* this used to be printfield */
581 /* cheaper than strlen call */
582 for ( len = 0 ; p[len] ; len++ ) { }
589 flag[FLAG_PLUS] = *p++;
592 if (flag[FLAG_PLUS]) {
595 if (flag[FLAG_PLUS] == '0') { /* base 16 */
596 ++preci; /* account for x or X */
626 if (!flag[FLAG_MINUS_LJUSTIFY]
627 /* && flag[FLAG_PLUS] */
628 && (flag[FLAG_0_PAD] == '0')) {
634 while (width + len + preci) {
636 /* right padding || left padding */
638 || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
641 } else if (flag[FLAG_PLUS]) {
642 ch = flag[FLAG_PLUS]; /* sign */
643 if (flag[FLAG_PLUS]=='0') { /* base 16 case */
644 flag[FLAG_PLUS] = *p++; /* get the x|X */
646 flag[FLAG_PLUS] = '\0';
653 ch = *p++; /* main field */
657 if (++cnt < max_size) {
660 if ((ch == '\n') && (buffer_mode == _IOLBF)) {
668 fmt = fmt0; /* this was an illegal format */
672 if (++cnt < max_size) {
673 putc(*fmt, op); /* normal char out */
675 if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
683 op->mode |= buffer_mode;
684 if (buffer_mode == _IOLBF) {
685 op->bufwrite = op->bufpos;