]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/vfwscanf.c.patch
Libc-391.2.3.tar.gz
[apple/libc.git] / stdio / FreeBSD / vfwscanf.c.patch
CommitLineData
3d9156a7
A
1--- vfwscanf.c.orig 2004-11-25 11:38:36.000000000 -0800
2+++ vfwscanf.c 2005-02-23 16:25:00.000000000 -0800
3@@ -42,6 +42,8 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
6
7+#include "xlocale_private.h"
8+
9 #include "namespace.h"
10 #include <ctype.h>
11 #include <inttypes.h>
12@@ -98,7 +100,9 @@
59e0d9fe
A
13 #define CT_INT 3 /* %[dioupxX] conversion */
14 #define CT_FLOAT 4 /* %[efgEFG] conversion */
15
16-static int parsefloat(FILE *, wchar_t *, wchar_t *);
3d9156a7
A
17+#ifndef NO_FLOATING_POINT
18+static int parsefloat(FILE *, wchar_t **, size_t, locale_t loc);
19+#endif /* !NO_FLOATING_POINT */
59e0d9fe
A
20
21 extern int __scanfdebug;
22
3d9156a7
A
23@@ -116,7 +120,21 @@
24
25 FLOCKFILE(fp);
26 ORIENT(fp, 1);
27- ret = __vfwscanf(fp, fmt, ap);
28+ ret = __vfwscanf(fp, __current_locale(), fmt, ap);
29+ FUNLOCKFILE(fp);
30+ return (ret);
31+}
32+
33+int
34+vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
35+ va_list ap)
36+{
37+ int ret;
38+
39+ NORMALIZE_LOCALE(loc);
40+ FLOCKFILE(fp);
41+ ORIENT(fp, 1);
42+ ret = __vfwscanf(fp, loc, fmt, ap);
43 FUNLOCKFILE(fp);
44 return (ret);
45 }
46@@ -124,8 +142,9 @@
47 /*
48 * Non-MT-safe version.
49 */
50-int
51-__vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
52+__private_extern__ int
53+__vfwscanf(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
54+ va_list ap)
55 {
56 wint_t c; /* character from format, or conversion */
57 size_t width; /* field width, or 0 */
58@@ -134,7 +153,6 @@
59e0d9fe
A
59 int flags; /* flags as defined above */
60 wchar_t *p0; /* saves original value of p when necessary */
61 int nassigned; /* number of fields assigned */
62- int nconversions; /* number of conversions */
63 int nread; /* number of characters consumed from fp */
64 int base; /* base argument to conversion function */
65 wchar_t buf[BUF]; /* buffer for numeric conversions */
3d9156a7
A
66@@ -145,27 +163,30 @@
67 char *mbp; /* multibyte string pointer for %c %s %[ */
68 size_t nconv; /* number of bytes in mb. conversion */
69 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
70+ int index; /* for %index$ */
71+ va_list ap_orig; /* to reset ap to first argument */
72 static const mbstate_t initial;
73 mbstate_t mbs;
74+ int mb_cur_max = MB_CUR_MAX_L(loc);
75
76 /* `basefix' is used to avoid `if' tests in the integer scanner */
77 static short basefix[17] =
59e0d9fe
A
78 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
79
80 nassigned = 0;
81- nconversions = 0;
82 nread = 0;
83 ccls = ccle = NULL;
3d9156a7 84+ va_copy(ap_orig, ap);
59e0d9fe 85 for (;;) {
3d9156a7
A
86 c = *fmt++;
87 if (c == 0)
88 return (nassigned);
89- if (iswspace(c)) {
90- while ((c = __fgetwc(fp)) != WEOF &&
91- iswspace(c))
92+ if (iswspace_l(c, loc)) {
93+ while ((c = __fgetwc(fp, loc)) != WEOF &&
94+ iswspace_l(c, loc))
95 ;
96 if (c != WEOF)
97- __ungetwc(c, fp);
98+ __ungetwc(c, fp, loc);
99 continue;
100 }
101 if (c != '%')
102@@ -180,15 +201,27 @@
103 switch (c) {
104 case '%':
105 literal:
106- if ((wi = __fgetwc(fp)) == WEOF)
107+ if ((wi = __fgetwc(fp, loc)) == WEOF)
108 goto input_failure;
109 if (wi != c) {
110- __ungetwc(wi, fp);
111+ __ungetwc(wi, fp, loc);
112 goto input_failure;
113 }
114 nread++;
115 continue;
116
117+ case '$':
118+ index = width;
119+ if (index < 1 || index > NL_ARGMAX || fmt[-3] != '%') {
120+ goto input_failure;
121+ }
122+ width = 0;
123+ va_end(ap);
124+ va_copy(ap, ap_orig); /* reset to %1$ */
125+ for (; index > 1; index--) {
126+ va_arg(ap, void*);
127+ }
128+ goto again;
129 case '*':
130 flags |= SUPPRESS;
131 goto again;
132@@ -307,7 +340,6 @@
59e0d9fe
A
133 break;
134
135 case 'n':
136- nconversions++;
137 if (flags & SUPPRESS) /* ??? */
138 continue;
139 if (flags & SHORTSHORT)
3d9156a7
A
140@@ -343,11 +375,11 @@
141 * that suppress this.
142 */
143 if ((flags & NOSKIP) == 0) {
144- while ((wi = __fgetwc(fp)) != WEOF && iswspace(wi))
145+ while ((wi = __fgetwc(fp, loc)) != WEOF && iswspace_l(wi, loc))
146 nread++;
147 if (wi == WEOF)
148 goto input_failure;
149- __ungetwc(wi, fp);
150+ __ungetwc(wi, fp, loc);
151 }
152
153 /*
154@@ -364,7 +396,7 @@
155 p = va_arg(ap, wchar_t *);
156 n = 0;
157 while (width-- != 0 &&
158- (wi = __fgetwc(fp)) != WEOF) {
159+ (wi = __fgetwc(fp, loc)) != WEOF) {
160 if (!(flags & SUPPRESS))
161 *p++ = (wchar_t)wi;
162 n++;
163@@ -380,19 +412,19 @@
164 n = 0;
165 mbs = initial;
166 while (width != 0 &&
167- (wi = __fgetwc(fp)) != WEOF) {
168- if (width >= MB_CUR_MAX &&
169+ (wi = __fgetwc(fp, loc)) != WEOF) {
170+ if (width >= mb_cur_max &&
171 !(flags & SUPPRESS)) {
172- nconv = wcrtomb(mbp, wi, &mbs);
173+ nconv = wcrtomb_l(mbp, wi, &mbs, loc);
174 if (nconv == (size_t)-1)
175 goto input_failure;
176 } else {
177- nconv = wcrtomb(mbbuf, wi,
178- &mbs);
179+ nconv = wcrtomb_l(mbbuf, wi,
180+ &mbs, loc);
181 if (nconv == (size_t)-1)
182 goto input_failure;
183 if (nconv > width) {
184- __ungetwc(wi, fp);
185+ __ungetwc(wi, fp, loc);
186 break;
187 }
188 if (!(flags & SUPPRESS))
189@@ -410,7 +442,6 @@
59e0d9fe
A
190 if (!(flags & SUPPRESS))
191 nassigned++;
192 }
193- nconversions++;
194 break;
195
196 case CT_CCL:
3d9156a7
A
197@@ -420,20 +451,20 @@
198 /* take only those things in the class */
199 if ((flags & SUPPRESS) && (flags & LONG)) {
200 n = 0;
201- while ((wi = __fgetwc(fp)) != WEOF &&
202+ while ((wi = __fgetwc(fp, loc)) != WEOF &&
203 width-- != 0 && INCCL(wi))
204 n++;
205 if (wi != WEOF)
206- __ungetwc(wi, fp);
207+ __ungetwc(wi, fp, loc);
208 if (n == 0)
209 goto match_failure;
210 } else if (flags & LONG) {
211 p0 = p = va_arg(ap, wchar_t *);
212- while ((wi = __fgetwc(fp)) != WEOF &&
213+ while ((wi = __fgetwc(fp, loc)) != WEOF &&
214 width-- != 0 && INCCL(wi))
215 *p++ = (wchar_t)wi;
216 if (wi != WEOF)
217- __ungetwc(wi, fp);
218+ __ungetwc(wi, fp, loc);
219 n = p - p0;
220 if (n == 0)
221 goto match_failure;
222@@ -444,16 +475,16 @@
223 mbp = va_arg(ap, char *);
224 n = 0;
225 mbs = initial;
226- while ((wi = __fgetwc(fp)) != WEOF &&
227+ while ((wi = __fgetwc(fp, loc)) != WEOF &&
228 width != 0 && INCCL(wi)) {
229- if (width >= MB_CUR_MAX &&
230+ if (width >= mb_cur_max &&
231 !(flags & SUPPRESS)) {
232- nconv = wcrtomb(mbp, wi, &mbs);
233+ nconv = wcrtomb_l(mbp, wi, &mbs, loc);
234 if (nconv == (size_t)-1)
235 goto input_failure;
236 } else {
237- nconv = wcrtomb(mbbuf, wi,
238- &mbs);
239+ nconv = wcrtomb_l(mbbuf, wi,
240+ &mbs, loc);
241 if (nconv == (size_t)-1)
242 goto input_failure;
243 if (nconv > width)
244@@ -468,14 +499,15 @@
245 n++;
246 }
247 if (wi != WEOF)
248- __ungetwc(wi, fp);
249+ __ungetwc(wi, fp, loc);
250+ if (n == 0)
251+ goto match_failure;
252 if (!(flags & SUPPRESS)) {
253 *mbp = 0;
254 nassigned++;
59e0d9fe
A
255 }
256 }
257 nread += n;
258- nconversions++;
259 break;
260
261 case CT_STRING:
3d9156a7
A
262@@ -483,39 +515,39 @@
263 if (width == 0)
264 width = (size_t)~0;
265 if ((flags & SUPPRESS) && (flags & LONG)) {
266- while ((wi = __fgetwc(fp)) != WEOF &&
267+ while ((wi = __fgetwc(fp, loc)) != WEOF &&
268 width-- != 0 &&
269- !iswspace(wi))
270+ !iswspace_l(wi, loc))
271 nread++;
272 if (wi != WEOF)
273- __ungetwc(wi, fp);
274+ __ungetwc(wi, fp, loc);
275 } else if (flags & LONG) {
276 p0 = p = va_arg(ap, wchar_t *);
277- while ((wi = __fgetwc(fp)) != WEOF &&
278+ while ((wi = __fgetwc(fp, loc)) != WEOF &&
279 width-- != 0 &&
280- !iswspace(wi)) {
281+ !iswspace_l(wi, loc)) {
282 *p++ = (wchar_t)wi;
283 nread++;
284 }
285 if (wi != WEOF)
286- __ungetwc(wi, fp);
287+ __ungetwc(wi, fp, loc);
288 *p = '\0';
289 nassigned++;
290 } else {
291 if (!(flags & SUPPRESS))
292 mbp = va_arg(ap, char *);
293 mbs = initial;
294- while ((wi = __fgetwc(fp)) != WEOF &&
295+ while ((wi = __fgetwc(fp, loc)) != WEOF &&
296 width != 0 &&
297- !iswspace(wi)) {
298- if (width >= MB_CUR_MAX &&
299+ !iswspace_l(wi, loc)) {
300+ if (width >= mb_cur_max &&
301 !(flags & SUPPRESS)) {
302- nconv = wcrtomb(mbp, wi, &mbs);
303+ nconv = wcrtomb_l(mbp, wi, &mbs, loc);
304 if (nconv == (size_t)-1)
305 goto input_failure;
306 } else {
307- nconv = wcrtomb(mbbuf, wi,
308- &mbs);
309+ nconv = wcrtomb_l(mbbuf, wi,
310+ &mbs, loc);
311 if (nconv == (size_t)-1)
312 goto input_failure;
313 if (nconv > width)
314@@ -530,13 +562,12 @@
315 nread++;
316 }
317 if (wi != WEOF)
318- __ungetwc(wi, fp);
319+ __ungetwc(wi, fp, loc);
320 if (!(flags & SUPPRESS)) {
321 *mbp = 0;
59e0d9fe
A
322 nassigned++;
323 }
324 }
325- nconversions++;
326 continue;
327
328 case CT_INT:
3d9156a7
A
329@@ -546,7 +577,7 @@
330 width = sizeof(buf) / sizeof(*buf) - 1;
331 flags |= SIGNOK | NDIGITS | NZDIGITS;
332 for (p = buf; width; width--) {
333- c = __fgetwc(fp);
334+ c = __fgetwc(fp, loc);
335 /*
336 * Switch on the character; `goto ok'
337 * if we accept it as a part of number.
338@@ -630,7 +661,7 @@
339 * for a number. Stop accumulating digits.
340 */
341 if (c != WEOF)
342- __ungetwc(c, fp);
343+ __ungetwc(c, fp, loc);
344 break;
345 ok:
346 /*
347@@ -646,22 +677,22 @@
348 */
349 if (flags & NDIGITS) {
350 if (p > buf)
351- __ungetwc(*--p, fp);
352+ __ungetwc(*--p, fp, loc);
353 goto match_failure;
354 }
355 c = p[-1];
356 if (c == 'x' || c == 'X') {
357 --p;
358- __ungetwc(c, fp);
359+ __ungetwc(c, fp, loc);
360 }
361 if ((flags & SUPPRESS) == 0) {
362 uintmax_t res;
363
364 *p = 0;
365 if ((flags & UNSIGNED) == 0)
366- res = wcstoimax(buf, NULL, base);
367+ res = wcstoimax_l(buf, NULL, base, loc);
368 else
369- res = wcstoumax(buf, NULL, base);
370+ res = wcstoumax_l(buf, NULL, base, loc);
371 if (flags & POINTER)
372 *va_arg(ap, void **) =
373 (void *)(uintptr_t)res;
374@@ -684,47 +715,49 @@
59e0d9fe
A
375 nassigned++;
376 }
377 nread += p - buf;
378- nconversions++;
379 break;
380
3d9156a7 381 #ifndef NO_FLOATING_POINT
59e0d9fe
A
382 case CT_FLOAT:
383+ {
384+ wchar_t *pbuf;
385 /* scan a floating point number as if by strtod */
386- if (width == 0 || width > sizeof(buf) /
387- sizeof(*buf) - 1)
388- width = sizeof(buf) / sizeof(*buf) - 1;
389- if ((width = parsefloat(fp, buf, buf + width)) == 0)
3d9156a7 390+ if ((width = parsefloat(fp, &pbuf, width, loc)) == 0) {
59e0d9fe
A
391+ if (pbuf)
392+ free(pbuf);
393 goto match_failure;
394+ }
395 if ((flags & SUPPRESS) == 0) {
396 if (flags & LONGDBL) {
397- long double res = wcstold(buf, &p);
3d9156a7 398+ long double res = wcstold_l(pbuf, &p, loc);
59e0d9fe
A
399 *va_arg(ap, long double *) = res;
400 } else if (flags & LONG) {
401- double res = wcstod(buf, &p);
3d9156a7 402+ double res = wcstod_l(pbuf, &p, loc);
59e0d9fe
A
403 *va_arg(ap, double *) = res;
404 } else {
405- float res = wcstof(buf, &p);
3d9156a7 406+ float res = wcstof_l(pbuf, &p, loc);
59e0d9fe
A
407 *va_arg(ap, float *) = res;
408 }
409- if (__scanfdebug && p - buf != width)
410+ if (__scanfdebug && p - pbuf != width)
411 abort();
412 nassigned++;
413 }
414 nread += width;
415- nconversions++;
416+ free(pbuf);
417 break;
418+ }
3d9156a7 419 #endif /* !NO_FLOATING_POINT */
59e0d9fe
A
420 }
421 }
422 input_failure:
423- return (nconversions != 0 ? nassigned : EOF);
424+ return (nassigned ? nassigned : EOF);
425 match_failure:
426 return (nassigned);
427 }
428
3d9156a7 429 #ifndef NO_FLOATING_POINT
59e0d9fe
A
430 static int
431-parsefloat(FILE *fp, wchar_t *buf, wchar_t *end)
3d9156a7 432+parsefloat(FILE *fp, wchar_t **buf, size_t width, locale_t loc)
59e0d9fe
A
433 {
434 wchar_t *commit, *p;
435 int infnanpos = 0;
3d9156a7
A
436@@ -733,9 +766,18 @@
437 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
438 } state = S_START;
59e0d9fe 439 wchar_t c;
3d9156a7
A
440- wchar_t decpt = (wchar_t)(unsigned char)*localeconv()->decimal_point;
441+ wchar_t decpt = (wchar_t)(unsigned char)*localeconv_l(loc)->decimal_point;
59e0d9fe
A
442 _Bool gotmantdig = 0, ishex = 0;
443+ wchar_t *b, *e;
444+ size_t s;
445
446+ s = (width == 0 ? BUF : width + 1);
447+ b = (wchar_t *)malloc(s * sizeof(wchar_t));
448+ if (b == NULL) {
449+ *buf = NULL;
450+ return 0;
451+ }
452+ e = b + (s - 1);
453 /*
454 * We set commit = p whenever the string we have read so far
455 * constitutes a valid representation of a floating point
3d9156a7 456@@ -745,10 +787,10 @@
59e0d9fe
A
457 * always necessary to read at least one character that doesn't
458 * match; thus, we can't short-circuit "infinity" or "nan(...)".
459 */
460- commit = buf - 1;
461+ commit = b - 1;
462 c = WEOF;
463- for (p = buf; p < end; ) {
3d9156a7 464- if ((c = __fgetwc(fp)) == WEOF)
59e0d9fe 465+ for (p = b; width == 0 || p < e; ) {
3d9156a7 466+ if ((c = __fgetwc(fp, loc)) == WEOF)
59e0d9fe
A
467 break;
468 reswitch:
3d9156a7
A
469 switch (state) {
470@@ -808,7 +850,7 @@
471 if (c == ')') {
472 commit = p;
473 infnanpos = -2;
474- } else if (!iswalnum(c) && c != '_')
475+ } else if (!iswalnum_l(c, loc) && c != '_')
476 goto parsedone;
477 break;
478 }
479@@ -824,7 +866,7 @@
480 goto reswitch;
481 }
482 case S_DIGITS:
483- if ((ishex && iswxdigit(c)) || iswdigit(c))
484+ if ((ishex && iswxdigit_l(c, loc)) || iswdigit_l(c, loc))
485 gotmantdig = 1;
486 else {
487 state = S_FRAC;
488@@ -841,7 +883,7 @@
489 goto parsedone;
490 else
491 state = S_EXP;
492- } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
493+ } else if ((ishex && iswxdigit_l(c, loc)) || iswdigit_l(c, loc)) {
494 commit = p;
495 gotmantdig = 1;
496 } else
497@@ -854,7 +896,7 @@
498 else
499 goto reswitch;
500 case S_EXPDIGITS:
501- if (iswdigit(c))
502+ if (iswdigit_l(c, loc))
503 commit = p;
504 else
505 goto parsedone;
506@@ -862,16 +904,28 @@
59e0d9fe
A
507 default:
508 abort();
509 }
510+ if (p >= e) {
511+ size_t diff = (p - b);
512+ s += BUF;
513+ b = (wchar_t *)reallocf(b, s * sizeof(wchar_t));
514+ if (b == NULL) {
515+ *buf = NULL;
516+ return 0;
517+ }
518+ e = b + (s - 1);
519+ p = b + diff;
520+ }
521 *p++ = c;
522 c = WEOF;
523 }
3d9156a7
A
524
525 parsedone:
526 if (c != WEOF)
527- __ungetwc(c, fp);
528+ __ungetwc(c, fp, loc);
59e0d9fe 529 while (commit < --p)
3d9156a7
A
530- __ungetwc(*p, fp);
531+ __ungetwc(*p, fp, loc);
59e0d9fe
A
532 *++commit = '\0';
533- return (commit - buf);
534+ *buf = b;
535+ return (commit - b);
536 }
537 #endif