OSDN Git Service

85a0967c136da02de6477d50938c455c3930ecc0
[uclinux-h8/uClibc.git] / libc / stdio / old_vfprintf.c
1 /*
2  * This file based on printf.c from 'Dlibs' on the atari ST  (RdeBath)
3  *
4  * 
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."
9  */
10
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
14  * will be either
15  *  sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short)
16  *
17  * -RDB
18  */
19
20 /*
21  *                    Manuel Novoa III   Dec 2000
22  *
23  * The previous vfnprintf routine was almost completely rewritten with the
24  * goal of fixing some shortcomings and reducing object size.
25  *
26  * The summary of changes:
27  *
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
35  *   improvements.
36  *
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.
41  *
42  * Added optional support for the GNU extension %m which prints the string
43  *   corresponding the errno.
44  *
45  * Added optional support for long long ints and unsigned long long ints
46  *   using the conversion qualifiers "ll", "L", or "q" (like glibc).
47  *
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.
51  *
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.
54  *
55  * Other Modifications:
56  *   Modified sprintf, snprintf, vsprintf, vsnprintf to share on fake-file.
57  */
58
59 /*
60  *                    Manuel Novoa III   Jan 2001
61  *
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!
75  * 
76  *
77  * May 2001     Fixes from Johan Adolfsson (johan.adolfsson@axis.com)
78  *    1) printf("%c",0) returned 0 instead of 1.
79  *    2) unrolled loop in asprintf to reduce size and remove compile warning.
80  *
81  *
82  * June 2001
83  *    1) fix %p so that "0x" is prepended to outputed hex val
84  *    2) fix %p so that "(nil)" is output for (void *)0 to match glibc
85  *
86  * Sep 5, 2003
87  *    Convert to new floating point conversion routine.
88  *    Fix qualifier handling on integer and %n conversions.
89  *    Add support for vsnprintf when in non-buffered/no-wchar configuration.
90  *
91  */
92
93 /*****************************************************************************/
94 /*                            OPTIONS                                        */
95 /*****************************************************************************/
96 /* The optional support for long longs and doubles comes in two forms.
97  *
98  *   1) Normal (or partial for doubles) output support.  Set to 1 to turn on.
99  *      Adds about 130 bytes for doubles, about 220 bytes for long longs,
100  *      and about 275 for both to the base code size of 1163 on i386.
101  */
102
103 /* These are now set in uClibc_config.h based on Config. */
104 /*
105 #define __UCLIBC_HAS_FLOATS__            1
106 */
107
108 /*   2) An error message is inserted into the stream, an arg of the
109  *      appropriate size is removed from the arglist, and processing
110  *      continues.  This is adds less code and may be useful in some
111  *      cases.  Set to 1 to turn on.  Adds about 50 bytes for doubles,
112  *      about 140 bytes for long longs, and about 175 bytes for both
113  *      to the base code size of 1163 on i386.
114  */
115
116 #define WANT_FLOAT_ERROR      0
117
118 /*
119  * Set to support GNU extension of %m to print string corresponding to errno.
120  *
121  * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
122  * strerror and the corresponding string table which together are about 3.8k.
123  */
124
125 /* Now controlled by uClibc_stdio.h and set below. */
126 /* #define WANT_GNU_ERRNO         0 */
127
128 /**************************************************************************/
129
130 #define _ISOC99_SOURCE                  /* for ULLONG primarily... */
131 #include "_stdio.h"
132 /* #include <stdio.h> */
133 #include <stdarg.h>
134 #include <limits.h>
135 #include <stdint.h>
136 #include <string.h>
137 #include <errno.h>
138 #include <ctype.h>
139 #include <bits/uClibc_uintmaxtostr.h>
140 #include <printf.h>
141
142 #ifdef __UCLIBC_HAS_THREADS__
143 #include <pthread.h>
144 #endif /* __UCLIBC_HAS_THREADS__ */
145
146 /* Experimentally off - libc_hidden_proto(strlen) */
147 /* Experimentally off - libc_hidden_proto(strnlen) */
148 /* Experimentally off - libc_hidden_proto(memcpy) */
149 libc_hidden_proto(putc_unlocked)
150 libc_hidden_proto(__fputc_unlocked)
151 libc_hidden_proto(__glibc_strerror_r)
152
153 /*  #undef __UCLIBC_HAS_FLOATS__ */
154 /*  #undef WANT_FLOAT_ERROR */
155 /*  #define WANT_FLOAT_ERROR      1 */
156
157 /*  #define __isdigit(c) (((unsigned int)(c - '0')) < 10) */
158
159 #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
160 #define WANT_GNU_ERRNO         1
161 #else
162 #define WANT_GNU_ERRNO         0
163 #endif
164
165 #undef PUTC
166 #undef OUTNSTR
167 #undef _outnstr
168
169 #ifdef __STDIO_BUFFERS
170
171 #define PUTC(C,F)      putc_unlocked((C),(F))
172 #define OUTNSTR        _outnstr
173 #define _outnstr(stream, string, len)   __stdio_fwrite(string, len, stream)
174
175 #else  /* __STDIO_BUFFERS */
176
177 typedef struct {
178         FILE f;
179         unsigned char *bufend;          /* pointer to 1 past end of buffer */
180         unsigned char *bufpos;
181 } __FILE_vsnprintf;
182
183 #ifdef __UCLIBC_HAS_FLOATS__
184 static void _outnstr(FILE *stream, const unsigned char *s, size_t n)
185 {
186         __FILE_vsnprintf *f = (__FILE_vsnprintf *) stream;
187
188         if (!__STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(&f->f)) {
189                 __stdio_fwrite(s, n, &f->f);
190         } else if (f->bufend > f->bufpos) {
191                 size_t r = f->bufend - f->bufpos;
192                 if (r > n) {
193                         r = n;
194                 }
195                 memcpy(f->bufpos, s, r);
196                 f->bufpos += r;
197         }
198 }
199 #endif
200
201 static void putc_unlocked_sprintf(int c, __FILE_vsnprintf *f)
202 {
203         if (!__STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(&f->f)) {
204                 putc_unlocked(c, &f->f);
205         } else if (f->bufpos < f->bufend) {
206                 *f->bufpos++ = c;
207         }
208 }
209
210
211 #define PUTC(C,F)      putc_unlocked_sprintf((C),(__FILE_vsnprintf *)(F))
212 #define OUTNSTR        _outnstr
213
214 #endif /* __STDIO_BUFFERS */
215
216 #ifdef __UCLIBC_HAS_FLOATS__
217 #include <float.h>
218 #include <bits/uClibc_fpmax.h>
219
220 typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
221                                                           intptr_t buf);
222
223 extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
224                                                   __fp_outfunc_t fp_outfunc) attribute_hidden;
225
226 static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
227 {
228         /* TODO -- Use a buffer to cut down on function calls... */
229         char pad[1];
230
231         *pad = padchar;
232         while (numpad) {
233                 OUTNSTR(stream, pad, 1);
234                 --numpad;
235         }
236 }
237
238 static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
239 {
240         if (type & 0x80) {                      /* Some type of padding needed. */
241                 int buflen = strlen((const char *) buf);
242                 if ((len -= buflen) > 0) {
243                         _charpad(fp, (type & 0x7f), len);
244                 }
245                 len = buflen;
246         }
247         if (len) {
248                 OUTNSTR(fp, (const char *) buf, len);
249         }
250 }
251
252 #endif
253
254
255 enum {
256         FLAG_PLUS = 0,
257         FLAG_MINUS_LJUSTIFY,
258         FLAG_HASH,
259         FLAG_0_PAD,
260         FLAG_SPACE,
261 };
262
263 /* layout                   01234  */
264 static const char spec[] = "+-#0 ";
265
266 /**********************************************************************/
267
268 extern void _store_inttype(void *dest, int desttype, uintmax_t val) attribute_hidden;
269 extern uintmax_t _load_inttype(int desttype, const void *src, int uflag) attribute_hidden;
270
271 /*
272  * In order to ease translation to what arginfo and _print_info._flags expect,
273  * we map:  0:int  1:char  2:longlong 4:long  8:short
274  * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
275  */
276
277 #ifdef PDS
278 #error PDS already defined!
279 #endif
280 #ifdef SS
281 #error SS already defined!
282 #endif
283 #ifdef IMS
284 #error IMS already defined!
285 #endif
286
287 #if PTRDIFF_MAX == INT_MAX
288 #define PDS             0
289 #elif PTRDIFF_MAX == LONG_MAX
290 #define PDS             4
291 #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
292 #define PDS             8
293 #else
294 #error fix QUAL_CHARS ptrdiff_t entry 't'!
295 #endif
296
297 #if SIZE_MAX == UINT_MAX
298 #define SS              0
299 #elif SIZE_MAX == ULONG_MAX
300 #define SS              4
301 #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
302 #define SS              8
303 #else
304 #error fix QUAL_CHARS size_t entries 'z', 'Z'!
305 #endif
306
307 #if INTMAX_MAX == INT_MAX
308 #define IMS             0
309 #elif INTMAX_MAX == LONG_MAX
310 #define IMS             4
311 #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
312 #define IMS             8
313 #else
314 #error fix QUAL_CHARS intmax_t entry 'j'!
315 #endif
316
317 #define QUAL_CHARS              { \
318         /* j:(u)intmax_t z:(s)size_t  t:ptrdiff_t  \0:int */ \
319         /* q:long_long  Z:(s)size_t */ \
320         'h',   'l',  'L',  'j',  'z',  't',  'q', 'Z',  0, \
321          2,     4,    8,  IMS,   SS,  PDS,    8,  SS,   0, /* TODO -- fix!!! */\
322      1,     8 \
323 }
324
325 static const char qual_chars[] = QUAL_CHARS;
326
327 /* static const char qual[] = "hlLq"; */
328 /**********************************************************************/
329
330 #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
331 static const char dbl_err[] = "<DOUBLE>";
332 #endif
333
334 #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
335 /* layout                     012345678901234567   */
336 static const char u_spec[] = "%nbopxXudicsfgGeEaA";
337 #else
338 /* layout                     0123456789012   */
339 static const char u_spec[] = "%nbopxXudics";
340 #endif
341
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";
345
346 libc_hidden_proto(vfprintf)
347 int vfprintf(FILE * __restrict op, register const char * __restrict fmt,
348                          va_list ap)
349 {
350         union {
351 #ifdef LLONG_MAX
352                 long long ll;
353 #endif
354 #if LONG_MAX != INT_MAX
355                 long l;
356 #endif
357                 int i;
358         } intarg;
359         int i, cnt, dataargtype, len;
360         const void *argptr = argptr; /* ok to be initialized. */
361         register char *p;
362         const char *fmt0;
363         int preci, width;
364 #define upcase i
365         int radix, dpoint /*, upcase*/;
366         char tmp[65];           /* TODO - determine needed size from headers */
367         char flag[sizeof(spec)];
368         __STDIO_AUTO_THREADLOCK_VAR;
369
370         __STDIO_AUTO_THREADLOCK(op);
371
372         __STDIO_STREAM_VALIDATE(op);
373
374         cnt = 0;
375
376         if (__STDIO_STREAM_IS_NARROW_WRITING(op)
377                 || !__STDIO_STREAM_TRANS_TO_WRITE(op, __FLAG_NARROW)
378                 ) {
379
380         while (*fmt) {
381                 if (*fmt == '%') {
382                         fmt0 = fmt;                     /* save our position in case of bad format */
383                         ++fmt;
384                         width = -1;                     /* min field width */
385                         preci = -5;                     /* max string width or mininum digits */
386                         radix = 10;                     /* number base */
387                         dpoint = 0;                     /* found decimal point */
388
389                         /* init flags */
390                         for (p =(char *) spec ; *p ; p++) {
391                                 flag[p-spec] = '\0';
392                         }
393                         flag[FLAG_0_PAD] = ' ';
394
395                         /* process optional flags */
396                         for (p = (char *)spec ; *p ; ) {
397                                 if (*fmt == *p) {
398                                         flag[p-spec] = *fmt++;
399                                         p = (char *)spec; /* restart scan */
400                                 } else {
401                                         p++;
402                                 }
403                         }
404                         
405                         if (!flag[FLAG_PLUS]) {
406                                 flag[FLAG_PLUS] = flag[FLAG_SPACE];
407                         }
408
409                         /* process optional width and precision */
410                         do {
411                                 if (*fmt == '.') {
412                                         ++fmt;
413                                         dpoint = 1;
414                                 }
415                                 if (*fmt == '*') {      /* parameter width value */
416                                         ++fmt;
417                                         i = va_arg(ap, int);
418                                 } else {
419                                         for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
420                                                 i = (i * 10) + (*fmt - '0');
421                                         }
422                                 }
423
424                                 if (dpoint) {
425                                         preci = i;
426                                         if (i<0) {
427                                                 preci = -5;
428                                         }
429                                 } else {
430                                         width = i;
431                                         if (i<0) {
432                                                 width = -i;
433                                                 flag[FLAG_MINUS_LJUSTIFY] = 1;
434                                         }
435                                 }
436                         } while ((*fmt == '.') && !dpoint );
437
438                         /* process optional qualifier */
439                         p = (char *) qual_chars;
440                         do {
441                                 if (*fmt == *p) {
442                                         ++fmt;
443                                         break;
444                                 }
445                         } while (*++p);
446                         if ((p - qual_chars < 2) && (*fmt == *p)) {
447                                 p += ((sizeof(qual_chars)-2) / 2);
448                                 ++fmt;
449                         }
450                         dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
451
452 #if WANT_GNU_ERRNO
453                         if (*fmt == 'm') {
454                                 flag[FLAG_PLUS] = '\0';
455                                 flag[FLAG_0_PAD] = ' ';
456                                 p = __glibc_strerror_r(errno, tmp, sizeof(tmp));
457                                 goto print;
458                         }
459 #endif
460
461                         /* process format specifier */
462                         for (p = (char *) u_spec ; *p ; p++) {
463                                 if (*fmt != *p) continue;
464                                 if (p-u_spec < 1) {     /* print a % */
465                                         goto charout;
466                                 }
467                                 if (p-u_spec < 2) {     /* store output count in int ptr */
468                                         _store_inttype(va_arg(ap, void *),
469                                                                    dataargtype,
470                                                                    (intmax_t) (cnt));
471                                         goto nextfmt;
472                                 }
473
474                                 if (p-u_spec < 10) {
475                                         if (*p == 'p') {
476 #if INTPTR_MAX == INT_MAX
477                                                 dataargtype = 0;
478 #else
479 #error Fix dataargtype for pointers!
480 #endif
481                                         }
482
483                                         switch(dataargtype) {
484                                                 case (PA_INT|PA_FLAG_LONG_LONG):
485 #ifdef LLONG_MAX
486                                                         intarg.ll = va_arg(ap, long long);
487                                                         argptr = &intarg.ll;
488                                                         break;
489 #endif
490                                                 case (PA_INT|PA_FLAG_LONG):
491 #if LONG_MAX != INT_MAX
492                                                         intarg.l = va_arg(ap, long);
493                                                         argptr = &intarg.l;
494                                                         break;
495 #endif
496                                                 default:
497                                                         intarg.i = va_arg(ap, int);
498                                                         argptr = &intarg.i;
499                                                         break;
500                                         }
501                                 }
502
503                                 if (p-u_spec < 8) { /* unsigned conversion */
504                                         radix = u_radix[p-u_spec-2];
505                                         upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER);
506                                         if (*p == 'p') {
507                                                 upcase = __UIM_LOWER;
508                                                 flag[FLAG_HASH] = 'p';
509                                         }
510                                         p = _uintmaxtostr(tmp + sizeof(tmp) - 1,
511                                                                           (uintmax_t)
512                                                                           _load_inttype(dataargtype, argptr, radix),
513                                                                           radix, upcase);
514
515                                         flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
516                                         if (*p != '0') { /* non-zero */
517                                                 if (flag[FLAG_HASH]) {
518                                                         if (radix == 8) {
519                                                                 *--p = '0';     /* add leadding zero */
520                                                         } else if (radix != 10) { /* either 2 or 16 */
521                                                                 flag[FLAG_PLUS] = '0';
522                                                                 *--p = 'b';
523                                                                 if (radix == 16) {
524                                                                         *p = 'x';
525                                                                         if (*fmt == 'X') {
526                                                                                 *p = 'X';
527                                                                         }
528                                                                 }
529                                                         }
530                                                 }
531                                         } else if (flag[FLAG_HASH] == 'p') { /* null pointer */
532                                                 p = "(nil)";
533                                         }
534                                 } else if (p-u_spec < 10) { /* signed conversion */
535                                         p = _uintmaxtostr(tmp + sizeof(tmp) - 1,
536                                                                           (uintmax_t)
537                                                                           _load_inttype(dataargtype, argptr, -radix),
538                                                                           -radix, upcase);
539
540                                 } else if (p-u_spec < 12) {     /* character or string */
541                                         flag[FLAG_PLUS] = '\0';
542                                         flag[FLAG_0_PAD] = ' ';
543                                         if (*p == 'c') {        /* character */
544                                                 p = tmp;
545                                                 *p = va_arg(ap, int);
546                                                 /* This takes care of the "%c",0 case */
547                                                 len = 1;
548                                                 goto print_len_set;
549                                         } else {        /* string */
550                                                 p = va_arg(ap, char *);
551                                                 if (!p) {
552                                                         p = "(null)";
553                                                         preci = 6;
554                                                 } else {
555                                                         if (preci < 0) {
556                                                                 preci = INT_MAX;
557                                                         }
558                                                 }
559                                                 len = strnlen(p, preci);
560                                                 goto print_len_set;
561                                         }
562 #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
563                                 } else if (p-u_spec < 27) {             /* floating point */
564 #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
565 #if defined(__UCLIBC_HAS_FLOATS__)
566                                         struct printf_info info;
567                                         if (preci < 0) {
568                                                 preci = 6;
569                                         }
570                                         info.width = width;
571                                         info.prec = preci;
572                                         info.spec = *fmt;
573                                         info.pad = flag[FLAG_0_PAD];
574                                         info._flags = 0;
575                                         if (flag[FLAG_PLUS] == '+') {
576                                                 PRINT_INFO_SET_FLAG(&info,showsign);
577                                         } else if (flag[FLAG_PLUS] == ' ') {
578                                                 PRINT_INFO_SET_FLAG(&info,space);
579                                         }
580                                         if (flag[FLAG_HASH]) {
581                                                 PRINT_INFO_SET_FLAG(&info,alt);
582                                         }
583                                         if (flag[FLAG_MINUS_LJUSTIFY]) {
584                                                 PRINT_INFO_SET_FLAG(&info,left);
585                                         }
586 #if 1
587                                         cnt += _fpmaxtostr(op, 
588                                                                            (__fpmax_t)
589                                                                            ((dataargtype == (8 << 8))
590                                                                                 ? va_arg(ap, long double)
591                                                                                 : (long double) va_arg(ap, double)),
592                                                                            &info, _fp_out_narrow);
593 #else
594                                         cnt += _fpmaxtostr(op, 
595                                                                            (__fpmax_t)
596                                                                            ((lval > 1)
597                                                                                 ? va_arg(ap, long double)
598                                                                                 : (long double) va_arg(ap, double)),
599                                                                            &info, _fp_out_narrow);
600 #endif
601                                         goto nextfmt;
602 #elif WANT_FLOAT_ERROR
603                                         (void) ((lval > 1) ? va_arg(ap, long double)
604                                                         : va_arg(ap, double)); /* carry on */
605                                         p = (char *) dbl_err;
606 #endif /* defined(__UCLIBC_HAS_FLOATS__) */
607                                 }
608
609 #if WANT_GNU_ERRNO
610                         print:
611 #endif
612                                 {                               /* this used to be printfield */
613                                         /* cheaper than strlen call */
614 /*                                      for ( len = 0 ; p[len] ; len++ ) { } */
615                                         len = strnlen(p, SIZE_MAX);
616                                 print_len_set:
617                                         if ((*p == '-')
618 #if WANT_GNU_ERRNO
619                                                 && (*fmt != 'm')
620 #endif
621                                                 && (*fmt != 's')) {
622                                                 flag[FLAG_PLUS] = *p++;
623                                                 --len;
624                                         }
625                                     if (flag[FLAG_PLUS]) {
626                                                 ++len;
627                                                 ++preci;
628                                                 if (flag[FLAG_PLUS] == '0') { /* base 16 */
629                                                         ++preci; /* account for x or X */
630                                                 }
631                                         }
632
633                                         if (preci >= 0) {
634                                                 if ((*fmt == 's')
635 #if WANT_GNU_ERRNO
636                                                 || (*fmt == 'm')
637 #endif
638                                                 ) {
639                                                         if (len > preci) {
640                                                                 len = preci;
641                                                         } else {
642                                                                 preci = len;
643                                                         }
644                                                 }
645                                                 preci -= len;
646                                                 if (preci < 0) {
647                                                         preci = 0;
648                                                 }
649                                                 width -= preci;
650                                         }
651
652                                         width -= len;
653                                         if (width < 0) {
654                                                 width = 0;
655                                         }
656
657                                         if (preci < 0) {
658                                                 preci = 0;
659                                                 if (!flag[FLAG_MINUS_LJUSTIFY]
660                                                         /* && flag[FLAG_PLUS] */
661                                                         && (flag[FLAG_0_PAD] == '0')) { 
662                                                         preci = width;
663                                                         width = 0;
664                                                 }
665                                         }
666
667                                         while (width + len + preci) {
668                                                 unsigned char ch;
669                                                 /* right padding || left padding */
670                                                 if ((!len && !preci)
671                                                         || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
672                                                         ch = ' ';
673                                                         --width;
674                                                 } else if (flag[FLAG_PLUS]) {
675                                                         ch = flag[FLAG_PLUS]; /* sign */
676                                                         if (flag[FLAG_PLUS]=='0') {     /* base 16 case */
677                                                                 flag[FLAG_PLUS] = *p++; /* get the x|X */
678                                                         } else {
679                                                                 flag[FLAG_PLUS] = '\0';
680                                                         }
681                                                         --len;
682                                                 } else if (preci) {
683                                                         ch = '0';
684                                                         --preci;
685                                                 } else {
686                                                         ch = *p++; /* main field */
687                                                         --len;
688                                                 }
689                                                 ++cnt;
690                                                 PUTC(ch, op);
691                                         }
692                                 }
693                                 goto nextfmt;
694                         }
695
696                         fmt = fmt0;     /* this was an illegal format */
697                 }
698
699         charout:
700                 ++cnt;
701                 PUTC(*fmt, op); /* normal char out */
702
703         nextfmt:
704                 ++fmt;
705         }
706
707         }
708
709         i = (__FERROR_UNLOCKED(op)) ? -1 : cnt;
710
711         __STDIO_STREAM_VALIDATE(op);
712
713         __STDIO_AUTO_THREADUNLOCK(op);
714
715         return i;
716 }
717 libc_hidden_def(vfprintf)