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

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [stdio/] [vfprintf.c] - Blame information for rev 39

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

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

powered by: WebSVN 2.1.0

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