]>
Commit | Line | Data |
---|---|---|
3d9156a7 A |
1 | --- vswprintf.c.orig 2004-11-25 11:38:36.000000000 -0800 |
2 | +++ vswprintf.c 2005-02-24 15:20:20.000000000 -0800 | |
3 | @@ -33,6 +33,8 @@ | |
4 | #endif | |
5 | __FBSDID("$FreeBSD: src/lib/libc/stdio/vswprintf.c,v 1.5 2004/04/07 09:55:05 tjr Exp $"); | |
6 | ||
7 | +#include "xlocale_private.h" | |
8 | + | |
9 | #include <errno.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | @@ -49,7 +51,66 @@ | |
13 | struct __sFILEX ext; | |
59e0d9fe A |
14 | char *mbp; |
15 | int ret, sverrno; | |
16 | + size_t conv; | |
3d9156a7 A |
17 | + locale_t loc = __current_locale(); |
18 | + | |
19 | + if (n == 0) { | |
20 | + errno = EINVAL; | |
21 | + return (-1); | |
22 | + } | |
23 | + | |
24 | + f._file = -1; | |
25 | + f._flags = __SWR | __SSTR | __SALC; | |
26 | + f._bf._base = f._p = (unsigned char *)malloc(128); | |
27 | + if (f._bf._base == NULL) { | |
28 | + errno = ENOMEM; | |
29 | + return (-1); | |
30 | + } | |
31 | + f._bf._size = f._w = 127; /* Leave room for the NUL */ | |
32 | + f._extra = &ext; | |
33 | + INITEXTRA(&f); | |
34 | + ret = __vfwprintf(&f, loc, fmt, ap); | |
35 | + if (ret < 0) { | |
36 | + sverrno = errno; | |
37 | + free(f._bf._base); | |
38 | + errno = sverrno; | |
39 | + return (-1); | |
40 | + } | |
41 | + *f._p = '\0'; | |
42 | + mbp = (char *)f._bf._base; | |
43 | + /* | |
44 | + * XXX Undo the conversion from wide characters to multibyte that | |
45 | + * fputwc() did in __vfwprintf(). | |
46 | + */ | |
47 | + mbs = initial; | |
48 | + if ((conv = mbsrtowcs_l(s, (const char **)&mbp, n, &mbs, loc)) == (size_t)-1) { | |
49 | + free(f._bf._base); | |
50 | + errno = EILSEQ; | |
51 | + return (-1); | |
52 | + } | |
53 | + free(f._bf._base); | |
54 | + if (conv >= n) { | |
55 | + s[n - 1] = L'\0'; | |
56 | + errno = EOVERFLOW; | |
57 | + return (-1); | |
58 | + } | |
59 | + | |
60 | + return (ret); | |
61 | +} | |
62 | + | |
63 | +int | |
64 | +vswprintf_l(wchar_t * __restrict s, size_t n, locale_t loc, | |
65 | + const wchar_t * __restrict fmt, __va_list ap) | |
66 | +{ | |
67 | + static const mbstate_t initial; | |
68 | + mbstate_t mbs; | |
69 | + FILE f; | |
70 | + struct __sFILEX ext; | |
71 | + char *mbp; | |
72 | + int ret, sverrno; | |
73 | + size_t conv; | |
59e0d9fe | 74 | |
3d9156a7 | 75 | + NORMALIZE_LOCALE(loc); |
59e0d9fe A |
76 | if (n == 0) { |
77 | errno = EINVAL; | |
3d9156a7 A |
78 | return (-1); |
79 | @@ -65,7 +126,7 @@ | |
80 | f._bf._size = f._w = 127; /* Leave room for the NUL */ | |
81 | f._extra = &ext; | |
82 | INITEXTRA(&f); | |
83 | - ret = __vfwprintf(&f, fmt, ap); | |
84 | + ret = __vfwprintf(&f, loc, fmt, ap); | |
85 | if (ret < 0) { | |
86 | sverrno = errno; | |
87 | free(f._bf._base); | |
88 | @@ -73,19 +134,19 @@ | |
89 | return (-1); | |
90 | } | |
91 | *f._p = '\0'; | |
92 | - mbp = f._bf._base; | |
93 | + mbp = (char *)f._bf._base; | |
94 | /* | |
59e0d9fe A |
95 | * XXX Undo the conversion from wide characters to multibyte that |
96 | * fputwc() did in __vfwprintf(). | |
97 | */ | |
3d9156a7 | 98 | mbs = initial; |
59e0d9fe | 99 | - if (mbsrtowcs(s, (const char **)&mbp, n, &mbs) == (size_t)-1) { |
3d9156a7 | 100 | + if ((conv = mbsrtowcs_l(s, (const char **)&mbp, n, &mbs, loc)) == (size_t)-1) { |
59e0d9fe A |
101 | free(f._bf._base); |
102 | errno = EILSEQ; | |
103 | return (-1); | |
104 | } | |
105 | free(f._bf._base); | |
106 | - if (s[n - 1] != L'\0') { | |
107 | + if (conv >= n) { | |
108 | s[n - 1] = L'\0'; | |
109 | errno = EOVERFLOW; | |
110 | return (-1); |