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