OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdio/] [old_vfprintf.c] - Blame information for rev 1766

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
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
#define _GNU_SOURCE                             /* for strnlen */
132
#define _STDIO_UTILITY
133
#include <stdio.h>
134
#include <stdarg.h>
135
#include <limits.h>
136
#include <stdint.h>
137
#include <string.h>
138
#include <errno.h>
139
#include <ctype.h>
140
 
141
#define __PRINTF_INFO_NO_BITFIELD
142
#include <printf.h>
143
 
144
#ifdef __STDIO_THREADSAFE
145
#include <pthread.h>
146
#endif /* __STDIO_THREADSAFE */
147
 
148
/*  #undef __UCLIBC_HAS_FLOATS__ */
149
/*  #undef WANT_FLOAT_ERROR */
150
/*  #define WANT_FLOAT_ERROR      1 */
151
 
152
/*  #define __isdigit(c) (((unsigned int)(c - '0')) < 10) */
153
 
154
#ifdef __STDIO_PRINTF_M_SUPPORT
155
#define WANT_GNU_ERRNO         1
156
#else
157
#define WANT_GNU_ERRNO         0
158
#endif
159
 
160
#undef PUTC
161
#undef OUTNSTR
162
#undef _outnstr
163
 
164
#ifdef __STDIO_BUFFERS
165
 
166
#define PUTC(C,F)      putc_unlocked((C),(F))
167
#define OUTNSTR        _outnstr
168
#define _outnstr(stream, string, len)   _stdio_fwrite(string, len, stream)
169
 
170
#else  /* __STDIO_BUFFERS */
171
 
172
typedef struct {
173
        FILE f;
174
        unsigned char *bufend;          /* pointer to 1 past end of buffer */
175
        unsigned char *bufpos;
176
} __FILE_vsnprintf;
177
 
178
#ifdef __UCLIBC_HAS_FLOATS__
179
static void _outnstr(FILE *stream, const unsigned char *s, size_t n)
180
{
181
        __FILE_vsnprintf *f = (__FILE_vsnprintf *) stream;
182
 
183
        if (f->f.filedes != -2) {
184
                _stdio_fwrite(s, n, &f->f);
185
        } else {
186
                if (f->bufend > f->bufpos) {
187
                        size_t r = f->bufend - f->bufpos;
188
                        if (r > n) {
189
                                r = n;
190
                        }
191
                        memcpy(f->bufpos, s, r);
192
                        f->bufpos += r;
193
                }
194
        }
195
}
196
#endif
197
 
198
static void putc_unlocked_sprintf(int c, __FILE_vsnprintf *f)
199
{
200
        if (f->f.filedes != -2) {
201
                putc_unlocked(c, &f->f);
202
        } else if (f->bufpos < f->bufend) {
203
                *f->bufpos++ = c;
204
        }
205
}
206
 
207
 
208
#define PUTC(C,F)      putc_unlocked_sprintf((C),(__FILE_vsnprintf *)(F))
209
#define OUTNSTR        _outnstr
210
 
211
#endif /* __STDIO_BUFFERS */
212
 
213
#ifdef __UCLIBC_HAS_FLOATS__
214
#include <float.h>
215
#include <bits/uClibc_fpmax.h>
216
 
217
typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
218
                                                          intptr_t buf);
219
 
220
extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
221
                                                  __fp_outfunc_t fp_outfunc);
222
 
223
static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
224
{
225
        /* TODO -- Use a buffer to cut down on function calls... */
226
        char pad[1];
227
 
228
        *pad = padchar;
229
        while (numpad) {
230
                OUTNSTR(stream, pad, 1);
231
                --numpad;
232
        }
233
}
234
 
235
static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
236
{
237
        if (type & 0x80) {                      /* Some type of padding needed. */
238
                int buflen = strlen((const char *) buf);
239
                if ((len -= buflen) > 0) {
240
                        _charpad(fp, (type & 0x7f), len);
241
                }
242
                len = buflen;
243
        }
244
        OUTNSTR(fp, (const char *) buf, len);
245
}
246
 
247
#endif
248
 
249
 
250
enum {
251
        FLAG_PLUS = 0,
252
        FLAG_MINUS_LJUSTIFY,
253
        FLAG_HASH,
254
        FLAG_0_PAD,
255
        FLAG_SPACE,
256
};
257
 
258
/* layout                   01234  */
259
static const char spec[] = "+-#0 ";
260
 
261
/**********************************************************************/
262
 
263
extern void _store_inttype(void *dest, int desttype, uintmax_t val);
264
extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
265
 
266
/*
267
 * In order to ease translation to what arginfo and _print_info._flags expect,
268
 * we map:  0:int  1:char  2:longlong 4:long  8:short
269
 * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
270
 */
271
 
272
#ifdef PDS
273
#error PDS already defined!
274
#endif
275
#ifdef SS
276
#error SS already defined!
277
#endif
278
#ifdef IMS
279
#error IMS already defined!
280
#endif
281
 
282
#if PTRDIFF_MAX == INT_MAX
283
#define PDS             0
284
#elif PTRDIFF_MAX == LONG_MAX
285
#define PDS             4
286
#elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
287
#define PDS             8
288
#else
289
#error fix QUAL_CHARS ptrdiff_t entry 't'!
290
#endif
291
 
292
#if SIZE_MAX == UINT_MAX
293
#define SS              0
294
#elif SIZE_MAX == ULONG_MAX
295
#define SS              4
296
#elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
297
#define SS              8
298
#else
299
#error fix QUAL_CHARS size_t entries 'z', 'Z'!
300
#endif
301
 
302
#if INTMAX_MAX == INT_MAX
303
#define IMS             0
304
#elif INTMAX_MAX == LONG_MAX
305
#define IMS             4
306
#elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
307
#define IMS             8
308
#else
309
#error fix QUAL_CHARS intmax_t entry 'j'!
310
#endif
311
 
312
#define QUAL_CHARS              { \
313
        /* j:(u)intmax_t z:(s)size_t  t:ptrdiff_t  \0:int */ \
314
        /* q:long_long  Z:(s)size_t */ \
315
        'h',   'l',  'L',  'j',  'z',  't',  'q', 'Z',  0, \
316
         2,     4,    8,  IMS,   SS,  PDS,    8,  SS,   0, /* TODO -- fix!!! */\
317
     1,     8 \
318
}
319
 
320
static const char qual_chars[] = QUAL_CHARS;
321
 
322
/* static const char qual[] = "hlLq"; */
323
/**********************************************************************/
324
 
325
#if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
326
static const char dbl_err[] = "<DOUBLE>";
327
#endif
328
 
329
#if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
330
/* layout                     012345678901234567   */
331
static const char u_spec[] = "%nbopxXudicsfgGeEaA";
332
#else
333
/* layout                     0123456789012   */
334
static const char u_spec[] = "%nbopxXudics";
335
#endif
336
 
337
/* WARNING: u_spec and u_radix need to stay in agreement!!! */
338
/* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
339
static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
340
 
341
int vfprintf(FILE * __restrict op, register const char * __restrict fmt,
342
                         va_list ap)
343
{
344
        union {
345
#ifdef LLONG_MAX
346
                long long ll;
347
#endif
348
#if LONG_MAX != INT_MAX
349
                long l;
350
#endif
351
                int i;
352
        } intarg;
353
        int i, cnt, dataargtype, len;
354
        const void *argptr;                     /* This does not need to be initialized. */
355
        register char *p;
356
        const char *fmt0;
357
        int preci, width;
358
#define upcase i
359
        int radix, dpoint /*, upcase*/;
360
        char tmp[65];                           /* TODO - determing needed size from headers */
361
        char flag[sizeof(spec)];
362
 
363
        __STDIO_THREADLOCK(op);
364
 
365
        cnt = 0;
366
 
367
        while (*fmt) {
368
                if (*fmt == '%') {
369
                        fmt0 = fmt;                     /* save our position in case of bad format */
370
                        ++fmt;
371
                        width = -1;                     /* min field width */
372
                        preci = -5;                     /* max string width or mininum digits */
373
                        radix = 10;                     /* number base */
374
                        dpoint = 0;                      /* found decimal point */
375
 
376
                        /* init flags */
377
                        for (p =(char *) spec ; *p ; p++) {
378
                                flag[p-spec] = '\0';
379
                        }
380
                        flag[FLAG_0_PAD] = ' ';
381
 
382
                        /* process optional flags */
383
                        for (p = (char *)spec ; *p ; ) {
384
                                if (*fmt == *p) {
385
                                        flag[p-spec] = *fmt++;
386
                                        p = (char *)spec; /* restart scan */
387
                                } else {
388
                                        p++;
389
                                }
390
                        }
391
 
392
                        if (!flag[FLAG_PLUS]) {
393
                                flag[FLAG_PLUS] = flag[FLAG_SPACE];
394
                        }
395
 
396
                        /* process optional width and precision */
397
                        do {
398
                                if (*fmt == '.') {
399
                                        ++fmt;
400
                                        dpoint = 1;
401
                                }
402
                                if (*fmt == '*') {      /* parameter width value */
403
                                        ++fmt;
404
                                        i = va_arg(ap, int);
405
                                } else {
406
                                        for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
407
                                                i = (i * 10) + (*fmt - '0');
408
                                        }
409
                                }
410
 
411
                                if (dpoint) {
412
                                        preci = i;
413
                                        if (i<0) {
414
                                                preci = -5;
415
                                        }
416
                                } else {
417
                                        width = i;
418
                                        if (i<0) {
419
                                                width = -i;
420
                                                flag[FLAG_MINUS_LJUSTIFY] = 1;
421
                                        }
422
                                }
423
                        } while ((*fmt == '.') && !dpoint );
424
 
425
                        /* process optional qualifier */
426
                        p = (char *) qual_chars;
427
                        do {
428
                                if (*fmt == *p) {
429
                                        ++fmt;
430
                                        break;
431
                                }
432
                        } while (*++p);
433
                        if ((p - qual_chars < 2) && (*fmt == *p)) {
434
                                p += ((sizeof(qual_chars)-2) / 2);
435
                                ++fmt;
436
                        }
437
                        dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
438
 
439
#if WANT_GNU_ERRNO
440
                        if (*fmt == 'm') {
441
                                flag[FLAG_PLUS] = '\0';
442
                                flag[FLAG_0_PAD] = ' ';
443
                                p = _glibc_strerror_r(errno, tmp, sizeof(tmp));
444
                                goto print;
445
                        }
446
#endif
447
 
448
                        /* process format specifier */
449
                        for (p = (char *) u_spec ; *p ; p++) {
450
                                if (*fmt != *p) continue;
451
                                if (p-u_spec < 1) {     /* print a % */
452
                                        goto charout;
453
                                }
454
                                if (p-u_spec < 2) {     /* store output count in int ptr */
455
                                        _store_inttype(va_arg(ap, void *),
456
                                                                   dataargtype,
457
                                                                   (intmax_t) (cnt));
458
                                        goto nextfmt;
459
                                }
460
 
461
                                if (p-u_spec < 10) {
462
                                        if (*p == 'p') {
463
#if INTPTR_MAX == INT_MAX
464
                                                dataargtype = 0;
465
#else
466
#error Fix dataargtype for pointers!
467
#endif
468
                                        }
469
 
470
                                        switch(dataargtype) {
471
                                                case (PA_INT|PA_FLAG_LONG_LONG):
472
#ifdef LLONG_MAX
473
                                                        intarg.ll = va_arg(ap, long long);
474
                                                        argptr = &intarg.ll;
475
                                                        break;
476
#endif
477
                                                case (PA_INT|PA_FLAG_LONG):
478
#if LONG_MAX != INT_MAX
479
                                                        intarg.l = va_arg(ap, long);
480
                                                        argptr = &intarg.l;
481
                                                        break;
482
#endif
483
                                                default:
484
                                                        intarg.i = va_arg(ap, int);
485
                                                        argptr = &intarg.i;
486
                                                        break;
487
                                        }
488
                                }
489
 
490
                                if (p-u_spec < 8) { /* unsigned conversion */
491
                                        radix = u_radix[p-u_spec-2];
492
                                        upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER);
493
                                        if (*p == 'p') {
494
                                                upcase = __UIM_LOWER;
495
                                                flag[FLAG_HASH] = 'p';
496
                                        }
497
                                        p = _uintmaxtostr(tmp + sizeof(tmp) - 1,
498
                                                                          (uintmax_t)
499
                                                                          _load_inttype(dataargtype, argptr, radix),
500
                                                                          radix, upcase);
501
 
502
                                        flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
503
                                        if (*p != '0') { /* non-zero */
504
                                                if (flag[FLAG_HASH]) {
505
                                                        if (radix == 8) {
506
                                                                *--p = '0';     /* add leadding zero */
507
                                                        } else if (radix != 10) { /* either 2 or 16 */
508
                                                                flag[FLAG_PLUS] = '0';
509
                                                                *--p = 'b';
510
                                                                if (radix == 16) {
511
                                                                        *p = 'x';
512
                                                                        if (*fmt == 'X') {
513
                                                                                *p = 'X';
514
                                                                        }
515
                                                                }
516
                                                        }
517
                                                }
518
                                        } else if (flag[FLAG_HASH] == 'p') { /* null pointer */
519
                                                p = "(nil)";
520
                                        }
521
                                } else if (p-u_spec < 10) { /* signed conversion */
522
                                        p = _uintmaxtostr(tmp + sizeof(tmp) - 1,
523
                                                                          (uintmax_t)
524
                                                                          _load_inttype(dataargtype, argptr, -radix),
525
                                                                          -radix, upcase);
526
 
527
                                } else if (p-u_spec < 12) {     /* character or string */
528
                                        flag[FLAG_PLUS] = '\0';
529
                                        flag[FLAG_0_PAD] = ' ';
530
                                        if (*p == 'c') {        /* character */
531
                                                p = tmp;
532
                                                *p = va_arg(ap, int);
533
                                                /* This takes care of the "%c",0 case */
534
                                                len = 1;
535
                                                goto print_len_set;
536
                                        } else {        /* string */
537
                                                p = va_arg(ap, char *);
538
                                                if (!p) {
539
                                                        p = "(null)";
540
                                                        preci = 6;
541
                                                } else {
542
                                                        if (preci < 0) {
543
                                                                preci = INT_MAX;
544
                                                        }
545
                                                }
546
                                                len = strnlen(p, preci);
547
                                                goto print_len_set;
548
                                        }
549
#if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
550
                                } else if (p-u_spec < 27) {             /* floating point */
551
#endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
552
#if defined(__UCLIBC_HAS_FLOATS__)
553
                                        struct printf_info info;
554
                                        if (preci < 0) {
555
                                                preci = 6;
556
                                        }
557
                                        info.width = width;
558
                                        info.prec = preci;
559
                                        info.spec = *fmt;
560
                                        info.pad = flag[FLAG_0_PAD];
561
                                        info._flags = 0;
562
                                        if (flag[FLAG_PLUS] == '+') {
563
                                                PRINT_INFO_SET_FLAG(&info,showsign);
564
                                        } else if (flag[FLAG_PLUS] == ' ') {
565
                                                PRINT_INFO_SET_FLAG(&info,space);
566
                                        }
567
                                        if (flag[FLAG_HASH]) {
568
                                                PRINT_INFO_SET_FLAG(&info,alt);
569
                                        }
570
                                        if (flag[FLAG_MINUS_LJUSTIFY]) {
571
                                                PRINT_INFO_SET_FLAG(&info,left);
572
                                        }
573
#if 1
574
                                        cnt += _fpmaxtostr(op,
575
                                                                           (__fpmax_t)
576
                                                                           ((dataargtype == (8 << 8))
577
                                                                                ? va_arg(ap, long double)
578
                                                                                : (long double) va_arg(ap, double)),
579
                                                                           &info, _fp_out_narrow);
580
#else
581
                                        cnt += _fpmaxtostr(op,
582
                                                                           (__fpmax_t)
583
                                                                           ((lval > 1)
584
                                                                                ? va_arg(ap, long double)
585
                                                                                : (long double) va_arg(ap, double)),
586
                                                                           &info, _fp_out_narrow);
587
#endif
588
                                        goto nextfmt;
589
#elif WANT_FLOAT_ERROR
590
                                        (void) ((lval > 1) ? va_arg(ap, long double)
591
                                                        : va_arg(ap, double)); /* carry on */
592
                                        p = (char *) dbl_err;
593
#endif /* defined(__UCLIBC_HAS_FLOATS__) */
594
                                }
595
 
596
#if WANT_GNU_ERRNO
597
                        print:
598
#endif
599
                                {                               /* this used to be printfield */
600
                                        /* cheaper than strlen call */
601
/*                                      for ( len = 0 ; p[len] ; len++ ) { } */
602
                                        len = strnlen(p, SIZE_MAX);
603
                                print_len_set:
604
                                        if ((*p == '-')
605
#if WANT_GNU_ERRNO
606
                                                && (*fmt != 'm')
607
#endif
608
                                                && (*fmt != 's')) {
609
                                                flag[FLAG_PLUS] = *p++;
610
                                                --len;
611
                                        }
612
                                    if (flag[FLAG_PLUS]) {
613
                                                ++len;
614
                                                ++preci;
615
                                                if (flag[FLAG_PLUS] == '0') { /* base 16 */
616
                                                        ++preci; /* account for x or X */
617
                                                }
618
                                        }
619
 
620
                                        if (preci >= 0) {
621
                                                if ((*fmt == 's')
622
#if WANT_GNU_ERRNO
623
                                                || (*fmt == 'm')
624
#endif
625
                                                ) {
626
                                                        if (len > preci) {
627
                                                                len = preci;
628
                                                        } else {
629
                                                                preci = len;
630
                                                        }
631
                                                }
632
                                                preci -= len;
633
                                                if (preci < 0) {
634
                                                        preci = 0;
635
                                                }
636
                                                width -= preci;
637
                                        }
638
 
639
                                        width -= len;
640
                                        if (width < 0) {
641
                                                width = 0;
642
                                        }
643
 
644
                                        if (preci < 0) {
645
                                                preci = 0;
646
                                                if (!flag[FLAG_MINUS_LJUSTIFY]
647
                                                        /* && flag[FLAG_PLUS] */
648
                                                        && (flag[FLAG_0_PAD] == '0')) {
649
                                                        preci = width;
650
                                                        width = 0;
651
                                                }
652
                                        }
653
 
654
                                        while (width + len + preci) {
655
                                                unsigned char ch;
656
                                                /* right padding || left padding */
657
                                                if ((!len && !preci)
658
                                                        || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
659
                                                        ch = ' ';
660
                                                        --width;
661
                                                } else if (flag[FLAG_PLUS]) {
662
                                                        ch = flag[FLAG_PLUS]; /* sign */
663
                                                        if (flag[FLAG_PLUS]=='0') {     /* base 16 case */
664
                                                                flag[FLAG_PLUS] = *p++; /* get the x|X */
665
                                                        } else {
666
                                                                flag[FLAG_PLUS] = '\0';
667
                                                        }
668
                                                        --len;
669
                                                } else if (preci) {
670
                                                        ch = '0';
671
                                                        --preci;
672
                                                } else {
673
                                                        ch = *p++; /* main field */
674
                                                        --len;
675
                                                }
676
                                                ++cnt;
677
                                                PUTC(ch, op);
678
                                        }
679
                                }
680
                                goto nextfmt;
681
                        }
682
 
683
                        fmt = fmt0;     /* this was an illegal format */
684
                }
685
 
686
        charout:
687
                ++cnt;
688
                PUTC(*fmt, op); /* normal char out */
689
 
690
        nextfmt:
691
                ++fmt;
692
        }
693
 
694
        i = (__FERROR(op)) ? -1 : cnt;
695
 
696
        __STDIO_THREADUNLOCK(op);
697
 
698
        return i;
699
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.