1 --- vfwprintf.c.orig 2010-07-15 10:03:36.000000000 -0700
2 +++ vfwprintf.c 2010-07-15 10:49:02.000000000 -0700
3 @@ -38,6 +38,8 @@ static char sccsid[] = "@(#)vfprintf.c 8
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.42 2009/11/25 04:27:55 wollman Exp $");
7 +#include "xlocale_private.h"
10 * Actual wprintf innards.
12 @@ -59,6 +61,7 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/v
17 #include "un-namespace.h"
19 #include "libc_private.h"
20 @@ -66,10 +69,11 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/v
22 #include "printflocal.h"
24 -static int __sprint(FILE *, struct __suio *);
25 -static int __sbprintf(FILE *, const wchar_t *, va_list) __noinline;
26 -static wint_t __xfputwc(wchar_t, FILE *);
27 -static wchar_t *__mbsconv(char *, int);
28 +static int __sprint(FILE *, locale_t, struct __suio *);
29 +static int __sbprintf(FILE *, locale_t, const wchar_t *, va_list);
30 +static wint_t __xfputwc(wchar_t, FILE *, locale_t);
31 +static wchar_t *__mbsconv(char *, int, locale_t);
32 +__private_extern__ const char *__fix_nogrouping(const char *);
35 #include "printfcommon.h"
36 @@ -85,29 +89,29 @@ struct grouping_state {
37 static const mbstate_t initial_mbs;
41 +get_decpt(locale_t loc)
48 - nconv = mbrtowc(&decpt, localeconv()->decimal_point, MB_CUR_MAX, &mbs);
49 + nconv = mbrtowc_l(&decpt, localeconv_l(loc)->decimal_point, MB_CUR_MAX_L(loc), &mbs, loc);
50 if (nconv == (size_t)-1 || nconv == (size_t)-2)
51 decpt = '.'; /* failsafe */
57 +get_thousep(locale_t loc)
64 - nconv = mbrtowc(&thousep, localeconv()->thousands_sep,
66 + nconv = mbrtowc_l(&thousep, localeconv_l(loc)->thousands_sep,
67 + MB_CUR_MAX_L(loc), &mbs, loc);
68 if (nconv == (size_t)-1 || nconv == (size_t)-2)
69 thousep = '\0'; /* failsafe */
71 @@ -119,11 +123,11 @@ get_thousep(void)
72 * of wide characters that will be printed.
75 -grouping_init(struct grouping_state *gs, int ndigits)
76 +grouping_init(struct grouping_state *gs, int ndigits, locale_t loc)
79 - gs->grouping = localeconv()->grouping;
80 - gs->thousands_sep = get_thousep();
81 + gs->grouping = __fix_nogrouping(localeconv_l(loc)->grouping);
82 + gs->thousands_sep = get_thousep(loc);
84 gs->nseps = gs->nrepeats = 0;
86 @@ -145,11 +149,11 @@ grouping_init(struct grouping_state *gs,
89 grouping_print(struct grouping_state *gs, struct io_state *iop,
90 - const CHAR *cp, const CHAR *ep)
91 + const CHAR *cp, const CHAR *ep, locale_t loc)
95 - if (io_printandpad(iop, cp, ep, gs->lead, zeroes))
96 + if (io_printandpad(iop, cp, ep, gs->lead, zeroes, loc))
99 while (gs->nseps > 0 || gs->nrepeats > 0) {
100 @@ -159,9 +163,9 @@ grouping_print(struct grouping_state *gs
104 - if (io_print(iop, &gs->thousands_sep, 1))
105 + if (io_print(iop, &gs->thousands_sep, 1, loc))
107 - if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes))
108 + if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, loc))
112 @@ -180,7 +184,7 @@ grouping_print(struct grouping_state *gs
113 * string eclipses the benefits of buffering.
116 -__sprint(FILE *fp, struct __suio *uio)
117 +__sprint(FILE *fp, locale_t loc, struct __suio *uio)
121 @@ -191,7 +195,7 @@ __sprint(FILE *fp, struct __suio *uio)
122 p = (wchar_t *)iov->iov_base;
124 for (i = 0; i < len; i++) {
125 - if (__xfputwc(p[i], fp) == WEOF)
126 + if (__xfputwc(p[i], fp, loc) == WEOF)
130 @@ -205,11 +209,14 @@ __sprint(FILE *fp, struct __suio *uio)
131 * worries about ungetc buffers and so forth.
134 -__sbprintf(FILE *fp, const wchar_t *fmt, va_list ap)
135 +__sbprintf(FILE *fp, locale_t loc, const wchar_t *fmt, va_list ap)
139 unsigned char buf[BUFSIZ];
140 + struct __sFILEX ext;
141 + fake._extra = &ext;
144 /* XXX This is probably not needed. */
145 if (prepwrite(fp) != 0)
146 @@ -229,7 +236,7 @@ __sbprintf(FILE *fp, const wchar_t *fmt,
147 fake._lbfsize = 0; /* not actually used, but Just In Case */
149 /* do the work, then copy any error status */
150 - ret = __vfwprintf(&fake, fmt, ap);
151 + ret = __vfwprintf(&fake, loc, fmt, ap);
152 if (ret >= 0 && __fflush(&fake))
154 if (fake._flags & __SERR)
155 @@ -242,7 +249,7 @@ __sbprintf(FILE *fp, const wchar_t *fmt,
156 * File must already be locked.
159 -__xfputwc(wchar_t wc, FILE *fp)
160 +__xfputwc(wchar_t wc, FILE *fp, locale_t loc)
163 char buf[MB_LEN_MAX];
164 @@ -251,10 +258,10 @@ __xfputwc(wchar_t wc, FILE *fp)
167 if ((fp->_flags & __SSTR) == 0)
168 - return (__fputwc(wc, fp));
169 + return (__fputwc(wc, fp, loc));
172 - if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
173 + if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
174 fp->_flags |= __SERR;
177 @@ -273,12 +280,13 @@ __xfputwc(wchar_t wc, FILE *fp)
178 * that the multibyte char. string ends in a null character.
181 -__mbsconv(char *mbsarg, int prec)
182 +__mbsconv(char *mbsarg, int prec, locale_t loc)
185 wchar_t *convbuf, *wcp;
187 - size_t insize, nchars, nconv;
188 + size_t insize, nchars, nconv = 0;
189 + int mb_cur_max = MB_CUR_MAX_L(loc);
193 @@ -296,7 +304,7 @@ __mbsconv(char *mbsarg, int prec)
194 insize = nchars = nconv = 0;
196 while (nchars != (size_t)prec) {
197 - nconv = mbrlen(p, MB_CUR_MAX, &mbs);
198 + nconv = mbrlen_l(p, mb_cur_max, &mbs, loc);
199 if (nconv == 0 || nconv == (size_t)-1 ||
202 @@ -323,7 +331,7 @@ __mbsconv(char *mbsarg, int prec)
205 while (insize != 0) {
206 - nconv = mbrtowc(wcp, p, insize, &mbs);
207 + nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
208 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
211 @@ -343,22 +351,28 @@ __mbsconv(char *mbsarg, int prec)
215 -vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
217 +vfwprintf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt0, va_list ap)
221 + NORMALIZE_LOCALE(loc);
223 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
224 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
226 - ret = __sbprintf(fp, fmt0, ap);
227 + ret = __sbprintf(fp, loc, fmt0, ap);
229 - ret = __vfwprintf(fp, fmt0, ap);
230 + ret = __vfwprintf(fp, loc, fmt0, ap);
236 +vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
238 + return vfwprintf_l(fp, __current_locale(), fmt0, ap);
242 * The size of the buffer we use as scratch space for integer
243 * conversions, among other things. We need enough space to
244 @@ -373,8 +387,8 @@ vfwprintf(FILE * __restrict fp, const wc
246 * Non-MT-safe version
249 -__vfwprintf(FILE *fp, const wchar_t *fmt0, va_list ap)
250 +__private_extern__ int
251 +__vfwprintf(FILE *fp, locale_t loc, const wchar_t *fmt0, va_list ap)
253 wchar_t *fmt; /* format string */
254 wchar_t ch; /* character from fmt */
255 @@ -415,6 +429,11 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
256 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
257 char *dtoaresult; /* buffer allocated by dtoa */
260 + union arg vval; /* Vector argument. */
261 + wchar_t *pct; /* Pointer to '%' at beginning of specifier. */
262 + wchar_t vsep; /* Vector separator character. */
264 u_long ulval; /* integer arguments %[diouxX] */
265 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
266 int base; /* base for [diouxX] conversion */
267 @@ -437,19 +456,19 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
269 /* BEWARE, these `goto error' on error. */
270 #define PRINT(ptr, len) do { \
271 - if (io_print(&io, (ptr), (len))) \
272 + if (io_print(&io, (ptr), (len), loc)) \
275 #define PAD(howmany, with) { \
276 - if (io_pad(&io, (howmany), (with))) \
277 + if (io_pad(&io, (howmany), (with), loc)) \
280 #define PRINTANDPAD(p, ep, len, with) { \
281 - if (io_printandpad(&io, (p), (ep), (len), (with))) \
282 + if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
286 - if (io_flush(&io)) \
287 + if (io_flush(&io, loc)) \
291 @@ -485,7 +504,7 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
293 (flags&INTMAXT ? GETARG(uintmax_t) : \
294 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
295 - flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
296 + flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
297 (uintmax_t)GETARG(unsigned long long))
300 @@ -518,8 +537,11 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
303 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
304 - if (prepwrite(fp) != 0)
305 + if (prepwrite(fp) != 0) {
312 fmt = (wchar_t *)fmt0;
313 @@ -529,7 +551,7 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
316 #ifndef NO_FLOATING_POINT
317 - decimal_point = get_decpt();
318 + decimal_point = get_decpt(loc);
322 @@ -548,6 +570,9 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
328 +#endif /* VECTORS */
329 fmt++; /* skip over '%' */
332 @@ -557,6 +582,9 @@ __vfwprintf(FILE *fp, const wchar_t *fmt
337 + vsep = 'X'; /* Illegal value, changed to defaults later. */
338 +#endif /* VECTORS */
341 reswitch: switch (ch) {
342 @@ -572,6 +600,11 @@ reswitch: switch (ch) {
347 + case ',': case ';': case ':': case '_':
350 +#endif /* VECTORS */
353 * ``A negative field width argument is taken as a
354 @@ -668,10 +701,14 @@ reswitch: switch (ch) {
359 + if (flags & VECTOR)
361 +#endif /* VECTORS */
363 *(cp = buf) = (wchar_t)GETARG(wint_t);
365 - *(cp = buf) = (wchar_t)btowc(GETARG(int));
366 + *(cp = buf) = (wchar_t)btowc_l(GETARG(int), loc);
370 @@ -680,6 +717,10 @@ reswitch: switch (ch) {
375 + if (flags & VECTOR)
377 +#endif /* VECTORS */
378 if (flags & INTMAX_SIZE) {
380 if ((intmax_t)ujval < 0) {
381 @@ -698,6 +739,12 @@ reswitch: switch (ch) {
382 #ifndef NO_FLOATING_POINT
386 + if (flags & VECTOR) {
390 +#endif /* VECTORS */
394 @@ -709,6 +756,12 @@ reswitch: switch (ch) {
399 + fparg.dbl = GETARG(double);
401 + __hdtoa(fparg.dbl, xdigs, prec,
402 + &expt, &signflag, &dtoaend);
403 +#else /* !LDBL_COMPAT */
404 if (flags & LONGDBL) {
405 fparg.ldbl = GETARG(long double);
407 @@ -720,6 +773,7 @@ reswitch: switch (ch) {
408 __hdtoa(fparg.dbl, xdigs, prec,
409 &expt, &signflag, &dtoaend);
411 +#endif /* LDBL_COMPAT */
413 prec = dtoaend - dtoaresult;
415 @@ -727,11 +781,17 @@ reswitch: switch (ch) {
418 ndig = dtoaend - dtoaresult;
419 - cp = convbuf = __mbsconv(dtoaresult, -1);
420 + cp = convbuf = __mbsconv(dtoaresult, -1, loc);
421 freedtoa(dtoaresult);
426 + if (flags & VECTOR) {
430 +#endif /* VECTORS */
432 if (prec < 0) /* account for digit before decpt */
434 @@ -740,10 +800,22 @@ reswitch: switch (ch) {
439 + if (flags & VECTOR) {
443 +#endif /* VECTORS */
449 + if (flags & VECTOR) {
453 +#endif /* VECTORS */
454 expchar = ch - ('g' - 'e');
457 @@ -752,6 +824,14 @@ fp_begin:
462 + fparg.dbl = GETARG(double);
464 + dtoa(fparg.dbl, expchar ? 2 : 3, prec,
465 + &expt, &signflag, &dtoaend);
468 +#else /* !LDBL_COMPAT */
469 if (flags & LONGDBL) {
470 fparg.ldbl = GETARG(long double);
472 @@ -765,8 +845,9 @@ fp_begin:
476 +#endif /* LDBL_COMPAT */
477 ndig = dtoaend - dtoaresult;
478 - cp = convbuf = __mbsconv(dtoaresult, -1);
479 + cp = convbuf = __mbsconv(dtoaresult, -1, loc);
480 freedtoa(dtoaresult);
483 @@ -816,37 +897,46 @@ fp_common:
484 if (prec || flags & ALT)
486 if ((flags & GROUPING) && expt > 0)
487 - size += grouping_init(&gs, expt);
488 + size += grouping_init(&gs, expt, loc);
491 #endif /* !NO_FLOATING_POINT */
495 * Assignment-like behavior is specified if the
496 * value overflows or is otherwise unrepresentable.
497 * C99 says to use `signed char' for %hhn conversions.
499 - if (flags & LLONGINT)
500 - *GETARG(long long *) = ret;
501 + void *ptr = GETARG(void *);
504 + else if (flags & LLONGINT)
505 + *(long long *)ptr = ret;
506 else if (flags & SIZET)
507 - *GETARG(ssize_t *) = (ssize_t)ret;
508 + *(ssize_t *)ptr = (ssize_t)ret;
509 else if (flags & PTRDIFFT)
510 - *GETARG(ptrdiff_t *) = ret;
511 + *(ptrdiff_t *)ptr = ret;
512 else if (flags & INTMAXT)
513 - *GETARG(intmax_t *) = ret;
514 + *(intmax_t *)ptr = ret;
515 else if (flags & LONGINT)
516 - *GETARG(long *) = ret;
517 + *(long *)ptr = ret;
518 else if (flags & SHORTINT)
519 - *GETARG(short *) = ret;
520 + *(short *)ptr = ret;
521 else if (flags & CHARINT)
522 - *GETARG(signed char *) = ret;
523 + *(signed char *)ptr = ret;
525 - *GETARG(int *) = ret;
527 continue; /* no output */
534 + if (flags & VECTOR)
536 +#endif /* VECTORS */
537 if (flags & INTMAX_SIZE)
540 @@ -861,6 +951,10 @@ fp_common:
545 + if (flags & VECTOR)
547 +#endif /* VECTORS */
548 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
551 @@ -882,7 +976,7 @@ fp_common:
552 if ((mbp = GETARG(char *)) == NULL)
555 - convbuf = __mbsconv(mbp, prec);
556 + convbuf = __mbsconv(mbp, prec, loc);
557 if (convbuf == NULL) {
558 fp->_flags |= __SERR;
560 @@ -890,13 +984,23 @@ fp_common:
564 +#if 0 // wcsnlen needs API review first
565 size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp);
568 + if(prec >= 0 && prec < size)
578 + if (flags & VECTOR)
580 +#endif /* VECTORS */
581 if (flags & INTMAX_SIZE)
584 @@ -909,6 +1013,10 @@ fp_common:
589 + if (flags & VECTOR)
591 +#endif /* VECTORS */
592 if (flags & INTMAX_SIZE)
595 @@ -926,6 +1034,7 @@ nosign: sign = '\0';
596 * ``... diouXx conversions ... if a precision is
597 * specified, the 0 flag will be ignored.''
599 + * except for %#.0o and zero value
601 number: if ((dprec = prec) >= 0)
603 @@ -953,10 +1062,15 @@ number: if ((dprec = prec) >= 0)
605 size = buf + BUF - cp;
606 if (size > BUF) /* should never happen */
608 + LIBC_ABORT("size (%d) > BUF (%d)", size, BUF);
609 if ((flags & GROUPING) && size != 0)
610 - size += grouping_init(&gs, size);
611 + size += grouping_init(&gs, size, loc);
617 +#endif /* VECTORS */
618 default: /* "%?" prints ?, unless ? is NUL */
621 @@ -968,6 +1082,288 @@ number: if ((dprec = prec) >= 0)
626 + if (flags & VECTOR) {
628 + * Do the minimum amount of work necessary to construct
629 + * a format specifier that can be used to recursively
630 + * call vfprintf() for each element in the vector.
632 + int i, j; /* Counter. */
633 + int vcnt; /* Number of elements in vector. */
634 + char *vfmt; /* Pointer to format specifier. */
636 + char vfmt_buf[32 + EXTRAHH]; /* Static buffer for format spec. */
637 + int vwidth = 0; /* Width specified via '*'. */
638 + int vprec = 0; /* Precision specified via '*'. */
639 + char *vstr; /* Used for asprintf(). */
640 + int vlen; /* Length returned by asprintf(). */
642 + V_CHAR, V_SHORT, V_INT,
643 + V_PCHAR, V_PSHORT, V_PINT,
646 + V_LONGLONG, V_PLONGLONG,
648 +#endif /* V64TYPE */
651 + vval.vectorarg = GETARG(VECTORTYPE);
653 + * Set vfmt. If vfmt_buf may not be big enough,
654 + * malloc() space, taking care to free it later.
655 + * (EXTRAHH is for possible extra "hh")
657 + if (&fmt[-1] - pct + EXTRAHH < sizeof(vfmt_buf))
660 + vfmt = (char *)malloc(&fmt[-1] - pct + EXTRAHH + 1);
662 + /* Set the separator character, if not specified. */
670 + /* Create the format specifier. */
671 + for (i = j = 0; i < &fmt[-1] - pct; i++) {
673 + case ',': case ';': case ':': case '_':
674 + case 'v': case 'h': case 'l':
678 + if (pct[i - 1] != '.')
684 + vfmt[j++] = pct[i];
689 + * Determine the number of elements in the vector and
690 + * finish up the format specifier.
692 + if (flags & SHORTINT) {
706 + } else if (flags & LONGINT) {
708 + vtype = (ch == 'p') ? V_PINT : V_INT;
710 + } else if (flags & LLONGINT) {
732 + vtype = (ch == 'p') ? V_PLONGLONG : V_LONGLONG;
736 + * The default case should never
743 +#endif /* V64TYPE */
758 + * The default case should never
773 + vtype = (ch == 'p') ? V_PCHAR : V_CHAR;
779 +/* Get a vector element. */
781 +#define VPRINT(type, ind, args...) do { \
784 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
787 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
790 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
793 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
796 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
799 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
802 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
804 + case V_PLONGLONG: \
805 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
808 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
811 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
815 + PRINT(vstr, vlen); \
818 +#else /* !V64TYPE */
819 +#define VPRINT(type, ind, args...) do { \
822 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
825 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
828 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
831 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
834 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
837 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
840 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
844 + PRINT(vstr, vlen); \
847 +#endif /* V64TYPE */
849 + /* Actually print. */
852 + /* First element. */
854 + for (i = 1; i < vcnt; i++) {
863 + /* First element. */
864 + VPRINT(vtype, 0, prec);
865 + for (i = 1; i < vcnt; i++) {
871 + VPRINT(vtype, i, prec);
876 + /* First element. */
877 + VPRINT(vtype, 0, width);
878 + for (i = 1; i < vcnt; i++) {
884 + VPRINT(vtype, i, width);
887 + /* First element. */
888 + VPRINT(vtype, 0, width, prec);
889 + for (i = 1; i < vcnt; i++) {
895 + VPRINT(vtype, i, width, prec);
901 + if (vfmt != vfmt_buf)
906 +#endif /* VECTORS */
908 * All reasonable formats wind up here. At this point, `cp'
909 * points to a string which (if not flags&LADJUST) should be
910 @@ -1018,7 +1414,7 @@ number: if ((dprec = prec) >= 0)
911 /* leading zeroes from decimal precision */
912 PAD(dprec - size, zeroes);
914 - if (grouping_print(&gs, &io, cp, buf+BUF) < 0)
915 + if (grouping_print(&gs, &io, cp, buf+BUF, loc) < 0)
919 @@ -1036,7 +1432,7 @@ number: if ((dprec = prec) >= 0)
922 n = grouping_print(&gs, &io,
923 - cp, convbuf + ndig);
924 + cp, convbuf + ndig, loc);