3 /* vsnprintf( char *, size_t, const char *, va_list ap )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
14 int vsnprintf( char * s, size_t n, const char * format, _PDCLIB_va_list arg )
16 /* TODO: This function should interpret format as multibyte characters. */
17 /* Members: base, flags, n, i, this, s, width, prec, stream, arg */
18 struct _PDCLIB_status_t status = { 0, 0, n, 0, 0, s, 0, 0, NULL, arg };
19 while ( *format != '\0' )
22 if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
24 /* No conversion specifier, print verbatim */
25 s[ status.i++ ] = *(format++);
29 /* Continue parsing after conversion specifier */
40 #include <_PDCLIB_test.h>
47 static int testprintf( char * s, size_t n, const char * format, ... )
51 va_start( arg, format );
52 i = vsnprintf( s, n, format, arg );
60 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
61 TESTCASE( strcmp( buffer, "-128" ) == 0 );
62 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
63 TESTCASE( strcmp( buffer, "127" ) == 0 );
64 TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
65 TESTCASE( strcmp( buffer, "0" ) == 0 );
66 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
67 TESTCASE( strcmp( buffer, "-32768" ) == 0 );
68 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
69 TESTCASE( strcmp( buffer, "32767" ) == 0 );
70 TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
71 TESTCASE( strcmp( buffer, "0" ) == 0 );
72 TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
73 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
74 TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
75 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
76 TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
77 TESTCASE( strcmp( buffer, "0" ) == 0 );
78 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
79 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
80 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
81 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
82 TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
83 TESTCASE( strcmp( buffer, "0" ) == 0 );
84 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
85 TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
86 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
87 TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
88 TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
89 TESTCASE( strcmp( buffer, "0" ) == 0 );
90 TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
91 TESTCASE( strcmp( buffer, "255" ) == 0 );
92 TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
93 TESTCASE( strcmp( buffer, "255" ) == 0 );
94 TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
95 TESTCASE( strcmp( buffer, "65535" ) == 0 );
96 TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
97 TESTCASE( strcmp( buffer, "65535" ) == 0 );
98 TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
99 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
100 TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
101 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
102 TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
103 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
104 TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
105 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
106 TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
107 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
108 TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
109 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
110 TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
111 TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
112 TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
113 TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
114 TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
115 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
116 TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
117 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
118 TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
119 TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
120 TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
121 TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
122 /* TODO: This test case is broken, doesn't test what it was intended to. */
123 TESTCASE( testprintf( buffer, 100, "%.0#o", 0 ) == 5 );
124 TESTCASE( strcmp( buffer, "%.0#o" ) == 0 );
125 TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
126 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
127 TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
128 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
129 TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
130 TESTCASE( strcmp( buffer, "+0" ) == 0 );
131 TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
132 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
133 TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
134 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
135 TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
136 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
137 TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
138 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
139 TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
140 TESTCASE( strcmp( buffer, " 0" ) == 0 );
141 TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
142 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
143 TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
144 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
145 TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
146 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
147 TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
148 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
149 TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
150 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
151 TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
152 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
153 TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
154 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
155 TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
156 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
157 TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
158 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
159 TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
160 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
161 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
162 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
163 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
164 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
165 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
166 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
167 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
168 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
169 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
170 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
171 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
172 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
173 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
174 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
175 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
176 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
177 TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
178 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
179 TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
180 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
181 TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
182 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
183 TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
184 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
185 TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
186 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
187 TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
188 TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
189 TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
190 TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
191 TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
192 TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
193 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
194 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
195 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
196 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
197 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
198 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
199 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
200 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
201 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
202 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
203 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
204 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
205 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
206 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
207 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
208 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
209 TESTCASE( testprintf( buffer, 8, "%9d", INT_MAX ) == 10 );
210 TESTCASE( strcmp( buffer, "2147483" ) == 0 );
211 TESTCASE( testprintf( buffer, 8, "%9d", INT_MIN ) == 11 );
212 TESTCASE( strcmp( buffer, "-214748" ) == 0 );
213 TESTCASE( testprintf( buffer, 9, "%9d", INT_MAX ) == 10 );
214 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
215 TESTCASE( testprintf( buffer, 9, "%9d", INT_MIN ) == 11 );
216 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
217 TESTCASE( testprintf( buffer, 10, "%9d", INT_MAX ) == 10 );
218 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
219 TESTCASE( testprintf( buffer, 10, "%9d", INT_MIN ) == 11 );
220 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
221 TESTCASE( testprintf( buffer, 9, "%10d", INT_MAX ) == 10 );
222 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
223 TESTCASE( testprintf( buffer, 9, "%10d", INT_MIN ) == 11 );
224 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
225 TESTCASE( testprintf( buffer, 10, "%10d", INT_MAX ) == 10 );
226 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
227 TESTCASE( testprintf( buffer, 10, "%10d", INT_MIN ) == 11 );
228 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
229 TESTCASE( testprintf( buffer, 11, "%10d", INT_MAX ) == 10 );
230 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
231 TESTCASE( testprintf( buffer, 11, "%10d", INT_MIN ) == 11 );
232 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
233 TESTCASE( testprintf( buffer, 10, "%11d", INT_MAX ) == 11 );
234 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
235 TESTCASE( testprintf( buffer, 10, "%11d", INT_MIN ) == 11 );
236 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
237 TESTCASE( testprintf( buffer, 11, "%11d", INT_MAX ) == 11 );
238 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
239 TESTCASE( testprintf( buffer, 11, "%11d", INT_MIN ) == 11 );
240 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
241 TESTCASE( testprintf( buffer, 12, "%11d", INT_MAX ) == 11 );
242 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
243 TESTCASE( testprintf( buffer, 12, "%11d", INT_MIN ) == 11 );
244 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
245 TESTCASE( testprintf( buffer, 11, "%12d", INT_MAX ) == 12 );
246 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
247 TESTCASE( testprintf( buffer, 11, "%12d", INT_MIN ) == 12 );
248 TESTCASE( strcmp( buffer, " -21474836" ) == 0 );
249 TESTCASE( testprintf( buffer, 12, "%12d", INT_MAX ) == 12 );
250 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
251 TESTCASE( testprintf( buffer, 12, "%12d", INT_MIN ) == 12 );
252 TESTCASE( strcmp( buffer, " -214748364" ) == 0 );
253 TESTCASE( testprintf( buffer, 13, "%12d", INT_MAX ) == 12 );
254 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
255 TESTCASE( testprintf( buffer, 13, "%12d", INT_MIN ) == 12 );
256 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
257 TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
258 TESTCASE( strcmp( buffer, " 00000000002147483647" ) == 0 );
259 TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
260 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
261 TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
262 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
263 TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
264 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
265 TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
266 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
267 TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
268 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
269 TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
270 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
271 TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
272 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
273 TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
274 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
275 TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
276 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
277 TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
278 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
279 TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
280 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
281 TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
282 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
283 TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
284 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
285 TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
286 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
287 TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
288 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
289 TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
290 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
291 TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
292 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
293 TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
294 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
295 TESTCASE( testprintf( buffer, 100, "- %d", INT_MAX ) == 12 );
296 TESTCASE( strcmp( buffer, "- 2147483647" ) == 0 );
297 TESTCASE( testprintf( buffer, 100, "- %d %% %d", INT_MAX, INT_MIN ) == 26 );
298 TESTCASE( strcmp( buffer, "- 2147483647 % -2147483648" ) == 0 );
299 TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
300 TESTCASE( strcmp( buffer, "x" ) == 0 );
301 TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
302 TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
303 TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
304 TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
307 TESTCASE( testprintf( buffer, 100, "123456%n789%n", &val1, &val2 ) == 9 );
308 TESTCASE( strcmp( buffer, "123456789" ) == 0 );
309 TESTCASE( val1 == 6 );
310 TESTCASE( val2 == 9 );