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

Subversion Repositories or1k

[/] [or1k/] [tags/] [first/] [mp3/] [sw/] [console-xess/] [cprintf.c] - Blame information for rev 1780

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

Line No. Rev Author Line
1 268 lampret
/*
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
#if defined(LIBC_SCCS) && !defined(lint)
147
/*static char *sccsid = "from: @(#)vfprintf.c   5.50 (Berkeley) 12/16/92";*/
148
static char *rcsid = "$Id: cprintf.c,v 1.1.1.1 2001-11-04 19:38:07 lampret Exp $";
149
#endif /* LIBC_SCCS and not lint */
150
 
151
/*
152
 * Actual printf innards.
153
 *
154
 * This code is large and complicated...
155
 */
156
 
157
#ifdef INTEGER_ONLY
158
#define VFPRINTF vfiprintf
159
#define _VFPRINTF_R _vfiprintf_r
160
#else
161
#define VFPRINTF vfprintf
162
#define _VFPRINTF_R _vfprintf_r
163
#define FLOATING_POINT
164
#endif
165
 
166
#define _NO_LONGLONG
167
#if defined WANT_PRINTF_LONG_LONG && defined __GNUC__
168
# undef _NO_LONGLONG
169
#endif
170
 
171
#include <stdarg.h>
172
 
173
#ifdef FLOATING_POINT
174
#include <locale.h>
175
#include <math.h>
176
#include "floatio.h"
177
 
178
#define BUF             (MAXEXP+MAXFRACT+1)     /* + decimal point */
179
#define DEFPREC         6
180
 
181
static char *cvt _PARAMS((struct _reent *, double, int, int, char *, int *, int, int *));
182
static int exponent _PARAMS((char *, int, int));
183
 
184
#else /* no FLOATING_POINT */
185
 
186
#define BUF             40
187
 
188
#endif /* FLOATING_POINT */
189
 
190
/*
191
 * Macros for converting digits to letters and vice versa
192
 */
193
#define to_digit(c)     ((c) - '0')
194
#define is_digit(c)     ((unsigned)to_digit(c) <= 9)
195
#define to_char(n)      ((n) + '0')
196
 
197
/*
198
 * Flags used during conversion.
199
 */
200
#define ALT             0x001           /* alternate form */
201
#define HEXPREFIX       0x002           /* add 0x or 0X prefix */
202
#define LADJUST         0x004           /* left adjustment */
203
#define LONGDBL         0x008           /* long double; unimplemented */
204
#define LONGINT         0x010           /* long integer */
205
#define QUADINT         0x020           /* quad integer */
206
#define SHORTINT        0x040           /* short integer */
207
#define ZEROPAD         0x080           /* zero (as opposed to blank) pad */
208
#define FPT             0x100           /* Floating point number */
209
 
210
        /*
211
         * Choose PADSIZE to trade efficiency vs. size.  If larger printf
212
         * fields occur frequently, increase PADSIZE and make the initialisers
213
         * below longer.
214
         */
215
#define PADSIZE 16              /* pad chunk size */
216
        static _CONST char blanks[PADSIZE] =
217
         {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
218
        static _CONST char zeroes[PADSIZE] =
219
         {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
220
 
221
inline void pc (_CONST char c) {
222
#ifdef OR1K
223
  put_char (c);
224
#else
225
  printf ("%c", c);
226
#endif
227
}
228
        /*
229
         * BEWARE, these `goto error' on error, and PAD uses `n'.
230
         */
231
inline void     PRINT(_CONST char *ptr, int len) {
232
  int i;
233
  for (i = 0; i < len; i++)
234
          pc(*(ptr++));
235
}
236
 
237
inline void PAD(int howmany, _CONST char *with) {
238
  int n;
239
        if ((n = howmany) > 0) {
240
                while (n > PADSIZE) {
241
                        PRINT(with, PADSIZE);
242
                        n -= PADSIZE;
243
                }
244
                PRINT(with, n);
245
        }
246
}
247
 
248
int cprintf(const char *fmt0,   ...)
249
{
250
        register char *fmt;     /* format string */
251
        register int ch;        /* character from fmt */
252
        int n, m;       /* handy integers (short term usage) */
253
        register char *cp;      /* handy char pointer (short term usage) */
254
        register struct __siov *iovp;/* for PRINT macro */
255
        register int flags;     /* flags as above */
256
        int ret;                /* return value accumulator */
257
        int width;              /* width from format (%8d), or 0 */
258
        int prec;               /* precision from format (%.3d), or -1 */
259
        char sign;              /* sign prefix (' ', '+', '-', or \0) */
260
        char wc;
261
        va_list ap;
262
 
263
#ifdef FLOATING_POINT
264
        char *decimal_point = localeconv()->decimal_point;
265
        char softsign;          /* temporary negative sign for floats */
266
        /*
267
         * Although it is natural to declare this double here, the
268
         * declaration causes gcc to save FP registers even when not
269
         * printing an FP number.  This results in surprising use
270
         * of FP registers to print integers or strings on at least the
271
         * PowerPC.  A more proper solution would be to move FP printing
272
         * to another file, but this does seem to work.
273
         */
274
#if 0
275
        double _double;         /* double precision arguments %[eEfgG] */
276
#else
277
                                /* double precision arguments %[eEfgG] */
278
        union { int i; double d; } _double_ = {0};
279
#define _double (_double_.d)
280
#endif
281
        int expt;               /* integer value of exponent */
282
        int expsize;            /* character count for expstr */
283
        int ndig;               /* actual number of digits returned by cvt */
284
        char expstr[7];         /* buffer for exponent string */
285
#endif
286
 
287
#ifndef _NO_LONGLONG
288
#define quad_t    long long
289
#define u_quad_t  unsigned long long
290
#endif
291
 
292
#ifndef _NO_LONGLONG
293
        u_quad_t _uquad;        /* integer arguments %[diouxX] */
294
#else
295
        u_long _uquad;
296
#endif
297
        enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
298
        int dprec;              /* a copy of prec if [diouxX], 0 otherwise */
299
        int realsz;             /* field size expanded by dprec */
300
        int size;               /* size of converted field or string */
301
        char *xdigs;            /* digits for [xX] conversion */
302
#define NIOV 8
303
 
304
char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
305
char ox[2];             /* space for 0x hex-prefix */
306
int state = 0;          /* mbtowc calls from library must not change state */
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
error:
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.