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