~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mit-pthreads/stdio/vfprintf.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

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