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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [common/] [cprintf.c] - Blame information for rev 731

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

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

powered by: WebSVN 2.1.0

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