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