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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [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 828 markom
#define _NOLONGLONG
139 809 simons
#define u_long unsigned long
140
#define u_short unsigned short
141
#define u_int unsigned int
142
#define _uquad_t u_long
143
#define _POINTER_INT int
144
#define _CONST const
145
#define NULL 0
146
 
147
#include "common.h"
148
#include "support.h"
149
 
150
#if defined(LIBC_SCCS) && !defined(lint)
151
/*static char *sccsid = "from: @(#)vfprintf.c   5.50 (Berkeley) 12/16/92";*/
152 828 markom
static char *rcsid = "$Id: cprintf.c,v 1.2 2002-04-19 11:08:25 markom Exp $";
153 809 simons
#endif /* LIBC_SCCS and not lint */
154
 
155
/*
156
 * Actual printf innards.
157
 *
158
 * This code is large and complicated...
159
 */
160
 
161
#ifdef INTEGER_ONLY
162
#define VFPRINTF vfiprintf
163
#define _VFPRINTF_R _vfiprintf_r
164
#else
165
#define VFPRINTF vfprintf
166
#define _VFPRINTF_R _vfprintf_r
167
#define FLOATING_POINT
168
#endif
169
 
170
#define _NO_LONGLONG
171
#if defined WANT_PRINTF_LONG_LONG && defined __GNUC__
172
# undef _NO_LONGLONG
173
#endif
174
 
175
#include <stdarg.h>
176
 
177
#ifdef FLOATING_POINT
178
#include <locale.h>
179
#include <math.h>
180
#include "floatio.h"
181
 
182
#define BUF             (MAXEXP+MAXFRACT+1)     /* + decimal point */
183
#define DEFPREC         6
184
 
185
static char *cvt _PARAMS((struct _reent *, double, int, int, char *, int *, int, int *));
186
static int exponent _PARAMS((char *, int, int));
187
 
188
#else /* no FLOATING_POINT */
189
 
190
#define BUF             40
191
 
192
#endif /* FLOATING_POINT */
193
 
194
/*
195
 * Macros for converting digits to letters and vice versa
196
 */
197
#define to_digit(c)     ((c) - '0')
198
#define is_digit(c)     ((unsigned)to_digit(c) <= 9)
199
#define to_char(n)      ((n) + '0')
200
 
201
/*
202
 * Flags used during conversion.
203
 */
204
#define ALT             0x001           /* alternate form */
205
#define HEXPREFIX       0x002           /* add 0x or 0X prefix */
206
#define LADJUST         0x004           /* left adjustment */
207
#define LONGDBL         0x008           /* long double; unimplemented */
208
#define LONGINT         0x010           /* long integer */
209
#define QUADINT         0x020           /* quad integer */
210
#define SHORTINT        0x040           /* short integer */
211
#define ZEROPAD         0x080           /* zero (as opposed to blank) pad */
212
#define FPT             0x100           /* Floating point number */
213
 
214
        /*
215
         * Choose PADSIZE to trade efficiency vs. size.  If larger printf
216
         * fields occur frequently, increase PADSIZE and make the initialisers
217
         * below longer.
218
         */
219
#define PADSIZE 16              /* pad chunk size */
220
        static _CONST char blanks[PADSIZE] =
221
         {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
222
        static _CONST char zeroes[PADSIZE] =
223
         {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
224
 
225
inline void pc (_CONST char c) {
226
#ifdef OR1K
227
  putc (c);
228
#else
229
  printf ("%c", c);
230
#endif
231
}
232
        /*
233
         * BEWARE, these `goto error' on error, and PAD uses `n'.
234
         */
235
inline void     PRINT(_CONST char *ptr, int len) {
236
  int i;
237
  for (i = 0; i < len; i++)
238
          pc(*(ptr++));
239
}
240
 
241
inline void PAD(int howmany, _CONST char *with) {
242
  int n;
243
        if ((n = howmany) > 0) {
244
                while (n > PADSIZE) {
245
                        PRINT(with, PADSIZE);
246
                        n -= PADSIZE;
247
                }
248
                PRINT(with, n);
249
        }
250
}
251
 
252
int printf(const char *fmt0,    ...)
253
{
254
        register char *fmt;     /* format string */
255
        register int ch;        /* character from fmt */
256
        int n;  /* handy integers (short term usage) */
257
        register char *cp;      /* handy char pointer (short term usage) */
258
        register int flags;     /* flags as above */
259
        int ret;                /* return value accumulator */
260
        int width;              /* width from format (%8d), or 0 */
261
        int prec;               /* precision from format (%.3d), or -1 */
262
        char sign;              /* sign prefix (' ', '+', '-', or \0) */
263
        va_list ap;
264
 
265
#ifdef FLOATING_POINT
266
        char *decimal_point = localeconv()->decimal_point;
267
        char softsign;          /* temporary negative sign for floats */
268
        /*
269
         * Although it is natural to declare this double here, the
270
         * declaration causes gcc to save FP registers even when not
271
         * printing an FP number.  This results in surprising use
272
         * of FP registers to print integers or strings on at least the
273
         * PowerPC.  A more proper solution would be to move FP printing
274
         * to another file, but this does seem to work.
275
         */
276
#if 0
277
        double _double;         /* double precision arguments %[eEfgG] */
278
#else
279
                                /* double precision arguments %[eEfgG] */
280
        union { int i; double d; } _double_ = {0};
281
#define _double (_double_.d)
282
#endif
283
        int expt;               /* integer value of exponent */
284
        int expsize;            /* character count for expstr */
285
        int ndig;               /* actual number of digits returned by cvt */
286
        char expstr[7];         /* buffer for exponent string */
287
#endif
288
 
289
#ifndef _NO_LONGLONG
290
#define quad_t    long long
291
#define u_quad_t  unsigned long long
292
#endif
293
 
294
#ifndef _NO_LONGLONG
295
        u_quad_t _uquad;        /* integer arguments %[diouxX] */
296
#else
297
        u_long _uquad;
298
#endif
299
        enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
300
        int dprec;              /* a copy of prec if [diouxX], 0 otherwise */
301
        int realsz;             /* field size expanded by dprec */
302
        int size;               /* size of converted field or string */
303
        char *xdigs = (char *)0;         /* digits for [xX] conversion */
304
#define NIOV 8
305
 
306
char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
307
char ox[2];             /* space for 0x hex-prefix */
308
 
309
#define FLUSH()
310
 
311
        /*
312
         * To extend shorts properly, we need both signed and unsigned
313
         * argument extraction methods.
314
         */
315
#ifndef _NO_LONGLONG
316
#define SARG() \
317
        (flags&QUADINT ? va_arg(ap, quad_t) : \
318
            flags&LONGINT ? va_arg(ap, long) : \
319
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
320
            (long)va_arg(ap, int))
321
#define UARG() \
322
        (flags&QUADINT ? va_arg(ap, u_quad_t) : \
323
            flags&LONGINT ? va_arg(ap, u_long) : \
324
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
325
            (u_long)va_arg(ap, u_int))
326
#else
327
#define SARG() \
328
        (flags&LONGINT ? va_arg(ap, long) : \
329
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
330
            (long)va_arg(ap, int))
331
#define UARG() \
332
        (flags&LONGINT ? va_arg(ap, u_long) : \
333
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
334
            (u_long)va_arg(ap, u_int))
335
#endif
336
 
337
  va_start (ap, fmt0);
338
        fmt = (char *)fmt0;
339
        ret = 0;
340
 
341
        /*
342
         * Scan the format for conversions (`%' character).
343
         */
344
        for (;;) {
345
 
346
          while (*fmt != 0 && *fmt != '%') {
347
            pc (*fmt);
348
      fmt++;
349
      ret++;
350
    }
351
                if (!*fmt)
352
                        goto done;
353
 
354
    fmt++; /* Skip % */
355
                flags = 0;
356
                dprec = 0;
357
                width = 0;
358
                prec = -1;
359
                sign = '\0';
360
 
361
rflag:          ch = *fmt++;
362
reswitch:       switch (ch) {
363
                case ' ':
364
                        /*
365
                         * ``If the space and + flags both appear, the space
366
                         * flag will be ignored.''
367
                         *      -- ANSI X3J11
368
                         */
369
                        if (!sign)
370
                                sign = ' ';
371
                        goto rflag;
372
                case '#':
373
                        flags |= ALT;
374
                        goto rflag;
375
                case '*':
376
                        /*
377
                         * ``A negative field width argument is taken as a
378
                         * - flag followed by a positive field width.''
379
                         *      -- ANSI X3J11
380
                         * They don't exclude field widths read from args.
381
                         */
382
                        if ((width = va_arg(ap, int)) >= 0)
383
                                goto rflag;
384
                        width = -width;
385
                        /* FALLTHROUGH */
386
                case '-':
387
                        flags |= LADJUST;
388
                        goto rflag;
389
                case '+':
390
                        sign = '+';
391
                        goto rflag;
392
                case '.':
393
                        if ((ch = *fmt++) == '*') {
394
                                n = va_arg(ap, int);
395
                                prec = n < 0 ? -1 : n;
396
                                goto rflag;
397
                        }
398
                        n = 0;
399
                        while (is_digit(ch)) {
400
                                n = 10 * n + to_digit(ch);
401
                                ch = *fmt++;
402
                        }
403
                        prec = n < 0 ? -1 : n;
404
                        goto reswitch;
405
                case '0':
406
                        /*
407
                         * ``Note that 0 is taken as a flag, not as the
408
                         * beginning of a field width.''
409
                         *      -- ANSI X3J11
410
                         */
411
                        flags |= ZEROPAD;
412
                        goto rflag;
413
                case '1': case '2': case '3': case '4':
414
                case '5': case '6': case '7': case '8': case '9':
415
                        n = 0;
416
                        do {
417
                                n = 10 * n + to_digit(ch);
418
                                ch = *fmt++;
419
                        } while (is_digit(ch));
420
                        width = n;
421
                        goto reswitch;
422
#ifdef FLOATING_POINT
423
                case 'L':
424
                        flags |= LONGDBL;
425
                        goto rflag;
426
#endif
427
                case 'h':
428
                        flags |= SHORTINT;
429
                        goto rflag;
430
                case 'l':
431
                        if (*fmt == 'l') {
432
                                fmt++;
433
                                flags |= QUADINT;
434
                        } else {
435
                                flags |= LONGINT;
436
                        }
437
                        goto rflag;
438
                case 'q':
439
                        flags |= QUADINT;
440
                        goto rflag;
441
                case 'c':
442
                        *(cp = buf) = va_arg(ap, int);
443
                        size = 1;
444
                        sign = '\0';
445
                        break;
446
                case 'D':
447
                        flags |= LONGINT;
448
                        /*FALLTHROUGH*/
449
                case 'd':
450
                case 'i':
451
                        _uquad = SARG();
452
#ifndef _NO_LONGLONG
453
                        if ((quad_t)_uquad < 0)
454
#else
455
                        if ((long) _uquad < 0)
456
#endif
457
                        {
458
 
459
                                _uquad = -_uquad;
460
                                sign = '-';
461
                        }
462
                        base = DEC;
463
                        goto number;
464
#ifdef FLOATING_POINT
465
                case 'e':
466
                case 'E':
467
                case 'f':
468
                case 'g':
469
                case 'G':
470
                        if (prec == -1) {
471
                                prec = DEFPREC;
472
                        } else if ((ch == 'g' || ch == 'G') && prec == 0) {
473
                                prec = 1;
474
                        }
475
 
476
                        if (flags & LONGDBL) {
477
                                _double = (double) va_arg(ap, long double);
478
                        } else {
479
                                _double = va_arg(ap, double);
480
                        }
481
 
482
                        /* do this before tricky precision changes */
483
                        if (isinf(_double)) {
484
                                if (_double < 0)
485
                                        sign = '-';
486
                                cp = "Inf";
487
                                size = 3;
488
                                break;
489
                        }
490
                        if (isnan(_double)) {
491
                                cp = "NaN";
492
                                size = 3;
493
                                break;
494
                        }
495
 
496
                        flags |= FPT;
497
                        cp = cvt(data, _double, prec, flags, &softsign,
498
                                &expt, ch, &ndig);
499
                        if (ch == 'g' || ch == 'G') {
500
                                if (expt <= -4 || expt > prec)
501
                                        ch = (ch == 'g') ? 'e' : 'E';
502
                                else
503
                                        ch = 'g';
504
                        }
505
                        if (ch <= 'e') {        /* 'e' or 'E' fmt */
506
                                --expt;
507
                                expsize = exponent(expstr, expt, ch);
508
                                size = expsize + ndig;
509
                                if (ndig > 1 || flags & ALT)
510
                                        ++size;
511
                        } else if (ch == 'f') {         /* f fmt */
512
                                if (expt > 0) {
513
                                        size = expt;
514
                                        if (prec || flags & ALT)
515
                                                size += prec + 1;
516
                                } else  /* "0.X" */
517
                                        size = prec + 2;
518
                        } else if (expt >= ndig) {      /* fixed g fmt */
519
                                size = expt;
520
                                if (flags & ALT)
521
                                        ++size;
522
                        } else
523
                                size = ndig + (expt > 0 ?
524
                                        1 : 2 - expt);
525
 
526
                        if (softsign)
527
                                sign = '-';
528
                        break;
529
#endif /* FLOATING_POINT */
530
                case 'n':
531
#ifndef _NO_LONGLONG
532
                        if (flags & QUADINT)
533
                                *va_arg(ap, quad_t *) = ret;
534
                        else
535
#endif
536
                        if (flags & LONGINT)
537
                                *va_arg(ap, long *) = ret;
538
                        else if (flags & SHORTINT)
539
                                *va_arg(ap, short *) = ret;
540
                        else
541
                                *va_arg(ap, int *) = ret;
542
                        continue;       /* no output */
543
                case 'O':
544
                        flags |= LONGINT;
545
                        /*FALLTHROUGH*/
546
                case 'o':
547
                        _uquad = UARG();
548
                        base = OCT;
549
                        goto nosign;
550
                case 'p':
551
                        /*
552
                         * ``The argument shall be a pointer to void.  The
553
                         * value of the pointer is converted to a sequence
554
                         * of printable characters, in an implementation-
555
                         * defined manner.''
556
                         *      -- ANSI X3J11
557
                         */
558
                        /* NOSTRICT */
559
                        _uquad = (u_long)(unsigned _POINTER_INT)va_arg(ap, void *);
560
                        base = HEX;
561
                        xdigs = "0123456789abcdef";
562
                        flags |= HEXPREFIX;
563
                        ch = 'x';
564
                        goto nosign;
565
                case 's':
566
                        if ((cp = va_arg(ap, char *)) == NULL)
567
                                cp = "(null)";
568
                        if (prec >= 0) {
569
                                /*
570
                                 * can't use strlen; can only look for the
571
                                 * NUL in the first `prec' characters, and
572
                                 * strlen() will go further.
573
                                 */
574
                                char *p = (char *)memchr(cp, 0, prec);
575
 
576
                                if (p != NULL) {
577
                                        size = p - cp;
578
                                        if (size > prec)
579
                                                size = prec;
580
                                } else
581
                                        size = prec;
582
                        } else
583
                                size = strlen(cp);
584
                        sign = '\0';
585
                        break;
586
                case 'U':
587
                        flags |= LONGINT;
588
                        /*FALLTHROUGH*/
589
                case 'u':
590
                        _uquad = UARG();
591
                        base = DEC;
592
                        goto nosign;
593
                case 'X':
594
                        xdigs = "0123456789ABCDEF";
595
                        goto hex;
596
                case 'x':
597
                        xdigs = "0123456789abcdef";
598
hex:                    _uquad = UARG();
599
                        base = HEX;
600
                        /* leading 0x/X only if non-zero */
601
                        if (flags & ALT && _uquad != 0)
602
                                flags |= HEXPREFIX;
603
 
604
                        /* unsigned conversions */
605
nosign:                 sign = '\0';
606
                        /*
607
                         * ``... diouXx conversions ... if a precision is
608
                         * specified, the 0 flag will be ignored.''
609
                         *      -- ANSI X3J11
610
                         */
611
number:                 if ((dprec = prec) >= 0)
612
                                flags &= ~ZEROPAD;
613
 
614
                        /*
615
                         * ``The result of converting a zero value with an
616
                         * explicit precision of zero is no characters.''
617
                         *      -- ANSI X3J11
618
                         */
619
                        cp = buf + BUF;
620
                        if (_uquad != 0 || prec != 0) {
621
                                /*
622
                                 * Unsigned mod is hard, and unsigned mod
623
                                 * by a constant is easier than that by
624
                                 * a variable; hence this switch.
625
                                 */
626
                                switch (base) {
627
                                case OCT:
628
                                        do {
629
                                                *--cp = to_char(_uquad & 7);
630
                                                _uquad >>= 3;
631
                                        } while (_uquad);
632
                                        /* handle octal leading 0 */
633
                                        if (flags & ALT && *cp != '0')
634
                                                *--cp = '0';
635
                                        break;
636
 
637
                                case DEC:
638
                                        /* many numbers are 1 digit */
639
                                        while (_uquad >= 10) {
640
                                                *--cp = to_char(_uquad % 10);
641
                                                _uquad /= 10;
642
                                        }
643
                                        *--cp = to_char(_uquad);
644
                                        break;
645
 
646
                                case HEX:
647
                                        do {
648
                                                *--cp = xdigs[_uquad & 15];
649
                                                _uquad >>= 4;
650
                                        } while (_uquad);
651
                                        break;
652
 
653
                                default:
654
                                        cp = "bug in vfprintf: bad base";
655
                                        size = strlen(cp);
656
                                        goto skipsize;
657
                                }
658
                        }
659
                        size = buf + BUF - cp;
660
                skipsize:
661
                        break;
662
                default:        /* "%?" prints ?, unless ? is NUL */
663
                        if (ch == '\0')
664
                                goto done;
665
                        /* pretend it was %c with argument ch */
666
                        cp = buf;
667
                        *cp = ch;
668
                        size = 1;
669
                        sign = '\0';
670
                        break;
671
                }
672
 
673
                /*
674
                 * All reasonable formats wind up here.  At this point, `cp'
675
                 * points to a string which (if not flags&LADJUST) should be
676
                 * padded out to `width' places.  If flags&ZEROPAD, it should
677
                 * first be prefixed by any sign or other prefix; otherwise,
678
                 * it should be blank padded before the prefix is emitted.
679
                 * After any left-hand padding and prefixing, emit zeroes
680
                 * required by a decimal [diouxX] precision, then print the
681
                 * string proper, then emit zeroes required by any leftover
682
                 * floating precision; finally, if LADJUST, pad with blanks.
683
                 *
684
                 * Compute actual size, so we know how much to pad.
685
                 * size excludes decimal prec; realsz includes it.
686
                 */
687
                realsz = dprec > size ? dprec : size;
688
                if (sign)
689
                        realsz++;
690
                else if (flags & HEXPREFIX)
691
                        realsz+= 2;
692
 
693
                /* right-adjusting blank padding */
694
                if ((flags & (LADJUST|ZEROPAD)) == 0)
695
                        PAD(width - realsz, blanks);
696
 
697
                /* prefix */
698
                if (sign) {
699
                        PRINT(&sign, 1);
700
                } else if (flags & HEXPREFIX) {
701
                        ox[0] = '0';
702
                        ox[1] = ch;
703
                        PRINT(ox, 2);
704
                }
705
 
706
                /* right-adjusting zero padding */
707
                if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
708
                        PAD(width - realsz, zeroes);
709
 
710
                /* leading zeroes from decimal precision */
711
                PAD(dprec - size, zeroes);
712
 
713
                /* the string or number proper */
714
#ifdef FLOATING_POINT
715
                if ((flags & FPT) == 0) {
716
                        PRINT(cp, size);
717
                } else {        /* glue together f_p fragments */
718
                        if (ch >= 'f') {        /* 'f' or 'g' */
719
                                if (_double == 0) {
720
                                        /* kludge for __dtoa irregularity */
721
                                        PRINT("0", 1);
722
                                        if (expt < ndig || (flags & ALT) != 0) {
723
                                                PRINT(decimal_point, 1);
724
                                                PAD(ndig - 1, zeroes);
725
                                        }
726
                                } else if (expt <= 0) {
727
                                        PRINT("0", 1);
728
                                        PRINT(decimal_point, 1);
729
                                        PAD(-expt, zeroes);
730
                                        PRINT(cp, ndig);
731
                                } else if (expt >= ndig) {
732
                                        PRINT(cp, ndig);
733
                                        PAD(expt - ndig, zeroes);
734
                                        if (flags & ALT)
735
                                                PRINT(".", 1);
736
                                } else {
737
                                        PRINT(cp, expt);
738
                                        cp += expt;
739
                                        PRINT(".", 1);
740
                                        PRINT(cp, ndig-expt);
741
                                }
742
                        } else {        /* 'e' or 'E' */
743
                                if (ndig > 1 || flags & ALT) {
744
                                        ox[0] = *cp++;
745
                                        ox[1] = '.';
746
                                        PRINT(ox, 2);
747
                                        if (_double || flags & ALT == 0) {
748
                                                PRINT(cp, ndig-1);
749
                                        } else  /* 0.[0..] */
750
                                                /* __dtoa irregularity */
751
                                                PAD(ndig - 1, zeroes);
752
                                } else  /* XeYYY */
753
                                        PRINT(cp, 1);
754
                                PRINT(expstr, expsize);
755
                        }
756
                }
757
#else
758
                PRINT(cp, size);
759
#endif
760
                /* left-adjusting padding (always blank) */
761
                if (flags & LADJUST)
762
                        PAD(width - realsz, blanks);
763
 
764
                /* finally, adjust ret */
765
                ret += width > realsz ? width : realsz;
766
 
767
                FLUSH();        /* copy out the I/O vectors */
768
        }
769
done:
770
  va_end (ap);
771
        FLUSH();
772
        return (ret);
773
        /* NOTREACHED */
774
}
775
 
776
#ifdef FLOATING_POINT
777
 
778
extern char *_dtoa_r _PARAMS((struct _reent *, double, int,
779
                              int, int *, int *, char **));
780
 
781
static char *
782
cvt(data, value, ndigits, flags, sign, decpt, ch, length)
783
        struct _reent *data;
784
        double value;
785
        int ndigits, flags, *decpt, ch, *length;
786
        char *sign;
787
{
788
        int mode, dsgn;
789
        char *digits, *bp, *rve;
790
        union double_union tmp;
791
 
792
        if (ch == 'f') {
793
                mode = 3;               /* ndigits after the decimal point */
794
        } else {
795
                /* To obtain ndigits after the decimal point for the 'e'
796
                 * and 'E' formats, round to ndigits + 1 significant
797
                 * figures.
798
                 */
799
                if (ch == 'e' || ch == 'E') {
800
                        ndigits++;
801
                }
802
                mode = 2;               /* ndigits significant digits */
803
        }
804
 
805
        tmp.d = value;
806
        if (word0(tmp) & Sign_bit) { /* this will check for < 0 and -0.0 */
807
                value = -value;
808
                *sign = '-';
809
        } else
810
                *sign = '\000';
811
        digits = _dtoa_r(data, value, mode, ndigits, decpt, &dsgn, &rve);
812
        if ((ch != 'g' && ch != 'G') || flags & ALT) {  /* Print trailing zeros */
813
                bp = digits + ndigits;
814
                if (ch == 'f') {
815
                        if (*digits == '0' && value)
816
                                *decpt = -ndigits + 1;
817
                        bp += *decpt;
818
                }
819
                if (value == 0)  /* kludge for __dtoa irregularity */
820
                        rve = bp;
821
                while (rve < bp)
822
                        *rve++ = '0';
823
        }
824
        *length = rve - digits;
825
        return (digits);
826
}
827
 
828
static int
829
exponent(p0, exp, fmtch)
830
        char *p0;
831
        int exp, fmtch;
832
{
833
        register char *p, *t;
834
        char expbuf[MAXEXP];
835
 
836
        p = p0;
837
        *p++ = fmtch;
838
        if (exp < 0) {
839
                exp = -exp;
840
                *p++ = '-';
841
        }
842
        else
843
                *p++ = '+';
844
        t = expbuf + MAXEXP;
845
        if (exp > 9) {
846
                do {
847
                        *--t = to_char(exp % 10);
848
                } while ((exp /= 10) > 9);
849
                *--t = to_char(exp);
850
                for (; t < expbuf + MAXEXP; *p++ = *t++);
851
        }
852
        else {
853
                *p++ = '0';
854
                *p++ = to_char(exp);
855
        }
856
        return (p - p0);
857
}
858
#endif /* FLOATING_POINT */

powered by: WebSVN 2.1.0

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