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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [vfprintf.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/*
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
#if defined(LIBC_SCCS) && !defined(lint)
137
/*static char *sccsid = "from: @(#)vfprintf.c   5.50 (Berkeley) 12/16/92";*/
138
static char *rcsid = "$Id: vfprintf.c,v 1.1 2002-08-23 22:31:18 ivang Exp $";
139
#endif /* LIBC_SCCS and not lint */
140
 
141
/*
142
 * Actual printf innards.
143
 *
144
 * This code is large and complicated...
145
 */
146
 
147
#ifdef INTEGER_ONLY
148
#define VFPRINTF vfiprintf
149
#define _VFPRINTF_R _vfiprintf_r
150
#else
151
#define VFPRINTF vfprintf
152
#define _VFPRINTF_R _vfprintf_r
153
#define FLOATING_POINT
154
#endif
155
 
156
#define _NO_LONGLONG
157
#if defined WANT_PRINTF_LONG_LONG && defined __GNUC__
158
# undef _NO_LONGLONG
159
#endif
160
 
161
#include <_ansi.h>
162
#include <stdio.h>
163
#include <stdlib.h>
164
#include <string.h>
165
#include <reent.h>
166
 
167
#ifdef _HAVE_STDC
168
#include <stdarg.h>
169
#else
170
#include <varargs.h>
171
#endif
172
 
173
#include "local.h"
174
#include "fvwrite.h"
175
#include "vfieeefp.h"
176
 
177
/* Currently a test is made to see if long double processing is warranted.
178
   This could be changed in the future should the _ldtoa_r code be
179
   preferred over _dtoa_r.  */
180
#define _NO_LONGDBL
181
#if defined WANT_IO_LONG_DBL && (LDBL_MANT_DIG > DBL_MANT_DIG)
182
#undef _NO_LONGDBL
183
#endif
184
 
185
/*
186
 * Flush out all the vectors defined by the given uio,
187
 * then reset it so that it can be reused.
188
 */
189
static int
190
__sprint(fp, uio)
191
        FILE *fp;
192
        register struct __suio *uio;
193
{
194
        register int err;
195
 
196
        if (uio->uio_resid == 0) {
197
                uio->uio_iovcnt = 0;
198
                return (0);
199
        }
200
        err = __sfvwrite(fp, uio);
201
        uio->uio_resid = 0;
202
        uio->uio_iovcnt = 0;
203
        return (err);
204
}
205
 
206
/*
207
 * Helper function for `fprintf to unbuffered unix file': creates a
208
 * temporary buffer.  We only work on write-only files; this avoids
209
 * worries about ungetc buffers and so forth.
210
 */
211
static int
212
__sbprintf(fp, fmt, ap)
213
        register FILE *fp;
214
        const char *fmt;
215
        va_list ap;
216
{
217
        int ret;
218
        FILE fake;
219
        unsigned char buf[BUFSIZ];
220
 
221
        /* copy the important variables */
222
        fake._data = fp->_data;
223
        fake._flags = fp->_flags & ~__SNBF;
224
        fake._file = fp->_file;
225
        fake._cookie = fp->_cookie;
226
        fake._write = fp->_write;
227
 
228
        /* set up the buffer */
229
        fake._bf._base = fake._p = buf;
230
        fake._bf._size = fake._w = sizeof(buf);
231
        fake._lbfsize = 0;       /* not actually used, but Just In Case */
232
 
233
        /* do the work, then copy any error status */
234
        ret = VFPRINTF(&fake, fmt, ap);
235
        if (ret >= 0 && fflush(&fake))
236
                ret = EOF;
237
        if (fake._flags & __SERR)
238
                fp->_flags |= __SERR;
239
        return (ret);
240
}
241
 
242
 
243
#ifdef FLOATING_POINT
244
#include <locale.h>
245
#include <math.h>
246
#include "floatio.h"
247
 
248
#define BUF             (MAXEXP+MAXFRACT+1)     /* + decimal point */
249
#define DEFPREC         6
250
 
251
#ifdef _NO_LONGDBL
252
static char *cvt _PARAMS((struct _reent *, double, int, int, char *, int *, int, int *));
253
#else
254
static char *cvt _PARAMS((struct _reent *, _LONG_DOUBLE, int, int, char *, int *, int, int *));
255
extern int  _ldcheck _PARAMS((_LONG_DOUBLE *));
256
#endif
257
 
258
static int exponent _PARAMS((char *, int, int));
259
 
260
#else /* no FLOATING_POINT */
261
 
262
#define BUF             40
263
 
264
#endif /* FLOATING_POINT */
265
 
266
 
267
/*
268
 * Macros for converting digits to letters and vice versa
269
 */
270
#define to_digit(c)     ((c) - '0')
271
#define is_digit(c)     ((unsigned)to_digit(c) <= 9)
272
#define to_char(n)      ((n) + '0')
273
 
274
/*
275
 * Flags used during conversion.
276
 */
277
#define ALT             0x001           /* alternate form */
278
#define HEXPREFIX       0x002           /* add 0x or 0X prefix */
279
#define LADJUST         0x004           /* left adjustment */
280
#define LONGDBL         0x008           /* long double */
281
#define LONGINT         0x010           /* long integer */
282
#ifndef _NO_LONGLONG
283
#define QUADINT         0x020           /* quad integer */
284
#else /* ifdef _NO_LONGLONG, make QUADINT equivalent to LONGINT, so
285
         that %lld behaves the same as %ld, not as %d, as expected if:
286
         sizeof (long long) = sizeof long > sizeof int  */
287
#define QUADINT         LONGINT
288
#endif
289
#define SHORTINT        0x040           /* short integer */
290
#define ZEROPAD         0x080           /* zero (as opposed to blank) pad */
291
#define FPT             0x100           /* Floating point number */
292
 
293
int
294
_DEFUN (VFPRINTF, (fp, fmt0, ap),
295
        FILE * fp _AND
296
        _CONST char *fmt0 _AND
297
        va_list ap)
298
{
299
  CHECK_INIT (fp);
300
  return _VFPRINTF_R (fp->_data, fp, fmt0, ap);
301
}
302
 
303
int
304
_DEFUN (_VFPRINTF_R, (data, fp, fmt0, ap),
305
        struct _reent *data _AND
306
        FILE * fp _AND
307
        _CONST char *fmt0 _AND
308
        va_list ap)
309
{
310
        register char *fmt;     /* format string */
311
        register int ch;        /* character from fmt */
312
        register int n, m;      /* handy integers (short term usage) */
313
        register char *cp;      /* handy char pointer (short term usage) */
314
        register struct __siov *iovp;/* for PRINT macro */
315
        register int flags;     /* flags as above */
316
        int ret;                /* return value accumulator */
317
        int width;              /* width from format (%8d), or 0 */
318
        int prec;               /* precision from format (%.3d), or -1 */
319
        char sign;              /* sign prefix (' ', '+', '-', or \0) */
320
        wchar_t wc;
321
#ifdef FLOATING_POINT
322
        char *decimal_point = localeconv()->decimal_point;
323
        char softsign;          /* temporary negative sign for floats */
324
#ifdef _NO_LONGDBL
325
        union { int i; double d; } _double_ = {0};
326
        #define _fpvalue (_double_.d)
327
#else
328
        union { int i; _LONG_DOUBLE ld; } _long_double_ = {0};
329
        #define _fpvalue (_long_double_.ld)
330
        int tmp;
331
#endif
332
        int expt;               /* integer value of exponent */
333
        int expsize = 0; /* character count for expstr */
334
        int ndig;               /* actual number of digits returned by cvt */
335
        char expstr[7];         /* buffer for exponent string */
336
#endif
337
 
338
#ifndef _NO_LONGLONG
339
#define quad_t    long long
340
#define u_quad_t  unsigned long long
341
#endif
342
 
343
#ifndef _NO_LONGLONG
344
        u_quad_t _uquad;        /* integer arguments %[diouxX] */
345
#else
346
        u_long _uquad;
347
#endif
348
        enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
349
        int dprec;              /* a copy of prec if [diouxX], 0 otherwise */
350
        int realsz;             /* field size expanded by dprec */
351
        int size;               /* size of converted field or string */
352
        char *xdigs = NULL;     /* digits for [xX] conversion */
353
#define NIOV 8
354
        struct __suio uio;      /* output information: summary */
355
        struct __siov iov[NIOV];/* ... and individual io vectors */
356
        char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
357
        char ox[2];             /* space for 0x hex-prefix */
358
        int state = 0;          /* mbtowc calls from library must not change state */
359
 
360
        /*
361
         * Choose PADSIZE to trade efficiency vs. size.  If larger printf
362
         * fields occur frequently, increase PADSIZE and make the initialisers
363
         * below longer.
364
         */
365
#define PADSIZE 16              /* pad chunk size */
366
        static _CONST char blanks[PADSIZE] =
367
         {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
368
        static _CONST char zeroes[PADSIZE] =
369
         {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
370
 
371
        /*
372
         * BEWARE, these `goto error' on error, and PAD uses `n'.
373
         */
374
#define PRINT(ptr, len) { \
375
        iovp->iov_base = (ptr); \
376
        iovp->iov_len = (len); \
377
        uio.uio_resid += (len); \
378
        iovp++; \
379
        if (++uio.uio_iovcnt >= NIOV) { \
380
                if (__sprint(fp, &uio)) \
381
                        goto error; \
382
                iovp = iov; \
383
        } \
384
}
385
#define PAD(howmany, with) { \
386
        if ((n = (howmany)) > 0) { \
387
                while (n > PADSIZE) { \
388
                        PRINT(with, PADSIZE); \
389
                        n -= PADSIZE; \
390
                } \
391
                PRINT(with, n); \
392
        } \
393
}
394
#define FLUSH() { \
395
        if (uio.uio_resid && __sprint(fp, &uio)) \
396
                goto error; \
397
        uio.uio_iovcnt = 0; \
398
        iovp = iov; \
399
}
400
 
401
        /*
402
         * To extend shorts properly, we need both signed and unsigned
403
         * argument extraction methods.
404
         */
405
#ifndef _NO_LONGLONG
406
#define SARG() \
407
        (flags&QUADINT ? va_arg(ap, quad_t) : \
408
            flags&LONGINT ? va_arg(ap, long) : \
409
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
410
            (long)va_arg(ap, int))
411
#define UARG() \
412
        (flags&QUADINT ? va_arg(ap, u_quad_t) : \
413
            flags&LONGINT ? va_arg(ap, u_long) : \
414
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
415
            (u_long)va_arg(ap, u_int))
416
#else
417
#define SARG() \
418
        (flags&LONGINT ? va_arg(ap, long) : \
419
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
420
            (long)va_arg(ap, int))
421
#define UARG() \
422
        (flags&LONGINT ? va_arg(ap, u_long) : \
423
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
424
            (u_long)va_arg(ap, u_int))
425
#endif
426
 
427
        /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
428
        if (cantwrite(fp))
429
                return (EOF);
430
 
431
        /* optimise fprintf(stderr) (and other unbuffered Unix files) */
432
        if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
433
            fp->_file >= 0)
434
                return (__sbprintf(fp, fmt0, ap));
435
 
436
        fmt = (char *)fmt0;
437
        uio.uio_iov = iovp = iov;
438
        uio.uio_resid = 0;
439
        uio.uio_iovcnt = 0;
440
        ret = 0;
441
 
442
        /*
443
         * Scan the format for conversions (`%' character).
444
         */
445
        for (;;) {
446
                cp = fmt;
447
                while ((n = _mbtowc_r(_REENT, &wc, fmt, MB_CUR_MAX, &state)) > 0) {
448
                        fmt += n;
449
                        if (wc == '%') {
450
                                fmt--;
451
                                break;
452
                        }
453
                }
454
                if ((m = fmt - cp) != 0) {
455
                        PRINT(cp, m);
456
                        ret += m;
457
                }
458
                if (n <= 0)
459
                        goto done;
460
                fmt++;          /* skip over '%' */
461
 
462
                flags = 0;
463
                dprec = 0;
464
                width = 0;
465
                prec = -1;
466
                sign = '\0';
467
 
468
rflag:          ch = *fmt++;
469
reswitch:       switch (ch) {
470
                case ' ':
471
                        /*
472
                         * ``If the space and + flags both appear, the space
473
                         * flag will be ignored.''
474
                         *      -- ANSI X3J11
475
                         */
476
                        if (!sign)
477
                                sign = ' ';
478
                        goto rflag;
479
                case '#':
480
                        flags |= ALT;
481
                        goto rflag;
482
                case '*':
483
                        /*
484
                         * ``A negative field width argument is taken as a
485
                         * - flag followed by a positive field width.''
486
                         *      -- ANSI X3J11
487
                         * They don't exclude field widths read from args.
488
                         */
489
                        if ((width = va_arg(ap, int)) >= 0)
490
                                goto rflag;
491
                        width = -width;
492
                        /* FALLTHROUGH */
493
                case '-':
494
                        flags |= LADJUST;
495
                        goto rflag;
496
                case '+':
497
                        sign = '+';
498
                        goto rflag;
499
                case '.':
500
                        if ((ch = *fmt++) == '*') {
501
                                n = va_arg(ap, int);
502
                                prec = n < 0 ? -1 : n;
503
                                goto rflag;
504
                        }
505
                        n = 0;
506
                        while (is_digit(ch)) {
507
                                n = 10 * n + to_digit(ch);
508
                                ch = *fmt++;
509
                        }
510
                        prec = n < 0 ? -1 : n;
511
                        goto reswitch;
512
                case '0':
513
                        /*
514
                         * ``Note that 0 is taken as a flag, not as the
515
                         * beginning of a field width.''
516
                         *      -- ANSI X3J11
517
                         */
518
                        flags |= ZEROPAD;
519
                        goto rflag;
520
                case '1': case '2': case '3': case '4':
521
                case '5': case '6': case '7': case '8': case '9':
522
                        n = 0;
523
                        do {
524
                                n = 10 * n + to_digit(ch);
525
                                ch = *fmt++;
526
                        } while (is_digit(ch));
527
                        width = n;
528
                        goto reswitch;
529
#ifdef FLOATING_POINT
530
                case 'L':
531
                        flags |= LONGDBL;
532
                        goto rflag;
533
#endif
534
                case 'h':
535
                        flags |= SHORTINT;
536
                        goto rflag;
537
                case 'l':
538
                        if (*fmt == 'l') {
539
                                fmt++;
540
                                flags |= QUADINT;
541
                        } else {
542
                                flags |= LONGINT;
543
                        }
544
                        goto rflag;
545
                case 'q':
546
                        flags |= QUADINT;
547
                        goto rflag;
548
                case 'c':
549
                        *(cp = buf) = va_arg(ap, int);
550
                        size = 1;
551
                        sign = '\0';
552
                        break;
553
                case 'D':
554
                        flags |= LONGINT;
555
                        /*FALLTHROUGH*/
556
                case 'd':
557
                case 'i':
558
                        _uquad = SARG();
559
#ifndef _NO_LONGLONG
560
                        if ((quad_t)_uquad < 0)
561
#else
562
                        if ((long) _uquad < 0)
563
#endif
564
                        {
565
 
566
                                _uquad = -_uquad;
567
                                sign = '-';
568
                        }
569
                        base = DEC;
570
                        goto number;
571
#ifdef FLOATING_POINT
572
                case 'e':
573
                case 'E':
574
                case 'f':
575
                case 'g':
576
                case 'G':
577
                        if (prec == -1) {
578
                                prec = DEFPREC;
579
                        } else if ((ch == 'g' || ch == 'G') && prec == 0) {
580
                                prec = 1;
581
                        }
582
 
583
#ifdef _NO_LONGDBL
584
                        if (flags & LONGDBL) {
585
                                _fpvalue = (double) va_arg(ap, _LONG_DOUBLE);
586
                        } else {
587
                                _fpvalue = va_arg(ap, double);
588
                        }
589
 
590
                        /* do this before tricky precision changes */
591
                        if (isinf(_fpvalue)) {
592
                                if (_fpvalue < 0)
593
                                        sign = '-';
594
                                cp = "Inf";
595
                                size = 3;
596
                                break;
597
                        }
598
                        if (isnan(_fpvalue)) {
599
                                cp = "NaN";
600
                                size = 3;
601
                                break;
602
                        }
603
 
604
#else /* !_NO_LONGDBL */
605
 
606
                        if (flags & LONGDBL) {
607
                                _fpvalue = va_arg(ap, _LONG_DOUBLE);
608
                        } else {
609
                                _fpvalue = (_LONG_DOUBLE)va_arg(ap, double);
610
                        }
611
 
612
                        /* do this before tricky precision changes */
613
                        tmp = _ldcheck (&_fpvalue);
614
                        if (tmp == 2) {
615
                                if (_fpvalue < 0)
616
                                        sign = '-';
617
                                cp = "Inf";
618
                                size = 3;
619
                                break;
620
                        }
621
                        if (tmp == 1) {
622
                                cp = "NaN";
623
                                size = 3;
624
                                break;
625
                        }
626
#endif /* !_NO_LONGDBL */
627
 
628
                        flags |= FPT;
629
 
630
                        cp = cvt(data, _fpvalue, prec, flags, &softsign,
631
                                &expt, ch, &ndig);
632
 
633
                        if (ch == 'g' || ch == 'G') {
634
                                if (expt <= -4 || expt > prec)
635
                                        ch = (ch == 'g') ? 'e' : 'E';
636
                                else
637
                                        ch = 'g';
638
                        }
639
                        if (ch <= 'e') {        /* 'e' or 'E' fmt */
640
                                --expt;
641
                                expsize = exponent(expstr, expt, ch);
642
                                size = expsize + ndig;
643
                                if (ndig > 1 || flags & ALT)
644
                                        ++size;
645
                        } else if (ch == 'f') {         /* f fmt */
646
                                if (expt > 0) {
647
                                        size = expt;
648
                                        if (prec || flags & ALT)
649
                                                size += prec + 1;
650
                                } else  /* "0.X" */
651
                                        size = prec + 2;
652
                        } else if (expt >= ndig) {      /* fixed g fmt */
653
                                size = expt;
654
                                if (flags & ALT)
655
                                        ++size;
656
                        } else
657
                                size = ndig + (expt > 0 ?
658
                                        1 : 2 - expt);
659
 
660
                        if (softsign)
661
                                sign = '-';
662
                        break;
663
#endif /* FLOATING_POINT */
664
                case 'n':
665
#ifndef _NO_LONGLONG
666
                        if (flags & QUADINT)
667
                                *va_arg(ap, quad_t *) = ret;
668
                        else
669
#endif
670
                        if (flags & LONGINT)
671
                                *va_arg(ap, long *) = ret;
672
                        else if (flags & SHORTINT)
673
                                *va_arg(ap, short *) = ret;
674
                        else
675
                                *va_arg(ap, int *) = ret;
676
                        continue;       /* no output */
677
                case 'O':
678
                        flags |= LONGINT;
679
                        /*FALLTHROUGH*/
680
                case 'o':
681
                        _uquad = UARG();
682
                        base = OCT;
683
                        goto nosign;
684
                case 'p':
685
                        /*
686
                         * ``The argument shall be a pointer to void.  The
687
                         * value of the pointer is converted to a sequence
688
                         * of printable characters, in an implementation-
689
                         * defined manner.''
690
                         *      -- ANSI X3J11
691
                         */
692
                        /* NOSTRICT */
693
                        _uquad = (u_long)(unsigned _POINTER_INT)va_arg(ap, void *);
694
                        base = HEX;
695
                        xdigs = "0123456789abcdef";
696
                        flags |= HEXPREFIX;
697
                        ch = 'x';
698
                        goto nosign;
699
                case 's':
700
                        if ((cp = va_arg(ap, char *)) == NULL)
701
                                cp = "(null)";
702
                        if (prec >= 0) {
703
                                /*
704
                                 * can't use strlen; can only look for the
705
                                 * NUL in the first `prec' characters, and
706
                                 * strlen() will go further.
707
                                 */
708
                                char *p = memchr(cp, 0, prec);
709
 
710
                                if (p != NULL) {
711
                                        size = p - cp;
712
                                        if (size > prec)
713
                                                size = prec;
714
                                } else
715
                                        size = prec;
716
                        } else
717
                                size = strlen(cp);
718
                        sign = '\0';
719
                        break;
720
                case 'U':
721
                        flags |= LONGINT;
722
                        /*FALLTHROUGH*/
723
                case 'u':
724
                        _uquad = UARG();
725
                        base = DEC;
726
                        goto nosign;
727
                case 'X':
728
                        xdigs = "0123456789ABCDEF";
729
                        goto hex;
730
                case 'x':
731
                        xdigs = "0123456789abcdef";
732
hex:                    _uquad = UARG();
733
                        base = HEX;
734
                        /* leading 0x/X only if non-zero */
735
                        if (flags & ALT && _uquad != 0)
736
                                flags |= HEXPREFIX;
737
 
738
                        /* unsigned conversions */
739
nosign:                 sign = '\0';
740
                        /*
741
                         * ``... diouXx conversions ... if a precision is
742
                         * specified, the 0 flag will be ignored.''
743
                         *      -- ANSI X3J11
744
                         */
745
number:                 if ((dprec = prec) >= 0)
746
                                flags &= ~ZEROPAD;
747
 
748
                        /*
749
                         * ``The result of converting a zero value with an
750
                         * explicit precision of zero is no characters.''
751
                         *      -- ANSI X3J11
752
                         */
753
                        cp = buf + BUF;
754
                        if (_uquad != 0 || prec != 0) {
755
                                /*
756
                                 * Unsigned mod is hard, and unsigned mod
757
                                 * by a constant is easier than that by
758
                                 * a variable; hence this switch.
759
                                 */
760
                                switch (base) {
761
                                case OCT:
762
                                        do {
763
                                                *--cp = to_char(_uquad & 7);
764
                                                _uquad >>= 3;
765
                                        } while (_uquad);
766
                                        /* handle octal leading 0 */
767
                                        if (flags & ALT && *cp != '0')
768
                                                *--cp = '0';
769
                                        break;
770
 
771
                                case DEC:
772
                                        /* many numbers are 1 digit */
773
                                        while (_uquad >= 10) {
774
                                                *--cp = to_char(_uquad % 10);
775
                                                _uquad /= 10;
776
                                        }
777
                                        *--cp = to_char(_uquad);
778
                                        break;
779
 
780
                                case HEX:
781
                                        do {
782
                                                *--cp = xdigs[_uquad & 15];
783
                                                _uquad >>= 4;
784
                                        } while (_uquad);
785
                                        break;
786
 
787
                                default:
788
                                        cp = "bug in vfprintf: bad base";
789
                                        size = strlen(cp);
790
                                        goto skipsize;
791
                                }
792
                        }
793
                       /*
794
                        * ...result is to be converted to an 'alternate form'.
795
                        * For o conversion, it increases the precision to force
796
                        * the first digit of the result to be a zero."
797
                        *     -- ANSI X3J11
798
                        *
799
                        * To demonstrate this case, compile and run:
800
                        *    printf ("%#.0o",0);
801
                        */
802
                       else if (base == OCT && (flags & ALT))
803
                         *--cp = '0';
804
 
805
                        size = buf + BUF - cp;
806
                skipsize:
807
                        break;
808
                default:        /* "%?" prints ?, unless ? is NUL */
809
                        if (ch == '\0')
810
                                goto done;
811
                        /* pretend it was %c with argument ch */
812
                        cp = buf;
813
                        *cp = ch;
814
                        size = 1;
815
                        sign = '\0';
816
                        break;
817
                }
818
 
819
                /*
820
                 * All reasonable formats wind up here.  At this point, `cp'
821
                 * points to a string which (if not flags&LADJUST) should be
822
                 * padded out to `width' places.  If flags&ZEROPAD, it should
823
                 * first be prefixed by any sign or other prefix; otherwise,
824
                 * it should be blank padded before the prefix is emitted.
825
                 * After any left-hand padding and prefixing, emit zeroes
826
                 * required by a decimal [diouxX] precision, then print the
827
                 * string proper, then emit zeroes required by any leftover
828
                 * floating precision; finally, if LADJUST, pad with blanks.
829
                 *
830
                 * Compute actual size, so we know how much to pad.
831
                 * size excludes decimal prec; realsz includes it.
832
                 */
833
                realsz = dprec > size ? dprec : size;
834
                if (sign)
835
                        realsz++;
836
                else if (flags & HEXPREFIX)
837
                        realsz+= 2;
838
 
839
                /* right-adjusting blank padding */
840
                if ((flags & (LADJUST|ZEROPAD)) == 0)
841
                        PAD(width - realsz, blanks);
842
 
843
                /* prefix */
844
                if (sign) {
845
                        PRINT(&sign, 1);
846
                } else if (flags & HEXPREFIX) {
847
                        ox[0] = '0';
848
                        ox[1] = ch;
849
                        PRINT(ox, 2);
850
                }
851
 
852
                /* right-adjusting zero padding */
853
                if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
854
                        PAD(width - realsz, zeroes);
855
 
856
                /* leading zeroes from decimal precision */
857
                PAD(dprec - size, zeroes);
858
 
859
                /* the string or number proper */
860
#ifdef FLOATING_POINT
861
                if ((flags & FPT) == 0) {
862
                        PRINT(cp, size);
863
                } else {        /* glue together f_p fragments */
864
                        if (ch >= 'f') {        /* 'f' or 'g' */
865
                                if (_fpvalue == 0) {
866
                                        /* kludge for __dtoa irregularity */
867
                                        PRINT("0", 1);
868
                                        if (expt < ndig || (flags & ALT) != 0) {
869
                                                PRINT(decimal_point, 1);
870
                                                PAD(ndig - 1, zeroes);
871
                                        }
872
                                } else if (expt <= 0) {
873
                                        PRINT("0", 1);
874
                                        if(expt || ndig) {
875
                                                PRINT(decimal_point, 1);
876
                                                PAD(-expt, zeroes);
877
                                                PRINT(cp, ndig);
878
                                        }
879
                                } else if (expt >= ndig) {
880
                                        PRINT(cp, ndig);
881
                                        PAD(expt - ndig, zeroes);
882
                                        if (flags & ALT)
883
                                                PRINT(".", 1);
884
                                } else {
885
                                        PRINT(cp, expt);
886
                                        cp += expt;
887
                                        PRINT(".", 1);
888
                                        PRINT(cp, ndig-expt);
889
                                }
890
                        } else {        /* 'e' or 'E' */
891
                                if (ndig > 1 || flags & ALT) {
892
                                        ox[0] = *cp++;
893
                                        ox[1] = '.';
894
                                        PRINT(ox, 2);
895
                                       if (_fpvalue) {
896
                                                PRINT(cp, ndig-1);
897
                                        } else  /* 0.[0..] */
898
                                                /* __dtoa irregularity */
899
                                                PAD(ndig - 1, zeroes);
900
                                } else  /* XeYYY */
901
                                        PRINT(cp, 1);
902
                                PRINT(expstr, expsize);
903
                        }
904
                }
905
#else
906
                PRINT(cp, size);
907
#endif
908
                /* left-adjusting padding (always blank) */
909
                if (flags & LADJUST)
910
                        PAD(width - realsz, blanks);
911
 
912
                /* finally, adjust ret */
913
                ret += width > realsz ? width : realsz;
914
 
915
                FLUSH();        /* copy out the I/O vectors */
916
        }
917
done:
918
        FLUSH();
919
error:
920
        return (__sferror(fp) ? EOF : ret);
921
        /* NOTREACHED */
922
}
923
 
924
#ifdef FLOATING_POINT
925
 
926
#ifdef _NO_LONGDBL
927
extern char *_dtoa_r _PARAMS((struct _reent *, double, int,
928
                              int, int *, int *, char **));
929
#else
930
extern char *_ldtoa_r _PARAMS((struct _reent *, _LONG_DOUBLE, int,
931
                              int, int *, int *, char **));
932
#undef word0
933
#define word0(x) ldword0(x)
934
#endif
935
 
936
static char *
937
cvt(data, value, ndigits, flags, sign, decpt, ch, length)
938
        struct _reent *data;
939
#ifdef _NO_LONGDBL
940
        double value;
941
#else
942
        _LONG_DOUBLE value;
943
#endif
944
        int ndigits, flags, *decpt, ch, *length;
945
        char *sign;
946
{
947
        int mode, dsgn;
948
        char *digits, *bp, *rve;
949
#ifdef _NO_LONGDBL
950
        union double_union tmp;
951
#else
952
        struct ldieee *ldptr;
953
#endif
954
 
955
        if (ch == 'f') {
956
                mode = 3;               /* ndigits after the decimal point */
957
        } else {
958
                /* To obtain ndigits after the decimal point for the 'e'
959
                 * and 'E' formats, round to ndigits + 1 significant
960
                 * figures.
961
                 */
962
                if (ch == 'e' || ch == 'E') {
963
                        ndigits++;
964
                }
965
                mode = 2;               /* ndigits significant digits */
966
        }
967
 
968
#ifdef _NO_LONGDBL
969
        tmp.d = value;
970
 
971
        if (word0(tmp) & Sign_bit) { /* this will check for < 0 and -0.0 */
972
                value = -value;
973
                *sign = '-';
974
        } else
975
                *sign = '\000';
976
 
977
        digits = _dtoa_r(data, value, mode, ndigits, decpt, &dsgn, &rve);
978
#else /* !_NO_LONGDBL */
979
        ldptr = (struct ldieee *)&value;
980
        if (ldptr->sign) { /* this will check for < 0 and -0.0 */
981
                value = -value;
982
                *sign = '-';
983
        } else
984
                *sign = '\000';
985
 
986
        digits = _ldtoa_r(data, value, mode, ndigits, decpt, &dsgn, &rve);
987
#endif /* !_NO_LONGDBL */
988
 
989
        if ((ch != 'g' && ch != 'G') || flags & ALT) {  /* Print trailing zeros */
990
                bp = digits + ndigits;
991
                if (ch == 'f') {
992
                        if (*digits == '0' && value)
993
                                *decpt = -ndigits + 1;
994
                        bp += *decpt;
995
                }
996
                if (value == 0)  /* kludge for __dtoa irregularity */
997
                        rve = bp;
998
                while (rve < bp)
999
                        *rve++ = '0';
1000
        }
1001
        *length = rve - digits;
1002
        return (digits);
1003
}
1004
 
1005
static int
1006
exponent(p0, exp, fmtch)
1007
        char *p0;
1008
        int exp, fmtch;
1009
{
1010
        register char *p, *t;
1011
        char expbuf[40];
1012
 
1013
        p = p0;
1014
        *p++ = fmtch;
1015
        if (exp < 0) {
1016
                exp = -exp;
1017
                *p++ = '-';
1018
        }
1019
        else
1020
                *p++ = '+';
1021
        t = expbuf + 40;
1022
        if (exp > 9) {
1023
                do {
1024
                        *--t = to_char(exp % 10);
1025
                } while ((exp /= 10) > 9);
1026
                *--t = to_char(exp);
1027
                for (; t < expbuf + 40; *p++ = *t++);
1028
        }
1029
        else {
1030
                *p++ = '0';
1031
                *p++ = to_char(exp);
1032
        }
1033
        return (p - p0);
1034
}
1035
#endif /* FLOATING_POINT */

powered by: WebSVN 2.1.0

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