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