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 57

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 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 56 joel
static char *rcsid = "$Id: vfprintf.c,v 1.1.1.2 2000-08-30 19:01:48 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
        double _double;         /* double precision arguments %[eEfgG] */
304
        int expt;               /* integer value of exponent */
305
        int expsize;            /* character count for expstr */
306
        int ndig;               /* actual number of digits returned by cvt */
307
        char expstr[7];         /* buffer for exponent string */
308
#endif
309
 
310
#ifndef _NO_LONGLONG
311
#define quad_t    long long
312
#define u_quad_t  unsigned long long
313
#endif
314
 
315
#ifndef _NO_LONGLONG
316
        u_quad_t _uquad;        /* integer arguments %[diouxX] */
317
#else
318
        u_long _uquad;
319
#endif
320
        enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
321
        int dprec;              /* a copy of prec if [diouxX], 0 otherwise */
322
        int realsz;             /* field size expanded by dprec */
323
        int size;               /* size of converted field or string */
324
        char *xdigs;            /* digits for [xX] conversion */
325
#define NIOV 8
326
        struct __suio uio;      /* output information: summary */
327
        struct __siov iov[NIOV];/* ... and individual io vectors */
328
        char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
329
        char ox[2];             /* space for 0x hex-prefix */
330
        int state = 0;          /* mbtowc calls from library must not change state */
331
 
332
        /*
333
         * Choose PADSIZE to trade efficiency vs. size.  If larger printf
334
         * fields occur frequently, increase PADSIZE and make the initialisers
335
         * below longer.
336
         */
337
#define PADSIZE 16              /* pad chunk size */
338 56 joel
        static _CONST char blanks[PADSIZE] =
339 39 lampret
         {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
340 56 joel
        static _CONST char zeroes[PADSIZE] =
341 39 lampret
         {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
342
 
343
        /*
344
         * BEWARE, these `goto error' on error, and PAD uses `n'.
345
         */
346
#define PRINT(ptr, len) { \
347
        iovp->iov_base = (ptr); \
348
        iovp->iov_len = (len); \
349
        uio.uio_resid += (len); \
350
        iovp++; \
351
        if (++uio.uio_iovcnt >= NIOV) { \
352
                if (__sprint(fp, &uio)) \
353
                        goto error; \
354
                iovp = iov; \
355
        } \
356
}
357
#define PAD(howmany, with) { \
358
        if ((n = (howmany)) > 0) { \
359
                while (n > PADSIZE) { \
360
                        PRINT(with, PADSIZE); \
361
                        n -= PADSIZE; \
362
                } \
363
                PRINT(with, n); \
364
        } \
365
}
366
#define FLUSH() { \
367
        if (uio.uio_resid && __sprint(fp, &uio)) \
368
                goto error; \
369
        uio.uio_iovcnt = 0; \
370
        iovp = iov; \
371
}
372
 
373
        /*
374
         * To extend shorts properly, we need both signed and unsigned
375
         * argument extraction methods.
376
         */
377
#ifndef _NO_LONGLONG
378
#define SARG() \
379
        (flags&QUADINT ? va_arg(ap, quad_t) : \
380
            flags&LONGINT ? va_arg(ap, long) : \
381
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
382
            (long)va_arg(ap, int))
383
#define UARG() \
384
        (flags&QUADINT ? va_arg(ap, u_quad_t) : \
385
            flags&LONGINT ? va_arg(ap, u_long) : \
386
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
387
            (u_long)va_arg(ap, u_int))
388
#else
389
#define SARG() \
390
        (flags&LONGINT ? va_arg(ap, long) : \
391
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
392
            (long)va_arg(ap, int))
393
#define UARG() \
394
        (flags&LONGINT ? va_arg(ap, u_long) : \
395
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
396
            (u_long)va_arg(ap, u_int))
397
#endif
398
 
399
        CHECK_INIT (fp);
400
 
401
        /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
402
        if (cantwrite(fp))
403
                return (EOF);
404
 
405
        /* optimise fprintf(stderr) (and other unbuffered Unix files) */
406
        if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
407
            fp->_file >= 0)
408
                return (__sbprintf(fp, fmt0, ap));
409
 
410
        fmt = (char *)fmt0;
411
        uio.uio_iov = iovp = iov;
412
        uio.uio_resid = 0;
413
        uio.uio_iovcnt = 0;
414
        ret = 0;
415
 
416
        /*
417
         * Scan the format for conversions (`%' character).
418
         */
419
        for (;;) {
420
                cp = fmt;
421
                while ((n = _mbtowc_r(_REENT, &wc, fmt, MB_CUR_MAX, &state)) > 0) {
422
                        fmt += n;
423
                        if (wc == '%') {
424
                                fmt--;
425
                                break;
426
                        }
427
                }
428
                if ((m = fmt - cp) != 0) {
429
                        PRINT(cp, m);
430
                        ret += m;
431
                }
432
                if (n <= 0)
433
                        goto done;
434
                fmt++;          /* skip over '%' */
435
 
436
                flags = 0;
437
                dprec = 0;
438
                width = 0;
439
                prec = -1;
440
                sign = '\0';
441
 
442
rflag:          ch = *fmt++;
443
reswitch:       switch (ch) {
444
                case ' ':
445
                        /*
446
                         * ``If the space and + flags both appear, the space
447
                         * flag will be ignored.''
448
                         *      -- ANSI X3J11
449
                         */
450
                        if (!sign)
451
                                sign = ' ';
452
                        goto rflag;
453
                case '#':
454
                        flags |= ALT;
455
                        goto rflag;
456
                case '*':
457
                        /*
458
                         * ``A negative field width argument is taken as a
459
                         * - flag followed by a positive field width.''
460
                         *      -- ANSI X3J11
461
                         * They don't exclude field widths read from args.
462
                         */
463
                        if ((width = va_arg(ap, int)) >= 0)
464
                                goto rflag;
465
                        width = -width;
466
                        /* FALLTHROUGH */
467
                case '-':
468
                        flags |= LADJUST;
469
                        goto rflag;
470
                case '+':
471
                        sign = '+';
472
                        goto rflag;
473
                case '.':
474
                        if ((ch = *fmt++) == '*') {
475
                                n = va_arg(ap, int);
476
                                prec = n < 0 ? -1 : n;
477
                                goto rflag;
478
                        }
479
                        n = 0;
480
                        while (is_digit(ch)) {
481
                                n = 10 * n + to_digit(ch);
482
                                ch = *fmt++;
483
                        }
484
                        prec = n < 0 ? -1 : n;
485
                        goto reswitch;
486
                case '0':
487
                        /*
488
                         * ``Note that 0 is taken as a flag, not as the
489
                         * beginning of a field width.''
490
                         *      -- ANSI X3J11
491
                         */
492
                        flags |= ZEROPAD;
493
                        goto rflag;
494
                case '1': case '2': case '3': case '4':
495
                case '5': case '6': case '7': case '8': case '9':
496
                        n = 0;
497
                        do {
498
                                n = 10 * n + to_digit(ch);
499
                                ch = *fmt++;
500
                        } while (is_digit(ch));
501
                        width = n;
502
                        goto reswitch;
503
#ifdef FLOATING_POINT
504
                case 'L':
505
                        flags |= LONGDBL;
506
                        goto rflag;
507
#endif
508
                case 'h':
509
                        flags |= SHORTINT;
510
                        goto rflag;
511
                case 'l':
512
                        if (*fmt == 'l') {
513
                                fmt++;
514
                                flags |= QUADINT;
515
                        } else {
516
                                flags |= LONGINT;
517
                        }
518
                        goto rflag;
519
                case 'q':
520
                        flags |= QUADINT;
521
                        goto rflag;
522
                case 'c':
523
                        *(cp = buf) = va_arg(ap, int);
524
                        size = 1;
525
                        sign = '\0';
526
                        break;
527
                case 'D':
528
                        flags |= LONGINT;
529
                        /*FALLTHROUGH*/
530
                case 'd':
531
                case 'i':
532
                        _uquad = SARG();
533
#ifndef _NO_LONGLONG
534
                        if ((quad_t)_uquad < 0)
535
#else
536
                        if ((long) _uquad < 0)
537
#endif
538
                        {
539
 
540
                                _uquad = -_uquad;
541
                                sign = '-';
542
                        }
543
                        base = DEC;
544
                        goto number;
545
#ifdef FLOATING_POINT
546
                case 'e':
547
                case 'E':
548
                case 'f':
549
                case 'g':
550
                case 'G':
551
                        if (prec == -1) {
552
                                prec = DEFPREC;
553
                        } else if ((ch == 'g' || ch == 'G') && prec == 0) {
554
                                prec = 1;
555
                        }
556
 
557
                        if (flags & LONGDBL) {
558
                                _double = (double) va_arg(ap, long double);
559
                        } else {
560
                                _double = va_arg(ap, double);
561
                        }
562
 
563
                        /* do this before tricky precision changes */
564
                        if (isinf(_double)) {
565
                                if (_double < 0)
566
                                        sign = '-';
567
                                cp = "Inf";
568
                                size = 3;
569
                                break;
570
                        }
571
                        if (isnan(_double)) {
572
                                cp = "NaN";
573
                                size = 3;
574
                                break;
575
                        }
576
 
577
                        flags |= FPT;
578
                        cp = cvt(data, _double, prec, flags, &softsign,
579
                                &expt, ch, &ndig);
580
                        if (ch == 'g' || ch == 'G') {
581
                                if (expt <= -4 || expt > prec)
582
                                        ch = (ch == 'g') ? 'e' : 'E';
583
                                else
584
                                        ch = 'g';
585
                        }
586
                        if (ch <= 'e') {        /* 'e' or 'E' fmt */
587
                                --expt;
588
                                expsize = exponent(expstr, expt, ch);
589
                                size = expsize + ndig;
590
                                if (ndig > 1 || flags & ALT)
591
                                        ++size;
592
                        } else if (ch == 'f') {         /* f fmt */
593
                                if (expt > 0) {
594
                                        size = expt;
595
                                        if (prec || flags & ALT)
596
                                                size += prec + 1;
597
                                } else  /* "0.X" */
598
                                        size = prec + 2;
599
                        } else if (expt >= ndig) {      /* fixed g fmt */
600
                                size = expt;
601
                                if (flags & ALT)
602
                                        ++size;
603
                        } else
604
                                size = ndig + (expt > 0 ?
605
                                        1 : 2 - expt);
606
 
607
                        if (softsign)
608
                                sign = '-';
609
                        break;
610
#endif /* FLOATING_POINT */
611
                case 'n':
612
#ifndef _NO_LONGLONG
613
                        if (flags & QUADINT)
614
                                *va_arg(ap, quad_t *) = ret;
615
                        else
616
#endif
617
                        if (flags & LONGINT)
618
                                *va_arg(ap, long *) = ret;
619
                        else if (flags & SHORTINT)
620
                                *va_arg(ap, short *) = ret;
621
                        else
622
                                *va_arg(ap, int *) = ret;
623
                        continue;       /* no output */
624
                case 'O':
625
                        flags |= LONGINT;
626
                        /*FALLTHROUGH*/
627
                case 'o':
628
                        _uquad = UARG();
629
                        base = OCT;
630
                        goto nosign;
631
                case 'p':
632
                        /*
633
                         * ``The argument shall be a pointer to void.  The
634
                         * value of the pointer is converted to a sequence
635
                         * of printable characters, in an implementation-
636
                         * defined manner.''
637
                         *      -- ANSI X3J11
638
                         */
639
                        /* NOSTRICT */
640
                        _uquad = (u_long)(unsigned _POINTER_INT)va_arg(ap, void *);
641
                        base = HEX;
642
                        xdigs = "0123456789abcdef";
643
                        flags |= HEXPREFIX;
644
                        ch = 'x';
645
                        goto nosign;
646
                case 's':
647
                        if ((cp = va_arg(ap, char *)) == NULL)
648
                                cp = "(null)";
649
                        if (prec >= 0) {
650
                                /*
651
                                 * can't use strlen; can only look for the
652
                                 * NUL in the first `prec' characters, and
653
                                 * strlen() will go further.
654
                                 */
655
                                char *p = memchr(cp, 0, prec);
656
 
657
                                if (p != NULL) {
658
                                        size = p - cp;
659
                                        if (size > prec)
660
                                                size = prec;
661
                                } else
662
                                        size = prec;
663
                        } else
664
                                size = strlen(cp);
665
                        sign = '\0';
666
                        break;
667
                case 'U':
668
                        flags |= LONGINT;
669
                        /*FALLTHROUGH*/
670
                case 'u':
671
                        _uquad = UARG();
672
                        base = DEC;
673
                        goto nosign;
674
                case 'X':
675
                        xdigs = "0123456789ABCDEF";
676
                        goto hex;
677
                case 'x':
678
                        xdigs = "0123456789abcdef";
679
hex:                    _uquad = UARG();
680
                        base = HEX;
681
                        /* leading 0x/X only if non-zero */
682
                        if (flags & ALT && _uquad != 0)
683
                                flags |= HEXPREFIX;
684
 
685
                        /* unsigned conversions */
686
nosign:                 sign = '\0';
687
                        /*
688
                         * ``... diouXx conversions ... if a precision is
689
                         * specified, the 0 flag will be ignored.''
690
                         *      -- ANSI X3J11
691
                         */
692
number:                 if ((dprec = prec) >= 0)
693
                                flags &= ~ZEROPAD;
694
 
695
                        /*
696
                         * ``The result of converting a zero value with an
697
                         * explicit precision of zero is no characters.''
698
                         *      -- ANSI X3J11
699
                         */
700
                        cp = buf + BUF;
701
                        if (_uquad != 0 || prec != 0) {
702
                                /*
703
                                 * Unsigned mod is hard, and unsigned mod
704
                                 * by a constant is easier than that by
705
                                 * a variable; hence this switch.
706
                                 */
707
                                switch (base) {
708
                                case OCT:
709
                                        do {
710
                                                *--cp = to_char(_uquad & 7);
711
                                                _uquad >>= 3;
712
                                        } while (_uquad);
713
                                        /* handle octal leading 0 */
714
                                        if (flags & ALT && *cp != '0')
715
                                                *--cp = '0';
716
                                        break;
717
 
718
                                case DEC:
719
                                        /* many numbers are 1 digit */
720
                                        while (_uquad >= 10) {
721
                                                *--cp = to_char(_uquad % 10);
722
                                                _uquad /= 10;
723
                                        }
724
                                        *--cp = to_char(_uquad);
725
                                        break;
726
 
727
                                case HEX:
728
                                        do {
729
                                                *--cp = xdigs[_uquad & 15];
730
                                                _uquad >>= 4;
731
                                        } while (_uquad);
732
                                        break;
733
 
734
                                default:
735
                                        cp = "bug in vfprintf: bad base";
736
                                        size = strlen(cp);
737
                                        goto skipsize;
738
                                }
739
                        }
740
                        size = buf + BUF - cp;
741
                skipsize:
742
                        break;
743
                default:        /* "%?" prints ?, unless ? is NUL */
744
                        if (ch == '\0')
745
                                goto done;
746
                        /* pretend it was %c with argument ch */
747
                        cp = buf;
748
                        *cp = ch;
749
                        size = 1;
750
                        sign = '\0';
751
                        break;
752
                }
753
 
754
                /*
755
                 * All reasonable formats wind up here.  At this point, `cp'
756
                 * points to a string which (if not flags&LADJUST) should be
757
                 * padded out to `width' places.  If flags&ZEROPAD, it should
758
                 * first be prefixed by any sign or other prefix; otherwise,
759
                 * it should be blank padded before the prefix is emitted.
760
                 * After any left-hand padding and prefixing, emit zeroes
761
                 * required by a decimal [diouxX] precision, then print the
762
                 * string proper, then emit zeroes required by any leftover
763
                 * floating precision; finally, if LADJUST, pad with blanks.
764
                 *
765
                 * Compute actual size, so we know how much to pad.
766
                 * size excludes decimal prec; realsz includes it.
767
                 */
768
                realsz = dprec > size ? dprec : size;
769
                if (sign)
770
                        realsz++;
771
                else if (flags & HEXPREFIX)
772
                        realsz+= 2;
773
 
774
                /* right-adjusting blank padding */
775
                if ((flags & (LADJUST|ZEROPAD)) == 0)
776
                        PAD(width - realsz, blanks);
777
 
778
                /* prefix */
779
                if (sign) {
780
                        PRINT(&sign, 1);
781
                } else if (flags & HEXPREFIX) {
782
                        ox[0] = '0';
783
                        ox[1] = ch;
784
                        PRINT(ox, 2);
785
                }
786
 
787
                /* right-adjusting zero padding */
788
                if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
789
                        PAD(width - realsz, zeroes);
790
 
791
                /* leading zeroes from decimal precision */
792
                PAD(dprec - size, zeroes);
793
 
794
                /* the string or number proper */
795
#ifdef FLOATING_POINT
796
                if ((flags & FPT) == 0) {
797
                        PRINT(cp, size);
798
                } else {        /* glue together f_p fragments */
799
                        if (ch >= 'f') {        /* 'f' or 'g' */
800
                                if (_double == 0) {
801
                                        /* kludge for __dtoa irregularity */
802
                                        PRINT("0", 1);
803
                                        if (expt < ndig || (flags & ALT) != 0) {
804
                                                PRINT(decimal_point, 1);
805
                                                PAD(ndig - 1, zeroes);
806
                                        }
807
                                } else if (expt <= 0) {
808
                                        PRINT("0", 1);
809
                                        PRINT(decimal_point, 1);
810
                                        PAD(-expt, zeroes);
811
                                        PRINT(cp, ndig);
812
                                } else if (expt >= ndig) {
813
                                        PRINT(cp, ndig);
814
                                        PAD(expt - ndig, zeroes);
815
                                        if (flags & ALT)
816
                                                PRINT(".", 1);
817
                                } else {
818
                                        PRINT(cp, expt);
819
                                        cp += expt;
820
                                        PRINT(".", 1);
821
                                        PRINT(cp, ndig-expt);
822
                                }
823
                        } else {        /* 'e' or 'E' */
824
                                if (ndig > 1 || flags & ALT) {
825
                                        ox[0] = *cp++;
826
                                        ox[1] = '.';
827
                                        PRINT(ox, 2);
828
                                        if (_double || flags & ALT == 0) {
829
                                                PRINT(cp, ndig-1);
830
                                        } else  /* 0.[0..] */
831
                                                /* __dtoa irregularity */
832
                                                PAD(ndig - 1, zeroes);
833
                                } else  /* XeYYY */
834
                                        PRINT(cp, 1);
835
                                PRINT(expstr, expsize);
836
                        }
837
                }
838
#else
839
                PRINT(cp, size);
840
#endif
841
                /* left-adjusting padding (always blank) */
842
                if (flags & LADJUST)
843
                        PAD(width - realsz, blanks);
844
 
845
                /* finally, adjust ret */
846
                ret += width > realsz ? width : realsz;
847
 
848
                FLUSH();        /* copy out the I/O vectors */
849
        }
850
done:
851
        FLUSH();
852
error:
853
        return (__sferror(fp) ? EOF : ret);
854
        /* NOTREACHED */
855
}
856
 
857
#ifdef FLOATING_POINT
858
 
859
extern char *_dtoa_r _PARAMS((struct _reent *, double, int,
860
                              int, int *, int *, char **));
861
 
862
static char *
863
cvt(data, value, ndigits, flags, sign, decpt, ch, length)
864
        struct _reent *data;
865
        double value;
866
        int ndigits, flags, *decpt, ch, *length;
867
        char *sign;
868
{
869
        int mode, dsgn;
870
        char *digits, *bp, *rve;
871 56 joel
        union double_union tmp;
872 39 lampret
 
873
        if (ch == 'f') {
874
                mode = 3;               /* ndigits after the decimal point */
875
        } else {
876
                /* To obtain ndigits after the decimal point for the 'e'
877
                 * and 'E' formats, round to ndigits + 1 significant
878
                 * figures.
879
                 */
880
                if (ch == 'e' || ch == 'E') {
881
                        ndigits++;
882
                }
883
                mode = 2;               /* ndigits significant digits */
884
        }
885
 
886 56 joel
        tmp.d = value;
887
        if (word0(tmp) & Sign_bit) { /* this will check for < 0 and -0.0 */
888 39 lampret
                value = -value;
889
                *sign = '-';
890 56 joel
        } else
891 39 lampret
                *sign = '\000';
892
        digits = _dtoa_r(data, value, mode, ndigits, decpt, &dsgn, &rve);
893
        if ((ch != 'g' && ch != 'G') || flags & ALT) {  /* Print trailing zeros */
894
                bp = digits + ndigits;
895
                if (ch == 'f') {
896
                        if (*digits == '0' && value)
897
                                *decpt = -ndigits + 1;
898
                        bp += *decpt;
899
                }
900
                if (value == 0)  /* kludge for __dtoa irregularity */
901
                        rve = bp;
902
                while (rve < bp)
903
                        *rve++ = '0';
904
        }
905
        *length = rve - digits;
906
        return (digits);
907
}
908
 
909
static int
910
exponent(p0, exp, fmtch)
911
        char *p0;
912
        int exp, fmtch;
913
{
914
        register char *p, *t;
915
        char expbuf[MAXEXP];
916
 
917
        p = p0;
918
        *p++ = fmtch;
919
        if (exp < 0) {
920
                exp = -exp;
921
                *p++ = '-';
922
        }
923
        else
924
                *p++ = '+';
925
        t = expbuf + MAXEXP;
926
        if (exp > 9) {
927
                do {
928
                        *--t = to_char(exp % 10);
929
                } while ((exp /= 10) > 9);
930
                *--t = to_char(exp);
931
                for (; t < expbuf + MAXEXP; *p++ = *t++);
932
        }
933
        else {
934
                *p++ = '0';
935
                *p++ = to_char(exp);
936
        }
937
        return (p - p0);
938
}
939
#endif /* FLOATING_POINT */

powered by: WebSVN 2.1.0

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