Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /*- |
2 | * Copyright (c) 1990, 1993 |
||
3 | * The Regents of the University of California. 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 | /* MODIFIED: |
||
38 | * --added include <ll/math.h> |
||
39 | * --initialization to prevent warnings |
||
40 | */ |
||
41 | |||
42 | #if defined(LIBC_SCCS) && !defined(lint) |
||
43 | #if 0 |
||
44 | static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; |
||
45 | #endif |
||
46 | static const char rcsid[] = |
||
47 | "$\Id: vfprintf.c,v 1.9 1996/06/22 10:34:02 jraynard Exp $"; |
||
48 | #endif /* LIBC_SCCS and not lint */ |
||
49 | |||
50 | /* |
||
51 | * Actual printf innards. |
||
52 | * |
||
53 | * This code is large and complicated... |
||
54 | */ |
||
55 | |||
56 | #include <sys/types.h> |
||
57 | |||
58 | #include <limits.h> |
||
59 | #include <stdio.h> |
||
60 | #include <stdlib.h> |
||
61 | #include <string.h> |
||
62 | |||
63 | #if __STDC__ |
||
64 | #include <stdarg.h> |
||
65 | #else |
||
66 | #include <varargs.h> |
||
67 | #endif |
||
68 | |||
69 | #include "local.h" |
||
70 | #include "fvwrite.h" |
||
71 | #ifdef _THREAD_SAFE |
||
72 | #include <pthread.h> |
||
73 | #include "pthread_private.h" |
||
74 | #endif |
||
75 | |||
76 | /* Define FLOATING_POINT to get floating point. */ |
||
77 | #define FLOATING_POINT |
||
78 | |||
79 | /* MG */ |
||
80 | #ifdef FLOATING_POINT |
||
81 | # if defined(_ANSI_SOURCE) |
||
82 | # undef _ANSI_SOURCE |
||
83 | # if defined(_POSIX_SOURCE) |
||
84 | # undef _POSIX_SOURCE |
||
85 | # include <ll/math.h> |
||
86 | # define _POSIX_SOURCE |
||
87 | # else |
||
88 | # include <ll/math.h> |
||
89 | # endif |
||
90 | # define _ANSI_SOURCE |
||
91 | # elif defined(_POSIX_SOURCE) |
||
92 | # undef _POSIX_SOURCE |
||
93 | # include <ll/math.h> |
||
94 | # define _POSIX_SOURCE |
||
95 | # else |
||
96 | # include <ll/math.h> |
||
97 | # endif |
||
98 | #endif |
||
99 | |||
100 | static int __sprint __P((FILE *, struct __suio *)); |
||
101 | static int __sbprintf __P((FILE *, const char *, va_list)); |
||
102 | static char * __ultoa __P((u_long, char *, int, int, char *)); |
||
103 | static char * __uqtoa __P((u_quad_t, char *, int, int, char *)); |
||
104 | |||
105 | /* |
||
106 | * Flush out all the vectors defined by the given uio, |
||
107 | * then reset it so that it can be reused. |
||
108 | */ |
||
109 | static int |
||
110 | __sprint(fp, uio) |
||
111 | FILE *fp; |
||
112 | register struct __suio *uio; |
||
113 | { |
||
114 | register int err; |
||
115 | |||
116 | if (uio->uio_resid == 0) { |
||
117 | uio->uio_iovcnt = 0; |
||
118 | return (0); |
||
119 | } |
||
120 | err = __sfvwrite(fp, uio); |
||
121 | uio->uio_resid = 0; |
||
122 | uio->uio_iovcnt = 0; |
||
123 | return (err); |
||
124 | } |
||
125 | |||
126 | /* |
||
127 | * Helper function for `fprintf to unbuffered unix file': creates a |
||
128 | * temporary buffer. We only work on write-only files; this avoids |
||
129 | * worries about ungetc buffers and so forth. |
||
130 | */ |
||
131 | static int |
||
132 | __sbprintf(fp, fmt, ap) |
||
133 | register FILE *fp; |
||
134 | const char *fmt; |
||
135 | va_list ap; |
||
136 | { |
||
137 | int ret; |
||
138 | FILE fake; |
||
139 | unsigned char buf[BUFSIZ]; |
||
140 | |||
141 | /* copy the important variables */ |
||
142 | fake._flags = fp->_flags & ~__SNBF; |
||
143 | fake._file = fp->_file; |
||
144 | fake._cookie = fp->_cookie; |
||
145 | fake._write = fp->_write; |
||
146 | |||
147 | /* set up the buffer */ |
||
148 | fake._bf._base = fake._p = buf; |
||
149 | fake._bf._size = fake._w = sizeof(buf); |
||
150 | fake._lbfsize = 0; /* not actually used, but Just In Case */ |
||
151 | |||
152 | /* do the work, then copy any error status */ |
||
153 | ret = vfprintf(&fake, fmt, ap); |
||
154 | if (ret >= 0 && fflush(&fake)) |
||
155 | ret = EOF; |
||
156 | if (fake._flags & __SERR) |
||
157 | fp->_flags |= __SERR; |
||
158 | return (ret); |
||
159 | } |
||
160 | |||
161 | /* |
||
162 | * Macros for converting digits to letters and vice versa |
||
163 | */ |
||
164 | #define to_digit(c) ((c) - '0') |
||
165 | #define is_digit(c) ((unsigned)to_digit(c) <= 9) |
||
166 | #define to_char(n) ((n) + '0') |
||
167 | |||
168 | /* |
||
169 | * Convert an unsigned long to ASCII for printf purposes, returning |
||
170 | * a pointer to the first character of the string representation. |
||
171 | * Octal numbers can be forced to have a leading zero; hex numbers |
||
172 | * use the given digits. |
||
173 | */ |
||
174 | static char * |
||
175 | __ultoa(val, endp, base, octzero, xdigs) |
||
176 | register u_long val; |
||
177 | char *endp; |
||
178 | int base, octzero; |
||
179 | char *xdigs; |
||
180 | { |
||
181 | register char *cp = endp; |
||
182 | register long sval; |
||
183 | |||
184 | /* |
||
185 | * Handle the three cases separately, in the hope of getting |
||
186 | * better/faster code. |
||
187 | */ |
||
188 | switch (base) { |
||
189 | case 10: |
||
190 | if (val < 10) { /* many numbers are 1 digit */ |
||
191 | *--cp = to_char(val); |
||
192 | return (cp); |
||
193 | } |
||
194 | /* |
||
195 | * On many machines, unsigned arithmetic is harder than |
||
196 | * signed arithmetic, so we do at most one unsigned mod and |
||
197 | * divide; this is sufficient to reduce the range of |
||
198 | * the incoming value to where signed arithmetic works. |
||
199 | */ |
||
200 | if (val > LONG_MAX) { |
||
201 | *--cp = to_char(val % 10); |
||
202 | sval = val / 10; |
||
203 | } else |
||
204 | sval = val; |
||
205 | do { |
||
206 | *--cp = to_char(sval % 10); |
||
207 | sval /= 10; |
||
208 | } while (sval != 0); |
||
209 | break; |
||
210 | |||
211 | case 8: |
||
212 | do { |
||
213 | *--cp = to_char(val & 7); |
||
214 | val >>= 3; |
||
215 | } while (val); |
||
216 | if (octzero && *cp != '0') |
||
217 | *--cp = '0'; |
||
218 | break; |
||
219 | |||
220 | case 16: |
||
221 | do { |
||
222 | *--cp = xdigs[val & 15]; |
||
223 | val >>= 4; |
||
224 | } while (val); |
||
225 | break; |
||
226 | |||
227 | default: /* oops */ |
||
228 | abort(); |
||
229 | } |
||
230 | return (cp); |
||
231 | } |
||
232 | |||
233 | /* Identical to __ultoa, but for quads. */ |
||
234 | static char * |
||
235 | __uqtoa(val, endp, base, octzero, xdigs) |
||
236 | register u_quad_t val; |
||
237 | char *endp; |
||
238 | int base, octzero; |
||
239 | char *xdigs; |
||
240 | { |
||
241 | register char *cp = endp; |
||
242 | register quad_t sval; |
||
243 | |||
244 | /* quick test for small values; __ultoa is typically much faster */ |
||
245 | /* (perhaps instead we should run until small, then call __ultoa?) */ |
||
246 | if (val <= ULONG_MAX) |
||
247 | return (__ultoa((u_long)val, endp, base, octzero, xdigs)); |
||
248 | switch (base) { |
||
249 | case 10: |
||
250 | if (val < 10) { |
||
251 | *--cp = to_char(val % 10); |
||
252 | return (cp); |
||
253 | } |
||
254 | if (val > QUAD_MAX) { |
||
255 | *--cp = to_char(val % 10); |
||
256 | sval = val / 10; |
||
257 | } else |
||
258 | sval = val; |
||
259 | do { |
||
260 | *--cp = to_char(sval % 10); |
||
261 | sval /= 10; |
||
262 | } while (sval != 0); |
||
263 | break; |
||
264 | |||
265 | case 8: |
||
266 | do { |
||
267 | *--cp = to_char(val & 7); |
||
268 | val >>= 3; |
||
269 | } while (val); |
||
270 | if (octzero && *cp != '0') |
||
271 | *--cp = '0'; |
||
272 | break; |
||
273 | |||
274 | case 16: |
||
275 | do { |
||
276 | *--cp = xdigs[val & 15]; |
||
277 | val >>= 4; |
||
278 | } while (val); |
||
279 | break; |
||
280 | |||
281 | default: |
||
282 | abort(); |
||
283 | } |
||
284 | return (cp); |
||
285 | } |
||
286 | |||
287 | #ifdef FLOATING_POINT |
||
288 | #include <math.h> |
||
289 | #include "floatio.h" |
||
290 | |||
291 | #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ |
||
292 | #define DEFPREC 6 |
||
293 | |||
294 | static char *cvt __P((double, int, int, char *, int *, int, int *)); |
||
295 | static int exponent __P((char *, int, int)); |
||
296 | |||
297 | #else /* no FLOATING_POINT */ |
||
298 | |||
299 | #define BUF 68 |
||
300 | |||
301 | #endif /* FLOATING_POINT */ |
||
302 | |||
303 | |||
304 | /* |
||
305 | * Flags used during conversion. |
||
306 | */ |
||
307 | #define ALT 0x001 /* alternate form */ |
||
308 | #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ |
||
309 | #define LADJUST 0x004 /* left adjustment */ |
||
310 | #define LONGDBL 0x008 /* long double; unimplemented */ |
||
311 | #define LONGINT 0x010 /* long integer */ |
||
312 | #define QUADINT 0x020 /* quad integer */ |
||
313 | #define SHORTINT 0x040 /* short integer */ |
||
314 | #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ |
||
315 | #define FPT 0x100 /* Floating point number */ |
||
316 | int |
||
317 | vfprintf(fp, fmt0, ap) |
||
318 | FILE *fp; |
||
319 | const char *fmt0; |
||
320 | va_list ap; |
||
321 | { |
||
322 | register char *fmt; /* format string */ |
||
323 | register int ch; /* character from fmt */ |
||
324 | register int n; /* handy integer (short term usage) */ |
||
325 | register char *cp; /* handy char pointer (short term usage) */ |
||
326 | register struct __siov *iovp;/* for PRINT macro */ |
||
327 | register int flags; /* flags as above */ |
||
328 | int ret; /* return value accumulator */ |
||
329 | int width; /* width from format (%8d), or 0 */ |
||
330 | int prec; /* precision from format (%.3d), or -1 */ |
||
331 | char sign; /* sign prefix (' ', '+', '-', or \0) */ |
||
332 | #ifdef FLOATING_POINT |
||
333 | char softsign; /* temporary negative sign for floats */ |
||
334 | double _double; /* double precision arguments %[eEfgG] */ |
||
335 | int expt; /* integer value of exponent */ |
||
336 | int expsize; /* character count for expstr */ |
||
337 | int ndig; /* actual number of digits returned by cvt */ |
||
338 | char expstr[7]; /* buffer for exponent string */ |
||
339 | #endif |
||
340 | u_long ulval; /* integer arguments %[diouxX] */ |
||
341 | u_quad_t uqval; /* %q integers */ |
||
342 | int base; /* base for [diouxX] conversion */ |
||
343 | int dprec; /* a copy of prec if [diouxX], 0 otherwise */ |
||
344 | int realsz; /* field size expanded by dprec, sign, etc */ |
||
345 | int size; /* size of converted field or string */ |
||
346 | char *xdigs; /* digits for [xX] conversion */ |
||
347 | #define NIOV 8 |
||
348 | struct __suio uio; /* output information: summary */ |
||
349 | struct __siov iov[NIOV];/* ... and individual io vectors */ |
||
350 | char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ |
||
351 | char ox[2]; /* space for 0x hex-prefix */ |
||
352 | |||
353 | /* |
||
354 | * Choose PADSIZE to trade efficiency vs. size. If larger printf |
||
355 | * fields occur frequently, increase PADSIZE and make the initialisers |
||
356 | * below longer. |
||
357 | */ |
||
358 | #define PADSIZE 16 /* pad chunk size */ |
||
359 | static char blanks[PADSIZE] = |
||
360 | {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; |
||
361 | static char zeroes[PADSIZE] = |
||
362 | {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; |
||
363 | |||
364 | /* |
||
365 | * BEWARE, these `goto error' on error, and PAD uses `n'. |
||
366 | */ |
||
367 | #define PRINT(ptr, len) { \ |
||
368 | iovp->iov_base = (ptr); \ |
||
369 | iovp->iov_len = (len); \ |
||
370 | uio.uio_resid += (len); \ |
||
371 | iovp++; \ |
||
372 | if (++uio.uio_iovcnt >= NIOV) { \ |
||
373 | if (__sprint(fp, &uio)) \ |
||
374 | goto error; \ |
||
375 | iovp = iov; \ |
||
376 | } \ |
||
377 | } |
||
378 | #define PAD(howmany, with) { \ |
||
379 | if ((n = (howmany)) > 0) { \ |
||
380 | while (n > PADSIZE) { \ |
||
381 | PRINT(with, PADSIZE); \ |
||
382 | n -= PADSIZE; \ |
||
383 | } \ |
||
384 | PRINT(with, n); \ |
||
385 | } \ |
||
386 | } |
||
387 | #define FLUSH() { \ |
||
388 | if (uio.uio_resid && __sprint(fp, &uio)) \ |
||
389 | goto error; \ |
||
390 | uio.uio_iovcnt = 0; \ |
||
391 | iovp = iov; \ |
||
392 | } |
||
393 | |||
394 | /* |
||
395 | * To extend shorts properly, we need both signed and unsigned |
||
396 | * argument extraction methods. |
||
397 | */ |
||
398 | #define SARG() \ |
||
399 | (flags&LONGINT ? va_arg(ap, long) : \ |
||
400 | flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ |
||
401 | (long)va_arg(ap, int)) |
||
402 | #define UARG() \ |
||
403 | (flags&LONGINT ? va_arg(ap, u_long) : \ |
||
404 | flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ |
||
405 | (u_long)va_arg(ap, u_int)) |
||
406 | |||
407 | /*MG (to prevent warnings) */ |
||
408 | #ifdef FLOATING_POINT |
||
409 | _double=0; |
||
410 | expsize=0; |
||
411 | #endif |
||
412 | ulval=0; |
||
413 | uqval=0; |
||
414 | xdigs=NULL; |
||
415 | |||
416 | |||
417 | #ifdef _THREAD_SAFE |
||
418 | _thread_flockfile(fp,__FILE__,__LINE__); |
||
419 | #endif |
||
420 | /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ |
||
421 | if (cantwrite(fp)) { |
||
422 | #ifdef _THREAD_SAFE |
||
423 | _thread_funlockfile(fp); |
||
424 | #endif |
||
425 | return (EOF); |
||
426 | } |
||
427 | |||
428 | /* optimise fprintf(stderr) (and other unbuffered Unix files) */ |
||
429 | |||
430 | |||
431 | /* |
||
432 | if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && |
||
433 | fp->_file >= 0) { |
||
434 | #ifdef _THREAD_SAFE |
||
435 | _thread_funlockfile(fp); |
||
436 | #endif |
||
437 | return (__sbprintf(fp, fmt0, ap)); |
||
438 | } |
||
439 | */ |
||
440 | |||
441 | fmt = (char *)fmt0; |
||
442 | uio.uio_iov = iovp = iov; |
||
443 | uio.uio_resid = 0; |
||
444 | uio.uio_iovcnt = 0; |
||
445 | ret = 0; |
||
446 | |||
447 | /* |
||
448 | * Scan the format for conversions (`%' character). |
||
449 | */ |
||
450 | for (;;) { |
||
451 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) |
||
452 | /* void */; |
||
453 | if ((n = fmt - cp) != 0) { |
||
454 | PRINT(cp, n); |
||
455 | ret += n; |
||
456 | } |
||
457 | if (ch == '\0') |
||
458 | goto done; |
||
459 | fmt++; /* skip over '%' */ |
||
460 | |||
461 | flags = 0; |
||
462 | dprec = 0; |
||
463 | width = 0; |
||
464 | prec = -1; |
||
465 | sign = '\0'; |
||
466 | |||
467 | rflag: ch = *fmt++; |
||
468 | reswitch: switch (ch) { |
||
469 | case ' ': |
||
470 | /* |
||
471 | * ``If the space and + flags both appear, the space |
||
472 | * flag will be ignored.'' |
||
473 | * -- ANSI X3J11 |
||
474 | */ |
||
475 | if (!sign) |
||
476 | sign = ' '; |
||
477 | goto rflag; |
||
478 | case '#': |
||
479 | flags |= ALT; |
||
480 | goto rflag; |
||
481 | case '*': |
||
482 | /* |
||
483 | * ``A negative field width argument is taken as a |
||
484 | * - flag followed by a positive field width.'' |
||
485 | * -- ANSI X3J11 |
||
486 | * They don't exclude field widths read from args. |
||
487 | */ |
||
488 | if ((width = va_arg(ap, int)) >= 0) |
||
489 | goto rflag; |
||
490 | width = -width; |
||
491 | /* FALLTHROUGH */ |
||
492 | case '-': |
||
493 | flags |= LADJUST; |
||
494 | goto rflag; |
||
495 | case '+': |
||
496 | sign = '+'; |
||
497 | goto rflag; |
||
498 | case '.': |
||
499 | if ((ch = *fmt++) == '*') { |
||
500 | n = va_arg(ap, int); |
||
501 | prec = n < 0 ? -1 : n; |
||
502 | goto rflag; |
||
503 | } |
||
504 | n = 0; |
||
505 | while (is_digit(ch)) { |
||
506 | n = 10 * n + to_digit(ch); |
||
507 | ch = *fmt++; |
||
508 | } |
||
509 | prec = n < 0 ? -1 : n; |
||
510 | goto reswitch; |
||
511 | case '0': |
||
512 | /* |
||
513 | * ``Note that 0 is taken as a flag, not as the |
||
514 | * beginning of a field width.'' |
||
515 | * -- ANSI X3J11 |
||
516 | */ |
||
517 | flags |= ZEROPAD; |
||
518 | goto rflag; |
||
519 | case '1': case '2': case '3': case '4': |
||
520 | case '5': case '6': case '7': case '8': case '9': |
||
521 | n = 0; |
||
522 | do { |
||
523 | n = 10 * n + to_digit(ch); |
||
524 | ch = *fmt++; |
||
525 | } while (is_digit(ch)); |
||
526 | width = n; |
||
527 | goto reswitch; |
||
528 | #ifdef FLOATING_POINT |
||
529 | case 'L': |
||
530 | flags |= LONGDBL; |
||
531 | goto rflag; |
||
532 | #endif |
||
533 | case 'h': |
||
534 | flags |= SHORTINT; |
||
535 | goto rflag; |
||
536 | case 'l': |
||
537 | flags |= LONGINT; |
||
538 | goto rflag; |
||
539 | case 'q': |
||
540 | flags |= QUADINT; |
||
541 | goto rflag; |
||
542 | case 'c': |
||
543 | *(cp = buf) = va_arg(ap, int); |
||
544 | size = 1; |
||
545 | sign = '\0'; |
||
546 | break; |
||
547 | case 'D': |
||
548 | flags |= LONGINT; |
||
549 | /*FALLTHROUGH*/ |
||
550 | case 'd': |
||
551 | case 'i': |
||
552 | if (flags & QUADINT) { |
||
553 | uqval = va_arg(ap, quad_t); |
||
554 | if ((quad_t)uqval < 0) { |
||
555 | uqval = -uqval; |
||
556 | sign = '-'; |
||
557 | } |
||
558 | } else { |
||
559 | ulval = SARG(); |
||
560 | if ((long)ulval < 0) { |
||
561 | ulval = -ulval; |
||
562 | sign = '-'; |
||
563 | } |
||
564 | } |
||
565 | base = 10; |
||
566 | goto number; |
||
567 | #ifdef FLOATING_POINT |
||
568 | case 'e': |
||
569 | case 'E': |
||
570 | case 'f': |
||
571 | goto fp_begin; |
||
572 | case 'g': |
||
573 | case 'G': |
||
574 | if (prec == 0) |
||
575 | prec = 1; |
||
576 | fp_begin: if (prec == -1) |
||
577 | prec = DEFPREC; |
||
578 | if (flags & LONGDBL) |
||
579 | _double = (double)va_arg(ap, long double); |
||
580 | else |
||
581 | _double = va_arg(ap, double); |
||
582 | /* do this before tricky precision changes */ |
||
583 | if (isinf(_double)) { |
||
584 | if (_double < 0) |
||
585 | sign = '-'; |
||
586 | cp = "Inf"; |
||
587 | size = 3; |
||
588 | break; |
||
589 | } |
||
590 | if (isnan(_double)) { |
||
591 | cp = "NaN"; |
||
592 | size = 3; |
||
593 | break; |
||
594 | } |
||
595 | flags |= FPT; |
||
596 | cp = cvt(_double, prec, flags, &softsign, |
||
597 | &expt, ch, &ndig); |
||
598 | if (ch == 'g' || ch == 'G') { |
||
599 | if (expt <= -4 || expt > prec) |
||
600 | ch = (ch == 'g') ? 'e' : 'E'; |
||
601 | else |
||
602 | ch = 'g'; |
||
603 | } |
||
604 | if (ch <= 'e') { /* 'e' or 'E' fmt */ |
||
605 | --expt; |
||
606 | expsize = exponent(expstr, expt, ch); |
||
607 | size = expsize + ndig; |
||
608 | if (ndig > 1 || flags & ALT) |
||
609 | ++size; |
||
610 | } else if (ch == 'f') { /* f fmt */ |
||
611 | if (expt > 0) { |
||
612 | size = expt; |
||
613 | if (prec || flags & ALT) |
||
614 | size += prec + 1; |
||
615 | } else /* "0.X" */ |
||
616 | size = prec + 2; |
||
617 | } else if (expt >= ndig) { /* fixed g fmt */ |
||
618 | size = expt; |
||
619 | if (flags & ALT) |
||
620 | ++size; |
||
621 | } else |
||
622 | size = ndig + (expt > 0 ? |
||
623 | 1 : 2 - expt); |
||
624 | |||
625 | if (softsign) |
||
626 | sign = '-'; |
||
627 | break; |
||
628 | #endif /* FLOATING_POINT */ |
||
629 | case 'n': |
||
630 | if (flags & QUADINT) |
||
631 | *va_arg(ap, quad_t *) = ret; |
||
632 | else if (flags & LONGINT) |
||
633 | *va_arg(ap, long *) = ret; |
||
634 | else if (flags & SHORTINT) |
||
635 | *va_arg(ap, short *) = ret; |
||
636 | else |
||
637 | *va_arg(ap, int *) = ret; |
||
638 | continue; /* no output */ |
||
639 | case 'O': |
||
640 | flags |= LONGINT; |
||
641 | /*FALLTHROUGH*/ |
||
642 | case 'o': |
||
643 | if (flags & QUADINT) |
||
644 | uqval = va_arg(ap, u_quad_t); |
||
645 | else |
||
646 | ulval = UARG(); |
||
647 | base = 8; |
||
648 | goto nosign; |
||
649 | case 'p': |
||
650 | /* |
||
651 | * ``The argument shall be a pointer to void. The |
||
652 | * value of the pointer is converted to a sequence |
||
653 | * of printable characters, in an implementation- |
||
654 | * defined manner.'' |
||
655 | * -- ANSI X3J11 |
||
656 | */ |
||
657 | ulval = (u_long)va_arg(ap, void *); |
||
658 | base = 16; |
||
659 | xdigs = "0123456789abcdef"; |
||
660 | flags = (flags & ~QUADINT) | HEXPREFIX; |
||
661 | ch = 'x'; |
||
662 | goto nosign; |
||
663 | case 's': |
||
664 | if ((cp = va_arg(ap, char *)) == NULL) |
||
665 | cp = "(null)"; |
||
666 | if (prec >= 0) { |
||
667 | /* |
||
668 | * can't use strlen; can only look for the |
||
669 | * NUL in the first `prec' characters, and |
||
670 | * strlen() will go further. |
||
671 | */ |
||
672 | char *p = memchr(cp, 0, (size_t)prec); |
||
673 | |||
674 | if (p != NULL) { |
||
675 | size = p - cp; |
||
676 | if (size > prec) |
||
677 | size = prec; |
||
678 | } else |
||
679 | size = prec; |
||
680 | } else |
||
681 | size = strlen(cp); |
||
682 | sign = '\0'; |
||
683 | break; |
||
684 | case 'U': |
||
685 | flags |= LONGINT; |
||
686 | /*FALLTHROUGH*/ |
||
687 | case 'u': |
||
688 | if (flags & QUADINT) |
||
689 | uqval = va_arg(ap, u_quad_t); |
||
690 | else |
||
691 | ulval = UARG(); |
||
692 | base = 10; |
||
693 | goto nosign; |
||
694 | case 'X': |
||
695 | xdigs = "0123456789ABCDEF"; |
||
696 | goto hex; |
||
697 | case 'x': |
||
698 | xdigs = "0123456789abcdef"; |
||
699 | hex: if (flags & QUADINT) |
||
700 | uqval = va_arg(ap, u_quad_t); |
||
701 | else |
||
702 | ulval = UARG(); |
||
703 | base = 16; |
||
704 | /* leading 0x/X only if non-zero */ |
||
705 | if (flags & ALT && |
||
706 | (flags & QUADINT ? uqval != 0 : ulval != 0)) |
||
707 | flags |= HEXPREFIX; |
||
708 | |||
709 | /* unsigned conversions */ |
||
710 | nosign: sign = '\0'; |
||
711 | /* |
||
712 | * ``... diouXx conversions ... if a precision is |
||
713 | * specified, the 0 flag will be ignored.'' |
||
714 | * -- ANSI X3J11 |
||
715 | */ |
||
716 | number: if ((dprec = prec) >= 0) |
||
717 | flags &= ~ZEROPAD; |
||
718 | |||
719 | /* |
||
720 | * ``The result of converting a zero value with an |
||
721 | * explicit precision of zero is no characters.'' |
||
722 | * -- ANSI X3J11 |
||
723 | */ |
||
724 | cp = buf + BUF; |
||
725 | if (flags & QUADINT) { |
||
726 | if (uqval != 0 || prec != 0) |
||
727 | cp = __uqtoa(uqval, cp, base, |
||
728 | flags & ALT, xdigs); |
||
729 | } else { |
||
730 | if (ulval != 0 || prec != 0) |
||
731 | cp = __ultoa(ulval, cp, base, |
||
732 | flags & ALT, xdigs); |
||
733 | } |
||
734 | size = buf + BUF - cp; |
||
735 | break; |
||
736 | default: /* "%?" prints ?, unless ? is NUL */ |
||
737 | if (ch == '\0') |
||
738 | goto done; |
||
739 | /* pretend it was %c with argument ch */ |
||
740 | cp = buf; |
||
741 | *cp = ch; |
||
742 | size = 1; |
||
743 | sign = '\0'; |
||
744 | break; |
||
745 | } |
||
746 | |||
747 | /* |
||
748 | * All reasonable formats wind up here. At this point, `cp' |
||
749 | * points to a string which (if not flags&LADJUST) should be |
||
750 | * padded out to `width' places. If flags&ZEROPAD, it should |
||
751 | * first be prefixed by any sign or other prefix; otherwise, |
||
752 | * it should be blank padded before the prefix is emitted. |
||
753 | * After any left-hand padding and prefixing, emit zeroes |
||
754 | * required by a decimal [diouxX] precision, then print the |
||
755 | * string proper, then emit zeroes required by any leftover |
||
756 | * floating precision; finally, if LADJUST, pad with blanks. |
||
757 | * |
||
758 | * Compute actual size, so we know how much to pad. |
||
759 | * size excludes decimal prec; realsz includes it. |
||
760 | */ |
||
761 | realsz = dprec > size ? dprec : size; |
||
762 | if (sign) |
||
763 | realsz++; |
||
764 | else if (flags & HEXPREFIX) |
||
765 | realsz += 2; |
||
766 | |||
767 | /* right-adjusting blank padding */ |
||
768 | if ((flags & (LADJUST|ZEROPAD)) == 0) |
||
769 | PAD(width - realsz, blanks); |
||
770 | |||
771 | /* prefix */ |
||
772 | if (sign) { |
||
773 | PRINT(&sign, 1); |
||
774 | } else if (flags & HEXPREFIX) { |
||
775 | ox[0] = '0'; |
||
776 | ox[1] = ch; |
||
777 | PRINT(ox, 2); |
||
778 | } |
||
779 | |||
780 | /* right-adjusting zero padding */ |
||
781 | if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) |
||
782 | PAD(width - realsz, zeroes); |
||
783 | |||
784 | /* leading zeroes from decimal precision */ |
||
785 | PAD(dprec - size, zeroes); |
||
786 | |||
787 | /* the string or number proper */ |
||
788 | #ifdef FLOATING_POINT |
||
789 | if ((flags & FPT) == 0) { |
||
790 | PRINT(cp, size); |
||
791 | } else { /* glue together f_p fragments */ |
||
792 | if (ch >= 'f') { /* 'f' or 'g' */ |
||
793 | if (_double == 0) { |
||
794 | /* kludge for __dtoa irregularity */ |
||
795 | if (expt >= ndig && |
||
796 | (flags & ALT) == 0) { |
||
797 | PRINT("0", 1); |
||
798 | } else { |
||
799 | PRINT("0.", 2); |
||
800 | PAD(ndig - 1, zeroes); |
||
801 | } |
||
802 | } else if (expt <= 0) { |
||
803 | PRINT("0.", 2); |
||
804 | PAD(-expt, zeroes); |
||
805 | PRINT(cp, ndig); |
||
806 | } else if (expt >= ndig) { |
||
807 | PRINT(cp, ndig); |
||
808 | PAD(expt - ndig, zeroes); |
||
809 | if (flags & ALT) |
||
810 | PRINT(".", 1); |
||
811 | } else { |
||
812 | PRINT(cp, expt); |
||
813 | cp += expt; |
||
814 | PRINT(".", 1); |
||
815 | PRINT(cp, ndig-expt); |
||
816 | } |
||
817 | } else { /* 'e' or 'E' */ |
||
818 | if (ndig > 1 || flags & ALT) { |
||
819 | ox[0] = *cp++; |
||
820 | ox[1] = '.'; |
||
821 | PRINT(ox, 2); |
||
822 | if (_double) { |
||
823 | PRINT(cp, ndig-1); |
||
824 | } else /* 0.[0..] */ |
||
825 | /* __dtoa irregularity */ |
||
826 | PAD(ndig - 1, zeroes); |
||
827 | } else /* XeYYY */ |
||
828 | PRINT(cp, 1); |
||
829 | PRINT(expstr, expsize); |
||
830 | } |
||
831 | } |
||
832 | #else |
||
833 | PRINT(cp, size); |
||
834 | #endif |
||
835 | /* left-adjusting padding (always blank) */ |
||
836 | if (flags & LADJUST) |
||
837 | PAD(width - realsz, blanks); |
||
838 | |||
839 | /* finally, adjust ret */ |
||
840 | ret += width > realsz ? width : realsz; |
||
841 | |||
842 | FLUSH(); /* copy out the I/O vectors */ |
||
843 | } |
||
844 | done: |
||
845 | FLUSH(); |
||
846 | error: |
||
847 | if (__sferror(fp)) |
||
848 | ret = EOF; |
||
849 | #ifdef _THREAD_SAFE |
||
850 | _thread_funlockfile(fp); |
||
851 | #endif |
||
852 | return (ret); |
||
853 | /* NOTREACHED */ |
||
854 | } |
||
855 | |||
856 | #ifdef FLOATING_POINT |
||
857 | |||
858 | extern char *__dtoa __P((double, int, int, int *, int *, char **)); |
||
859 | |||
860 | static char * |
||
861 | cvt(value, ndigits, flags, sign, decpt, ch, length) |
||
862 | double value; |
||
863 | int ndigits, flags, *decpt, ch, *length; |
||
864 | char *sign; |
||
865 | { |
||
866 | int mode, dsgn; |
||
867 | char *digits, *bp, *rve; |
||
868 | |||
869 | if (ch == 'f') |
||
870 | mode = 3; /* ndigits after the decimal point */ |
||
871 | else { |
||
872 | /* |
||
873 | * To obtain ndigits after the decimal point for the 'e' |
||
874 | * and 'E' formats, round to ndigits + 1 significant |
||
875 | * figures. |
||
876 | */ |
||
877 | if (ch == 'e' || ch == 'E') |
||
878 | ndigits++; |
||
879 | mode = 2; /* ndigits significant digits */ |
||
880 | } |
||
881 | if (value < 0) { |
||
882 | value = -value; |
||
883 | *sign = '-'; |
||
884 | } else |
||
885 | *sign = '\000'; |
||
886 | digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); |
||
887 | if ((ch != 'g' && ch != 'G') || flags & ALT) { |
||
888 | /* print trailing zeros */ |
||
889 | bp = digits + ndigits; |
||
890 | if (ch == 'f') { |
||
891 | if (*digits == '0' && value) |
||
892 | *decpt = -ndigits + 1; |
||
893 | bp += *decpt; |
||
894 | } |
||
895 | if (value == 0) /* kludge for __dtoa irregularity */ |
||
896 | rve = bp; |
||
897 | while (rve < bp) |
||
898 | *rve++ = '0'; |
||
899 | } |
||
900 | *length = rve - digits; |
||
901 | return (digits); |
||
902 | } |
||
903 | |||
904 | static int |
||
905 | exponent(p0, exp, fmtch) |
||
906 | char *p0; |
||
907 | int exp, fmtch; |
||
908 | { |
||
909 | register char *p, *t; |
||
910 | char expbuf[MAXEXP]; |
||
911 | |||
912 | p = p0; |
||
913 | *p++ = fmtch; |
||
914 | if (exp < 0) { |
||
915 | exp = -exp; |
||
916 | *p++ = '-'; |
||
917 | } |
||
918 | else |
||
919 | *p++ = '+'; |
||
920 | t = expbuf + MAXEXP; |
||
921 | if (exp > 9) { |
||
922 | do { |
||
923 | *--t = to_char(exp % 10); |
||
924 | } while ((exp /= 10) > 9); |
||
925 | *--t = to_char(exp); |
||
926 | for (; t < expbuf + MAXEXP; *p++ = *t++); |
||
927 | } |
||
928 | else { |
||
929 | *p++ = '0'; |
||
930 | *p++ = to_char(exp); |
||
931 | } |
||
932 | return (p - p0); |
||
933 | } |
||
934 | #endif /* FLOATING_POINT */ |