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