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

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [orpmon/] [common/] [cprintf.c] - Blame information for rev 1767

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

Line No. Rev Author Line
1 809 simons
/*
2
FUNCTION
3
<<vprintf>>, <<vfprintf>>, <<vsprintf>>---format argument list
4
 
5
INDEX
6
        vprintf
7
INDEX
8
        vfprintf
9
INDEX
10
        vsprintf
11
INDEX
12
        vsnprintf
13
 
14
ANSI_SYNOPSIS
15
        #include <stdio.h>
16
        #include <stdarg.h>
17
        int vprintf(const char *<[fmt]>, va_list <[list]>);
18
        int vfprintf(FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>);
19
        int vsprintf(char *<[str]>, const char *<[fmt]>, va_list <[list]>);
20
        int vsnprintf(char *<[str]>, size_t <[size]>, const char *<[fmt]>, va_list <[list]>);
21
 
22
        int _vprintf_r(void *<[reent]>, const char *<[fmt]>,
23
                        va_list <[list]>);
24
        int _vfprintf_r(void *<[reent]>, FILE *<[fp]>, const char *<[fmt]>,
25
                        va_list <[list]>);
26
        int _vsprintf_r(void *<[reent]>, char *<[str]>, const char *<[fmt]>,
27
                        va_list <[list]>);
28
        int _vsnprintf_r(void *<[reent]>, char *<[str]>, size_t <[size]>, const char *<[fmt]>,
29
                        va_list <[list]>);
30
 
31
TRAD_SYNOPSIS
32
        #include <stdio.h>
33
        #include <varargs.h>
34
        int vprintf( <[fmt]>, <[list]>)
35
        char *<[fmt]>;
36
        va_list <[list]>;
37
 
38
        int vfprintf(<[fp]>, <[fmt]>, <[list]>)
39
        FILE *<[fp]>;
40
        char *<[fmt]>;
41
        va_list <[list]>;
42
 
43
        int vsprintf(<[str]>, <[fmt]>, <[list]>)
44
        char *<[str]>;
45
        char *<[fmt]>;
46
        va_list <[list]>;
47
 
48
        int vsnprintf(<[str]>, <[size]>, <[fmt]>, <[list]>)
49
        char *<[str]>;
50
        size_t <[size]>;
51
        char *<[fmt]>;
52
        va_list <[list]>;
53
 
54
        int _vprintf_r(<[reent]>, <[fmt]>, <[list]>)
55
        char *<[reent]>;
56
        char *<[fmt]>;
57
        va_list <[list]>;
58
 
59
        int _vfprintf_r(<[reent]>, <[fp]>, <[fmt]>, <[list]>)
60
        char *<[reent]>;
61
        FILE *<[fp]>;
62
        char *<[fmt]>;
63
        va_list <[list]>;
64
 
65
        int _vsprintf_r(<[reent]>, <[str]>, <[fmt]>, <[list]>)
66
        char *<[reent]>;
67
        char *<[str]>;
68
        char *<[fmt]>;
69
        va_list <[list]>;
70
 
71
        int _vsnprintf_r(<[reent]>, <[str]>, <[size]>, <[fmt]>, <[list]>)
72
        char *<[reent]>;
73
        char *<[str]>;
74
        size_t <[size]>;
75
        char *<[fmt]>;
76
        va_list <[list]>;
77
 
78
DESCRIPTION
79
<<vprintf>>, <<vfprintf>>, <<vsprintf>> and <<vsnprintf>> are (respectively)
80
variants of <<printf>>, <<fprintf>>, <<sprintf>> and <<snprintf>>.  They differ
81
only in allowing their caller to pass the variable argument list as a
82
<<va_list>> object (initialized by <<va_start>>) rather than directly
83
accepting a variable number of arguments.
84
 
85
RETURNS
86
The return values are consistent with the corresponding functions:
87
<<vsprintf>> returns the number of bytes in the output string,
88
save that the concluding <<NULL>> is not counted.
89
<<vprintf>> and <<vfprintf>> return the number of characters transmitted.
90
If an error occurs, <<vprintf>> and <<vfprintf>> return <<EOF>>. No
91
error returns occur for <<vsprintf>>.
92
 
93
PORTABILITY
94
ANSI C requires all three functions.
95
 
96
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
97
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
98
*/
99
 
100
/*-
101
 * Copyright (c) 1990 The Regents of the University of California.
102
 * All rights reserved.
103
 *
104
 * This code is derived from software contributed to Berkeley by
105
 * Chris Torek.
106
 *
107
 * Redistribution and use in source and binary forms, with or without
108
 * modification, are permitted provided that the following conditions
109
 * are met:
110
 * 1. Redistributions of source code must retain the above copyright
111
 *    notice, this list of conditions and the following disclaimer.
112
 * 2. Redistributions in binary form must reproduce the above copyright
113
 *    notice, this list of conditions and the following disclaimer in the
114
 *    documentation and/or other materials provided with the distribution.
115
 * 3. All advertising materials mentioning features or use of this software
116
 *    must display the following acknowledgement:
117
 *      This product includes software developed by the University of
118
 *      California, Berkeley and its contributors.
119
 * 4. Neither the name of the University nor the names of its contributors
120
 *    may be used to endorse or promote products derived from this software
121
 *    without specific prior written permission.
122
 *
123
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
124
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
125
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
126
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
127
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
128
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
129
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
130
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
131
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
132
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
133
 * SUCH DAMAGE.
134
 */
135
 
136
#define INTEGER_ONLY
137
#define _HAVE_STDC_
138
#define u_long unsigned long
139
#define u_short unsigned short
140
#define u_int unsigned int
141
#define _uquad_t u_long
142
#define _POINTER_INT int
143
#define _CONST const
144
#define NULL 0
145
 
146
#include "common.h"
147
#include "support.h"
148
 
149
#if defined(LIBC_SCCS) && !defined(lint)
150
/*static char *sccsid = "from: @(#)vfprintf.c   5.50 (Berkeley) 12/16/92";*/
151
static char *rcsid = "$Id: cprintf.c,v 1.1.1.1 2002-04-11 10:47:29 simons Exp $";
152
#endif /* LIBC_SCCS and not lint */
153
 
154
/*
155
 * Actual printf innards.
156
 *
157
 * This code is large and complicated...
158
 */
159
 
160
#ifdef INTEGER_ONLY
161
#define VFPRINTF vfiprintf
162
#define _VFPRINTF_R _vfiprintf_r
163
#else
164
#define VFPRINTF vfprintf
165
#define _VFPRINTF_R _vfprintf_r
166
#define FLOATING_POINT
167
#endif
168
 
169
#define _NO_LONGLONG
170
#if defined WANT_PRINTF_LONG_LONG && defined __GNUC__
171
# undef _NO_LONGLONG
172
#endif
173
 
174
#include <stdarg.h>
175
 
176
#ifdef FLOATING_POINT
177
#include <locale.h>
178
#include <math.h>
179
#include "floatio.h"
180
 
181
#define BUF             (MAXEXP+MAXFRACT+1)     /* + decimal point */
182
#define DEFPREC         6
183
 
184
static char *cvt _PARAMS((struct _reent *, double, int, int, char *, int *, int, int *));
185
static int exponent _PARAMS((char *, int, int));
186
 
187
#else /* no FLOATING_POINT */
188
 
189
#define BUF             40
190
 
191
#endif /* FLOATING_POINT */
192
 
193
/*
194
 * Macros for converting digits to letters and vice versa
195
 */
196
#define to_digit(c)     ((c) - '0')
197
#define is_digit(c)     ((unsigned)to_digit(c) <= 9)
198
#define to_char(n)      ((n) + '0')
199
 
200
/*
201
 * Flags used during conversion.
202
 */
203
#define ALT             0x001           /* alternate form */
204
#define HEXPREFIX       0x002           /* add 0x or 0X prefix */
205
#define LADJUST         0x004           /* left adjustment */
206
#define LONGDBL         0x008           /* long double; unimplemented */
207
#define LONGINT         0x010           /* long integer */
208
#define QUADINT         0x020           /* quad integer */
209
#define SHORTINT        0x040           /* short integer */
210
#define ZEROPAD         0x080           /* zero (as opposed to blank) pad */
211
#define FPT             0x100           /* Floating point number */
212
 
213
        /*
214
         * Choose PADSIZE to trade efficiency vs. size.  If larger printf
215
         * fields occur frequently, increase PADSIZE and make the initialisers
216
         * below longer.
217
         */
218
#define PADSIZE 16              /* pad chunk size */
219
        static _CONST char blanks[PADSIZE] =
220
         {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
221
        static _CONST char zeroes[PADSIZE] =
222
         {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
223
 
224
inline void pc (_CONST char c) {
225
#ifdef OR1K
226
  putc (c);
227
#else
228
  printf ("%c", c);
229
#endif
230
}
231
        /*
232
         * BEWARE, these `goto error' on error, and PAD uses `n'.
233
         */
234
inline void     PRINT(_CONST char *ptr, int len) {
235
  int i;
236
  for (i = 0; i < len; i++)
237
          pc(*(ptr++));
238
}
239
 
240
inline void PAD(int howmany, _CONST char *with) {
241
  int n;
242
        if ((n = howmany) > 0) {
243
                while (n > PADSIZE) {
244
                        PRINT(with, PADSIZE);
245
                        n -= PADSIZE;
246
                }
247
                PRINT(with, n);
248
        }
249
}
250
 
251
int printf(const char *fmt0,    ...)
252
{
253
        register char *fmt;     /* format string */
254
        register int ch;        /* character from fmt */
255
        int n;  /* handy integers (short term usage) */
256
        register char *cp;      /* handy char pointer (short term usage) */
257
        register int flags;     /* flags as above */
258
        int ret;                /* return value accumulator */
259
        int width;              /* width from format (%8d), or 0 */
260
        int prec;               /* precision from format (%.3d), or -1 */
261
        char sign;              /* sign prefix (' ', '+', '-', or \0) */
262
        va_list ap;
263
 
264
#ifdef FLOATING_POINT
265
        char *decimal_point = localeconv()->decimal_point;
266
        char softsign;          /* temporary negative sign for floats */
267
        /*
268
         * Although it is natural to declare this double here, the
269
         * declaration causes gcc to save FP registers even when not
270
         * printing an FP number.  This results in surprising use
271
         * of FP registers to print integers or strings on at least the
272
         * PowerPC.  A more proper solution would be to move FP printing
273
         * to another file, but this does seem to work.
274
         */
275
#if 0
276
        double _double;         /* double precision arguments %[eEfgG] */
277
#else
278
                                /* double precision arguments %[eEfgG] */
279
        union { int i; double d; } _double_ = {0};
280
#define _double (_double_.d)
281
#endif
282
        int expt;               /* integer value of exponent */
283
        int expsize;            /* character count for expstr */
284
        int ndig;               /* actual number of digits returned by cvt */
285
        char expstr[7];         /* buffer for exponent string */
286
#endif
287
 
288
#ifndef _NO_LONGLONG
289
#define quad_t    long long
290
#define u_quad_t  unsigned long long
291
#endif
292
 
293
#ifndef _NO_LONGLONG
294
        u_quad_t _uquad;        /* integer arguments %[diouxX] */
295
#else
296
        u_long _uquad;
297
#endif
298
        enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
299
        int dprec;              /* a copy of prec if [diouxX], 0 otherwise */
300
        int realsz;             /* field size expanded by dprec */
301
        int size;               /* size of converted field or string */
302
        char *xdigs = (char *)0;         /* digits for [xX] conversion */
303
#define NIOV 8
304
 
305
char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
306
char ox[2];             /* space for 0x hex-prefix */
307
 
308
#define FLUSH()
309
 
310
        /*
311
         * To extend shorts properly, we need both signed and unsigned
312
         * argument extraction methods.
313
         */
314
#ifndef _NO_LONGLONG
315
#define SARG() \
316
        (flags&QUADINT ? va_arg(ap, quad_t) : \
317
            flags&LONGINT ? va_arg(ap, long) : \
318
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
319
            (long)va_arg(ap, int))
320
#define UARG() \
321
        (flags&QUADINT ? va_arg(ap, u_quad_t) : \
322
            flags&LONGINT ? va_arg(ap, u_long) : \
323
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
324
            (u_long)va_arg(ap, u_int))
325
#else
326
#define SARG() \
327
        (flags&LONGINT ? va_arg(ap, long) : \
328
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
329
            (long)va_arg(ap, int))
330
#define UARG() \
331
        (flags&LONGINT ? va_arg(ap, u_long) : \
332
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
333
            (u_long)va_arg(ap, u_int))
334
#endif
335
 
336
  va_start (ap, fmt0);
337
        fmt = (char *)fmt0;
338
        ret = 0;
339
 
340
        /*
341
         * Scan the format for conversions (`%' character).
342
         */
343
        for (;;) {
344
 
345
          while (*fmt != 0 && *fmt != '%') {
346
            pc (*fmt);
347
      fmt++;
348
      ret++;
349
    }
350
                if (!*fmt)
351
                        goto done;
352
 
353
    fmt++; /* Skip % */
354
                flags = 0;
355
                dprec = 0;
356
                width = 0;
357
                prec = -1;
358
                sign = '\0';
359
 
360
rflag:          ch = *fmt++;
361
reswitch:       switch (ch) {
362
                case ' ':
363
                        /*
364
                         * ``If the space and + flags both appear, the space
365
                         * flag will be ignored.''
366
                         *      -- ANSI X3J11
367
                         */
368
                        if (!sign)
369
                                sign = ' ';
370
                        goto rflag;
371
                case '#':
372
                        flags |= ALT;
373
                        goto rflag;
374
                case '*':
375
                        /*
376
                         * ``A negative field width argument is taken as a
377
                         * - flag followed by a positive field width.''
378
                         *      -- ANSI X3J11
379
                         * They don't exclude field widths read from args.
380
                         */
381
                        if ((width = va_arg(ap, int)) >= 0)
382
                                goto rflag;
383
                        width = -width;
384
                        /* FALLTHROUGH */
385
                case '-':
386
                        flags |= LADJUST;
387
                        goto rflag;
388
                case '+':
389
                        sign = '+';
390
                        goto rflag;
391
                case '.':
392
                        if ((ch = *fmt++) == '*') {
393
                                n = va_arg(ap, int);
394
                                prec = n < 0 ? -1 : n;
395
                                goto rflag;
396
                        }
397
                        n = 0;
398
                        while (is_digit(ch)) {
399
                                n = 10 * n + to_digit(ch);
400
                                ch = *fmt++;
401
                        }
402
                        prec = n < 0 ? -1 : n;
403
                        goto reswitch;
404
                case '0':
405
                        /*
406
                         * ``Note that 0 is taken as a flag, not as the
407
                         * beginning of a field width.''
408
                         *      -- ANSI X3J11
409
                         */
410
                        flags |= ZEROPAD;
411
                        goto rflag;
412
                case '1': case '2': case '3': case '4':
413
                case '5': case '6': case '7': case '8': case '9':
414
                        n = 0;
415
                        do {
416
                                n = 10 * n + to_digit(ch);
417
                                ch = *fmt++;
418
                        } while (is_digit(ch));
419
                        width = n;
420
                        goto reswitch;
421
#ifdef FLOATING_POINT
422
                case 'L':
423
                        flags |= LONGDBL;
424
                        goto rflag;
425
#endif
426
                case 'h':
427
                        flags |= SHORTINT;
428
                        goto rflag;
429
                case 'l':
430
                        if (*fmt == 'l') {
431
                                fmt++;
432
                                flags |= QUADINT;
433
                        } else {
434
                                flags |= LONGINT;
435
                        }
436
                        goto rflag;
437
                case 'q':
438
                        flags |= QUADINT;
439
                        goto rflag;
440
                case 'c':
441
                        *(cp = buf) = va_arg(ap, int);
442
                        size = 1;
443
                        sign = '\0';
444
                        break;
445
                case 'D':
446
                        flags |= LONGINT;
447
                        /*FALLTHROUGH*/
448
                case 'd':
449
                case 'i':
450
                        _uquad = SARG();
451
#ifndef _NO_LONGLONG
452
                        if ((quad_t)_uquad < 0)
453
#else
454
                        if ((long) _uquad < 0)
455
#endif
456
                        {
457
 
458
                                _uquad = -_uquad;
459
                                sign = '-';
460
                        }
461
                        base = DEC;
462
                        goto number;
463
#ifdef FLOATING_POINT
464
                case 'e':
465
                case 'E':
466
                case 'f':
467
                case 'g':
468
                case 'G':
469
                        if (prec == -1) {
470
                                prec = DEFPREC;
471
                        } else if ((ch == 'g' || ch == 'G') && prec == 0) {
472
                                prec = 1;
473
                        }
474
 
475
                        if (flags & LONGDBL) {
476
                                _double = (double) va_arg(ap, long double);
477
                        } else {
478
                                _double = va_arg(ap, double);
479
                        }
480
 
481
                        /* do this before tricky precision changes */
482
                        if (isinf(_double)) {
483
                                if (_double < 0)
484
                                        sign = '-';
485
                                cp = "Inf";
486
                                size = 3;
487
                                break;
488
                        }
489
                        if (isnan(_double)) {
490
                                cp = "NaN";
491
                                size = 3;
492
                                break;
493
                        }
494
 
495
                        flags |= FPT;
496
                        cp = cvt(data, _double, prec, flags, &softsign,
497
                                &expt, ch, &ndig);
498
                        if (ch == 'g' || ch == 'G') {
499
                                if (expt <= -4 || expt > prec)
500
                                        ch = (ch == 'g') ? 'e' : 'E';
501
                                else
502
                                        ch = 'g';
503
                        }
504
                        if (ch <= 'e') {        /* 'e' or 'E' fmt */
505
                                --expt;
506
                                expsize = exponent(expstr, expt, ch);
507
                                size = expsize + ndig;
508
                                if (ndig > 1 || flags & ALT)
509
                                        ++size;
510
                        } else if (ch == 'f') {         /* f fmt */
511
                                if (expt > 0) {
512
                                        size = expt;
513
                                        if (prec || flags & ALT)
514
                                                size += prec + 1;
515
                                } else  /* "0.X" */
516
                                        size = prec + 2;
517
                        } else if (expt >= ndig) {      /* fixed g fmt */
518
                                size = expt;
519
                                if (flags & ALT)
520
                                        ++size;
521
                        } else
522
                                size = ndig + (expt > 0 ?
523
                                        1 : 2 - expt);
524
 
525
                        if (softsign)
526
                                sign = '-';
527
                        break;
528
#endif /* FLOATING_POINT */
529
                case 'n':
530
#ifndef _NO_LONGLONG
531
                        if (flags & QUADINT)
532
                                *va_arg(ap, quad_t *) = ret;
533
                        else
534
#endif
535
                        if (flags & LONGINT)
536
                                *va_arg(ap, long *) = ret;
537
                        else if (flags & SHORTINT)
538
                                *va_arg(ap, short *) = ret;
539
                        else
540
                                *va_arg(ap, int *) = ret;
541
                        continue;       /* no output */
542
                case 'O':
543
                        flags |= LONGINT;
544
                        /*FALLTHROUGH*/
545
                case 'o':
546
                        _uquad = UARG();
547
                        base = OCT;
548
                        goto nosign;
549
                case 'p':
550
                        /*
551
                         * ``The argument shall be a pointer to void.  The
552
                         * value of the pointer is converted to a sequence
553
                         * of printable characters, in an implementation-
554
                         * defined manner.''
555
                         *      -- ANSI X3J11
556
                         */
557
                        /* NOSTRICT */
558
                        _uquad = (u_long)(unsigned _POINTER_INT)va_arg(ap, void *);
559
                        base = HEX;
560
                        xdigs = "0123456789abcdef";
561
                        flags |= HEXPREFIX;
562
                        ch = 'x';
563
                        goto nosign;
564
                case 's':
565
                        if ((cp = va_arg(ap, char *)) == NULL)
566
                                cp = "(null)";
567
                        if (prec >= 0) {
568
                                /*
569
                                 * can't use strlen; can only look for the
570
                                 * NUL in the first `prec' characters, and
571
                                 * strlen() will go further.
572
                                 */
573
                                char *p = (char *)memchr(cp, 0, prec);
574
 
575
                                if (p != NULL) {
576
                                        size = p - cp;
577
                                        if (size > prec)
578
                                                size = prec;
579
                                } else
580
                                        size = prec;
581
                        } else
582
                                size = strlen(cp);
583
                        sign = '\0';
584
                        break;
585
                case 'U':
586
                        flags |= LONGINT;
587
                        /*FALLTHROUGH*/
588
                case 'u':
589
                        _uquad = UARG();
590
                        base = DEC;
591
                        goto nosign;
592
                case 'X':
593
                        xdigs = "0123456789ABCDEF";
594
                        goto hex;
595
                case 'x':
596
                        xdigs = "0123456789abcdef";
597
hex:                    _uquad = UARG();
598
                        base = HEX;
599
                        /* leading 0x/X only if non-zero */
600
                        if (flags & ALT && _uquad != 0)
601
                                flags |= HEXPREFIX;
602
 
603
                        /* unsigned conversions */
604
nosign:                 sign = '\0';
605
                        /*
606
                         * ``... diouXx conversions ... if a precision is
607
                         * specified, the 0 flag will be ignored.''
608
                         *      -- ANSI X3J11
609
                         */
610
number:                 if ((dprec = prec) >= 0)
611
                                flags &= ~ZEROPAD;
612
 
613
                        /*
614
                         * ``The result of converting a zero value with an
615
                         * explicit precision of zero is no characters.''
616
                         *      -- ANSI X3J11
617
                         */
618
                        cp = buf + BUF;
619
                        if (_uquad != 0 || prec != 0) {
620
                                /*
621
                                 * Unsigned mod is hard, and unsigned mod
622
                                 * by a constant is easier than that by
623
                                 * a variable; hence this switch.
624
                                 */
625
                                switch (base) {
626
                                case OCT:
627
                                        do {
628
                                                *--cp = to_char(_uquad & 7);
629
                                                _uquad >>= 3;
630
                                        } while (_uquad);
631
                                        /* handle octal leading 0 */
632
                                        if (flags & ALT && *cp != '0')
633
                                                *--cp = '0';
634
                                        break;
635
 
636
                                case DEC:
637
                                        /* many numbers are 1 digit */
638
                                        while (_uquad >= 10) {
639
                                                *--cp = to_char(_uquad % 10);
640
                                                _uquad /= 10;
641
                                        }
642
                                        *--cp = to_char(_uquad);
643
                                        break;
644
 
645
                                case HEX:
646
                                        do {
647
                                                *--cp = xdigs[_uquad & 15];
648
                                                _uquad >>= 4;
649
                                        } while (_uquad);
650
                                        break;
651
 
652
                                default:
653
                                        cp = "bug in vfprintf: bad base";
654
                                        size = strlen(cp);
655
                                        goto skipsize;
656
                                }
657
                        }
658
                        size = buf + BUF - cp;
659
                skipsize:
660
                        break;
661
                default:        /* "%?" prints ?, unless ? is NUL */
662
                        if (ch == '\0')
663
                                goto done;
664
                        /* pretend it was %c with argument ch */
665
                        cp = buf;
666
                        *cp = ch;
667
                        size = 1;
668
                        sign = '\0';
669
                        break;
670
                }
671
 
672
                /*
673
                 * All reasonable formats wind up here.  At this point, `cp'
674
                 * points to a string which (if not flags&LADJUST) should be
675
                 * padded out to `width' places.  If flags&ZEROPAD, it should
676
                 * first be prefixed by any sign or other prefix; otherwise,
677
                 * it should be blank padded before the prefix is emitted.
678
                 * After any left-hand padding and prefixing, emit zeroes
679
                 * required by a decimal [diouxX] precision, then print the
680
                 * string proper, then emit zeroes required by any leftover
681
                 * floating precision; finally, if LADJUST, pad with blanks.
682
                 *
683
                 * Compute actual size, so we know how much to pad.
684
                 * size excludes decimal prec; realsz includes it.
685
                 */
686
                realsz = dprec > size ? dprec : size;
687
                if (sign)
688
                        realsz++;
689
                else if (flags & HEXPREFIX)
690
                        realsz+= 2;
691
 
692
                /* right-adjusting blank padding */
693
                if ((flags & (LADJUST|ZEROPAD)) == 0)
694
                        PAD(width - realsz, blanks);
695
 
696
                /* prefix */
697
                if (sign) {
698
                        PRINT(&sign, 1);
699
                } else if (flags & HEXPREFIX) {
700
                        ox[0] = '0';
701
                        ox[1] = ch;
702
                        PRINT(ox, 2);
703
                }
704
 
705
                /* right-adjusting zero padding */
706
                if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
707
                        PAD(width - realsz, zeroes);
708
 
709
                /* leading zeroes from decimal precision */
710
                PAD(dprec - size, zeroes);
711
 
712
                /* the string or number proper */
713
#ifdef FLOATING_POINT
714
                if ((flags & FPT) == 0) {
715
                        PRINT(cp, size);
716
                } else {        /* glue together f_p fragments */
717
                        if (ch >= 'f') {        /* 'f' or 'g' */
718
                                if (_double == 0) {
719
                                        /* kludge for __dtoa irregularity */
720
                                        PRINT("0", 1);
721
                                        if (expt < ndig || (flags & ALT) != 0) {
722
                                                PRINT(decimal_point, 1);
723
                                                PAD(ndig - 1, zeroes);
724
                                        }
725
                                } else if (expt <= 0) {
726
                                        PRINT("0", 1);
727
                                        PRINT(decimal_point, 1);
728
                                        PAD(-expt, zeroes);
729
                                        PRINT(cp, ndig);
730
                                } else if (expt >= ndig) {
731
                                        PRINT(cp, ndig);
732
                                        PAD(expt - ndig, zeroes);
733
                                        if (flags & ALT)
734
                                                PRINT(".", 1);
735
                                } else {
736
                                        PRINT(cp, expt);
737
                                        cp += expt;
738
                                        PRINT(".", 1);
739
                                        PRINT(cp, ndig-expt);
740
                                }
741
                        } else {        /* 'e' or 'E' */
742
                                if (ndig > 1 || flags & ALT) {
743
                                        ox[0] = *cp++;
744
                                        ox[1] = '.';
745
                                        PRINT(ox, 2);
746
                                        if (_double || flags & ALT == 0) {
747
                                                PRINT(cp, ndig-1);
748
                                        } else  /* 0.[0..] */
749
                                                /* __dtoa irregularity */
750
                                                PAD(ndig - 1, zeroes);
751
                                } else  /* XeYYY */
752
                                        PRINT(cp, 1);
753
                                PRINT(expstr, expsize);
754
                        }
755
                }
756
#else
757
                PRINT(cp, size);
758
#endif
759
                /* left-adjusting padding (always blank) */
760
                if (flags & LADJUST)
761
                        PAD(width - realsz, blanks);
762
 
763
                /* finally, adjust ret */
764
                ret += width > realsz ? width : realsz;
765
 
766
                FLUSH();        /* copy out the I/O vectors */
767
        }
768
done:
769
  va_end (ap);
770
        FLUSH();
771
        return (ret);
772
        /* NOTREACHED */
773
}
774
 
775
#ifdef FLOATING_POINT
776
 
777
extern char *_dtoa_r _PARAMS((struct _reent *, double, int,
778
                              int, int *, int *, char **));
779
 
780
static char *
781
cvt(data, value, ndigits, flags, sign, decpt, ch, length)
782
        struct _reent *data;
783
        double value;
784
        int ndigits, flags, *decpt, ch, *length;
785
        char *sign;
786
{
787
        int mode, dsgn;
788
        char *digits, *bp, *rve;
789
        union double_union tmp;
790
 
791
        if (ch == 'f') {
792
                mode = 3;               /* ndigits after the decimal point */
793
        } else {
794
                /* To obtain ndigits after the decimal point for the 'e'
795
                 * and 'E' formats, round to ndigits + 1 significant
796
                 * figures.
797
                 */
798
                if (ch == 'e' || ch == 'E') {
799
                        ndigits++;
800
                }
801
                mode = 2;               /* ndigits significant digits */
802
        }
803
 
804
        tmp.d = value;
805
        if (word0(tmp) & Sign_bit) { /* this will check for < 0 and -0.0 */
806
                value = -value;
807
                *sign = '-';
808
        } else
809
                *sign = '\000';
810
        digits = _dtoa_r(data, value, mode, ndigits, decpt, &dsgn, &rve);
811
        if ((ch != 'g' && ch != 'G') || flags & ALT) {  /* Print trailing zeros */
812
                bp = digits + ndigits;
813
                if (ch == 'f') {
814
                        if (*digits == '0' && value)
815
                                *decpt = -ndigits + 1;
816
                        bp += *decpt;
817
                }
818
                if (value == 0)  /* kludge for __dtoa irregularity */
819
                        rve = bp;
820
                while (rve < bp)
821
                        *rve++ = '0';
822
        }
823
        *length = rve - digits;
824
        return (digits);
825
}
826
 
827
static int
828
exponent(p0, exp, fmtch)
829
        char *p0;
830
        int exp, fmtch;
831
{
832
        register char *p, *t;
833
        char expbuf[MAXEXP];
834
 
835
        p = p0;
836
        *p++ = fmtch;
837
        if (exp < 0) {
838
                exp = -exp;
839
                *p++ = '-';
840
        }
841
        else
842
                *p++ = '+';
843
        t = expbuf + MAXEXP;
844
        if (exp > 9) {
845
                do {
846
                        *--t = to_char(exp % 10);
847
                } while ((exp /= 10) > 9);
848
                *--t = to_char(exp);
849
                for (; t < expbuf + MAXEXP; *p++ = *t++);
850
        }
851
        else {
852
                *p++ = '0';
853
                *p++ = to_char(exp);
854
        }
855
        return (p - p0);
856
}
857
#endif /* FLOATING_POINT */

powered by: WebSVN 2.1.0

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