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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [newlib/] [newlib/] [libc/] [stdio/] [vfprintf.c] - Blame information for rev 1782

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

powered by: WebSVN 2.1.0

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