File: | src/lib/libc/stdio/vfprintf.c |
Warning: | line 1204, column 8 Value stored to 'cp' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: vfprintf.c,v 1.81 2021/09/08 15:57:27 jca Exp $ */ |
2 | /*- |
3 | * Copyright (c) 1990 The Regents of the University of California. |
4 | * All rights reserved. |
5 | * |
6 | * This code is derived from software contributed to Berkeley by |
7 | * Chris Torek. |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions |
11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
17 | * 3. Neither the name of the University nor the names of its contributors |
18 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. |
20 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | * SUCH DAMAGE. |
32 | */ |
33 | |
34 | /* |
35 | * Actual printf innards. |
36 | * |
37 | * This code is large and complicated... |
38 | */ |
39 | |
40 | #include <sys/types.h> |
41 | #include <sys/mman.h> |
42 | |
43 | #include <errno(*__errno()).h> |
44 | #include <langinfo.h> |
45 | #include <limits.h> |
46 | #include <stdarg.h> |
47 | #include <stddef.h> |
48 | #include <stdio.h> |
49 | #include <stdint.h> |
50 | #include <stdlib.h> |
51 | #include <string.h> |
52 | #include <unistd.h> |
53 | #include <syslog.h> |
54 | #include <wchar.h> |
55 | |
56 | #include "local.h" |
57 | #include "fvwrite.h" |
58 | |
59 | union arg { |
60 | int intarg; |
61 | unsigned int uintarg; |
62 | long longarg; |
63 | unsigned long ulongarg; |
64 | long long longlongarg; |
65 | unsigned long long ulonglongarg; |
66 | ptrdiff_t ptrdiffarg; |
67 | size_t sizearg; |
68 | ssize_t ssizearg; |
69 | intmax_t intmaxarg; |
70 | uintmax_t uintmaxarg; |
71 | void *pvoidarg; |
72 | char *pchararg; |
73 | signed char *pschararg; |
74 | short *pshortarg; |
75 | int *pintarg; |
76 | long *plongarg; |
77 | long long *plonglongarg; |
78 | ptrdiff_t *pptrdiffarg; |
79 | ssize_t *pssizearg; |
80 | intmax_t *pintmaxarg; |
81 | #ifdef FLOATING_POINT1 |
82 | double doublearg; |
83 | long double longdoublearg; |
84 | #endif |
85 | #ifdef PRINTF_WIDE_CHAR1 |
86 | wint_t wintarg; |
87 | wchar_t *pwchararg; |
88 | #endif |
89 | }; |
90 | |
91 | static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable, |
92 | size_t *argtablesiz); |
93 | static int __grow_type_table(unsigned char **typetable, int *tablesize); |
94 | |
95 | /* |
96 | * Flush out all the vectors defined by the given uio, |
97 | * then reset it so that it can be reused. |
98 | */ |
99 | static int |
100 | __sprint(FILE *fp, struct __suio *uio) |
101 | { |
102 | int err; |
103 | |
104 | if (uio->uio_resid == 0) { |
105 | uio->uio_iovcnt = 0; |
106 | return (0); |
107 | } |
108 | err = __sfvwrite(fp, uio); |
109 | uio->uio_resid = 0; |
110 | uio->uio_iovcnt = 0; |
111 | return (err); |
112 | } |
113 | |
114 | /* |
115 | * Helper function for `fprintf to unbuffered unix file': creates a |
116 | * temporary buffer. We only work on write-only files; this avoids |
117 | * worries about ungetc buffers and so forth. |
118 | */ |
119 | static int |
120 | __sbprintf(FILE *fp, const char *fmt, va_list ap) |
121 | { |
122 | int ret; |
123 | FILE fake; |
124 | struct __sfileext fakeext; |
125 | unsigned char buf[BUFSIZ1024]; |
126 | |
127 | _FILEEXT_SETUP(&fake, &fakeext)do { (&fake)->_ext._base = (unsigned char *)(&fakeext ); do { ((struct __sfileext *)((&fake)->_ext._base))-> _ub._base = ((void*)0); ((struct __sfileext *)((&fake)-> _ext._base))->_ub._size = 0; memset(&(((struct __sfileext *)((&fake)->_ext._base))->_wcio), 0, sizeof(struct wchar_io_data)); } while (0); } while (0); |
128 | /* copy the important variables */ |
129 | fake._flags = fp->_flags & ~__SNBF0x0002; |
130 | fake._file = fp->_file; |
131 | fake._cookie = fp->_cookie; |
132 | fake._write = fp->_write; |
133 | |
134 | /* set up the buffer */ |
135 | fake._bf._base = fake._p = buf; |
136 | fake._bf._size = fake._w = sizeof(buf); |
137 | fake._lbfsize = 0; /* not actually used, but Just In Case */ |
138 | |
139 | /* do the work, then copy any error status */ |
140 | ret = __vfprintf(&fake, fmt, ap); |
141 | if (ret >= 0 && __sflush(&fake)) |
142 | ret = EOF(-1); |
143 | if (fake._flags & __SERR0x0040) |
144 | fp->_flags |= __SERR0x0040; |
145 | return (ret); |
146 | } |
147 | |
148 | #ifdef PRINTF_WIDE_CHAR1 |
149 | /* |
150 | * Convert a wide character string argument for the %ls format to a multibyte |
151 | * string representation. If not -1, prec specifies the maximum number of |
152 | * bytes to output, and also means that we can't assume that the wide char |
153 | * string is null-terminated. |
154 | */ |
155 | static char * |
156 | __wcsconv(wchar_t *wcsarg, int prec) |
157 | { |
158 | mbstate_t mbs; |
159 | char buf[MB_LEN_MAX4]; |
160 | wchar_t *p; |
161 | char *convbuf; |
162 | size_t clen, nbytes; |
163 | |
164 | /* Allocate space for the maximum number of bytes we could output. */ |
165 | if (prec < 0) { |
166 | memset(&mbs, 0, sizeof(mbs)); |
167 | p = wcsarg; |
168 | nbytes = wcsrtombs(NULL((void*)0), (const wchar_t **)&p, 0, &mbs); |
169 | if (nbytes == (size_t)-1) |
170 | return (NULL((void*)0)); |
171 | } else { |
172 | /* |
173 | * Optimisation: if the output precision is small enough, |
174 | * just allocate enough memory for the maximum instead of |
175 | * scanning the string. |
176 | */ |
177 | if (prec < 128) |
178 | nbytes = prec; |
179 | else { |
180 | nbytes = 0; |
181 | p = wcsarg; |
182 | memset(&mbs, 0, sizeof(mbs)); |
183 | for (;;) { |
184 | clen = wcrtomb(buf, *p++, &mbs); |
185 | if (clen == 0 || clen == (size_t)-1 || |
186 | nbytes + clen > (size_t)prec) |
187 | break; |
188 | nbytes += clen; |
189 | } |
190 | if (clen == (size_t)-1) |
191 | return (NULL((void*)0)); |
192 | } |
193 | } |
194 | if ((convbuf = malloc(nbytes + 1)) == NULL((void*)0)) |
195 | return (NULL((void*)0)); |
196 | |
197 | /* Fill the output buffer. */ |
198 | p = wcsarg; |
199 | memset(&mbs, 0, sizeof(mbs)); |
200 | if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p, |
201 | nbytes, &mbs)) == (size_t)-1) { |
202 | free(convbuf); |
203 | return (NULL((void*)0)); |
204 | } |
205 | convbuf[nbytes] = '\0'; |
206 | return (convbuf); |
207 | } |
208 | #endif |
209 | |
210 | #ifdef FLOATING_POINT1 |
211 | #include <float.h> |
212 | #include <locale.h> |
213 | #include <math.h> |
214 | #include "floatio.h" |
215 | #include "gdtoa.h" |
216 | |
217 | #define DEFPREC6 6 |
218 | |
219 | static int exponent(char *, int, int); |
220 | #endif /* FLOATING_POINT */ |
221 | |
222 | /* |
223 | * The size of the buffer we use as scratch space for integer |
224 | * conversions, among other things. Technically, we would need the |
225 | * most space for base 10 conversions with thousands' grouping |
226 | * characters between each pair of digits. 100 bytes is a |
227 | * conservative overestimate even for a 128-bit uintmax_t. |
228 | */ |
229 | #define BUF100 100 |
230 | |
231 | #define STATIC_ARG_TBL_SIZE8 8 /* Size of static argument table. */ |
232 | |
233 | |
234 | /* |
235 | * Macros for converting digits to letters and vice versa |
236 | */ |
237 | #define to_digit(c)((c) - '0') ((c) - '0') |
238 | #define is_digit(c)((unsigned)((c) - '0') <= 9) ((unsigned)to_digit(c)((c) - '0') <= 9) |
239 | #define to_char(n)((n) + '0') ((n) + '0') |
240 | |
241 | /* |
242 | * Flags used during conversion. |
243 | */ |
244 | #define ALT0x0001 0x0001 /* alternate form */ |
245 | #define LADJUST0x0004 0x0004 /* left adjustment */ |
246 | #define LONGDBL0x0008 0x0008 /* long double */ |
247 | #define LONGINT0x0010 0x0010 /* long integer */ |
248 | #define LLONGINT0x0020 0x0020 /* long long integer */ |
249 | #define SHORTINT0x0040 0x0040 /* short integer */ |
250 | #define ZEROPAD0x0080 0x0080 /* zero (as opposed to blank) pad */ |
251 | #define FPT0x0100 0x0100 /* Floating point number */ |
252 | #define PTRINT0x0200 0x0200 /* (unsigned) ptrdiff_t */ |
253 | #define SIZEINT0x0400 0x0400 /* (signed) size_t */ |
254 | #define CHARINT0x0800 0x0800 /* 8 bit integer */ |
255 | #define MAXINT0x1000 0x1000 /* largest integer size (intmax_t) */ |
256 | |
257 | int |
258 | vfprintf(FILE *fp, const char *fmt0, __va_list ap) |
259 | { |
260 | int ret; |
261 | |
262 | FLOCKFILE(fp)do { if (_thread_cb.tc_flockfile != ((void*)0)) _thread_cb.tc_flockfile (fp); } while (0); |
263 | ret = __vfprintf(fp, fmt0, ap); |
264 | FUNLOCKFILE(fp)do { if (_thread_cb.tc_funlockfile != ((void*)0)) _thread_cb. tc_funlockfile(fp); } while (0); |
265 | return (ret); |
266 | } |
267 | DEF_STRONG(vfprintf)__asm__(".global " "vfprintf" " ; " "vfprintf" " = " "_libc_vfprintf" ); |
268 | |
269 | int |
270 | __vfprintf(FILE *fp, const char *fmt0, __va_list ap) |
271 | { |
272 | char *fmt; /* format string */ |
273 | int ch; /* character from fmt */ |
274 | int n, n2; /* handy integers (short term usage) */ |
275 | char *cp; /* handy char pointer (short term usage) */ |
276 | struct __siov *iovp; /* for PRINT macro */ |
277 | int flags; /* flags as above */ |
278 | int ret; /* return value accumulator */ |
279 | int width; /* width from format (%8d), or 0 */ |
280 | int prec; /* precision from format; <0 for N/A */ |
281 | char sign; /* sign prefix (' ', '+', '-', or \0) */ |
282 | #ifdef FLOATING_POINT1 |
283 | /* |
284 | * We can decompose the printed representation of floating |
285 | * point numbers into several parts, some of which may be empty: |
286 | * |
287 | * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ |
288 | * A B ---C--- D E F |
289 | * |
290 | * A: 'sign' holds this value if present; '\0' otherwise |
291 | * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal |
292 | * C: cp points to the string MMMNNN. Leading and trailing |
293 | * zeros are not in the string and must be added. |
294 | * D: expchar holds this character; '\0' if no exponent, e.g. %f |
295 | * F: at least two digits for decimal, at least one digit for hex |
296 | */ |
297 | char *decimal_point = NULL((void*)0); |
298 | int signflag; /* true if float is negative */ |
299 | union { /* floating point arguments %[aAeEfFgG] */ |
300 | double dbl; |
301 | long double ldbl; |
302 | } fparg; |
303 | int expt; /* integer value of exponent */ |
304 | char expchar; /* exponent character: [eEpP\0] */ |
305 | char *dtoaend; /* pointer to end of converted digits */ |
306 | int expsize; /* character count for expstr */ |
307 | int lead; /* sig figs before decimal or group sep */ |
308 | int ndig; /* actual number of digits returned by dtoa */ |
309 | char expstr[MAXEXPDIG6+2]; /* buffer for exponent string: e+ZZZ */ |
310 | char *dtoaresult = NULL((void*)0); |
311 | #endif |
312 | |
313 | uintmax_t _umax; /* integer arguments %[diouxX] */ |
314 | enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */ |
315 | int dprec; /* a copy of prec if %[diouxX], 0 otherwise */ |
316 | int realsz; /* field size expanded by dprec */ |
317 | int size; /* size of converted field or string */ |
318 | const char *xdigs; /* digits for %[xX] conversion */ |
319 | #define NIOV8 8 |
320 | struct __suio uio; /* output information: summary */ |
321 | struct __siov iov[NIOV8];/* ... and individual io vectors */ |
322 | char buf[BUF100]; /* buffer with space for digits of uintmax_t */ |
323 | char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ |
324 | union arg *argtable; /* args, built due to positional arg */ |
325 | union arg statargtable[STATIC_ARG_TBL_SIZE8]; |
326 | size_t argtablesiz; |
327 | int nextarg; /* 1-based argument index */ |
328 | va_list orgap; /* original argument pointer */ |
329 | #ifdef PRINTF_WIDE_CHAR1 |
330 | char *convbuf; /* buffer for wide to multi-byte conversion */ |
331 | #endif |
332 | |
333 | /* |
334 | * Choose PADSIZE to trade efficiency vs. size. If larger printf |
335 | * fields occur frequently, increase PADSIZE and make the initialisers |
336 | * below longer. |
337 | */ |
338 | #define PADSIZE16 16 /* pad chunk size */ |
339 | static char blanks[PADSIZE16] = |
340 | {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; |
341 | static char zeroes[PADSIZE16] = |
342 | {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; |
343 | |
344 | static const char xdigs_lower[16] = "0123456789abcdef"; |
345 | static const char xdigs_upper[16] = "0123456789ABCDEF"; |
346 | |
347 | /* |
348 | * BEWARE, these `goto error' on error, and PAD uses `n'. |
349 | */ |
350 | #define PRINT(ptr, len)do { iovp->iov_base = (ptr); iovp->iov_len = (len); uio .uio_resid += (len); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0) do { \ |
351 | iovp->iov_base = (ptr); \ |
352 | iovp->iov_len = (len); \ |
353 | uio.uio_resid += (len); \ |
354 | iovp++; \ |
355 | if (++uio.uio_iovcnt >= NIOV8) { \ |
356 | if (__sprint(fp, &uio)) \ |
357 | goto error; \ |
358 | iovp = iov; \ |
359 | } \ |
360 | } while (0) |
361 | #define PAD(howmany, with)do { if ((n = (howmany)) > 0) { while (n > 16) { do { iovp ->iov_base = (with); iovp->iov_len = (16); uio.uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint (fp, &uio)) goto error; iovp = iov; } } while (0); n -= 16 ; } do { iovp->iov_base = (with); iovp->iov_len = (n); uio .uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); } } while (0) do { \ |
362 | if ((n = (howmany)) > 0) { \ |
363 | while (n > PADSIZE16) { \ |
364 | PRINT(with, PADSIZE)do { iovp->iov_base = (with); iovp->iov_len = (16); uio .uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); \ |
365 | n -= PADSIZE16; \ |
366 | } \ |
367 | PRINT(with, n)do { iovp->iov_base = (with); iovp->iov_len = (n); uio. uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if ( __sprint(fp, &uio)) goto error; iovp = iov; } } while (0); \ |
368 | } \ |
369 | } while (0) |
370 | #define PRINTANDPAD(p, ep, len, with)do { n2 = (ep) - (p); if (n2 > (len)) n2 = (len); if (n2 > 0) do { iovp->iov_base = ((p)); iovp->iov_len = (n2); uio .uio_resid += (n2); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); do { if ((n = ((len) - (n2 > 0 ? n2 : 0))) > 0) { while (n > 16) { do { iovp->iov_base = ((with)); iovp->iov_len = (16); uio.uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); n -= 16; } do { iovp->iov_base = ((with)); iovp ->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov ; } } while (0); } } while (0); } while(0) do { \ |
371 | n2 = (ep) - (p); \ |
372 | if (n2 > (len)) \ |
373 | n2 = (len); \ |
374 | if (n2 > 0) \ |
375 | PRINT((p), n2)do { iovp->iov_base = ((p)); iovp->iov_len = (n2); uio. uio_resid += (n2); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); \ |
376 | PAD((len) - (n2 > 0 ? n2 : 0), (with))do { if ((n = ((len) - (n2 > 0 ? n2 : 0))) > 0) { while (n > 16) { do { iovp->iov_base = ((with)); iovp->iov_len = (16); uio.uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); n -= 16; } do { iovp->iov_base = ((with)); iovp ->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov ; } } while (0); } } while (0); \ |
377 | } while(0) |
378 | #define FLUSH()do { if (uio.uio_resid && __sprint(fp, &uio)) goto error; uio.uio_iovcnt = 0; iovp = iov; } while (0) do { \ |
379 | if (uio.uio_resid && __sprint(fp, &uio)) \ |
380 | goto error; \ |
381 | uio.uio_iovcnt = 0; \ |
382 | iovp = iov; \ |
383 | } while (0) |
384 | |
385 | /* |
386 | * To extend shorts properly, we need both signed and unsigned |
387 | * argument extraction methods. |
388 | */ |
389 | #define SARG()((intmax_t)(flags&0x1000 ? ((argtable != ((void*)0)) ? *( (intmax_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, intmax_t))) : flags&0x0020 ? ((argtable != ((void*)0 )) ? *((long long*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, long long))) : flags&0x0010 ? ((argtable != ((void*) 0)) ? *((long*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, long))) : flags&0x0200 ? ((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ptrdiff_t))) : flags&0x0400 ? ((argtable != ((void*) 0)) ? *((ssize_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ssize_t))) : flags&0x0040 ? (short)((argtable != ((void *)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))) : flags&0x0800 ? (signed char)((argtable != ( (void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg++ , __builtin_va_arg(ap, int))) : ((argtable != ((void*)0)) ? * ((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))))) \ |
390 | ((intmax_t)(flags&MAXINT0x1000 ? GETARG(intmax_t)((argtable != ((void*)0)) ? *((intmax_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, intmax_t))) : \ |
391 | flags&LLONGINT0x0020 ? GETARG(long long)((argtable != ((void*)0)) ? *((long long*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, long long))) : \ |
392 | flags&LONGINT0x0010 ? GETARG(long)((argtable != ((void*)0)) ? *((long*)(&argtable[nextarg++ ])) : (nextarg++, __builtin_va_arg(ap, long))) : \ |
393 | flags&PTRINT0x0200 ? GETARG(ptrdiff_t)((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, ptrdiff_t))) : \ |
394 | flags&SIZEINT0x0400 ? GETARG(ssize_t)((argtable != ((void*)0)) ? *((ssize_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, ssize_t))) : \ |
395 | flags&SHORTINT0x0040 ? (short)GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))) : \ |
396 | flags&CHARINT0x0800 ? (signed char)GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))) : \ |
397 | GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))))) |
398 | #define UARG()((uintmax_t)(flags&0x1000 ? ((argtable != ((void*)0)) ? * ((uintmax_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, uintmax_t))) : flags&0x0020 ? ((argtable != ((void*) 0)) ? *((unsigned long long*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, unsigned long long))) : flags&0x0010 ? ((argtable != ((void*)0)) ? *((unsigned long*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned long ))) : flags&0x0200 ? (uintptr_t)((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ptrdiff_t))) : flags&0x0400 ? ((argtable != ((void*) 0)) ? *((size_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, size_t))) : flags&0x0040 ? (unsigned short)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, int))) : flags&0x0800 ? (unsigned char)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, int))) : ((argtable != ((void*)0)) ? *((unsigned int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned int))))) \ |
399 | ((uintmax_t)(flags&MAXINT0x1000 ? GETARG(uintmax_t)((argtable != ((void*)0)) ? *((uintmax_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, uintmax_t))) : \ |
400 | flags&LLONGINT0x0020 ? GETARG(unsigned long long)((argtable != ((void*)0)) ? *((unsigned long long*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned long long))) : \ |
401 | flags&LONGINT0x0010 ? GETARG(unsigned long)((argtable != ((void*)0)) ? *((unsigned long*)(&argtable[ nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned long ))) : \ |
402 | flags&PTRINT0x0200 ? (uintptr_t)GETARG(ptrdiff_t)((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, ptrdiff_t))) : /* XXX */ \ |
403 | flags&SIZEINT0x0400 ? GETARG(size_t)((argtable != ((void*)0)) ? *((size_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, size_t))) : \ |
404 | flags&SHORTINT0x0040 ? (unsigned short)GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))) : \ |
405 | flags&CHARINT0x0800 ? (unsigned char)GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))) : \ |
406 | GETARG(unsigned int)((argtable != ((void*)0)) ? *((unsigned int*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, unsigned int))))) |
407 | |
408 | /* |
409 | * Append a digit to a value and check for overflow. |
410 | */ |
411 | #define APPEND_DIGIT(val, dig)do { if ((val) > 2147483647 / 10) goto overflow; (val) *= 10 ; if ((val) > 2147483647 - (((dig)) - '0')) goto overflow; (val) += (((dig)) - '0'); } while (0) do { \ |
412 | if ((val) > INT_MAX2147483647 / 10) \ |
413 | goto overflow; \ |
414 | (val) *= 10; \ |
415 | if ((val) > INT_MAX2147483647 - to_digit((dig))(((dig)) - '0')) \ |
416 | goto overflow; \ |
417 | (val) += to_digit((dig))(((dig)) - '0'); \ |
418 | } while (0) |
419 | |
420 | /* |
421 | * Get * arguments, including the form *nn$. Preserve the nextarg |
422 | * that the argument can be gotten once the type is determined. |
423 | */ |
424 | #define GETASTER(val)n2 = 0; cp = fmt; while (((unsigned)((*cp) - '0') <= 9)) { do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); cp++; } if (*cp == '$') { int hold = nextarg; if (argtable == ((void*)0)) { argtable = statargtable; if (__find_arguments(fmt0, orgap, &argtable , &argtablesiz) == -1) { ret = -1; goto error; } } nextarg = n2; val = ((argtable != ((void*)0)) ? *((int*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, int))); nextarg = hold; fmt = ++cp; } else { val = ((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))); } \ |
425 | n2 = 0; \ |
426 | cp = fmt; \ |
427 | while (is_digit(*cp)((unsigned)((*cp) - '0') <= 9)) { \ |
428 | APPEND_DIGIT(n2, *cp)do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); \ |
429 | cp++; \ |
430 | } \ |
431 | if (*cp == '$') { \ |
432 | int hold = nextarg; \ |
433 | if (argtable == NULL((void*)0)) { \ |
434 | argtable = statargtable; \ |
435 | if (__find_arguments(fmt0, orgap, &argtable, \ |
436 | &argtablesiz) == -1) { \ |
437 | ret = -1; \ |
438 | goto error; \ |
439 | } \ |
440 | } \ |
441 | nextarg = n2; \ |
442 | val = GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))); \ |
443 | nextarg = hold; \ |
444 | fmt = ++cp; \ |
445 | } else { \ |
446 | val = GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))); \ |
447 | } |
448 | |
449 | /* |
450 | * Get the argument indexed by nextarg. If the argument table is |
451 | * built, use it to get the argument. If its not, get the next |
452 | * argument (and arguments must be gotten sequentially). |
453 | */ |
454 | #define GETARG(type)((argtable != ((void*)0)) ? *((type*)(&argtable[nextarg++ ])) : (nextarg++, __builtin_va_arg(ap, type))) \ |
455 | ((argtable != NULL((void*)0)) ? *((type*)(&argtable[nextarg++])) : \ |
456 | (nextarg++, va_arg(ap, type)__builtin_va_arg(ap, type))) |
457 | |
458 | _SET_ORIENTATION(fp, -1)do { struct wchar_io_data *_wcio = (((struct __sfileext *)((fp )->_ext._base)) ? &(((struct __sfileext *)((fp)->_ext ._base))->_wcio) : (struct wchar_io_data *)0); if (_wcio && _wcio->wcio_mode == 0) _wcio->wcio_mode = (-1);} while (0); |
459 | /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ |
460 | if (cantwrite(fp)((((fp)->_flags & 0x0008) == 0 || (fp)->_bf._base == ((void*)0)) && __swsetup(fp))) { |
461 | errno(*__errno()) = EBADF9; |
462 | return (EOF(-1)); |
463 | } |
464 | |
465 | /* optimise fprintf(stderr) (and other unbuffered Unix files) */ |
466 | if ((fp->_flags & (__SNBF0x0002|__SWR0x0008|__SRW0x0010)) == (__SNBF0x0002|__SWR0x0008) && |
467 | fp->_file >= 0) |
468 | return (__sbprintf(fp, fmt0, ap)); |
469 | |
470 | fmt = (char *)fmt0; |
471 | argtable = NULL((void*)0); |
472 | nextarg = 1; |
473 | va_copy(orgap, ap)__builtin_va_copy(orgap, ap); |
474 | uio.uio_iov = iovp = iov; |
475 | uio.uio_resid = 0; |
476 | uio.uio_iovcnt = 0; |
477 | ret = 0; |
478 | #ifdef PRINTF_WIDE_CHAR1 |
479 | convbuf = NULL((void*)0); |
480 | #endif |
481 | |
482 | /* |
483 | * Scan the format for conversions (`%' character). |
484 | */ |
485 | for (;;) { |
486 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) |
487 | continue; |
488 | |
489 | if (fmt != cp) { |
490 | ptrdiff_t m = fmt - cp; |
491 | if (m < 0 || m > INT_MAX2147483647 - ret) |
492 | goto overflow; |
493 | PRINT(cp, m)do { iovp->iov_base = (cp); iovp->iov_len = (m); uio.uio_resid += (m); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint (fp, &uio)) goto error; iovp = iov; } } while (0); |
494 | ret += m; |
495 | } |
496 | if (ch == '\0') |
497 | goto done; |
498 | fmt++; /* skip over '%' */ |
499 | |
500 | flags = 0; |
501 | dprec = 0; |
502 | width = 0; |
503 | prec = -1; |
504 | sign = '\0'; |
505 | ox[1] = '\0'; |
506 | |
507 | rflag: ch = *fmt++; |
508 | reswitch: switch (ch) { |
509 | case ' ': |
510 | /* |
511 | * ``If the space and + flags both appear, the space |
512 | * flag will be ignored.'' |
513 | * -- ANSI X3J11 |
514 | */ |
515 | if (!sign) |
516 | sign = ' '; |
517 | goto rflag; |
518 | case '#': |
519 | flags |= ALT0x0001; |
520 | goto rflag; |
521 | case '\'': |
522 | /* grouping not implemented */ |
523 | goto rflag; |
524 | case '*': |
525 | /* |
526 | * ``A negative field width argument is taken as a |
527 | * - flag followed by a positive field width.'' |
528 | * -- ANSI X3J11 |
529 | * They don't exclude field widths read from args. |
530 | */ |
531 | GETASTER(width)n2 = 0; cp = fmt; while (((unsigned)((*cp) - '0') <= 9)) { do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); cp++; } if (*cp == '$') { int hold = nextarg; if (argtable == ((void*)0)) { argtable = statargtable; if (__find_arguments(fmt0, orgap, &argtable , &argtablesiz) == -1) { ret = -1; goto error; } } nextarg = n2; width = ((argtable != ((void*)0)) ? *((int*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, int))); nextarg = hold; fmt = ++cp; } else { width = ((argtable != ((void*)0 )) ? *((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))); }; |
532 | if (width >= 0) |
533 | goto rflag; |
534 | if (width == INT_MIN(-2147483647 -1)) |
535 | goto overflow; |
536 | width = -width; |
537 | /* FALLTHROUGH */ |
538 | case '-': |
539 | flags |= LADJUST0x0004; |
540 | goto rflag; |
541 | case '+': |
542 | sign = '+'; |
543 | goto rflag; |
544 | case '.': |
545 | if ((ch = *fmt++) == '*') { |
546 | GETASTER(n)n2 = 0; cp = fmt; while (((unsigned)((*cp) - '0') <= 9)) { do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); cp++; } if (*cp == '$') { int hold = nextarg; if (argtable == ((void*)0)) { argtable = statargtable; if (__find_arguments(fmt0, orgap, &argtable , &argtablesiz) == -1) { ret = -1; goto error; } } nextarg = n2; n = ((argtable != ((void*)0)) ? *((int*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, int))); nextarg = hold; fmt = ++cp; } else { n = ((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))); }; |
547 | prec = n < 0 ? -1 : n; |
548 | goto rflag; |
549 | } |
550 | n = 0; |
551 | while (is_digit(ch)((unsigned)((ch) - '0') <= 9)) { |
552 | APPEND_DIGIT(n, ch)do { if ((n) > 2147483647 / 10) goto overflow; (n) *= 10; if ((n) > 2147483647 - (((ch)) - '0')) goto overflow; (n) += (((ch)) - '0'); } while (0); |
553 | ch = *fmt++; |
554 | } |
555 | if (ch == '$') { |
556 | nextarg = n; |
557 | if (argtable == NULL((void*)0)) { |
558 | argtable = statargtable; |
559 | if (__find_arguments(fmt0, orgap, |
560 | &argtable, &argtablesiz) == -1) { |
561 | ret = -1; |
562 | goto error; |
563 | } |
564 | } |
565 | goto rflag; |
566 | } |
567 | prec = n; |
568 | goto reswitch; |
569 | case '0': |
570 | /* |
571 | * ``Note that 0 is taken as a flag, not as the |
572 | * beginning of a field width.'' |
573 | * -- ANSI X3J11 |
574 | */ |
575 | flags |= ZEROPAD0x0080; |
576 | goto rflag; |
577 | case '1': case '2': case '3': case '4': |
578 | case '5': case '6': case '7': case '8': case '9': |
579 | n = 0; |
580 | do { |
581 | APPEND_DIGIT(n, ch)do { if ((n) > 2147483647 / 10) goto overflow; (n) *= 10; if ((n) > 2147483647 - (((ch)) - '0')) goto overflow; (n) += (((ch)) - '0'); } while (0); |
582 | ch = *fmt++; |
583 | } while (is_digit(ch)((unsigned)((ch) - '0') <= 9)); |
584 | if (ch == '$') { |
585 | nextarg = n; |
586 | if (argtable == NULL((void*)0)) { |
587 | argtable = statargtable; |
588 | if (__find_arguments(fmt0, orgap, |
589 | &argtable, &argtablesiz) == -1) { |
590 | ret = -1; |
591 | goto error; |
592 | } |
593 | } |
594 | goto rflag; |
595 | } |
596 | width = n; |
597 | goto reswitch; |
598 | #ifdef FLOATING_POINT1 |
599 | case 'L': |
600 | flags |= LONGDBL0x0008; |
601 | goto rflag; |
602 | #endif |
603 | case 'h': |
604 | if (*fmt == 'h') { |
605 | fmt++; |
606 | flags |= CHARINT0x0800; |
607 | } else { |
608 | flags |= SHORTINT0x0040; |
609 | } |
610 | goto rflag; |
611 | case 'j': |
612 | flags |= MAXINT0x1000; |
613 | goto rflag; |
614 | case 'l': |
615 | if (*fmt == 'l') { |
616 | fmt++; |
617 | flags |= LLONGINT0x0020; |
618 | } else { |
619 | flags |= LONGINT0x0010; |
620 | } |
621 | goto rflag; |
622 | case 'q': |
623 | flags |= LLONGINT0x0020; |
624 | goto rflag; |
625 | case 't': |
626 | flags |= PTRINT0x0200; |
627 | goto rflag; |
628 | case 'z': |
629 | flags |= SIZEINT0x0400; |
630 | goto rflag; |
631 | case 'c': |
632 | #ifdef PRINTF_WIDE_CHAR1 |
633 | if (flags & LONGINT0x0010) { |
634 | mbstate_t mbs; |
635 | size_t mbseqlen; |
636 | |
637 | memset(&mbs, 0, sizeof(mbs)); |
638 | mbseqlen = wcrtomb(buf, |
639 | (wchar_t)GETARG(wint_t)((argtable != ((void*)0)) ? *((wint_t*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, wint_t))), &mbs); |
640 | if (mbseqlen == (size_t)-1) { |
641 | ret = -1; |
642 | goto error; |
643 | } |
644 | cp = buf; |
645 | size = (int)mbseqlen; |
646 | } else { |
647 | #endif |
648 | *(cp = buf) = GETARG(int)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++] )) : (nextarg++, __builtin_va_arg(ap, int))); |
649 | size = 1; |
650 | #ifdef PRINTF_WIDE_CHAR1 |
651 | } |
652 | #endif |
653 | sign = '\0'; |
654 | break; |
655 | case 'D': |
656 | flags |= LONGINT0x0010; |
657 | /*FALLTHROUGH*/ |
658 | case 'd': |
659 | case 'i': |
660 | _umax = SARG()((intmax_t)(flags&0x1000 ? ((argtable != ((void*)0)) ? *( (intmax_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, intmax_t))) : flags&0x0020 ? ((argtable != ((void*)0 )) ? *((long long*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, long long))) : flags&0x0010 ? ((argtable != ((void*) 0)) ? *((long*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, long))) : flags&0x0200 ? ((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ptrdiff_t))) : flags&0x0400 ? ((argtable != ((void*) 0)) ? *((ssize_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ssize_t))) : flags&0x0040 ? (short)((argtable != ((void *)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))) : flags&0x0800 ? (signed char)((argtable != ( (void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg++ , __builtin_va_arg(ap, int))) : ((argtable != ((void*)0)) ? * ((int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, int))))); |
661 | if ((intmax_t)_umax < 0) { |
662 | _umax = -_umax; |
663 | sign = '-'; |
664 | } |
665 | base = DEC; |
666 | goto number; |
667 | #ifdef FLOATING_POINT1 |
668 | case 'a': |
669 | case 'A': |
670 | if (ch == 'a') { |
671 | ox[1] = 'x'; |
672 | xdigs = xdigs_lower; |
673 | expchar = 'p'; |
674 | } else { |
675 | ox[1] = 'X'; |
676 | xdigs = xdigs_upper; |
677 | expchar = 'P'; |
678 | } |
679 | if (prec >= 0) |
680 | prec++; |
681 | if (dtoaresult) |
682 | __freedtoa(dtoaresult); |
683 | if (flags & LONGDBL0x0008) { |
684 | fparg.ldbl = GETARG(long double)((argtable != ((void*)0)) ? *((long double*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, long double))); |
685 | dtoaresult = cp = |
686 | __hldtoa(fparg.ldbl, xdigs, prec, |
687 | &expt, &signflag, &dtoaend); |
688 | if (dtoaresult == NULL((void*)0)) { |
689 | errno(*__errno()) = ENOMEM12; |
690 | goto error; |
691 | } |
692 | } else { |
693 | fparg.dbl = GETARG(double)((argtable != ((void*)0)) ? *((double*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, double))); |
694 | dtoaresult = cp = |
695 | __hdtoa(fparg.dbl, xdigs, prec, |
696 | &expt, &signflag, &dtoaend); |
697 | if (dtoaresult == NULL((void*)0)) { |
698 | errno(*__errno()) = ENOMEM12; |
699 | goto error; |
700 | } |
701 | } |
702 | if (prec < 0) |
703 | prec = dtoaend - cp; |
704 | if (expt == INT_MAX2147483647) |
705 | ox[1] = '\0'; |
706 | goto fp_common; |
707 | case 'e': |
708 | case 'E': |
709 | expchar = ch; |
710 | if (prec < 0) /* account for digit before decpt */ |
711 | prec = DEFPREC6 + 1; |
712 | else |
713 | prec++; |
714 | goto fp_begin; |
715 | case 'f': |
716 | case 'F': |
717 | expchar = '\0'; |
718 | goto fp_begin; |
719 | case 'g': |
720 | case 'G': |
721 | expchar = ch - ('g' - 'e'); |
722 | if (prec == 0) |
723 | prec = 1; |
724 | fp_begin: |
725 | if (prec < 0) |
726 | prec = DEFPREC6; |
727 | if (dtoaresult) |
728 | __freedtoa(dtoaresult); |
729 | if (flags & LONGDBL0x0008) { |
730 | fparg.ldbl = GETARG(long double)((argtable != ((void*)0)) ? *((long double*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, long double))); |
731 | dtoaresult = cp = |
732 | __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, |
733 | &expt, &signflag, &dtoaend); |
734 | if (dtoaresult == NULL((void*)0)) { |
735 | errno(*__errno()) = ENOMEM12; |
736 | goto error; |
737 | } |
738 | } else { |
739 | fparg.dbl = GETARG(double)((argtable != ((void*)0)) ? *((double*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, double))); |
740 | dtoaresult = cp = |
741 | __dtoa(fparg.dbl, expchar ? 2 : 3, prec, |
742 | &expt, &signflag, &dtoaend); |
743 | if (dtoaresult == NULL((void*)0)) { |
744 | errno(*__errno()) = ENOMEM12; |
745 | goto error; |
746 | } |
747 | if (expt == 9999) |
748 | expt = INT_MAX2147483647; |
749 | } |
750 | fp_common: |
751 | if (signflag) |
752 | sign = '-'; |
753 | if (expt == INT_MAX2147483647) { /* inf or nan */ |
754 | if (*cp == 'N') |
755 | cp = (ch >= 'a') ? "nan" : "NAN"; |
756 | else |
757 | cp = (ch >= 'a') ? "inf" : "INF"; |
758 | size = 3; |
759 | flags &= ~ZEROPAD0x0080; |
760 | break; |
761 | } |
762 | flags |= FPT0x0100; |
763 | ndig = dtoaend - cp; |
764 | if (ch == 'g' || ch == 'G') { |
765 | if (expt > -4 && expt <= prec) { |
766 | /* Make %[gG] smell like %[fF] */ |
767 | expchar = '\0'; |
768 | if (flags & ALT0x0001) |
769 | prec -= expt; |
770 | else |
771 | prec = ndig - expt; |
772 | if (prec < 0) |
773 | prec = 0; |
774 | } else { |
775 | /* |
776 | * Make %[gG] smell like %[eE], but |
777 | * trim trailing zeroes if no # flag. |
778 | */ |
779 | if (!(flags & ALT0x0001)) |
780 | prec = ndig; |
781 | } |
782 | } |
783 | if (expchar) { |
784 | expsize = exponent(expstr, expt - 1, expchar); |
785 | size = expsize + prec; |
786 | if (prec > 1 || flags & ALT0x0001) |
787 | ++size; |
788 | } else { |
789 | /* space for digits before decimal point */ |
790 | if (expt > 0) |
791 | size = expt; |
792 | else /* "0" */ |
793 | size = 1; |
794 | /* space for decimal pt and following digits */ |
795 | if (prec || flags & ALT0x0001) |
796 | size += prec + 1; |
797 | lead = expt; |
798 | } |
799 | break; |
800 | #endif /* FLOATING_POINT */ |
801 | case 'n': { |
802 | static const char n_msg[] = ": *printf used %n, aborting: "; |
803 | char buf[1024], *p; |
804 | |
805 | /* <10> is LOG_CRIT */ |
806 | strlcpy(buf, "<10>", sizeof buf); |
807 | |
808 | /* Make sure progname does not fill the whole buffer */ |
809 | strlcat(buf, __progname, sizeof(buf) - sizeof n_msg); |
810 | strlcat(buf, n_msg, sizeof buf); |
811 | strlcat(buf, fmt0, sizeof buf); |
812 | if ((p = strchr(buf, '\n'))) |
813 | *p = '\0'; |
814 | sendsyslog(buf, strlen(buf), LOG_CONS0x02); |
815 | abort(); |
816 | break; |
817 | } |
818 | case 'O': |
819 | flags |= LONGINT0x0010; |
820 | /*FALLTHROUGH*/ |
821 | case 'o': |
822 | _umax = UARG()((uintmax_t)(flags&0x1000 ? ((argtable != ((void*)0)) ? * ((uintmax_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, uintmax_t))) : flags&0x0020 ? ((argtable != ((void*) 0)) ? *((unsigned long long*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, unsigned long long))) : flags&0x0010 ? ((argtable != ((void*)0)) ? *((unsigned long*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned long ))) : flags&0x0200 ? (uintptr_t)((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ptrdiff_t))) : flags&0x0400 ? ((argtable != ((void*) 0)) ? *((size_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, size_t))) : flags&0x0040 ? (unsigned short)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, int))) : flags&0x0800 ? (unsigned char)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, int))) : ((argtable != ((void*)0)) ? *((unsigned int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned int))))); |
823 | base = OCT; |
824 | goto nosign; |
825 | case 'p': |
826 | /* |
827 | * ``The argument shall be a pointer to void. The |
828 | * value of the pointer is converted to a sequence |
829 | * of printable characters, in an implementation- |
830 | * defined manner.'' |
831 | * -- ANSI X3J11 |
832 | */ |
833 | _umax = (u_long)GETARG(void *)((argtable != ((void*)0)) ? *((void **)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, void *))); |
834 | base = HEX; |
835 | xdigs = xdigs_lower; |
836 | ox[1] = 'x'; |
837 | goto nosign; |
838 | case 's': { |
839 | size_t len; |
840 | |
841 | #ifdef PRINTF_WIDE_CHAR1 |
842 | if (flags & LONGINT0x0010) { |
843 | wchar_t *wcp; |
844 | |
845 | free(convbuf); |
846 | convbuf = NULL((void*)0); |
847 | if ((wcp = GETARG(wchar_t *)((argtable != ((void*)0)) ? *((wchar_t **)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, wchar_t *)))) == NULL((void*)0)) { |
848 | struct syslog_data sdata = SYSLOG_DATA_INIT{0, (const char *)0, (1<<3), 0xff}; |
849 | int save_errno = errno(*__errno()); |
850 | |
851 | syslog_r(LOG_CRIT2 | LOG_CONS0x02, &sdata, |
852 | "vfprintf %%ls NULL in \"%s\"", fmt0); |
853 | errno(*__errno()) = save_errno; |
854 | |
855 | cp = "(null)"; |
856 | } else { |
857 | convbuf = __wcsconv(wcp, prec); |
858 | if (convbuf == NULL((void*)0)) { |
859 | ret = -1; |
860 | goto error; |
861 | } |
862 | cp = convbuf; |
863 | } |
864 | } else |
865 | #endif /* PRINTF_WIDE_CHAR */ |
866 | |
867 | if ((cp = GETARG(char *)((argtable != ((void*)0)) ? *((char **)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, char *)))) == NULL((void*)0)) { |
868 | struct syslog_data sdata = SYSLOG_DATA_INIT{0, (const char *)0, (1<<3), 0xff}; |
869 | int save_errno = errno(*__errno()); |
870 | |
871 | syslog_r(LOG_CRIT2 | LOG_CONS0x02, &sdata, |
872 | "vfprintf %%s NULL in \"%s\"", fmt0); |
873 | errno(*__errno()) = save_errno; |
874 | |
875 | cp = "(null)"; |
876 | } |
877 | len = prec >= 0 ? strnlen(cp, prec) : strlen(cp); |
878 | if (len > INT_MAX2147483647) |
879 | goto overflow; |
880 | size = (int)len; |
881 | sign = '\0'; |
882 | } |
883 | break; |
884 | case 'U': |
885 | flags |= LONGINT0x0010; |
886 | /*FALLTHROUGH*/ |
887 | case 'u': |
888 | _umax = UARG()((uintmax_t)(flags&0x1000 ? ((argtable != ((void*)0)) ? * ((uintmax_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, uintmax_t))) : flags&0x0020 ? ((argtable != ((void*) 0)) ? *((unsigned long long*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, unsigned long long))) : flags&0x0010 ? ((argtable != ((void*)0)) ? *((unsigned long*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned long ))) : flags&0x0200 ? (uintptr_t)((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ptrdiff_t))) : flags&0x0400 ? ((argtable != ((void*) 0)) ? *((size_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, size_t))) : flags&0x0040 ? (unsigned short)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, int))) : flags&0x0800 ? (unsigned char)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, int))) : ((argtable != ((void*)0)) ? *((unsigned int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned int))))); |
889 | base = DEC; |
890 | goto nosign; |
891 | case 'X': |
892 | xdigs = xdigs_upper; |
893 | goto hex; |
894 | case 'x': |
895 | xdigs = xdigs_lower; |
896 | hex: _umax = UARG()((uintmax_t)(flags&0x1000 ? ((argtable != ((void*)0)) ? * ((uintmax_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, uintmax_t))) : flags&0x0020 ? ((argtable != ((void*) 0)) ? *((unsigned long long*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, unsigned long long))) : flags&0x0010 ? ((argtable != ((void*)0)) ? *((unsigned long*)(&argtable [nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned long ))) : flags&0x0200 ? (uintptr_t)((argtable != ((void*)0)) ? *((ptrdiff_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, ptrdiff_t))) : flags&0x0400 ? ((argtable != ((void*) 0)) ? *((size_t*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg (ap, size_t))) : flags&0x0040 ? (unsigned short)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg++])) : (nextarg ++, __builtin_va_arg(ap, int))) : flags&0x0800 ? (unsigned char)((argtable != ((void*)0)) ? *((int*)(&argtable[nextarg ++])) : (nextarg++, __builtin_va_arg(ap, int))) : ((argtable != ((void*)0)) ? *((unsigned int*)(&argtable[nextarg++])) : (nextarg++, __builtin_va_arg(ap, unsigned int))))); |
897 | base = HEX; |
898 | /* leading 0x/X only if non-zero */ |
899 | if (flags & ALT0x0001 && _umax != 0) |
900 | ox[1] = ch; |
901 | |
902 | /* unsigned conversions */ |
903 | nosign: sign = '\0'; |
904 | /* |
905 | * ``... diouXx conversions ... if a precision is |
906 | * specified, the 0 flag will be ignored.'' |
907 | * -- ANSI X3J11 |
908 | */ |
909 | number: if ((dprec = prec) >= 0) |
910 | flags &= ~ZEROPAD0x0080; |
911 | |
912 | /* |
913 | * ``The result of converting a zero value with an |
914 | * explicit precision of zero is no characters.'' |
915 | * -- ANSI X3J11 |
916 | */ |
917 | cp = buf + BUF100; |
918 | if (_umax != 0 || prec != 0) { |
919 | /* |
920 | * Unsigned mod is hard, and unsigned mod |
921 | * by a constant is easier than that by |
922 | * a variable; hence this switch. |
923 | */ |
924 | switch (base) { |
925 | case OCT: |
926 | do { |
927 | *--cp = to_char(_umax & 7)((_umax & 7) + '0'); |
928 | _umax >>= 3; |
929 | } while (_umax); |
930 | /* handle octal leading 0 */ |
931 | if (flags & ALT0x0001 && *cp != '0') |
932 | *--cp = '0'; |
933 | break; |
934 | |
935 | case DEC: |
936 | /* many numbers are 1 digit */ |
937 | while (_umax >= 10) { |
938 | *--cp = to_char(_umax % 10)((_umax % 10) + '0'); |
939 | _umax /= 10; |
940 | } |
941 | *--cp = to_char(_umax)((_umax) + '0'); |
942 | break; |
943 | |
944 | case HEX: |
945 | do { |
946 | *--cp = xdigs[_umax & 15]; |
947 | _umax >>= 4; |
948 | } while (_umax); |
949 | break; |
950 | |
951 | default: |
952 | cp = "bug in vfprintf: bad base"; |
953 | size = strlen(cp); |
954 | goto skipsize; |
955 | } |
956 | } |
957 | size = buf + BUF100 - cp; |
958 | if (size > BUF100) /* should never happen */ |
959 | abort(); |
960 | skipsize: |
961 | break; |
962 | default: /* "%?" prints ?, unless ? is NUL */ |
963 | if (ch == '\0') |
964 | goto done; |
965 | /* pretend it was %c with argument ch */ |
966 | cp = buf; |
967 | *cp = ch; |
968 | size = 1; |
969 | sign = '\0'; |
970 | break; |
971 | } |
972 | |
973 | /* |
974 | * All reasonable formats wind up here. At this point, `cp' |
975 | * points to a string which (if not flags&LADJUST) should be |
976 | * padded out to `width' places. If flags&ZEROPAD, it should |
977 | * first be prefixed by any sign or other prefix; otherwise, |
978 | * it should be blank padded before the prefix is emitted. |
979 | * After any left-hand padding and prefixing, emit zeroes |
980 | * required by a decimal %[diouxX] precision, then print the |
981 | * string proper, then emit zeroes required by any leftover |
982 | * floating precision; finally, if LADJUST, pad with blanks. |
983 | * |
984 | * Compute actual size, so we know how much to pad. |
985 | * size excludes decimal prec; realsz includes it. |
986 | */ |
987 | realsz = dprec > size ? dprec : size; |
988 | if (sign) |
989 | realsz++; |
990 | if (ox[1]) |
991 | realsz+= 2; |
992 | |
993 | /* right-adjusting blank padding */ |
994 | if ((flags & (LADJUST0x0004|ZEROPAD0x0080)) == 0) |
995 | PAD(width - realsz, blanks)do { if ((n = (width - realsz)) > 0) { while (n > 16) { do { iovp->iov_base = (blanks); iovp->iov_len = (16); uio .uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); n -= 16; } do { iovp->iov_base = (blanks); iovp->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); } } while (0); |
996 | |
997 | /* prefix */ |
998 | if (sign) |
999 | PRINT(&sign, 1)do { iovp->iov_base = (&sign); iovp->iov_len = (1); uio.uio_resid += (1); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1000 | if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
1001 | ox[0] = '0'; |
1002 | PRINT(ox, 2)do { iovp->iov_base = (ox); iovp->iov_len = (2); uio.uio_resid += (2); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint (fp, &uio)) goto error; iovp = iov; } } while (0); |
1003 | } |
1004 | |
1005 | /* right-adjusting zero padding */ |
1006 | if ((flags & (LADJUST0x0004|ZEROPAD0x0080)) == ZEROPAD0x0080) |
1007 | PAD(width - realsz, zeroes)do { if ((n = (width - realsz)) > 0) { while (n > 16) { do { iovp->iov_base = (zeroes); iovp->iov_len = (16); uio .uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); n -= 16; } do { iovp->iov_base = (zeroes); iovp->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); } } while (0); |
1008 | |
1009 | /* leading zeroes from decimal precision */ |
1010 | PAD(dprec - size, zeroes)do { if ((n = (dprec - size)) > 0) { while (n > 16) { do { iovp->iov_base = (zeroes); iovp->iov_len = (16); uio .uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); n -= 16; } do { iovp->iov_base = (zeroes); iovp->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); } } while (0); |
1011 | |
1012 | /* the string or number proper */ |
1013 | #ifdef FLOATING_POINT1 |
1014 | if ((flags & FPT0x0100) == 0) { |
1015 | PRINT(cp, size)do { iovp->iov_base = (cp); iovp->iov_len = (size); uio .uio_resid += (size); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1016 | } else { /* glue together f_p fragments */ |
1017 | if (decimal_point == NULL((void*)0)) |
1018 | decimal_point = nl_langinfo(RADIXCHAR44); |
1019 | if (!expchar) { /* %[fF] or sufficiently short %[gG] */ |
1020 | if (expt <= 0) { |
1021 | PRINT(zeroes, 1)do { iovp->iov_base = (zeroes); iovp->iov_len = (1); uio .uio_resid += (1); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); |
1022 | if (prec || flags & ALT0x0001) |
1023 | PRINT(decimal_point, 1)do { iovp->iov_base = (decimal_point); iovp->iov_len = ( 1); uio.uio_resid += (1); iovp++; if (++uio.uio_iovcnt >= 8 ) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1024 | PAD(-expt, zeroes)do { if ((n = (-expt)) > 0) { while (n > 16) { do { iovp ->iov_base = (zeroes); iovp->iov_len = (16); uio.uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint (fp, &uio)) goto error; iovp = iov; } } while (0); n -= 16 ; } do { iovp->iov_base = (zeroes); iovp->iov_len = (n) ; uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); } } while (0); |
1025 | /* already handled initial 0's */ |
1026 | prec += expt; |
1027 | } else { |
1028 | PRINTANDPAD(cp, dtoaend, lead, zeroes)do { n2 = (dtoaend) - (cp); if (n2 > (lead)) n2 = (lead); if (n2 > 0) do { iovp->iov_base = ((cp)); iovp->iov_len = (n2); uio.uio_resid += (n2); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); do { if ((n = ((lead) - (n2 > 0 ? n2 : 0))) > 0) { while (n > 16) { do { iovp->iov_base = ((zeroes)) ; iovp->iov_len = (16); uio.uio_resid += (16); iovp++; if ( ++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error ; iovp = iov; } } while (0); n -= 16; } do { iovp->iov_base = ((zeroes)); iovp->iov_len = (n); uio.uio_resid += (n); iovp ++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio )) goto error; iovp = iov; } } while (0); } } while (0); } while (0); |
1029 | cp += lead; |
1030 | if (prec || flags & ALT0x0001) |
1031 | PRINT(decimal_point, 1)do { iovp->iov_base = (decimal_point); iovp->iov_len = ( 1); uio.uio_resid += (1); iovp++; if (++uio.uio_iovcnt >= 8 ) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1032 | } |
1033 | PRINTANDPAD(cp, dtoaend, prec, zeroes)do { n2 = (dtoaend) - (cp); if (n2 > (prec)) n2 = (prec); if (n2 > 0) do { iovp->iov_base = ((cp)); iovp->iov_len = (n2); uio.uio_resid += (n2); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); do { if ((n = ((prec) - (n2 > 0 ? n2 : 0))) > 0) { while (n > 16) { do { iovp->iov_base = ((zeroes)) ; iovp->iov_len = (16); uio.uio_resid += (16); iovp++; if ( ++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error ; iovp = iov; } } while (0); n -= 16; } do { iovp->iov_base = ((zeroes)); iovp->iov_len = (n); uio.uio_resid += (n); iovp ++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio )) goto error; iovp = iov; } } while (0); } } while (0); } while (0); |
1034 | } else { /* %[eE] or sufficiently long %[gG] */ |
1035 | if (prec > 1 || flags & ALT0x0001) { |
1036 | buf[0] = *cp++; |
1037 | buf[1] = *decimal_point; |
1038 | PRINT(buf, 2)do { iovp->iov_base = (buf); iovp->iov_len = (2); uio.uio_resid += (2); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint (fp, &uio)) goto error; iovp = iov; } } while (0); |
1039 | PRINT(cp, ndig-1)do { iovp->iov_base = (cp); iovp->iov_len = (ndig-1); uio .uio_resid += (ndig-1); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1040 | PAD(prec - ndig, zeroes)do { if ((n = (prec - ndig)) > 0) { while (n > 16) { do { iovp->iov_base = (zeroes); iovp->iov_len = (16); uio .uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); n -= 16; } do { iovp->iov_base = (zeroes); iovp->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); } } while (0); |
1041 | } else { /* XeYYY */ |
1042 | PRINT(cp, 1)do { iovp->iov_base = (cp); iovp->iov_len = (1); uio.uio_resid += (1); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint (fp, &uio)) goto error; iovp = iov; } } while (0); |
1043 | } |
1044 | PRINT(expstr, expsize)do { iovp->iov_base = (expstr); iovp->iov_len = (expsize ); uio.uio_resid += (expsize); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1045 | } |
1046 | } |
1047 | #else |
1048 | PRINT(cp, size)do { iovp->iov_base = (cp); iovp->iov_len = (size); uio .uio_resid += (size); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); |
1049 | #endif |
1050 | /* left-adjusting padding (always blank) */ |
1051 | if (flags & LADJUST0x0004) |
1052 | PAD(width - realsz, blanks)do { if ((n = (width - realsz)) > 0) { while (n > 16) { do { iovp->iov_base = (blanks); iovp->iov_len = (16); uio .uio_resid += (16); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while ( 0); n -= 16; } do { iovp->iov_base = (blanks); iovp->iov_len = (n); uio.uio_resid += (n); iovp++; if (++uio.uio_iovcnt >= 8) { if (__sprint(fp, &uio)) goto error; iovp = iov; } } while (0); } } while (0); |
1053 | |
1054 | /* finally, adjust ret */ |
1055 | if (width < realsz) |
1056 | width = realsz; |
1057 | if (width > INT_MAX2147483647 - ret) |
1058 | goto overflow; |
1059 | ret += width; |
1060 | |
1061 | FLUSH()do { if (uio.uio_resid && __sprint(fp, &uio)) goto error; uio.uio_iovcnt = 0; iovp = iov; } while (0); /* copy out the I/O vectors */ |
1062 | } |
1063 | done: |
1064 | FLUSH()do { if (uio.uio_resid && __sprint(fp, &uio)) goto error; uio.uio_iovcnt = 0; iovp = iov; } while (0); |
1065 | error: |
1066 | va_end(orgap)__builtin_va_end(orgap); |
1067 | if (__sferror(fp)(((fp)->_flags & 0x0040) != 0)) |
1068 | ret = -1; |
1069 | goto finish; |
1070 | |
1071 | overflow: |
1072 | errno(*__errno()) = EOVERFLOW87; |
1073 | ret = -1; |
1074 | |
1075 | finish: |
1076 | #ifdef PRINTF_WIDE_CHAR1 |
1077 | free(convbuf); |
1078 | #endif |
1079 | #ifdef FLOATING_POINT1 |
1080 | if (dtoaresult) |
1081 | __freedtoa(dtoaresult); |
1082 | #endif |
1083 | if (argtable != NULL((void*)0) && argtable != statargtable) { |
1084 | munmap(argtable, argtablesiz); |
1085 | argtable = NULL((void*)0); |
1086 | } |
1087 | return (ret); |
1088 | } |
1089 | |
1090 | /* |
1091 | * Type ids for argument type table. |
1092 | */ |
1093 | #define T_UNUSED0 0 |
1094 | #define T_SHORT1 1 |
1095 | #define T_U_SHORT2 2 |
1096 | #define TP_SHORT3 3 |
1097 | #define T_INT4 4 |
1098 | #define T_U_INT5 5 |
1099 | #define TP_INT6 6 |
1100 | #define T_LONG7 7 |
1101 | #define T_U_LONG8 8 |
1102 | #define TP_LONG9 9 |
1103 | #define T_LLONG10 10 |
1104 | #define T_U_LLONG11 11 |
1105 | #define TP_LLONG12 12 |
1106 | #define T_DOUBLE13 13 |
1107 | #define T_LONG_DOUBLE14 14 |
1108 | #define TP_CHAR15 15 |
1109 | #define TP_VOID16 16 |
1110 | #define T_PTRINT17 17 |
1111 | #define TP_PTRINT18 18 |
1112 | #define T_SIZEINT19 19 |
1113 | #define T_SSIZEINT20 20 |
1114 | #define TP_SSIZEINT21 21 |
1115 | #define T_MAXINT22 22 |
1116 | #define T_MAXUINT23 23 |
1117 | #define TP_MAXINT24 24 |
1118 | #define T_CHAR25 25 |
1119 | #define T_U_CHAR26 26 |
1120 | #define T_WINT27 27 |
1121 | #define TP_WCHAR28 28 |
1122 | |
1123 | /* |
1124 | * Find all arguments when a positional parameter is encountered. Returns a |
1125 | * table, indexed by argument number, of pointers to each arguments. The |
1126 | * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. |
1127 | * It will be replaced with a mmap-ed one if it overflows (malloc cannot be |
1128 | * used since we are attempting to make snprintf thread safe, and alloca is |
1129 | * problematic since we have nested functions..) |
1130 | */ |
1131 | static int |
1132 | __find_arguments(const char *fmt0, va_list ap, union arg **argtable, |
1133 | size_t *argtablesiz) |
1134 | { |
1135 | char *fmt; /* format string */ |
1136 | int ch; /* character from fmt */ |
1137 | int n, n2; /* handy integer (short term usage) */ |
1138 | char *cp; /* handy char pointer (short term usage) */ |
1139 | int flags; /* flags as above */ |
1140 | unsigned char *typetable; /* table of types */ |
1141 | unsigned char stattypetable[STATIC_ARG_TBL_SIZE8]; |
1142 | int tablesize; /* current size of type table */ |
1143 | int tablemax; /* largest used index in table */ |
1144 | int nextarg; /* 1-based argument index */ |
1145 | int ret = 0; /* return value */ |
1146 | |
1147 | /* |
1148 | * Add an argument type to the table, expanding if necessary. |
1149 | */ |
1150 | #define ADDTYPE(type)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = type) \ |
1151 | ((nextarg >= tablesize) ? \ |
1152 | __grow_type_table(&typetable, &tablesize) : 0, \ |
1153 | (nextarg > tablemax) ? tablemax = nextarg : 0, \ |
1154 | typetable[nextarg++] = type) |
1155 | |
1156 | #define ADDSARG()((flags&0x1000) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 22) : ((flags &0x0200) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : ((flags &0x0400) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 20) : ((flags &0x0020) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 10) : ((flags &0x0010) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 7) : ((flags &0x0040) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 1) : ((flags &0x0800) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 25) : ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize ) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable [nextarg++] = 4)))))))) \ |
1157 | ((flags&MAXINT0x1000) ? ADDTYPE(T_MAXINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 22) : \ |
1158 | ((flags&PTRINT0x0200) ? ADDTYPE(T_PTRINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : \ |
1159 | ((flags&SIZEINT0x0400) ? ADDTYPE(T_SSIZEINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 20) : \ |
1160 | ((flags&LLONGINT0x0020) ? ADDTYPE(T_LLONG)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 10) : \ |
1161 | ((flags&LONGINT0x0010) ? ADDTYPE(T_LONG)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 7) : \ |
1162 | ((flags&SHORTINT0x0040) ? ADDTYPE(T_SHORT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 1) : \ |
1163 | ((flags&CHARINT0x0800) ? ADDTYPE(T_CHAR)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 25) : ADDTYPE(T_INT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 4)))))))) |
1164 | |
1165 | #define ADDUARG()((flags&0x1000) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 23) : ((flags &0x0200) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : ((flags &0x0400) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 19) : ((flags &0x0020) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 11) : ((flags &0x0010) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 8) : ((flags &0x0040) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 2) : ((flags &0x0800) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 26) : ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize ) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable [nextarg++] = 5)))))))) \ |
1166 | ((flags&MAXINT0x1000) ? ADDTYPE(T_MAXUINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 23) : \ |
1167 | ((flags&PTRINT0x0200) ? ADDTYPE(T_PTRINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : \ |
1168 | ((flags&SIZEINT0x0400) ? ADDTYPE(T_SIZEINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 19) : \ |
1169 | ((flags&LLONGINT0x0020) ? ADDTYPE(T_U_LLONG)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 11) : \ |
1170 | ((flags&LONGINT0x0010) ? ADDTYPE(T_U_LONG)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 8) : \ |
1171 | ((flags&SHORTINT0x0040) ? ADDTYPE(T_U_SHORT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 2) : \ |
1172 | ((flags&CHARINT0x0800) ? ADDTYPE(T_U_CHAR)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 26) : ADDTYPE(T_U_INT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 5)))))))) |
1173 | |
1174 | /* |
1175 | * Add * arguments to the type array. |
1176 | */ |
1177 | #define ADDASTER()n2 = 0; cp = fmt; while (((unsigned)((*cp) - '0') <= 9)) { do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); cp++; } if (*cp == '$') { int hold = nextarg; nextarg = n2; ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++ ] = 4); nextarg = hold; fmt = ++cp; } else { ((nextarg >= tablesize ) ? __grow_type_table(&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++ ] = 4); } \ |
1178 | n2 = 0; \ |
1179 | cp = fmt; \ |
1180 | while (is_digit(*cp)((unsigned)((*cp) - '0') <= 9)) { \ |
1181 | APPEND_DIGIT(n2, *cp)do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); \ |
1182 | cp++; \ |
1183 | } \ |
1184 | if (*cp == '$') { \ |
1185 | int hold = nextarg; \ |
1186 | nextarg = n2; \ |
1187 | ADDTYPE(T_INT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 4); \ |
1188 | nextarg = hold; \ |
1189 | fmt = ++cp; \ |
1190 | } else { \ |
1191 | ADDTYPE(T_INT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 4); \ |
1192 | } |
1193 | fmt = (char *)fmt0; |
1194 | typetable = stattypetable; |
1195 | tablesize = STATIC_ARG_TBL_SIZE8; |
1196 | tablemax = 0; |
1197 | nextarg = 1; |
1198 | memset(typetable, T_UNUSED0, STATIC_ARG_TBL_SIZE8); |
1199 | |
1200 | /* |
1201 | * Scan the format for conversions (`%' character). |
1202 | */ |
1203 | for (;;) { |
1204 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) |
Value stored to 'cp' is never read | |
1205 | continue; |
1206 | if (ch == '\0') |
1207 | goto done; |
1208 | fmt++; /* skip over '%' */ |
1209 | |
1210 | flags = 0; |
1211 | |
1212 | rflag: ch = *fmt++; |
1213 | reswitch: switch (ch) { |
1214 | case ' ': |
1215 | case '#': |
1216 | case '\'': |
1217 | goto rflag; |
1218 | case '*': |
1219 | ADDASTER()n2 = 0; cp = fmt; while (((unsigned)((*cp) - '0') <= 9)) { do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); cp++; } if (*cp == '$') { int hold = nextarg; nextarg = n2; ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++ ] = 4); nextarg = hold; fmt = ++cp; } else { ((nextarg >= tablesize ) ? __grow_type_table(&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++ ] = 4); }; |
1220 | goto rflag; |
1221 | case '-': |
1222 | case '+': |
1223 | goto rflag; |
1224 | case '.': |
1225 | if ((ch = *fmt++) == '*') { |
1226 | ADDASTER()n2 = 0; cp = fmt; while (((unsigned)((*cp) - '0') <= 9)) { do { if ((n2) > 2147483647 / 10) goto overflow; (n2) *= 10 ; if ((n2) > 2147483647 - (((*cp)) - '0')) goto overflow; ( n2) += (((*cp)) - '0'); } while (0); cp++; } if (*cp == '$') { int hold = nextarg; nextarg = n2; ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++ ] = 4); nextarg = hold; fmt = ++cp; } else { ((nextarg >= tablesize ) ? __grow_type_table(&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++ ] = 4); }; |
1227 | goto rflag; |
1228 | } |
1229 | while (is_digit(ch)((unsigned)((ch) - '0') <= 9)) { |
1230 | ch = *fmt++; |
1231 | } |
1232 | goto reswitch; |
1233 | case '0': |
1234 | goto rflag; |
1235 | case '1': case '2': case '3': case '4': |
1236 | case '5': case '6': case '7': case '8': case '9': |
1237 | n = 0; |
1238 | do { |
1239 | APPEND_DIGIT(n ,ch)do { if ((n) > 2147483647 / 10) goto overflow; (n) *= 10; if ((n) > 2147483647 - (((ch)) - '0')) goto overflow; (n) += (((ch)) - '0'); } while (0); |
1240 | ch = *fmt++; |
1241 | } while (is_digit(ch)((unsigned)((ch) - '0') <= 9)); |
1242 | if (ch == '$') { |
1243 | nextarg = n; |
1244 | goto rflag; |
1245 | } |
1246 | goto reswitch; |
1247 | #ifdef FLOATING_POINT1 |
1248 | case 'L': |
1249 | flags |= LONGDBL0x0008; |
1250 | goto rflag; |
1251 | #endif |
1252 | case 'h': |
1253 | if (*fmt == 'h') { |
1254 | fmt++; |
1255 | flags |= CHARINT0x0800; |
1256 | } else { |
1257 | flags |= SHORTINT0x0040; |
1258 | } |
1259 | goto rflag; |
1260 | case 'j': |
1261 | flags |= MAXINT0x1000; |
1262 | goto rflag; |
1263 | case 'l': |
1264 | if (*fmt == 'l') { |
1265 | fmt++; |
1266 | flags |= LLONGINT0x0020; |
1267 | } else { |
1268 | flags |= LONGINT0x0010; |
1269 | } |
1270 | goto rflag; |
1271 | case 'q': |
1272 | flags |= LLONGINT0x0020; |
1273 | goto rflag; |
1274 | case 't': |
1275 | flags |= PTRINT0x0200; |
1276 | goto rflag; |
1277 | case 'z': |
1278 | flags |= SIZEINT0x0400; |
1279 | goto rflag; |
1280 | case 'c': |
1281 | #ifdef PRINTF_WIDE_CHAR1 |
1282 | if (flags & LONGINT0x0010) |
1283 | ADDTYPE(T_WINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 27); |
1284 | else |
1285 | #endif |
1286 | ADDTYPE(T_INT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 4); |
1287 | break; |
1288 | case 'D': |
1289 | flags |= LONGINT0x0010; |
1290 | /*FALLTHROUGH*/ |
1291 | case 'd': |
1292 | case 'i': |
1293 | ADDSARG()((flags&0x1000) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 22) : ((flags &0x0200) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : ((flags &0x0400) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 20) : ((flags &0x0020) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 10) : ((flags &0x0010) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 7) : ((flags &0x0040) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 1) : ((flags &0x0800) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 25) : ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize ) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable [nextarg++] = 4)))))))); |
1294 | break; |
1295 | #ifdef FLOATING_POINT1 |
1296 | case 'a': |
1297 | case 'A': |
1298 | case 'e': |
1299 | case 'E': |
1300 | case 'f': |
1301 | case 'F': |
1302 | case 'g': |
1303 | case 'G': |
1304 | if (flags & LONGDBL0x0008) |
1305 | ADDTYPE(T_LONG_DOUBLE)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 14); |
1306 | else |
1307 | ADDTYPE(T_DOUBLE)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 13); |
1308 | break; |
1309 | #endif /* FLOATING_POINT */ |
1310 | case 'n': |
1311 | if (flags & LLONGINT0x0020) |
1312 | ADDTYPE(TP_LLONG)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 12); |
1313 | else if (flags & LONGINT0x0010) |
1314 | ADDTYPE(TP_LONG)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 9); |
1315 | else if (flags & SHORTINT0x0040) |
1316 | ADDTYPE(TP_SHORT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 3); |
1317 | else if (flags & PTRINT0x0200) |
1318 | ADDTYPE(TP_PTRINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 18); |
1319 | else if (flags & SIZEINT0x0400) |
1320 | ADDTYPE(TP_SSIZEINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 21); |
1321 | else if (flags & MAXINT0x1000) |
1322 | ADDTYPE(TP_MAXINT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 24); |
1323 | else |
1324 | ADDTYPE(TP_INT)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 6); |
1325 | continue; /* no output */ |
1326 | case 'O': |
1327 | flags |= LONGINT0x0010; |
1328 | /*FALLTHROUGH*/ |
1329 | case 'o': |
1330 | ADDUARG()((flags&0x1000) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 23) : ((flags &0x0200) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : ((flags &0x0400) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 19) : ((flags &0x0020) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 11) : ((flags &0x0010) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 8) : ((flags &0x0040) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 2) : ((flags &0x0800) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 26) : ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize ) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable [nextarg++] = 5)))))))); |
1331 | break; |
1332 | case 'p': |
1333 | ADDTYPE(TP_VOID)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 16); |
1334 | break; |
1335 | case 's': |
1336 | #ifdef PRINTF_WIDE_CHAR1 |
1337 | if (flags & LONGINT0x0010) |
1338 | ADDTYPE(TP_WCHAR)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 28); |
1339 | else |
1340 | #endif |
1341 | ADDTYPE(TP_CHAR)((nextarg >= tablesize) ? __grow_type_table(&typetable , &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 15); |
1342 | break; |
1343 | case 'U': |
1344 | flags |= LONGINT0x0010; |
1345 | /*FALLTHROUGH*/ |
1346 | case 'u': |
1347 | case 'X': |
1348 | case 'x': |
1349 | ADDUARG()((flags&0x1000) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 23) : ((flags &0x0200) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 17) : ((flags &0x0400) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 19) : ((flags &0x0020) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 11) : ((flags &0x0010) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 8) : ((flags &0x0040) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 2) : ((flags &0x0800) ? ((nextarg >= tablesize) ? __grow_type_table (&typetable, &tablesize) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = 26) : ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize ) : 0, (nextarg > tablemax) ? tablemax = nextarg : 0, typetable [nextarg++] = 5)))))))); |
1350 | break; |
1351 | default: /* "%?" prints ?, unless ? is NUL */ |
1352 | if (ch == '\0') |
1353 | goto done; |
1354 | break; |
1355 | } |
1356 | } |
1357 | done: |
1358 | /* |
1359 | * Build the argument table. |
1360 | */ |
1361 | if (tablemax >= STATIC_ARG_TBL_SIZE8) { |
1362 | *argtablesiz = sizeof(union arg) * (tablemax + 1); |
1363 | *argtable = mmap(NULL((void*)0), *argtablesiz, |
1364 | PROT_WRITE0x02|PROT_READ0x01, MAP_ANON0x1000|MAP_PRIVATE0x0002, -1, 0); |
1365 | if (*argtable == MAP_FAILED((void *)-1)) |
1366 | return (-1); |
1367 | } |
1368 | |
1369 | for (n = 1; n <= tablemax; n++) { |
1370 | switch (typetable[n]) { |
1371 | case T_UNUSED0: |
1372 | case T_CHAR25: |
1373 | case T_U_CHAR26: |
1374 | case T_SHORT1: |
1375 | case T_U_SHORT2: |
1376 | case T_INT4: |
1377 | (*argtable)[n].intarg = va_arg(ap, int)__builtin_va_arg(ap, int); |
1378 | break; |
1379 | case TP_SHORT3: |
1380 | (*argtable)[n].pshortarg = va_arg(ap, short *)__builtin_va_arg(ap, short *); |
1381 | break; |
1382 | case T_U_INT5: |
1383 | (*argtable)[n].uintarg = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int); |
1384 | break; |
1385 | case TP_INT6: |
1386 | (*argtable)[n].pintarg = va_arg(ap, int *)__builtin_va_arg(ap, int *); |
1387 | break; |
1388 | case T_LONG7: |
1389 | (*argtable)[n].longarg = va_arg(ap, long)__builtin_va_arg(ap, long); |
1390 | break; |
1391 | case T_U_LONG8: |
1392 | (*argtable)[n].ulongarg = va_arg(ap, unsigned long)__builtin_va_arg(ap, unsigned long); |
1393 | break; |
1394 | case TP_LONG9: |
1395 | (*argtable)[n].plongarg = va_arg(ap, long *)__builtin_va_arg(ap, long *); |
1396 | break; |
1397 | case T_LLONG10: |
1398 | (*argtable)[n].longlongarg = va_arg(ap, long long)__builtin_va_arg(ap, long long); |
1399 | break; |
1400 | case T_U_LLONG11: |
1401 | (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long)__builtin_va_arg(ap, unsigned long long); |
1402 | break; |
1403 | case TP_LLONG12: |
1404 | (*argtable)[n].plonglongarg = va_arg(ap, long long *)__builtin_va_arg(ap, long long *); |
1405 | break; |
1406 | #ifdef FLOATING_POINT1 |
1407 | case T_DOUBLE13: |
1408 | (*argtable)[n].doublearg = va_arg(ap, double)__builtin_va_arg(ap, double); |
1409 | break; |
1410 | case T_LONG_DOUBLE14: |
1411 | (*argtable)[n].longdoublearg = va_arg(ap, long double)__builtin_va_arg(ap, long double); |
1412 | break; |
1413 | #endif |
1414 | case TP_CHAR15: |
1415 | (*argtable)[n].pchararg = va_arg(ap, char *)__builtin_va_arg(ap, char *); |
1416 | break; |
1417 | case TP_VOID16: |
1418 | (*argtable)[n].pvoidarg = va_arg(ap, void *)__builtin_va_arg(ap, void *); |
1419 | break; |
1420 | case T_PTRINT17: |
1421 | (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t)__builtin_va_arg(ap, ptrdiff_t); |
1422 | break; |
1423 | case TP_PTRINT18: |
1424 | (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *)__builtin_va_arg(ap, ptrdiff_t *); |
1425 | break; |
1426 | case T_SIZEINT19: |
1427 | (*argtable)[n].sizearg = va_arg(ap, size_t)__builtin_va_arg(ap, size_t); |
1428 | break; |
1429 | case T_SSIZEINT20: |
1430 | (*argtable)[n].ssizearg = va_arg(ap, ssize_t)__builtin_va_arg(ap, ssize_t); |
1431 | break; |
1432 | case TP_SSIZEINT21: |
1433 | (*argtable)[n].pssizearg = va_arg(ap, ssize_t *)__builtin_va_arg(ap, ssize_t *); |
1434 | break; |
1435 | case T_MAXINT22: |
1436 | (*argtable)[n].intmaxarg = va_arg(ap, intmax_t)__builtin_va_arg(ap, intmax_t); |
1437 | break; |
1438 | case T_MAXUINT23: |
1439 | (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t)__builtin_va_arg(ap, uintmax_t); |
1440 | break; |
1441 | case TP_MAXINT24: |
1442 | (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t *)__builtin_va_arg(ap, intmax_t *); |
1443 | break; |
1444 | #ifdef PRINTF_WIDE_CHAR1 |
1445 | case T_WINT27: |
1446 | (*argtable)[n].wintarg = va_arg(ap, wint_t)__builtin_va_arg(ap, wint_t); |
1447 | break; |
1448 | case TP_WCHAR28: |
1449 | (*argtable)[n].pwchararg = va_arg(ap, wchar_t *)__builtin_va_arg(ap, wchar_t *); |
1450 | break; |
1451 | #endif |
1452 | } |
1453 | } |
1454 | goto finish; |
1455 | |
1456 | overflow: |
1457 | errno(*__errno()) = EOVERFLOW87; |
1458 | ret = -1; |
1459 | |
1460 | finish: |
1461 | if (typetable != NULL((void*)0) && typetable != stattypetable) { |
1462 | munmap(typetable, *argtablesiz); |
1463 | typetable = NULL((void*)0); |
1464 | } |
1465 | return (ret); |
1466 | } |
1467 | |
1468 | /* |
1469 | * Increase the size of the type table. |
1470 | */ |
1471 | static int |
1472 | __grow_type_table(unsigned char **typetable, int *tablesize) |
1473 | { |
1474 | unsigned char *oldtable = *typetable; |
1475 | int newsize = *tablesize * 2; |
1476 | |
1477 | if (newsize < getpagesize()) |
1478 | newsize = getpagesize(); |
1479 | |
1480 | if (*tablesize == STATIC_ARG_TBL_SIZE8) { |
1481 | *typetable = mmap(NULL((void*)0), newsize, PROT_WRITE0x02|PROT_READ0x01, |
1482 | MAP_ANON0x1000|MAP_PRIVATE0x0002, -1, 0); |
1483 | if (*typetable == MAP_FAILED((void *)-1)) |
1484 | return (-1); |
1485 | bcopy(oldtable, *typetable, *tablesize); |
1486 | } else { |
1487 | unsigned char *new = mmap(NULL((void*)0), newsize, PROT_WRITE0x02|PROT_READ0x01, |
1488 | MAP_ANON0x1000|MAP_PRIVATE0x0002, -1, 0); |
1489 | if (new == MAP_FAILED((void *)-1)) |
1490 | return (-1); |
1491 | memmove(new, *typetable, *tablesize); |
1492 | munmap(*typetable, *tablesize); |
1493 | *typetable = new; |
1494 | } |
1495 | memset(*typetable + *tablesize, T_UNUSED0, (newsize - *tablesize)); |
1496 | |
1497 | *tablesize = newsize; |
1498 | return (0); |
1499 | } |
1500 | |
1501 | |
1502 | #ifdef FLOATING_POINT1 |
1503 | static int |
1504 | exponent(char *p0, int exp, int fmtch) |
1505 | { |
1506 | char *p, *t; |
1507 | char expbuf[MAXEXPDIG6]; |
1508 | |
1509 | p = p0; |
1510 | *p++ = fmtch; |
1511 | if (exp < 0) { |
1512 | exp = -exp; |
1513 | *p++ = '-'; |
1514 | } else |
1515 | *p++ = '+'; |
1516 | t = expbuf + MAXEXPDIG6; |
1517 | if (exp > 9) { |
1518 | do { |
1519 | *--t = to_char(exp % 10)((exp % 10) + '0'); |
1520 | } while ((exp /= 10) > 9); |
1521 | *--t = to_char(exp)((exp) + '0'); |
1522 | for (; t < expbuf + MAXEXPDIG6; *p++ = *t++) |
1523 | /* nothing */; |
1524 | } else { |
1525 | /* |
1526 | * Exponents for decimal floating point conversions |
1527 | * (%[eEgG]) must be at least two characters long, |
1528 | * whereas exponents for hexadecimal conversions can |
1529 | * be only one character long. |
1530 | */ |
1531 | if (fmtch == 'e' || fmtch == 'E') |
1532 | *p++ = '0'; |
1533 | *p++ = to_char(exp)((exp) + '0'); |
1534 | } |
1535 | return (p - p0); |
1536 | } |
1537 | #endif /* FLOATING_POINT */ |