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