OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [ucos-ii/] [2.91/] [common/] [cprintf.c] - Blame information for rev 495

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

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

powered by: WebSVN 2.1.0

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