Deleted Added
full compact
printf-pos.c (113194) printf-pos.c (113196)
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#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
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#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libc/stdio/vfprintf.c 113194 2003-04-07 01:07:48Z das $");
41__FBSDID("$FreeBSD: head/lib/libc/stdio/vfprintf.c 113196 2003-04-07 03:17:39Z ache $");
42
43/*
44 * Actual printf innards.
45 *
46 * This code is large and complicated...
47 */
48
49#include "namespace.h"
50#include <sys/types.h>
51
52#include <ctype.h>
53#include <limits.h>
54#include <locale.h>
55#include <stddef.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <wchar.h>
61
62#include <stdarg.h>
63#include "un-namespace.h"
64
65#include "libc_private.h"
66#include "local.h"
67#include "fvwrite.h"
68
69/* Define FLOATING_POINT to get floating point. */
70#define FLOATING_POINT
71
72union arg {
73 int intarg;
74 u_int uintarg;
75 long longarg;
76 u_long ulongarg;
77 long long longlongarg;
78 unsigned long long ulonglongarg;
79 ptrdiff_t ptrdiffarg;
80 size_t sizearg;
81 intmax_t intmaxarg;
82 uintmax_t uintmaxarg;
83 void *pvoidarg;
84 char *pchararg;
85 signed char *pschararg;
86 short *pshortarg;
87 int *pintarg;
88 long *plongarg;
89 long long *plonglongarg;
90 ptrdiff_t *pptrdiffarg;
91 size_t *psizearg;
92 intmax_t *pintmaxarg;
93#ifdef FLOATING_POINT
94 double doublearg;
95 long double longdoublearg;
96#endif
97 wint_t wintarg;
98 wchar_t *pwchararg;
99};
100
101/*
102 * Type ids for argument type table.
103 */
104enum typeid {
105 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
106 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
107 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
108 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
109 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
110};
111
112static int __sprint(FILE *, struct __suio *);
113static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
114static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
115 const char *);
116static char *__ultoa(u_long, char *, int, int, const char *, int, char,
117 const char *);
118static char *__wcsconv(wchar_t *, int);
119static void __find_arguments(const char *, va_list, union arg **);
120static void __grow_type_table(int, enum typeid **, int *);
121
122/*
123 * Flush out all the vectors defined by the given uio,
124 * then reset it so that it can be reused.
125 */
126static int
127__sprint(FILE *fp, struct __suio *uio)
128{
129 int err;
130
131 if (uio->uio_resid == 0) {
132 uio->uio_iovcnt = 0;
133 return (0);
134 }
135 err = __sfvwrite(fp, uio);
136 uio->uio_resid = 0;
137 uio->uio_iovcnt = 0;
138 return (err);
139}
140
141/*
142 * Helper function for `fprintf to unbuffered unix file': creates a
143 * temporary buffer. We only work on write-only files; this avoids
144 * worries about ungetc buffers and so forth.
145 */
146static int
147__sbprintf(FILE *fp, const char *fmt, va_list ap)
148{
149 int ret;
150 FILE fake;
151 unsigned char buf[BUFSIZ];
152
153 /* copy the important variables */
154 fake._flags = fp->_flags & ~__SNBF;
155 fake._file = fp->_file;
156 fake._cookie = fp->_cookie;
157 fake._write = fp->_write;
158 fake._extra = fp->_extra;
159
160 /* set up the buffer */
161 fake._bf._base = fake._p = buf;
162 fake._bf._size = fake._w = sizeof(buf);
163 fake._lbfsize = 0; /* not actually used, but Just In Case */
164
165 /* do the work, then copy any error status */
166 ret = __vfprintf(&fake, fmt, ap);
167 if (ret >= 0 && __fflush(&fake))
168 ret = EOF;
169 if (fake._flags & __SERR)
170 fp->_flags |= __SERR;
171 return (ret);
172}
173
174/*
175 * Macros for converting digits to letters and vice versa
176 */
177#define to_digit(c) ((c) - '0')
178#define is_digit(c) ((unsigned)to_digit(c) <= 9)
179#define to_char(n) ((n) + '0')
180
181/*
182 * Convert an unsigned long to ASCII for printf purposes, returning
183 * a pointer to the first character of the string representation.
184 * Octal numbers can be forced to have a leading zero; hex numbers
185 * use the given digits.
186 */
187static char *
188__ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs,
189 int needgrp, char thousep, const char *grp)
190{
191 char *cp = endp;
192 long sval;
193 int ndig;
194
195 /*
196 * Handle the three cases separately, in the hope of getting
197 * better/faster code.
198 */
199 switch (base) {
200 case 10:
201 if (val < 10) { /* many numbers are 1 digit */
202 *--cp = to_char(val);
203 return (cp);
204 }
205 ndig = 0;
206 /*
207 * On many machines, unsigned arithmetic is harder than
208 * signed arithmetic, so we do at most one unsigned mod and
209 * divide; this is sufficient to reduce the range of
210 * the incoming value to where signed arithmetic works.
211 */
212 if (val > LONG_MAX) {
213 *--cp = to_char(val % 10);
214 ndig++;
215 sval = val / 10;
216 } else
217 sval = val;
218 do {
219 *--cp = to_char(sval % 10);
220 ndig++;
221 /*
222 * If (*grp == CHAR_MAX) then no more grouping
223 * should be performed.
224 */
225 if (needgrp && ndig == *grp && *grp != CHAR_MAX
226 && sval > 9) {
227 *--cp = thousep;
228 ndig = 0;
229 /*
230 * If (*(grp+1) == '\0') then we have to
231 * use *grp character (last grouping rule)
232 * for all next cases
233 */
234 if (*(grp+1) != '\0')
235 grp++;
236 }
237 sval /= 10;
238 } while (sval != 0);
239 break;
240
241 case 8:
242 do {
243 *--cp = to_char(val & 7);
244 val >>= 3;
245 } while (val);
246 if (octzero && *cp != '0')
247 *--cp = '0';
248 break;
249
250 case 16:
251 do {
252 *--cp = xdigs[val & 15];
253 val >>= 4;
254 } while (val);
255 break;
256
257 default: /* oops */
258 abort();
259 }
260 return (cp);
261}
262
263/* Identical to __ultoa, but for intmax_t. */
264static char *
265__ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs,
266 int needgrp, char thousep, const char *grp)
267{
268 char *cp = endp;
269 intmax_t sval;
270 int ndig;
271
272 /* quick test for small values; __ultoa is typically much faster */
273 /* (perhaps instead we should run until small, then call __ultoa?) */
274 if (val <= ULONG_MAX)
275 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
276 needgrp, thousep, grp));
277 switch (base) {
278 case 10:
279 if (val < 10) {
280 *--cp = to_char(val % 10);
281 return (cp);
282 }
283 ndig = 0;
284 if (val > INTMAX_MAX) {
285 *--cp = to_char(val % 10);
286 ndig++;
287 sval = val / 10;
288 } else
289 sval = val;
290 do {
291 *--cp = to_char(sval % 10);
292 ndig++;
293 /*
294 * If (*grp == CHAR_MAX) then no more grouping
295 * should be performed.
296 */
297 if (needgrp && *grp != CHAR_MAX && ndig == *grp
298 && sval > 9) {
299 *--cp = thousep;
300 ndig = 0;
301 /*
302 * If (*(grp+1) == '\0') then we have to
303 * use *grp character (last grouping rule)
304 * for all next cases
305 */
306 if (*(grp+1) != '\0')
307 grp++;
308 }
309 sval /= 10;
310 } while (sval != 0);
311 break;
312
313 case 8:
314 do {
315 *--cp = to_char(val & 7);
316 val >>= 3;
317 } while (val);
318 if (octzero && *cp != '0')
319 *--cp = '0';
320 break;
321
322 case 16:
323 do {
324 *--cp = xdigs[val & 15];
325 val >>= 4;
326 } while (val);
327 break;
328
329 default:
330 abort();
331 }
332 return (cp);
333}
334
335/*
336 * Convert a wide character string argument for the %ls format to a multibyte
337 * string representation. ``prec'' specifies the maximum number of bytes
338 * to output. If ``prec'' is greater than or equal to zero, we can't assume
339 * that the wide char. string ends in a null character.
340 */
341static char *
342__wcsconv(wchar_t *wcsarg, int prec)
343{
344 char buf[MB_LEN_MAX];
345 wchar_t *p;
346 char *convbuf, *mbp;
347 size_t clen, nbytes;
348 mbstate_t mbs;
349
350 /*
351 * Determine the number of bytes to output and allocate space for
352 * the output.
353 */
354 memset(&mbs, 0, sizeof(mbs));
355 if (prec >= 0) {
356 nbytes = 0;
357 p = wcsarg;
358 for (;;) {
359 clen = wcrtomb(buf, *p++, &mbs);
360 if (clen == 0 || clen == (size_t)-1 ||
361 nbytes + clen > prec)
362 break;
363 nbytes += clen;
364 }
365 } else {
366 p = wcsarg;
367 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
368 if (nbytes == (size_t)-1)
369 return (NULL);
370 }
371 if ((convbuf = malloc(nbytes + 1)) == NULL)
372 return (NULL);
373
374 /*
375 * Fill the output buffer with the multibyte representations of as
376 * many wide characters as will fit.
377 */
378 mbp = convbuf;
379 p = wcsarg;
380 memset(&mbs, 0, sizeof(mbs));
381 while (mbp - convbuf < nbytes) {
382 clen = wcrtomb(mbp, *p++, &mbs);
383 if (clen == 0 || clen == (size_t)-1)
384 break;
385 mbp += clen;
386 }
42
43/*
44 * Actual printf innards.
45 *
46 * This code is large and complicated...
47 */
48
49#include "namespace.h"
50#include <sys/types.h>
51
52#include <ctype.h>
53#include <limits.h>
54#include <locale.h>
55#include <stddef.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <wchar.h>
61
62#include <stdarg.h>
63#include "un-namespace.h"
64
65#include "libc_private.h"
66#include "local.h"
67#include "fvwrite.h"
68
69/* Define FLOATING_POINT to get floating point. */
70#define FLOATING_POINT
71
72union arg {
73 int intarg;
74 u_int uintarg;
75 long longarg;
76 u_long ulongarg;
77 long long longlongarg;
78 unsigned long long ulonglongarg;
79 ptrdiff_t ptrdiffarg;
80 size_t sizearg;
81 intmax_t intmaxarg;
82 uintmax_t uintmaxarg;
83 void *pvoidarg;
84 char *pchararg;
85 signed char *pschararg;
86 short *pshortarg;
87 int *pintarg;
88 long *plongarg;
89 long long *plonglongarg;
90 ptrdiff_t *pptrdiffarg;
91 size_t *psizearg;
92 intmax_t *pintmaxarg;
93#ifdef FLOATING_POINT
94 double doublearg;
95 long double longdoublearg;
96#endif
97 wint_t wintarg;
98 wchar_t *pwchararg;
99};
100
101/*
102 * Type ids for argument type table.
103 */
104enum typeid {
105 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
106 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
107 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
108 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
109 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
110};
111
112static int __sprint(FILE *, struct __suio *);
113static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
114static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
115 const char *);
116static char *__ultoa(u_long, char *, int, int, const char *, int, char,
117 const char *);
118static char *__wcsconv(wchar_t *, int);
119static void __find_arguments(const char *, va_list, union arg **);
120static void __grow_type_table(int, enum typeid **, int *);
121
122/*
123 * Flush out all the vectors defined by the given uio,
124 * then reset it so that it can be reused.
125 */
126static int
127__sprint(FILE *fp, struct __suio *uio)
128{
129 int err;
130
131 if (uio->uio_resid == 0) {
132 uio->uio_iovcnt = 0;
133 return (0);
134 }
135 err = __sfvwrite(fp, uio);
136 uio->uio_resid = 0;
137 uio->uio_iovcnt = 0;
138 return (err);
139}
140
141/*
142 * Helper function for `fprintf to unbuffered unix file': creates a
143 * temporary buffer. We only work on write-only files; this avoids
144 * worries about ungetc buffers and so forth.
145 */
146static int
147__sbprintf(FILE *fp, const char *fmt, va_list ap)
148{
149 int ret;
150 FILE fake;
151 unsigned char buf[BUFSIZ];
152
153 /* copy the important variables */
154 fake._flags = fp->_flags & ~__SNBF;
155 fake._file = fp->_file;
156 fake._cookie = fp->_cookie;
157 fake._write = fp->_write;
158 fake._extra = fp->_extra;
159
160 /* set up the buffer */
161 fake._bf._base = fake._p = buf;
162 fake._bf._size = fake._w = sizeof(buf);
163 fake._lbfsize = 0; /* not actually used, but Just In Case */
164
165 /* do the work, then copy any error status */
166 ret = __vfprintf(&fake, fmt, ap);
167 if (ret >= 0 && __fflush(&fake))
168 ret = EOF;
169 if (fake._flags & __SERR)
170 fp->_flags |= __SERR;
171 return (ret);
172}
173
174/*
175 * Macros for converting digits to letters and vice versa
176 */
177#define to_digit(c) ((c) - '0')
178#define is_digit(c) ((unsigned)to_digit(c) <= 9)
179#define to_char(n) ((n) + '0')
180
181/*
182 * Convert an unsigned long to ASCII for printf purposes, returning
183 * a pointer to the first character of the string representation.
184 * Octal numbers can be forced to have a leading zero; hex numbers
185 * use the given digits.
186 */
187static char *
188__ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs,
189 int needgrp, char thousep, const char *grp)
190{
191 char *cp = endp;
192 long sval;
193 int ndig;
194
195 /*
196 * Handle the three cases separately, in the hope of getting
197 * better/faster code.
198 */
199 switch (base) {
200 case 10:
201 if (val < 10) { /* many numbers are 1 digit */
202 *--cp = to_char(val);
203 return (cp);
204 }
205 ndig = 0;
206 /*
207 * On many machines, unsigned arithmetic is harder than
208 * signed arithmetic, so we do at most one unsigned mod and
209 * divide; this is sufficient to reduce the range of
210 * the incoming value to where signed arithmetic works.
211 */
212 if (val > LONG_MAX) {
213 *--cp = to_char(val % 10);
214 ndig++;
215 sval = val / 10;
216 } else
217 sval = val;
218 do {
219 *--cp = to_char(sval % 10);
220 ndig++;
221 /*
222 * If (*grp == CHAR_MAX) then no more grouping
223 * should be performed.
224 */
225 if (needgrp && ndig == *grp && *grp != CHAR_MAX
226 && sval > 9) {
227 *--cp = thousep;
228 ndig = 0;
229 /*
230 * If (*(grp+1) == '\0') then we have to
231 * use *grp character (last grouping rule)
232 * for all next cases
233 */
234 if (*(grp+1) != '\0')
235 grp++;
236 }
237 sval /= 10;
238 } while (sval != 0);
239 break;
240
241 case 8:
242 do {
243 *--cp = to_char(val & 7);
244 val >>= 3;
245 } while (val);
246 if (octzero && *cp != '0')
247 *--cp = '0';
248 break;
249
250 case 16:
251 do {
252 *--cp = xdigs[val & 15];
253 val >>= 4;
254 } while (val);
255 break;
256
257 default: /* oops */
258 abort();
259 }
260 return (cp);
261}
262
263/* Identical to __ultoa, but for intmax_t. */
264static char *
265__ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs,
266 int needgrp, char thousep, const char *grp)
267{
268 char *cp = endp;
269 intmax_t sval;
270 int ndig;
271
272 /* quick test for small values; __ultoa is typically much faster */
273 /* (perhaps instead we should run until small, then call __ultoa?) */
274 if (val <= ULONG_MAX)
275 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
276 needgrp, thousep, grp));
277 switch (base) {
278 case 10:
279 if (val < 10) {
280 *--cp = to_char(val % 10);
281 return (cp);
282 }
283 ndig = 0;
284 if (val > INTMAX_MAX) {
285 *--cp = to_char(val % 10);
286 ndig++;
287 sval = val / 10;
288 } else
289 sval = val;
290 do {
291 *--cp = to_char(sval % 10);
292 ndig++;
293 /*
294 * If (*grp == CHAR_MAX) then no more grouping
295 * should be performed.
296 */
297 if (needgrp && *grp != CHAR_MAX && ndig == *grp
298 && sval > 9) {
299 *--cp = thousep;
300 ndig = 0;
301 /*
302 * If (*(grp+1) == '\0') then we have to
303 * use *grp character (last grouping rule)
304 * for all next cases
305 */
306 if (*(grp+1) != '\0')
307 grp++;
308 }
309 sval /= 10;
310 } while (sval != 0);
311 break;
312
313 case 8:
314 do {
315 *--cp = to_char(val & 7);
316 val >>= 3;
317 } while (val);
318 if (octzero && *cp != '0')
319 *--cp = '0';
320 break;
321
322 case 16:
323 do {
324 *--cp = xdigs[val & 15];
325 val >>= 4;
326 } while (val);
327 break;
328
329 default:
330 abort();
331 }
332 return (cp);
333}
334
335/*
336 * Convert a wide character string argument for the %ls format to a multibyte
337 * string representation. ``prec'' specifies the maximum number of bytes
338 * to output. If ``prec'' is greater than or equal to zero, we can't assume
339 * that the wide char. string ends in a null character.
340 */
341static char *
342__wcsconv(wchar_t *wcsarg, int prec)
343{
344 char buf[MB_LEN_MAX];
345 wchar_t *p;
346 char *convbuf, *mbp;
347 size_t clen, nbytes;
348 mbstate_t mbs;
349
350 /*
351 * Determine the number of bytes to output and allocate space for
352 * the output.
353 */
354 memset(&mbs, 0, sizeof(mbs));
355 if (prec >= 0) {
356 nbytes = 0;
357 p = wcsarg;
358 for (;;) {
359 clen = wcrtomb(buf, *p++, &mbs);
360 if (clen == 0 || clen == (size_t)-1 ||
361 nbytes + clen > prec)
362 break;
363 nbytes += clen;
364 }
365 } else {
366 p = wcsarg;
367 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
368 if (nbytes == (size_t)-1)
369 return (NULL);
370 }
371 if ((convbuf = malloc(nbytes + 1)) == NULL)
372 return (NULL);
373
374 /*
375 * Fill the output buffer with the multibyte representations of as
376 * many wide characters as will fit.
377 */
378 mbp = convbuf;
379 p = wcsarg;
380 memset(&mbs, 0, sizeof(mbs));
381 while (mbp - convbuf < nbytes) {
382 clen = wcrtomb(mbp, *p++, &mbs);
383 if (clen == 0 || clen == (size_t)-1)
384 break;
385 mbp += clen;
386 }
387 *mbp = '\0';
388 if (clen == (size_t)-1)
387 if (clen == (size_t)-1) {
388 free(convbuf);
389 return (NULL);
389 return (NULL);
390 }
391 *mbp = '\0';
390
391 return (convbuf);
392}
393
394/*
395 * MT-safe version
396 */
397int
398vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap)
399
400{
401 int ret;
402
403 FLOCKFILE(fp);
404 ret = __vfprintf(fp, fmt0, ap);
405 FUNLOCKFILE(fp);
406 return (ret);
407}
408
409#ifdef FLOATING_POINT
410
411#define dtoa __dtoa
412#define freedtoa __freedtoa
413
414#include <float.h>
415#include <math.h>
416#include "floatio.h"
417#include "gdtoa.h"
418
419#define DEFPREC 6
420
421static int exponent(char *, int, int);
422
423#endif /* FLOATING_POINT */
424
425/*
426 * The size of the buffer we use as scratch space for integer
427 * conversions, among other things. Technically, we would need the
428 * most space for base 10 conversions with thousands' grouping
429 * characters between each pair of digits. 100 bytes is a
430 * conservative overestimate even for a 128-bit uintmax_t.
431 */
432#define BUF 100
433
434#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
435
436/*
437 * Flags used during conversion.
438 */
439#define ALT 0x001 /* alternate form */
440#define LADJUST 0x004 /* left adjustment */
441#define LONGDBL 0x008 /* long double */
442#define LONGINT 0x010 /* long integer */
443#define LLONGINT 0x020 /* long long integer */
444#define SHORTINT 0x040 /* short integer */
445#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
446#define FPT 0x100 /* Floating point number */
447#define GROUPING 0x200 /* use grouping ("'" flag) */
448 /* C99 additional size modifiers: */
449#define SIZET 0x400 /* size_t */
450#define PTRDIFFT 0x800 /* ptrdiff_t */
451#define INTMAXT 0x1000 /* intmax_t */
452#define CHARINT 0x2000 /* print char using int format */
453
454/*
455 * Non-MT-safe version
456 */
457int
458__vfprintf(FILE *fp, const char *fmt0, va_list ap)
459{
460 char *fmt; /* format string */
461 int ch; /* character from fmt */
462 int n, n2; /* handy integer (short term usage) */
463 char *cp; /* handy char pointer (short term usage) */
464 struct __siov *iovp; /* for PRINT macro */
465 int flags; /* flags as above */
466 int ret; /* return value accumulator */
467 int width; /* width from format (%8d), or 0 */
468 int prec; /* precision from format; <0 for N/A */
469 char sign; /* sign prefix (' ', '+', '-', or \0) */
470 char thousands_sep; /* locale specific thousands separator */
471 const char *grouping; /* locale specific numeric grouping rules */
472#ifdef FLOATING_POINT
473 /*
474 * We can decompose the printed representation of floating
475 * point numbers into several parts, some of which may be empty:
476 *
477 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
478 * A B ---C--- D E F
479 *
480 * A: 'sign' holds this value if present; '\0' otherwise
481 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
482 * C: cp points to the string MMMNNN. Leading and trailing
483 * zeros are not in the string and must be added.
484 * D: expchar holds this character; '\0' if no exponent, e.g. %f
485 * F: at least two digits for decimal, at least one digit for hex
486 */
487 char *decimal_point; /* locale specific decimal point */
488 int signflag; /* true if float is negative */
489 union { /* floating point arguments %[aAeEfFgG] */
490 double dbl;
491 long double ldbl;
492 } fparg;
493 int expt; /* integer value of exponent */
494 char expchar; /* exponent character: [eEpP\0] */
495 char *dtoaend; /* pointer to end of converted digits */
496 int expsize; /* character count for expstr */
497 int lead; /* sig figs before decimal or group sep */
498 int ndig; /* actual number of digits returned by dtoa */
499 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
500 char *dtoaresult; /* buffer allocated by dtoa */
501 int nseps; /* number of group separators with ' */
502 int nrepeats; /* number of repeats of the last group */
503#endif
504 u_long ulval; /* integer arguments %[diouxX] */
505 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
506 int base; /* base for [diouxX] conversion */
507 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
508 int realsz; /* field size expanded by dprec, sign, etc */
509 int size; /* size of converted field or string */
510 int prsize; /* max size of printed field */
511 const char *xdigs; /* digits for %[xX] conversion */
512#define NIOV 8
513 struct __suio uio; /* output information: summary */
514 struct __siov iov[NIOV];/* ... and individual io vectors */
515 char buf[BUF]; /* buffer with space for digits of uintmax_t */
516 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
517 union arg *argtable; /* args, built due to positional arg */
518 union arg statargtable [STATIC_ARG_TBL_SIZE];
519 int nextarg; /* 1-based argument index */
520 va_list orgap; /* original argument pointer */
521 char *convbuf; /* wide to multibyte conversion result */
522
523 /*
524 * Choose PADSIZE to trade efficiency vs. size. If larger printf
525 * fields occur frequently, increase PADSIZE and make the initialisers
526 * below longer.
527 */
528#define PADSIZE 16 /* pad chunk size */
529 static char blanks[PADSIZE] =
530 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
531 static char zeroes[PADSIZE] =
532 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
533
534 static const char xdigs_lower[16] = "0123456789abcdef";
535 static const char xdigs_upper[16] = "0123456789ABCDEF";
536
537 /*
538 * BEWARE, these `goto error' on error, and PAD uses `n'.
539 */
540#define PRINT(ptr, len) { \
541 iovp->iov_base = (ptr); \
542 iovp->iov_len = (len); \
543 uio.uio_resid += (len); \
544 iovp++; \
545 if (++uio.uio_iovcnt >= NIOV) { \
546 if (__sprint(fp, &uio)) \
547 goto error; \
548 iovp = iov; \
549 } \
550}
551#define PAD(howmany, with) { \
552 if ((n = (howmany)) > 0) { \
553 while (n > PADSIZE) { \
554 PRINT(with, PADSIZE); \
555 n -= PADSIZE; \
556 } \
557 PRINT(with, n); \
558 } \
559}
560#define PRINTANDPAD(p, ep, len, with) do { \
561 n2 = (ep) - (p); \
562 if (n2 > (len)) \
563 n2 = (len); \
564 if (n2 > 0) \
565 PRINT((p), n2); \
566 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
567} while(0)
568#define FLUSH() { \
569 if (uio.uio_resid && __sprint(fp, &uio)) \
570 goto error; \
571 uio.uio_iovcnt = 0; \
572 iovp = iov; \
573}
574
575 /*
576 * Get the argument indexed by nextarg. If the argument table is
577 * built, use it to get the argument. If its not, get the next
578 * argument (and arguments must be gotten sequentially).
579 */
580#define GETARG(type) \
581 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
582 (nextarg++, va_arg(ap, type)))
583
584 /*
585 * To extend shorts properly, we need both signed and unsigned
586 * argument extraction methods.
587 */
588#define SARG() \
589 (flags&LONGINT ? GETARG(long) : \
590 flags&SHORTINT ? (long)(short)GETARG(int) : \
591 flags&CHARINT ? (long)(signed char)GETARG(int) : \
592 (long)GETARG(int))
593#define UARG() \
594 (flags&LONGINT ? GETARG(u_long) : \
595 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
596 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
597 (u_long)GETARG(u_int))
598#define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
599#define SJARG() \
600 (flags&INTMAXT ? GETARG(intmax_t) : \
601 flags&SIZET ? (intmax_t)GETARG(size_t) : \
602 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
603 (intmax_t)GETARG(long long))
604#define UJARG() \
605 (flags&INTMAXT ? GETARG(uintmax_t) : \
606 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
607 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
608 (uintmax_t)GETARG(unsigned long long))
609
610 /*
611 * Get * arguments, including the form *nn$. Preserve the nextarg
612 * that the argument can be gotten once the type is determined.
613 */
614#define GETASTER(val) \
615 n2 = 0; \
616 cp = fmt; \
617 while (is_digit(*cp)) { \
618 n2 = 10 * n2 + to_digit(*cp); \
619 cp++; \
620 } \
621 if (*cp == '$') { \
622 int hold = nextarg; \
623 if (argtable == NULL) { \
624 argtable = statargtable; \
625 __find_arguments (fmt0, orgap, &argtable); \
626 } \
627 nextarg = n2; \
628 val = GETARG (int); \
629 nextarg = hold; \
630 fmt = ++cp; \
631 } else { \
632 val = GETARG (int); \
633 }
634
635
636 thousands_sep = '\0';
637 grouping = NULL;
638 convbuf = NULL;
639#ifdef FLOATING_POINT
640 dtoaresult = NULL;
641 decimal_point = localeconv()->decimal_point;
642#endif
643 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
644 if (cantwrite(fp))
645 return (EOF);
646
647 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
648 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
649 fp->_file >= 0)
650 return (__sbprintf(fp, fmt0, ap));
651
652 fmt = (char *)fmt0;
653 argtable = NULL;
654 nextarg = 1;
655 va_copy(orgap, ap);
656 uio.uio_iov = iovp = iov;
657 uio.uio_resid = 0;
658 uio.uio_iovcnt = 0;
659 ret = 0;
660
661 /*
662 * Scan the format for conversions (`%' character).
663 */
664 for (;;) {
665 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
666 /* void */;
667 if ((n = fmt - cp) != 0) {
668 if ((unsigned)ret + n > INT_MAX) {
669 ret = EOF;
670 goto error;
671 }
672 PRINT(cp, n);
673 ret += n;
674 }
675 if (ch == '\0')
676 goto done;
677 fmt++; /* skip over '%' */
678
679 flags = 0;
680 dprec = 0;
681 width = 0;
682 prec = -1;
683 sign = '\0';
684 ox[1] = '\0';
685
686rflag: ch = *fmt++;
687reswitch: switch (ch) {
688 case ' ':
689 /*-
690 * ``If the space and + flags both appear, the space
691 * flag will be ignored.''
692 * -- ANSI X3J11
693 */
694 if (!sign)
695 sign = ' ';
696 goto rflag;
697 case '#':
698 flags |= ALT;
699 goto rflag;
700 case '*':
701 /*-
702 * ``A negative field width argument is taken as a
703 * - flag followed by a positive field width.''
704 * -- ANSI X3J11
705 * They don't exclude field widths read from args.
706 */
707 GETASTER (width);
708 if (width >= 0)
709 goto rflag;
710 width = -width;
711 /* FALLTHROUGH */
712 case '-':
713 flags |= LADJUST;
714 goto rflag;
715 case '+':
716 sign = '+';
717 goto rflag;
718 case '\'':
719 flags |= GROUPING;
720 thousands_sep = *(localeconv()->thousands_sep);
721 grouping = localeconv()->grouping;
722 goto rflag;
723 case '.':
724 if ((ch = *fmt++) == '*') {
725 GETASTER (prec);
726 goto rflag;
727 }
728 prec = 0;
729 while (is_digit(ch)) {
730 prec = 10 * prec + to_digit(ch);
731 ch = *fmt++;
732 }
733 goto reswitch;
734 case '0':
735 /*-
736 * ``Note that 0 is taken as a flag, not as the
737 * beginning of a field width.''
738 * -- ANSI X3J11
739 */
740 flags |= ZEROPAD;
741 goto rflag;
742 case '1': case '2': case '3': case '4':
743 case '5': case '6': case '7': case '8': case '9':
744 n = 0;
745 do {
746 n = 10 * n + to_digit(ch);
747 ch = *fmt++;
748 } while (is_digit(ch));
749 if (ch == '$') {
750 nextarg = n;
751 if (argtable == NULL) {
752 argtable = statargtable;
753 __find_arguments (fmt0, orgap,
754 &argtable);
755 }
756 goto rflag;
757 }
758 width = n;
759 goto reswitch;
760#ifdef FLOATING_POINT
761 case 'L':
762 flags |= LONGDBL;
763 goto rflag;
764#endif
765 case 'h':
766 if (flags & SHORTINT) {
767 flags &= ~SHORTINT;
768 flags |= CHARINT;
769 } else
770 flags |= SHORTINT;
771 goto rflag;
772 case 'j':
773 flags |= INTMAXT;
774 goto rflag;
775 case 'l':
776 if (flags & LONGINT) {
777 flags &= ~LONGINT;
778 flags |= LLONGINT;
779 } else
780 flags |= LONGINT;
781 goto rflag;
782 case 'q':
783 flags |= LLONGINT; /* not necessarily */
784 goto rflag;
785 case 't':
786 flags |= PTRDIFFT;
787 goto rflag;
788 case 'z':
789 flags |= SIZET;
790 goto rflag;
791 case 'C':
792 flags |= LONGINT;
793 /*FALLTHROUGH*/
794 case 'c':
795 if (flags & LONGINT) {
796 mbstate_t mbs;
797 size_t mbseqlen;
798
799 memset(&mbs, 0, sizeof(mbs));
800 mbseqlen = wcrtomb(cp = buf,
801 (wchar_t)GETARG(wint_t), &mbs);
802 if (mbseqlen == (size_t)-1) {
803 fp->_flags |= __SERR;
804 goto error;
805 }
806 size = (int)mbseqlen;
807 } else {
808 *(cp = buf) = GETARG(int);
809 size = 1;
810 }
811 sign = '\0';
812 break;
813 case 'D':
814 flags |= LONGINT;
815 /*FALLTHROUGH*/
816 case 'd':
817 case 'i':
818 if (flags & INTMAX_SIZE) {
819 ujval = SJARG();
820 if ((intmax_t)ujval < 0) {
821 ujval = -ujval;
822 sign = '-';
823 }
824 } else {
825 ulval = SARG();
826 if ((long)ulval < 0) {
827 ulval = -ulval;
828 sign = '-';
829 }
830 }
831 base = 10;
832 goto number;
833#ifdef FLOATING_POINT
834#ifdef HEXFLOAT
835 case 'a':
836 case 'A':
837 if (ch == 'a') {
838 ox[1] = 'x';
839 xdigs = xdigs_lower;
840 expchar = 'p';
841 } else {
842 ox[1] = 'X';
843 xdigs = xdigs_upper;
844 expchar = 'P';
845 }
846 /*
847 * XXX We don't actually have a conversion
848 * XXX routine for this yet.
849 */
850 if (flags & LONGDBL) {
851 fparg.ldbl = (double)GETARG(long double);
852 dtoaresult = cp =
853 __hldtoa(fparg.ldbl, xdigs, prec,
854 &expt, &signflag, &dtoaend);
855 } else {
856 fparg.dbl = GETARG(double);
857 dtoaresult = cp =
858 __hdtoa(fparg.dbl, xdigs, prec,
859 &expt, &signflag, &dtoaend);
860 }
861 goto fp_begin;
862#endif
863 case 'e':
864 case 'E':
865 expchar = ch;
866 if (prec < 0) /* account for digit before decpt */
867 prec = DEFPREC + 1;
868 else
869 prec++;
870 goto fp_begin;
871 case 'f':
872 case 'F':
873 expchar = '\0';
874 goto fp_begin;
875 case 'g':
876 case 'G':
877 expchar = ch - ('g' - 'e');
878 if (prec == 0)
879 prec = 1;
880fp_begin:
881 if (prec < 0)
882 prec = DEFPREC;
883 if (dtoaresult != NULL)
884 freedtoa(dtoaresult);
885 if (flags & LONGDBL) {
886 fparg.ldbl = GETARG(long double);
887 dtoaresult = cp =
888 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
889 &expt, &signflag, &dtoaend);
890 } else {
891 fparg.dbl = GETARG(double);
892 dtoaresult = cp =
893 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
894 &expt, &signflag, &dtoaend);
895 if (expt == 9999)
896 expt = INT_MAX;
897 }
898 if (signflag)
899 sign = '-';
900 if (expt == INT_MAX) { /* inf or nan */
901 if (*cp == 'N') {
902 cp = (ch >= 'a') ? "nan" : "NAN";
903 sign = '\0';
904 } else
905 cp = (ch >= 'a') ? "inf" : "INF";
906 size = 3;
907 break;
908 }
909 flags |= FPT;
910 ndig = dtoaend - cp;
911 if (ch == 'g' || ch == 'G') {
912 if (expt > -4 && expt <= prec) {
913 /* Make %[gG] smell like %[fF] */
914 expchar = '\0';
915 if (flags & ALT)
916 prec -= expt;
917 else
918 prec = ndig - expt;
919 if (prec < 0)
920 prec = 0;
921 }
922 }
923 if (expchar) {
924 expsize = exponent(expstr, expt - 1, expchar);
925 size = expsize + prec;
926 if (prec > 1 || flags & ALT)
927 ++size;
928 } else {
929 if (expt > 0) {
930 size = expt;
931 if (prec || flags & ALT)
932 size += prec + 1;
933 } else /* "0.X" */
934 size = prec + 2;
935 if (grouping && expt > 0) {
936 /* space for thousands' grouping */
937 nseps = nrepeats = 0;
938 lead = expt;
939 while (*grouping != CHAR_MAX) {
940 if (lead <= *grouping)
941 break;
942 lead -= *grouping;
943 if (*(grouping+1)) {
944 nseps++;
945 grouping++;
946 } else
947 nrepeats++;
948 }
949 size += nseps + nrepeats;
950 } else
951 lead = expt;
952 }
953 break;
954#endif /* FLOATING_POINT */
955 case 'n':
956 /*
957 * Assignment-like behavior is specified if the
958 * value overflows or is otherwise unrepresentable.
959 * C99 says to use `signed char' for %hhn conversions.
960 */
961 if (flags & LLONGINT)
962 *GETARG(long long *) = ret;
963 else if (flags & SIZET)
964 *GETARG(ssize_t *) = (ssize_t)ret;
965 else if (flags & PTRDIFFT)
966 *GETARG(ptrdiff_t *) = ret;
967 else if (flags & INTMAXT)
968 *GETARG(intmax_t *) = ret;
969 else if (flags & LONGINT)
970 *GETARG(long *) = ret;
971 else if (flags & SHORTINT)
972 *GETARG(short *) = ret;
973 else if (flags & CHARINT)
974 *GETARG(signed char *) = ret;
975 else
976 *GETARG(int *) = ret;
977 continue; /* no output */
978 case 'O':
979 flags |= LONGINT;
980 /*FALLTHROUGH*/
981 case 'o':
982 if (flags & INTMAX_SIZE)
983 ujval = UJARG();
984 else
985 ulval = UARG();
986 base = 8;
987 goto nosign;
988 case 'p':
989 /*-
990 * ``The argument shall be a pointer to void. The
991 * value of the pointer is converted to a sequence
992 * of printable characters, in an implementation-
993 * defined manner.''
994 * -- ANSI X3J11
995 */
996 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
997 base = 16;
998 xdigs = xdigs_lower;
999 flags = flags | INTMAXT;
1000 ox[1] = 'x';
1001 goto nosign;
1002 case 'S':
1003 flags |= LONGINT;
1004 /*FALLTHROUGH*/
1005 case 's':
1006 if (flags & LONGINT) {
1007 wchar_t *wcp;
1008
1009 if (convbuf != NULL)
1010 free(convbuf);
1011 if ((wcp = GETARG(wchar_t *)) == NULL)
1012 cp = "(null)";
1013 else {
1014 convbuf = __wcsconv(wcp, prec);
1015 if (convbuf == NULL) {
1016 fp->_flags |= __SERR;
1017 goto error;
1018 }
1019 cp = convbuf;
1020 }
1021 } else if ((cp = GETARG(char *)) == NULL)
1022 cp = "(null)";
1023 if (prec >= 0) {
1024 /*
1025 * can't use strlen; can only look for the
1026 * NUL in the first `prec' characters, and
1027 * strlen() will go further.
1028 */
1029 char *p = memchr(cp, 0, (size_t)prec);
1030
1031 if (p != NULL) {
1032 size = p - cp;
1033 if (size > prec)
1034 size = prec;
1035 } else
1036 size = prec;
1037 } else
1038 size = strlen(cp);
1039 sign = '\0';
1040 break;
1041 case 'U':
1042 flags |= LONGINT;
1043 /*FALLTHROUGH*/
1044 case 'u':
1045 if (flags & INTMAX_SIZE)
1046 ujval = UJARG();
1047 else
1048 ulval = UARG();
1049 base = 10;
1050 goto nosign;
1051 case 'X':
1052 xdigs = xdigs_upper;
1053 goto hex;
1054 case 'x':
1055 xdigs = xdigs_lower;
1056hex:
1057 if (flags & INTMAX_SIZE)
1058 ujval = UJARG();
1059 else
1060 ulval = UARG();
1061 base = 16;
1062 /* leading 0x/X only if non-zero */
1063 if (flags & ALT &&
1064 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1065 ox[1] = ch;
1066
1067 flags &= ~GROUPING;
1068 /* unsigned conversions */
1069nosign: sign = '\0';
1070 /*-
1071 * ``... diouXx conversions ... if a precision is
1072 * specified, the 0 flag will be ignored.''
1073 * -- ANSI X3J11
1074 */
1075number: if ((dprec = prec) >= 0)
1076 flags &= ~ZEROPAD;
1077
1078 /*-
1079 * ``The result of converting a zero value with an
1080 * explicit precision of zero is no characters.''
1081 * -- ANSI X3J11
1082 */
1083 cp = buf + BUF;
1084 if (flags & INTMAX_SIZE) {
1085 if (ujval != 0 || prec != 0)
1086 cp = __ujtoa(ujval, cp, base,
1087 flags & ALT, xdigs,
1088 flags & GROUPING, thousands_sep,
1089 grouping);
1090 } else {
1091 if (ulval != 0 || prec != 0)
1092 cp = __ultoa(ulval, cp, base,
1093 flags & ALT, xdigs,
1094 flags & GROUPING, thousands_sep,
1095 grouping);
1096 }
1097 size = buf + BUF - cp;
1098 if (size > BUF) /* should never happen */
1099 abort();
1100 break;
1101 default: /* "%?" prints ?, unless ? is NUL */
1102 if (ch == '\0')
1103 goto done;
1104 /* pretend it was %c with argument ch */
1105 cp = buf;
1106 *cp = ch;
1107 size = 1;
1108 sign = '\0';
1109 break;
1110 }
1111
1112 /*
1113 * All reasonable formats wind up here. At this point, `cp'
1114 * points to a string which (if not flags&LADJUST) should be
1115 * padded out to `width' places. If flags&ZEROPAD, it should
1116 * first be prefixed by any sign or other prefix; otherwise,
1117 * it should be blank padded before the prefix is emitted.
1118 * After any left-hand padding and prefixing, emit zeroes
1119 * required by a decimal [diouxX] precision, then print the
1120 * string proper, then emit zeroes required by any leftover
1121 * floating precision; finally, if LADJUST, pad with blanks.
1122 *
1123 * Compute actual size, so we know how much to pad.
1124 * size excludes decimal prec; realsz includes it.
1125 */
1126 realsz = dprec > size ? dprec : size;
1127 if (sign)
1128 realsz++;
1129 else if (ox[1])
1130 realsz += 2;
1131
1132 prsize = width > realsz ? width : realsz;
1133 if ((unsigned)ret + prsize > INT_MAX) {
1134 ret = EOF;
1135 goto error;
1136 }
1137
1138 /* right-adjusting blank padding */
1139 if ((flags & (LADJUST|ZEROPAD)) == 0)
1140 PAD(width - realsz, blanks);
1141
1142 /* prefix */
1143 if (sign) {
1144 PRINT(&sign, 1);
1145 } else if (ox[1]) { /* ox[1] is either x, X, or \0 */
1146 ox[0] = '0';
1147 PRINT(ox, 2);
1148 }
1149
1150 /* right-adjusting zero padding */
1151 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1152 PAD(width - realsz, zeroes);
1153
1154 /* leading zeroes from decimal precision */
1155 PAD(dprec - size, zeroes);
1156
1157 /* the string or number proper */
1158#ifdef FLOATING_POINT
1159 if ((flags & FPT) == 0) {
1160 PRINT(cp, size);
1161 } else { /* glue together f_p fragments */
1162 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1163 if (expt <= 0) {
1164 buf[0] = '0';
1165 buf[1] = *decimal_point;
1166 PRINT(buf, 2);
1167 PAD(-expt, zeroes);
1168 /* already handled initial 0's */
1169 prec += expt;
1170 } else {
1171 PRINTANDPAD(cp, dtoaend, lead, zeroes);
1172 cp += lead;
1173 if (grouping) {
1174 while (nseps>0 || nrepeats>0) {
1175 if (nrepeats > 0)
1176 nrepeats--;
1177 else {
1178 grouping--;
1179 nseps--;
1180 }
1181 PRINT(&thousands_sep,
1182 1);
1183 PRINTANDPAD(cp,dtoaend,
1184 *grouping, zeroes);
1185 cp += *grouping;
1186 }
1187 if (cp > dtoaend)
1188 cp = dtoaend;
1189 }
1190 if (prec || flags & ALT)
1191 PRINT(decimal_point,1);
1192 }
1193 PRINTANDPAD(cp, dtoaend, prec, zeroes);
1194 } else { /* %[eE] or sufficiently long %[gG] */
1195 if (prec > 1 || flags & ALT) {
1196 buf[0] = *cp++;
1197 buf[1] = *decimal_point;
1198 PRINT(buf, 2);
1199 PRINT(cp, ndig-1);
1200 PAD(prec - ndig, zeroes);
1201 } else /* XeYYY */
1202 PRINT(cp, 1);
1203 PRINT(expstr, expsize);
1204 }
1205 }
1206#else
1207 PRINT(cp, size);
1208#endif
1209 /* left-adjusting padding (always blank) */
1210 if (flags & LADJUST)
1211 PAD(width - realsz, blanks);
1212
1213 /* finally, adjust ret */
1214 ret += prsize;
1215
1216 FLUSH(); /* copy out the I/O vectors */
1217 }
1218done:
1219 FLUSH();
1220error:
1221#ifdef FLOATING_POINT
1222 if (dtoaresult != NULL)
1223 freedtoa(dtoaresult);
1224#endif
1225 if (convbuf != NULL)
1226 free(convbuf);
1227 if (__sferror(fp))
1228 ret = EOF;
1229 if ((argtable != NULL) && (argtable != statargtable))
1230 free (argtable);
1231 return (ret);
1232 /* NOTREACHED */
1233}
1234
1235/*
1236 * Find all arguments when a positional parameter is encountered. Returns a
1237 * table, indexed by argument number, of pointers to each arguments. The
1238 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1239 * It will be replaces with a malloc-ed one if it overflows.
1240 */
1241static void
1242__find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1243{
1244 char *fmt; /* format string */
1245 int ch; /* character from fmt */
1246 int n, n2; /* handy integer (short term usage) */
1247 char *cp; /* handy char pointer (short term usage) */
1248 int flags; /* flags as above */
1249 int width; /* width from format (%8d), or 0 */
1250 enum typeid *typetable; /* table of types */
1251 enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1252 int tablesize; /* current size of type table */
1253 int tablemax; /* largest used index in table */
1254 int nextarg; /* 1-based argument index */
1255
1256 /*
1257 * Add an argument type to the table, expanding if necessary.
1258 */
1259#define ADDTYPE(type) \
1260 ((nextarg >= tablesize) ? \
1261 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1262 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1263 typetable[nextarg++] = type)
1264
1265#define ADDSARG() \
1266 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1267 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1268 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1269 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1270 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1271
1272#define ADDUARG() \
1273 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1274 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1275 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1276 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1277 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1278
1279 /*
1280 * Add * arguments to the type array.
1281 */
1282#define ADDASTER() \
1283 n2 = 0; \
1284 cp = fmt; \
1285 while (is_digit(*cp)) { \
1286 n2 = 10 * n2 + to_digit(*cp); \
1287 cp++; \
1288 } \
1289 if (*cp == '$') { \
1290 int hold = nextarg; \
1291 nextarg = n2; \
1292 ADDTYPE (T_INT); \
1293 nextarg = hold; \
1294 fmt = ++cp; \
1295 } else { \
1296 ADDTYPE (T_INT); \
1297 }
1298 fmt = (char *)fmt0;
1299 typetable = stattypetable;
1300 tablesize = STATIC_ARG_TBL_SIZE;
1301 tablemax = 0;
1302 nextarg = 1;
1303 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1304
1305 /*
1306 * Scan the format for conversions (`%' character).
1307 */
1308 for (;;) {
1309 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1310 /* void */;
1311 if (ch == '\0')
1312 goto done;
1313 fmt++; /* skip over '%' */
1314
1315 flags = 0;
1316 width = 0;
1317
1318rflag: ch = *fmt++;
1319reswitch: switch (ch) {
1320 case ' ':
1321 case '#':
1322 goto rflag;
1323 case '*':
1324 ADDASTER ();
1325 goto rflag;
1326 case '-':
1327 case '+':
1328 case '\'':
1329 goto rflag;
1330 case '.':
1331 if ((ch = *fmt++) == '*') {
1332 ADDASTER ();
1333 goto rflag;
1334 }
1335 while (is_digit(ch)) {
1336 ch = *fmt++;
1337 }
1338 goto reswitch;
1339 case '0':
1340 goto rflag;
1341 case '1': case '2': case '3': case '4':
1342 case '5': case '6': case '7': case '8': case '9':
1343 n = 0;
1344 do {
1345 n = 10 * n + to_digit(ch);
1346 ch = *fmt++;
1347 } while (is_digit(ch));
1348 if (ch == '$') {
1349 nextarg = n;
1350 goto rflag;
1351 }
1352 width = n;
1353 goto reswitch;
1354#ifdef FLOATING_POINT
1355 case 'L':
1356 flags |= LONGDBL;
1357 goto rflag;
1358#endif
1359 case 'h':
1360 if (flags & SHORTINT) {
1361 flags &= ~SHORTINT;
1362 flags |= CHARINT;
1363 } else
1364 flags |= SHORTINT;
1365 goto rflag;
1366 case 'j':
1367 flags |= INTMAXT;
1368 goto rflag;
1369 case 'l':
1370 if (flags & LONGINT) {
1371 flags &= ~LONGINT;
1372 flags |= LLONGINT;
1373 } else
1374 flags |= LONGINT;
1375 goto rflag;
1376 case 'q':
1377 flags |= LLONGINT; /* not necessarily */
1378 goto rflag;
1379 case 't':
1380 flags |= PTRDIFFT;
1381 goto rflag;
1382 case 'z':
1383 flags |= SIZET;
1384 goto rflag;
1385 case 'C':
1386 flags |= LONGINT;
1387 /*FALLTHROUGH*/
1388 case 'c':
1389 if (flags & LONGINT)
1390 ADDTYPE(T_WINT);
1391 else
1392 ADDTYPE(T_INT);
1393 break;
1394 case 'D':
1395 flags |= LONGINT;
1396 /*FALLTHROUGH*/
1397 case 'd':
1398 case 'i':
1399 ADDSARG();
1400 break;
1401#ifdef FLOATING_POINT
1402#ifdef HEXFLOAT
1403 case 'a':
1404 case 'A':
1405#endif
1406 case 'e':
1407 case 'E':
1408 case 'f':
1409 case 'g':
1410 case 'G':
1411 if (flags & LONGDBL)
1412 ADDTYPE(T_LONG_DOUBLE);
1413 else
1414 ADDTYPE(T_DOUBLE);
1415 break;
1416#endif /* FLOATING_POINT */
1417 case 'n':
1418 if (flags & INTMAXT)
1419 ADDTYPE(TP_INTMAXT);
1420 else if (flags & PTRDIFFT)
1421 ADDTYPE(TP_PTRDIFFT);
1422 else if (flags & SIZET)
1423 ADDTYPE(TP_SIZET);
1424 else if (flags & LLONGINT)
1425 ADDTYPE(TP_LLONG);
1426 else if (flags & LONGINT)
1427 ADDTYPE(TP_LONG);
1428 else if (flags & SHORTINT)
1429 ADDTYPE(TP_SHORT);
1430 else if (flags & CHARINT)
1431 ADDTYPE(TP_SCHAR);
1432 else
1433 ADDTYPE(TP_INT);
1434 continue; /* no output */
1435 case 'O':
1436 flags |= LONGINT;
1437 /*FALLTHROUGH*/
1438 case 'o':
1439 ADDUARG();
1440 break;
1441 case 'p':
1442 ADDTYPE(TP_VOID);
1443 break;
1444 case 'S':
1445 flags |= LONGINT;
1446 /*FALLTHROUGH*/
1447 case 's':
1448 if (flags & LONGINT)
1449 ADDTYPE(TP_WCHAR);
1450 else
1451 ADDTYPE(TP_CHAR);
1452 break;
1453 case 'U':
1454 flags |= LONGINT;
1455 /*FALLTHROUGH*/
1456 case 'u':
1457 case 'X':
1458 case 'x':
1459 ADDUARG();
1460 break;
1461 default: /* "%?" prints ?, unless ? is NUL */
1462 if (ch == '\0')
1463 goto done;
1464 break;
1465 }
1466 }
1467done:
1468 /*
1469 * Build the argument table.
1470 */
1471 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1472 *argtable = (union arg *)
1473 malloc (sizeof (union arg) * (tablemax + 1));
1474 }
1475
1476 (*argtable) [0].intarg = 0;
1477 for (n = 1; n <= tablemax; n++) {
1478 switch (typetable [n]) {
1479 case T_UNUSED: /* whoops! */
1480 (*argtable) [n].intarg = va_arg (ap, int);
1481 break;
1482 case TP_SCHAR:
1483 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1484 break;
1485 case TP_SHORT:
1486 (*argtable) [n].pshortarg = va_arg (ap, short *);
1487 break;
1488 case T_INT:
1489 (*argtable) [n].intarg = va_arg (ap, int);
1490 break;
1491 case T_U_INT:
1492 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
1493 break;
1494 case TP_INT:
1495 (*argtable) [n].pintarg = va_arg (ap, int *);
1496 break;
1497 case T_LONG:
1498 (*argtable) [n].longarg = va_arg (ap, long);
1499 break;
1500 case T_U_LONG:
1501 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1502 break;
1503 case TP_LONG:
1504 (*argtable) [n].plongarg = va_arg (ap, long *);
1505 break;
1506 case T_LLONG:
1507 (*argtable) [n].longlongarg = va_arg (ap, long long);
1508 break;
1509 case T_U_LLONG:
1510 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1511 break;
1512 case TP_LLONG:
1513 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
1514 break;
1515 case T_PTRDIFFT:
1516 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1517 break;
1518 case TP_PTRDIFFT:
1519 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1520 break;
1521 case T_SIZET:
1522 (*argtable) [n].sizearg = va_arg (ap, size_t);
1523 break;
1524 case TP_SIZET:
1525 (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
1526 break;
1527 case T_INTMAXT:
1528 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1529 break;
1530 case T_UINTMAXT:
1531 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1532 break;
1533 case TP_INTMAXT:
1534 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1535 break;
1536#ifdef FLOATING_POINT
1537 case T_DOUBLE:
1538 (*argtable) [n].doublearg = va_arg (ap, double);
1539 break;
1540 case T_LONG_DOUBLE:
1541 (*argtable) [n].longdoublearg = va_arg (ap, long double);
1542 break;
1543#endif
1544 case TP_CHAR:
1545 (*argtable) [n].pchararg = va_arg (ap, char *);
1546 break;
1547 case TP_VOID:
1548 (*argtable) [n].pvoidarg = va_arg (ap, void *);
1549 break;
1550 case T_WINT:
1551 (*argtable) [n].wintarg = va_arg (ap, wint_t);
1552 break;
1553 case TP_WCHAR:
1554 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1555 break;
1556 }
1557 }
1558
1559 if ((typetable != NULL) && (typetable != stattypetable))
1560 free (typetable);
1561}
1562
1563/*
1564 * Increase the size of the type table.
1565 */
1566static void
1567__grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
1568{
1569 enum typeid *const oldtable = *typetable;
1570 const int oldsize = *tablesize;
1571 enum typeid *newtable;
1572 int newsize = oldsize * 2;
1573
1574 if (newsize < nextarg + 1)
1575 newsize = nextarg + 1;
1576 if (oldsize == STATIC_ARG_TBL_SIZE) {
1577 if ((newtable = malloc(newsize)) == NULL)
1578 abort(); /* XXX handle better */
1579 bcopy(oldtable, newtable, oldsize);
1580 } else {
1581 if ((newtable = reallocf(oldtable, newsize)) == NULL)
1582 abort(); /* XXX handle better */
1583 }
1584 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1585
1586 *typetable = newtable;
1587 *tablesize = newsize;
1588}
1589
1590
1591#ifdef FLOATING_POINT
1592
1593static int
1594exponent(char *p0, int exp, int fmtch)
1595{
1596 char *p, *t;
1597 char expbuf[MAXEXPDIG];
1598
1599 p = p0;
1600 *p++ = fmtch;
1601 if (exp < 0) {
1602 exp = -exp;
1603 *p++ = '-';
1604 }
1605 else
1606 *p++ = '+';
1607 t = expbuf + MAXEXPDIG;
1608 if (exp > 9) {
1609 do {
1610 *--t = to_char(exp % 10);
1611 } while ((exp /= 10) > 9);
1612 *--t = to_char(exp);
1613 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1614 }
1615 else {
1616 /*
1617 * Exponents for decimal floating point conversions
1618 * (%[eEgG]) must be at least two characters long,
1619 * whereas exponents for hexadecimal conversions can
1620 * be only one character long.
1621 */
1622 if (fmtch == 'e' || fmtch == 'E')
1623 *p++ = '0';
1624 *p++ = to_char(exp);
1625 }
1626 return (p - p0);
1627}
1628#endif /* FLOATING_POINT */
392
393 return (convbuf);
394}
395
396/*
397 * MT-safe version
398 */
399int
400vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap)
401
402{
403 int ret;
404
405 FLOCKFILE(fp);
406 ret = __vfprintf(fp, fmt0, ap);
407 FUNLOCKFILE(fp);
408 return (ret);
409}
410
411#ifdef FLOATING_POINT
412
413#define dtoa __dtoa
414#define freedtoa __freedtoa
415
416#include <float.h>
417#include <math.h>
418#include "floatio.h"
419#include "gdtoa.h"
420
421#define DEFPREC 6
422
423static int exponent(char *, int, int);
424
425#endif /* FLOATING_POINT */
426
427/*
428 * The size of the buffer we use as scratch space for integer
429 * conversions, among other things. Technically, we would need the
430 * most space for base 10 conversions with thousands' grouping
431 * characters between each pair of digits. 100 bytes is a
432 * conservative overestimate even for a 128-bit uintmax_t.
433 */
434#define BUF 100
435
436#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
437
438/*
439 * Flags used during conversion.
440 */
441#define ALT 0x001 /* alternate form */
442#define LADJUST 0x004 /* left adjustment */
443#define LONGDBL 0x008 /* long double */
444#define LONGINT 0x010 /* long integer */
445#define LLONGINT 0x020 /* long long integer */
446#define SHORTINT 0x040 /* short integer */
447#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
448#define FPT 0x100 /* Floating point number */
449#define GROUPING 0x200 /* use grouping ("'" flag) */
450 /* C99 additional size modifiers: */
451#define SIZET 0x400 /* size_t */
452#define PTRDIFFT 0x800 /* ptrdiff_t */
453#define INTMAXT 0x1000 /* intmax_t */
454#define CHARINT 0x2000 /* print char using int format */
455
456/*
457 * Non-MT-safe version
458 */
459int
460__vfprintf(FILE *fp, const char *fmt0, va_list ap)
461{
462 char *fmt; /* format string */
463 int ch; /* character from fmt */
464 int n, n2; /* handy integer (short term usage) */
465 char *cp; /* handy char pointer (short term usage) */
466 struct __siov *iovp; /* for PRINT macro */
467 int flags; /* flags as above */
468 int ret; /* return value accumulator */
469 int width; /* width from format (%8d), or 0 */
470 int prec; /* precision from format; <0 for N/A */
471 char sign; /* sign prefix (' ', '+', '-', or \0) */
472 char thousands_sep; /* locale specific thousands separator */
473 const char *grouping; /* locale specific numeric grouping rules */
474#ifdef FLOATING_POINT
475 /*
476 * We can decompose the printed representation of floating
477 * point numbers into several parts, some of which may be empty:
478 *
479 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
480 * A B ---C--- D E F
481 *
482 * A: 'sign' holds this value if present; '\0' otherwise
483 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
484 * C: cp points to the string MMMNNN. Leading and trailing
485 * zeros are not in the string and must be added.
486 * D: expchar holds this character; '\0' if no exponent, e.g. %f
487 * F: at least two digits for decimal, at least one digit for hex
488 */
489 char *decimal_point; /* locale specific decimal point */
490 int signflag; /* true if float is negative */
491 union { /* floating point arguments %[aAeEfFgG] */
492 double dbl;
493 long double ldbl;
494 } fparg;
495 int expt; /* integer value of exponent */
496 char expchar; /* exponent character: [eEpP\0] */
497 char *dtoaend; /* pointer to end of converted digits */
498 int expsize; /* character count for expstr */
499 int lead; /* sig figs before decimal or group sep */
500 int ndig; /* actual number of digits returned by dtoa */
501 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
502 char *dtoaresult; /* buffer allocated by dtoa */
503 int nseps; /* number of group separators with ' */
504 int nrepeats; /* number of repeats of the last group */
505#endif
506 u_long ulval; /* integer arguments %[diouxX] */
507 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
508 int base; /* base for [diouxX] conversion */
509 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
510 int realsz; /* field size expanded by dprec, sign, etc */
511 int size; /* size of converted field or string */
512 int prsize; /* max size of printed field */
513 const char *xdigs; /* digits for %[xX] conversion */
514#define NIOV 8
515 struct __suio uio; /* output information: summary */
516 struct __siov iov[NIOV];/* ... and individual io vectors */
517 char buf[BUF]; /* buffer with space for digits of uintmax_t */
518 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
519 union arg *argtable; /* args, built due to positional arg */
520 union arg statargtable [STATIC_ARG_TBL_SIZE];
521 int nextarg; /* 1-based argument index */
522 va_list orgap; /* original argument pointer */
523 char *convbuf; /* wide to multibyte conversion result */
524
525 /*
526 * Choose PADSIZE to trade efficiency vs. size. If larger printf
527 * fields occur frequently, increase PADSIZE and make the initialisers
528 * below longer.
529 */
530#define PADSIZE 16 /* pad chunk size */
531 static char blanks[PADSIZE] =
532 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
533 static char zeroes[PADSIZE] =
534 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
535
536 static const char xdigs_lower[16] = "0123456789abcdef";
537 static const char xdigs_upper[16] = "0123456789ABCDEF";
538
539 /*
540 * BEWARE, these `goto error' on error, and PAD uses `n'.
541 */
542#define PRINT(ptr, len) { \
543 iovp->iov_base = (ptr); \
544 iovp->iov_len = (len); \
545 uio.uio_resid += (len); \
546 iovp++; \
547 if (++uio.uio_iovcnt >= NIOV) { \
548 if (__sprint(fp, &uio)) \
549 goto error; \
550 iovp = iov; \
551 } \
552}
553#define PAD(howmany, with) { \
554 if ((n = (howmany)) > 0) { \
555 while (n > PADSIZE) { \
556 PRINT(with, PADSIZE); \
557 n -= PADSIZE; \
558 } \
559 PRINT(with, n); \
560 } \
561}
562#define PRINTANDPAD(p, ep, len, with) do { \
563 n2 = (ep) - (p); \
564 if (n2 > (len)) \
565 n2 = (len); \
566 if (n2 > 0) \
567 PRINT((p), n2); \
568 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
569} while(0)
570#define FLUSH() { \
571 if (uio.uio_resid && __sprint(fp, &uio)) \
572 goto error; \
573 uio.uio_iovcnt = 0; \
574 iovp = iov; \
575}
576
577 /*
578 * Get the argument indexed by nextarg. If the argument table is
579 * built, use it to get the argument. If its not, get the next
580 * argument (and arguments must be gotten sequentially).
581 */
582#define GETARG(type) \
583 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
584 (nextarg++, va_arg(ap, type)))
585
586 /*
587 * To extend shorts properly, we need both signed and unsigned
588 * argument extraction methods.
589 */
590#define SARG() \
591 (flags&LONGINT ? GETARG(long) : \
592 flags&SHORTINT ? (long)(short)GETARG(int) : \
593 flags&CHARINT ? (long)(signed char)GETARG(int) : \
594 (long)GETARG(int))
595#define UARG() \
596 (flags&LONGINT ? GETARG(u_long) : \
597 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
598 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
599 (u_long)GETARG(u_int))
600#define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
601#define SJARG() \
602 (flags&INTMAXT ? GETARG(intmax_t) : \
603 flags&SIZET ? (intmax_t)GETARG(size_t) : \
604 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
605 (intmax_t)GETARG(long long))
606#define UJARG() \
607 (flags&INTMAXT ? GETARG(uintmax_t) : \
608 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
609 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
610 (uintmax_t)GETARG(unsigned long long))
611
612 /*
613 * Get * arguments, including the form *nn$. Preserve the nextarg
614 * that the argument can be gotten once the type is determined.
615 */
616#define GETASTER(val) \
617 n2 = 0; \
618 cp = fmt; \
619 while (is_digit(*cp)) { \
620 n2 = 10 * n2 + to_digit(*cp); \
621 cp++; \
622 } \
623 if (*cp == '$') { \
624 int hold = nextarg; \
625 if (argtable == NULL) { \
626 argtable = statargtable; \
627 __find_arguments (fmt0, orgap, &argtable); \
628 } \
629 nextarg = n2; \
630 val = GETARG (int); \
631 nextarg = hold; \
632 fmt = ++cp; \
633 } else { \
634 val = GETARG (int); \
635 }
636
637
638 thousands_sep = '\0';
639 grouping = NULL;
640 convbuf = NULL;
641#ifdef FLOATING_POINT
642 dtoaresult = NULL;
643 decimal_point = localeconv()->decimal_point;
644#endif
645 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
646 if (cantwrite(fp))
647 return (EOF);
648
649 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
650 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
651 fp->_file >= 0)
652 return (__sbprintf(fp, fmt0, ap));
653
654 fmt = (char *)fmt0;
655 argtable = NULL;
656 nextarg = 1;
657 va_copy(orgap, ap);
658 uio.uio_iov = iovp = iov;
659 uio.uio_resid = 0;
660 uio.uio_iovcnt = 0;
661 ret = 0;
662
663 /*
664 * Scan the format for conversions (`%' character).
665 */
666 for (;;) {
667 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
668 /* void */;
669 if ((n = fmt - cp) != 0) {
670 if ((unsigned)ret + n > INT_MAX) {
671 ret = EOF;
672 goto error;
673 }
674 PRINT(cp, n);
675 ret += n;
676 }
677 if (ch == '\0')
678 goto done;
679 fmt++; /* skip over '%' */
680
681 flags = 0;
682 dprec = 0;
683 width = 0;
684 prec = -1;
685 sign = '\0';
686 ox[1] = '\0';
687
688rflag: ch = *fmt++;
689reswitch: switch (ch) {
690 case ' ':
691 /*-
692 * ``If the space and + flags both appear, the space
693 * flag will be ignored.''
694 * -- ANSI X3J11
695 */
696 if (!sign)
697 sign = ' ';
698 goto rflag;
699 case '#':
700 flags |= ALT;
701 goto rflag;
702 case '*':
703 /*-
704 * ``A negative field width argument is taken as a
705 * - flag followed by a positive field width.''
706 * -- ANSI X3J11
707 * They don't exclude field widths read from args.
708 */
709 GETASTER (width);
710 if (width >= 0)
711 goto rflag;
712 width = -width;
713 /* FALLTHROUGH */
714 case '-':
715 flags |= LADJUST;
716 goto rflag;
717 case '+':
718 sign = '+';
719 goto rflag;
720 case '\'':
721 flags |= GROUPING;
722 thousands_sep = *(localeconv()->thousands_sep);
723 grouping = localeconv()->grouping;
724 goto rflag;
725 case '.':
726 if ((ch = *fmt++) == '*') {
727 GETASTER (prec);
728 goto rflag;
729 }
730 prec = 0;
731 while (is_digit(ch)) {
732 prec = 10 * prec + to_digit(ch);
733 ch = *fmt++;
734 }
735 goto reswitch;
736 case '0':
737 /*-
738 * ``Note that 0 is taken as a flag, not as the
739 * beginning of a field width.''
740 * -- ANSI X3J11
741 */
742 flags |= ZEROPAD;
743 goto rflag;
744 case '1': case '2': case '3': case '4':
745 case '5': case '6': case '7': case '8': case '9':
746 n = 0;
747 do {
748 n = 10 * n + to_digit(ch);
749 ch = *fmt++;
750 } while (is_digit(ch));
751 if (ch == '$') {
752 nextarg = n;
753 if (argtable == NULL) {
754 argtable = statargtable;
755 __find_arguments (fmt0, orgap,
756 &argtable);
757 }
758 goto rflag;
759 }
760 width = n;
761 goto reswitch;
762#ifdef FLOATING_POINT
763 case 'L':
764 flags |= LONGDBL;
765 goto rflag;
766#endif
767 case 'h':
768 if (flags & SHORTINT) {
769 flags &= ~SHORTINT;
770 flags |= CHARINT;
771 } else
772 flags |= SHORTINT;
773 goto rflag;
774 case 'j':
775 flags |= INTMAXT;
776 goto rflag;
777 case 'l':
778 if (flags & LONGINT) {
779 flags &= ~LONGINT;
780 flags |= LLONGINT;
781 } else
782 flags |= LONGINT;
783 goto rflag;
784 case 'q':
785 flags |= LLONGINT; /* not necessarily */
786 goto rflag;
787 case 't':
788 flags |= PTRDIFFT;
789 goto rflag;
790 case 'z':
791 flags |= SIZET;
792 goto rflag;
793 case 'C':
794 flags |= LONGINT;
795 /*FALLTHROUGH*/
796 case 'c':
797 if (flags & LONGINT) {
798 mbstate_t mbs;
799 size_t mbseqlen;
800
801 memset(&mbs, 0, sizeof(mbs));
802 mbseqlen = wcrtomb(cp = buf,
803 (wchar_t)GETARG(wint_t), &mbs);
804 if (mbseqlen == (size_t)-1) {
805 fp->_flags |= __SERR;
806 goto error;
807 }
808 size = (int)mbseqlen;
809 } else {
810 *(cp = buf) = GETARG(int);
811 size = 1;
812 }
813 sign = '\0';
814 break;
815 case 'D':
816 flags |= LONGINT;
817 /*FALLTHROUGH*/
818 case 'd':
819 case 'i':
820 if (flags & INTMAX_SIZE) {
821 ujval = SJARG();
822 if ((intmax_t)ujval < 0) {
823 ujval = -ujval;
824 sign = '-';
825 }
826 } else {
827 ulval = SARG();
828 if ((long)ulval < 0) {
829 ulval = -ulval;
830 sign = '-';
831 }
832 }
833 base = 10;
834 goto number;
835#ifdef FLOATING_POINT
836#ifdef HEXFLOAT
837 case 'a':
838 case 'A':
839 if (ch == 'a') {
840 ox[1] = 'x';
841 xdigs = xdigs_lower;
842 expchar = 'p';
843 } else {
844 ox[1] = 'X';
845 xdigs = xdigs_upper;
846 expchar = 'P';
847 }
848 /*
849 * XXX We don't actually have a conversion
850 * XXX routine for this yet.
851 */
852 if (flags & LONGDBL) {
853 fparg.ldbl = (double)GETARG(long double);
854 dtoaresult = cp =
855 __hldtoa(fparg.ldbl, xdigs, prec,
856 &expt, &signflag, &dtoaend);
857 } else {
858 fparg.dbl = GETARG(double);
859 dtoaresult = cp =
860 __hdtoa(fparg.dbl, xdigs, prec,
861 &expt, &signflag, &dtoaend);
862 }
863 goto fp_begin;
864#endif
865 case 'e':
866 case 'E':
867 expchar = ch;
868 if (prec < 0) /* account for digit before decpt */
869 prec = DEFPREC + 1;
870 else
871 prec++;
872 goto fp_begin;
873 case 'f':
874 case 'F':
875 expchar = '\0';
876 goto fp_begin;
877 case 'g':
878 case 'G':
879 expchar = ch - ('g' - 'e');
880 if (prec == 0)
881 prec = 1;
882fp_begin:
883 if (prec < 0)
884 prec = DEFPREC;
885 if (dtoaresult != NULL)
886 freedtoa(dtoaresult);
887 if (flags & LONGDBL) {
888 fparg.ldbl = GETARG(long double);
889 dtoaresult = cp =
890 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
891 &expt, &signflag, &dtoaend);
892 } else {
893 fparg.dbl = GETARG(double);
894 dtoaresult = cp =
895 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
896 &expt, &signflag, &dtoaend);
897 if (expt == 9999)
898 expt = INT_MAX;
899 }
900 if (signflag)
901 sign = '-';
902 if (expt == INT_MAX) { /* inf or nan */
903 if (*cp == 'N') {
904 cp = (ch >= 'a') ? "nan" : "NAN";
905 sign = '\0';
906 } else
907 cp = (ch >= 'a') ? "inf" : "INF";
908 size = 3;
909 break;
910 }
911 flags |= FPT;
912 ndig = dtoaend - cp;
913 if (ch == 'g' || ch == 'G') {
914 if (expt > -4 && expt <= prec) {
915 /* Make %[gG] smell like %[fF] */
916 expchar = '\0';
917 if (flags & ALT)
918 prec -= expt;
919 else
920 prec = ndig - expt;
921 if (prec < 0)
922 prec = 0;
923 }
924 }
925 if (expchar) {
926 expsize = exponent(expstr, expt - 1, expchar);
927 size = expsize + prec;
928 if (prec > 1 || flags & ALT)
929 ++size;
930 } else {
931 if (expt > 0) {
932 size = expt;
933 if (prec || flags & ALT)
934 size += prec + 1;
935 } else /* "0.X" */
936 size = prec + 2;
937 if (grouping && expt > 0) {
938 /* space for thousands' grouping */
939 nseps = nrepeats = 0;
940 lead = expt;
941 while (*grouping != CHAR_MAX) {
942 if (lead <= *grouping)
943 break;
944 lead -= *grouping;
945 if (*(grouping+1)) {
946 nseps++;
947 grouping++;
948 } else
949 nrepeats++;
950 }
951 size += nseps + nrepeats;
952 } else
953 lead = expt;
954 }
955 break;
956#endif /* FLOATING_POINT */
957 case 'n':
958 /*
959 * Assignment-like behavior is specified if the
960 * value overflows or is otherwise unrepresentable.
961 * C99 says to use `signed char' for %hhn conversions.
962 */
963 if (flags & LLONGINT)
964 *GETARG(long long *) = ret;
965 else if (flags & SIZET)
966 *GETARG(ssize_t *) = (ssize_t)ret;
967 else if (flags & PTRDIFFT)
968 *GETARG(ptrdiff_t *) = ret;
969 else if (flags & INTMAXT)
970 *GETARG(intmax_t *) = ret;
971 else if (flags & LONGINT)
972 *GETARG(long *) = ret;
973 else if (flags & SHORTINT)
974 *GETARG(short *) = ret;
975 else if (flags & CHARINT)
976 *GETARG(signed char *) = ret;
977 else
978 *GETARG(int *) = ret;
979 continue; /* no output */
980 case 'O':
981 flags |= LONGINT;
982 /*FALLTHROUGH*/
983 case 'o':
984 if (flags & INTMAX_SIZE)
985 ujval = UJARG();
986 else
987 ulval = UARG();
988 base = 8;
989 goto nosign;
990 case 'p':
991 /*-
992 * ``The argument shall be a pointer to void. The
993 * value of the pointer is converted to a sequence
994 * of printable characters, in an implementation-
995 * defined manner.''
996 * -- ANSI X3J11
997 */
998 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
999 base = 16;
1000 xdigs = xdigs_lower;
1001 flags = flags | INTMAXT;
1002 ox[1] = 'x';
1003 goto nosign;
1004 case 'S':
1005 flags |= LONGINT;
1006 /*FALLTHROUGH*/
1007 case 's':
1008 if (flags & LONGINT) {
1009 wchar_t *wcp;
1010
1011 if (convbuf != NULL)
1012 free(convbuf);
1013 if ((wcp = GETARG(wchar_t *)) == NULL)
1014 cp = "(null)";
1015 else {
1016 convbuf = __wcsconv(wcp, prec);
1017 if (convbuf == NULL) {
1018 fp->_flags |= __SERR;
1019 goto error;
1020 }
1021 cp = convbuf;
1022 }
1023 } else if ((cp = GETARG(char *)) == NULL)
1024 cp = "(null)";
1025 if (prec >= 0) {
1026 /*
1027 * can't use strlen; can only look for the
1028 * NUL in the first `prec' characters, and
1029 * strlen() will go further.
1030 */
1031 char *p = memchr(cp, 0, (size_t)prec);
1032
1033 if (p != NULL) {
1034 size = p - cp;
1035 if (size > prec)
1036 size = prec;
1037 } else
1038 size = prec;
1039 } else
1040 size = strlen(cp);
1041 sign = '\0';
1042 break;
1043 case 'U':
1044 flags |= LONGINT;
1045 /*FALLTHROUGH*/
1046 case 'u':
1047 if (flags & INTMAX_SIZE)
1048 ujval = UJARG();
1049 else
1050 ulval = UARG();
1051 base = 10;
1052 goto nosign;
1053 case 'X':
1054 xdigs = xdigs_upper;
1055 goto hex;
1056 case 'x':
1057 xdigs = xdigs_lower;
1058hex:
1059 if (flags & INTMAX_SIZE)
1060 ujval = UJARG();
1061 else
1062 ulval = UARG();
1063 base = 16;
1064 /* leading 0x/X only if non-zero */
1065 if (flags & ALT &&
1066 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1067 ox[1] = ch;
1068
1069 flags &= ~GROUPING;
1070 /* unsigned conversions */
1071nosign: sign = '\0';
1072 /*-
1073 * ``... diouXx conversions ... if a precision is
1074 * specified, the 0 flag will be ignored.''
1075 * -- ANSI X3J11
1076 */
1077number: if ((dprec = prec) >= 0)
1078 flags &= ~ZEROPAD;
1079
1080 /*-
1081 * ``The result of converting a zero value with an
1082 * explicit precision of zero is no characters.''
1083 * -- ANSI X3J11
1084 */
1085 cp = buf + BUF;
1086 if (flags & INTMAX_SIZE) {
1087 if (ujval != 0 || prec != 0)
1088 cp = __ujtoa(ujval, cp, base,
1089 flags & ALT, xdigs,
1090 flags & GROUPING, thousands_sep,
1091 grouping);
1092 } else {
1093 if (ulval != 0 || prec != 0)
1094 cp = __ultoa(ulval, cp, base,
1095 flags & ALT, xdigs,
1096 flags & GROUPING, thousands_sep,
1097 grouping);
1098 }
1099 size = buf + BUF - cp;
1100 if (size > BUF) /* should never happen */
1101 abort();
1102 break;
1103 default: /* "%?" prints ?, unless ? is NUL */
1104 if (ch == '\0')
1105 goto done;
1106 /* pretend it was %c with argument ch */
1107 cp = buf;
1108 *cp = ch;
1109 size = 1;
1110 sign = '\0';
1111 break;
1112 }
1113
1114 /*
1115 * All reasonable formats wind up here. At this point, `cp'
1116 * points to a string which (if not flags&LADJUST) should be
1117 * padded out to `width' places. If flags&ZEROPAD, it should
1118 * first be prefixed by any sign or other prefix; otherwise,
1119 * it should be blank padded before the prefix is emitted.
1120 * After any left-hand padding and prefixing, emit zeroes
1121 * required by a decimal [diouxX] precision, then print the
1122 * string proper, then emit zeroes required by any leftover
1123 * floating precision; finally, if LADJUST, pad with blanks.
1124 *
1125 * Compute actual size, so we know how much to pad.
1126 * size excludes decimal prec; realsz includes it.
1127 */
1128 realsz = dprec > size ? dprec : size;
1129 if (sign)
1130 realsz++;
1131 else if (ox[1])
1132 realsz += 2;
1133
1134 prsize = width > realsz ? width : realsz;
1135 if ((unsigned)ret + prsize > INT_MAX) {
1136 ret = EOF;
1137 goto error;
1138 }
1139
1140 /* right-adjusting blank padding */
1141 if ((flags & (LADJUST|ZEROPAD)) == 0)
1142 PAD(width - realsz, blanks);
1143
1144 /* prefix */
1145 if (sign) {
1146 PRINT(&sign, 1);
1147 } else if (ox[1]) { /* ox[1] is either x, X, or \0 */
1148 ox[0] = '0';
1149 PRINT(ox, 2);
1150 }
1151
1152 /* right-adjusting zero padding */
1153 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1154 PAD(width - realsz, zeroes);
1155
1156 /* leading zeroes from decimal precision */
1157 PAD(dprec - size, zeroes);
1158
1159 /* the string or number proper */
1160#ifdef FLOATING_POINT
1161 if ((flags & FPT) == 0) {
1162 PRINT(cp, size);
1163 } else { /* glue together f_p fragments */
1164 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1165 if (expt <= 0) {
1166 buf[0] = '0';
1167 buf[1] = *decimal_point;
1168 PRINT(buf, 2);
1169 PAD(-expt, zeroes);
1170 /* already handled initial 0's */
1171 prec += expt;
1172 } else {
1173 PRINTANDPAD(cp, dtoaend, lead, zeroes);
1174 cp += lead;
1175 if (grouping) {
1176 while (nseps>0 || nrepeats>0) {
1177 if (nrepeats > 0)
1178 nrepeats--;
1179 else {
1180 grouping--;
1181 nseps--;
1182 }
1183 PRINT(&thousands_sep,
1184 1);
1185 PRINTANDPAD(cp,dtoaend,
1186 *grouping, zeroes);
1187 cp += *grouping;
1188 }
1189 if (cp > dtoaend)
1190 cp = dtoaend;
1191 }
1192 if (prec || flags & ALT)
1193 PRINT(decimal_point,1);
1194 }
1195 PRINTANDPAD(cp, dtoaend, prec, zeroes);
1196 } else { /* %[eE] or sufficiently long %[gG] */
1197 if (prec > 1 || flags & ALT) {
1198 buf[0] = *cp++;
1199 buf[1] = *decimal_point;
1200 PRINT(buf, 2);
1201 PRINT(cp, ndig-1);
1202 PAD(prec - ndig, zeroes);
1203 } else /* XeYYY */
1204 PRINT(cp, 1);
1205 PRINT(expstr, expsize);
1206 }
1207 }
1208#else
1209 PRINT(cp, size);
1210#endif
1211 /* left-adjusting padding (always blank) */
1212 if (flags & LADJUST)
1213 PAD(width - realsz, blanks);
1214
1215 /* finally, adjust ret */
1216 ret += prsize;
1217
1218 FLUSH(); /* copy out the I/O vectors */
1219 }
1220done:
1221 FLUSH();
1222error:
1223#ifdef FLOATING_POINT
1224 if (dtoaresult != NULL)
1225 freedtoa(dtoaresult);
1226#endif
1227 if (convbuf != NULL)
1228 free(convbuf);
1229 if (__sferror(fp))
1230 ret = EOF;
1231 if ((argtable != NULL) && (argtable != statargtable))
1232 free (argtable);
1233 return (ret);
1234 /* NOTREACHED */
1235}
1236
1237/*
1238 * Find all arguments when a positional parameter is encountered. Returns a
1239 * table, indexed by argument number, of pointers to each arguments. The
1240 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1241 * It will be replaces with a malloc-ed one if it overflows.
1242 */
1243static void
1244__find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1245{
1246 char *fmt; /* format string */
1247 int ch; /* character from fmt */
1248 int n, n2; /* handy integer (short term usage) */
1249 char *cp; /* handy char pointer (short term usage) */
1250 int flags; /* flags as above */
1251 int width; /* width from format (%8d), or 0 */
1252 enum typeid *typetable; /* table of types */
1253 enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1254 int tablesize; /* current size of type table */
1255 int tablemax; /* largest used index in table */
1256 int nextarg; /* 1-based argument index */
1257
1258 /*
1259 * Add an argument type to the table, expanding if necessary.
1260 */
1261#define ADDTYPE(type) \
1262 ((nextarg >= tablesize) ? \
1263 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1264 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1265 typetable[nextarg++] = type)
1266
1267#define ADDSARG() \
1268 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1269 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1270 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1271 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1272 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1273
1274#define ADDUARG() \
1275 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1276 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1277 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1278 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1279 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1280
1281 /*
1282 * Add * arguments to the type array.
1283 */
1284#define ADDASTER() \
1285 n2 = 0; \
1286 cp = fmt; \
1287 while (is_digit(*cp)) { \
1288 n2 = 10 * n2 + to_digit(*cp); \
1289 cp++; \
1290 } \
1291 if (*cp == '$') { \
1292 int hold = nextarg; \
1293 nextarg = n2; \
1294 ADDTYPE (T_INT); \
1295 nextarg = hold; \
1296 fmt = ++cp; \
1297 } else { \
1298 ADDTYPE (T_INT); \
1299 }
1300 fmt = (char *)fmt0;
1301 typetable = stattypetable;
1302 tablesize = STATIC_ARG_TBL_SIZE;
1303 tablemax = 0;
1304 nextarg = 1;
1305 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1306
1307 /*
1308 * Scan the format for conversions (`%' character).
1309 */
1310 for (;;) {
1311 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1312 /* void */;
1313 if (ch == '\0')
1314 goto done;
1315 fmt++; /* skip over '%' */
1316
1317 flags = 0;
1318 width = 0;
1319
1320rflag: ch = *fmt++;
1321reswitch: switch (ch) {
1322 case ' ':
1323 case '#':
1324 goto rflag;
1325 case '*':
1326 ADDASTER ();
1327 goto rflag;
1328 case '-':
1329 case '+':
1330 case '\'':
1331 goto rflag;
1332 case '.':
1333 if ((ch = *fmt++) == '*') {
1334 ADDASTER ();
1335 goto rflag;
1336 }
1337 while (is_digit(ch)) {
1338 ch = *fmt++;
1339 }
1340 goto reswitch;
1341 case '0':
1342 goto rflag;
1343 case '1': case '2': case '3': case '4':
1344 case '5': case '6': case '7': case '8': case '9':
1345 n = 0;
1346 do {
1347 n = 10 * n + to_digit(ch);
1348 ch = *fmt++;
1349 } while (is_digit(ch));
1350 if (ch == '$') {
1351 nextarg = n;
1352 goto rflag;
1353 }
1354 width = n;
1355 goto reswitch;
1356#ifdef FLOATING_POINT
1357 case 'L':
1358 flags |= LONGDBL;
1359 goto rflag;
1360#endif
1361 case 'h':
1362 if (flags & SHORTINT) {
1363 flags &= ~SHORTINT;
1364 flags |= CHARINT;
1365 } else
1366 flags |= SHORTINT;
1367 goto rflag;
1368 case 'j':
1369 flags |= INTMAXT;
1370 goto rflag;
1371 case 'l':
1372 if (flags & LONGINT) {
1373 flags &= ~LONGINT;
1374 flags |= LLONGINT;
1375 } else
1376 flags |= LONGINT;
1377 goto rflag;
1378 case 'q':
1379 flags |= LLONGINT; /* not necessarily */
1380 goto rflag;
1381 case 't':
1382 flags |= PTRDIFFT;
1383 goto rflag;
1384 case 'z':
1385 flags |= SIZET;
1386 goto rflag;
1387 case 'C':
1388 flags |= LONGINT;
1389 /*FALLTHROUGH*/
1390 case 'c':
1391 if (flags & LONGINT)
1392 ADDTYPE(T_WINT);
1393 else
1394 ADDTYPE(T_INT);
1395 break;
1396 case 'D':
1397 flags |= LONGINT;
1398 /*FALLTHROUGH*/
1399 case 'd':
1400 case 'i':
1401 ADDSARG();
1402 break;
1403#ifdef FLOATING_POINT
1404#ifdef HEXFLOAT
1405 case 'a':
1406 case 'A':
1407#endif
1408 case 'e':
1409 case 'E':
1410 case 'f':
1411 case 'g':
1412 case 'G':
1413 if (flags & LONGDBL)
1414 ADDTYPE(T_LONG_DOUBLE);
1415 else
1416 ADDTYPE(T_DOUBLE);
1417 break;
1418#endif /* FLOATING_POINT */
1419 case 'n':
1420 if (flags & INTMAXT)
1421 ADDTYPE(TP_INTMAXT);
1422 else if (flags & PTRDIFFT)
1423 ADDTYPE(TP_PTRDIFFT);
1424 else if (flags & SIZET)
1425 ADDTYPE(TP_SIZET);
1426 else if (flags & LLONGINT)
1427 ADDTYPE(TP_LLONG);
1428 else if (flags & LONGINT)
1429 ADDTYPE(TP_LONG);
1430 else if (flags & SHORTINT)
1431 ADDTYPE(TP_SHORT);
1432 else if (flags & CHARINT)
1433 ADDTYPE(TP_SCHAR);
1434 else
1435 ADDTYPE(TP_INT);
1436 continue; /* no output */
1437 case 'O':
1438 flags |= LONGINT;
1439 /*FALLTHROUGH*/
1440 case 'o':
1441 ADDUARG();
1442 break;
1443 case 'p':
1444 ADDTYPE(TP_VOID);
1445 break;
1446 case 'S':
1447 flags |= LONGINT;
1448 /*FALLTHROUGH*/
1449 case 's':
1450 if (flags & LONGINT)
1451 ADDTYPE(TP_WCHAR);
1452 else
1453 ADDTYPE(TP_CHAR);
1454 break;
1455 case 'U':
1456 flags |= LONGINT;
1457 /*FALLTHROUGH*/
1458 case 'u':
1459 case 'X':
1460 case 'x':
1461 ADDUARG();
1462 break;
1463 default: /* "%?" prints ?, unless ? is NUL */
1464 if (ch == '\0')
1465 goto done;
1466 break;
1467 }
1468 }
1469done:
1470 /*
1471 * Build the argument table.
1472 */
1473 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1474 *argtable = (union arg *)
1475 malloc (sizeof (union arg) * (tablemax + 1));
1476 }
1477
1478 (*argtable) [0].intarg = 0;
1479 for (n = 1; n <= tablemax; n++) {
1480 switch (typetable [n]) {
1481 case T_UNUSED: /* whoops! */
1482 (*argtable) [n].intarg = va_arg (ap, int);
1483 break;
1484 case TP_SCHAR:
1485 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1486 break;
1487 case TP_SHORT:
1488 (*argtable) [n].pshortarg = va_arg (ap, short *);
1489 break;
1490 case T_INT:
1491 (*argtable) [n].intarg = va_arg (ap, int);
1492 break;
1493 case T_U_INT:
1494 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
1495 break;
1496 case TP_INT:
1497 (*argtable) [n].pintarg = va_arg (ap, int *);
1498 break;
1499 case T_LONG:
1500 (*argtable) [n].longarg = va_arg (ap, long);
1501 break;
1502 case T_U_LONG:
1503 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1504 break;
1505 case TP_LONG:
1506 (*argtable) [n].plongarg = va_arg (ap, long *);
1507 break;
1508 case T_LLONG:
1509 (*argtable) [n].longlongarg = va_arg (ap, long long);
1510 break;
1511 case T_U_LLONG:
1512 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1513 break;
1514 case TP_LLONG:
1515 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
1516 break;
1517 case T_PTRDIFFT:
1518 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1519 break;
1520 case TP_PTRDIFFT:
1521 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1522 break;
1523 case T_SIZET:
1524 (*argtable) [n].sizearg = va_arg (ap, size_t);
1525 break;
1526 case TP_SIZET:
1527 (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
1528 break;
1529 case T_INTMAXT:
1530 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1531 break;
1532 case T_UINTMAXT:
1533 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1534 break;
1535 case TP_INTMAXT:
1536 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1537 break;
1538#ifdef FLOATING_POINT
1539 case T_DOUBLE:
1540 (*argtable) [n].doublearg = va_arg (ap, double);
1541 break;
1542 case T_LONG_DOUBLE:
1543 (*argtable) [n].longdoublearg = va_arg (ap, long double);
1544 break;
1545#endif
1546 case TP_CHAR:
1547 (*argtable) [n].pchararg = va_arg (ap, char *);
1548 break;
1549 case TP_VOID:
1550 (*argtable) [n].pvoidarg = va_arg (ap, void *);
1551 break;
1552 case T_WINT:
1553 (*argtable) [n].wintarg = va_arg (ap, wint_t);
1554 break;
1555 case TP_WCHAR:
1556 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1557 break;
1558 }
1559 }
1560
1561 if ((typetable != NULL) && (typetable != stattypetable))
1562 free (typetable);
1563}
1564
1565/*
1566 * Increase the size of the type table.
1567 */
1568static void
1569__grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
1570{
1571 enum typeid *const oldtable = *typetable;
1572 const int oldsize = *tablesize;
1573 enum typeid *newtable;
1574 int newsize = oldsize * 2;
1575
1576 if (newsize < nextarg + 1)
1577 newsize = nextarg + 1;
1578 if (oldsize == STATIC_ARG_TBL_SIZE) {
1579 if ((newtable = malloc(newsize)) == NULL)
1580 abort(); /* XXX handle better */
1581 bcopy(oldtable, newtable, oldsize);
1582 } else {
1583 if ((newtable = reallocf(oldtable, newsize)) == NULL)
1584 abort(); /* XXX handle better */
1585 }
1586 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1587
1588 *typetable = newtable;
1589 *tablesize = newsize;
1590}
1591
1592
1593#ifdef FLOATING_POINT
1594
1595static int
1596exponent(char *p0, int exp, int fmtch)
1597{
1598 char *p, *t;
1599 char expbuf[MAXEXPDIG];
1600
1601 p = p0;
1602 *p++ = fmtch;
1603 if (exp < 0) {
1604 exp = -exp;
1605 *p++ = '-';
1606 }
1607 else
1608 *p++ = '+';
1609 t = expbuf + MAXEXPDIG;
1610 if (exp > 9) {
1611 do {
1612 *--t = to_char(exp % 10);
1613 } while ((exp /= 10) > 9);
1614 *--t = to_char(exp);
1615 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1616 }
1617 else {
1618 /*
1619 * Exponents for decimal floating point conversions
1620 * (%[eEgG]) must be at least two characters long,
1621 * whereas exponents for hexadecimal conversions can
1622 * be only one character long.
1623 */
1624 if (fmtch == 'e' || fmtch == 'E')
1625 *p++ = '0';
1626 *p++ = to_char(exp);
1627 }
1628 return (p - p0);
1629}
1630#endif /* FLOATING_POINT */