3 /* vfprintf( FILE *, const char *, va_list )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
15 int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, va_list arg )
17 /* TODO: This function should interpret format as multibyte characters. */
18 struct _PDCLIB_status_t status;
27 status.stream = stream;
28 va_copy( status.arg, arg );
30 while ( *format != '\0' )
33 if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
35 /* No conversion specifier, print verbatim */
36 putc( *(format++), stream );
41 /* Continue parsing after conversion specifier */
55 #include <_PDCLIB_test.h>
57 static int testprintf( FILE * stream, size_t n, const char * format, ... )
61 va_start( arg, format );
62 i = vfprintf( stream, format, arg );
70 TESTCASE( ( buffer = fopen( "testfile", "w" ) ) != NULL );
71 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
72 //TESTCASE( strcmp( buffer, "-128" ) == 0 );
73 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
74 //TESTCASE( strcmp( buffer, "127" ) == 0 );
75 TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
76 //TESTCASE( strcmp( buffer, "0" ) == 0 );
77 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
78 //TESTCASE( strcmp( buffer, "-32768" ) == 0 );
79 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
80 //TESTCASE( strcmp( buffer, "32767" ) == 0 );
81 TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
82 //TESTCASE( strcmp( buffer, "0" ) == 0 );
83 TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
84 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
85 TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
86 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
87 TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
88 //TESTCASE( strcmp( buffer, "0" ) == 0 );
89 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
90 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
91 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
92 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
93 TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
94 //TESTCASE( strcmp( buffer, "0" ) == 0 );
95 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
96 //TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
97 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
98 //TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
99 TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
100 //TESTCASE( strcmp( buffer, "0" ) == 0 );
101 TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
102 //TESTCASE( strcmp( buffer, "255" ) == 0 );
103 TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
104 //TESTCASE( strcmp( buffer, "255" ) == 0 );
105 TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
106 //TESTCASE( strcmp( buffer, "65535" ) == 0 );
107 TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
108 //TESTCASE( strcmp( buffer, "65535" ) == 0 );
109 TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
110 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
111 TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
112 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
113 TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
114 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
115 TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
116 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
117 TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
118 //TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
119 TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
120 //TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
121 TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
122 //TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
123 TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
124 //TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
125 TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
126 //TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
127 TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
128 //TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
129 TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
130 //TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
131 TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
132 //TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
133 /* TODO: This test case is broken, doesn't test what it was intended to. */
134 TESTCASE( testprintf( buffer, 100, "%.0#o", 0 ) == 5 );
135 //TESTCASE( strcmp( buffer, "%.0#o" ) == 0 );
136 TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
137 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
138 TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
139 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
140 TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
141 //TESTCASE( strcmp( buffer, "+0" ) == 0 );
142 TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
143 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
144 TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
145 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
146 TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
147 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
148 TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
149 //TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
150 TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
151 //TESTCASE( strcmp( buffer, " 0" ) == 0 );
152 TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
153 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
154 TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
155 //TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
156 TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
157 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
158 TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
159 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
160 TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
161 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
162 TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
163 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
164 TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
165 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
166 TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
167 //TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
168 TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
169 //TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
170 TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
171 //TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
172 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
173 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
174 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
175 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
176 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
177 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
178 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
179 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
180 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
181 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
182 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
183 //TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
184 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
185 //TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
186 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
187 //TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
188 TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
189 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
190 TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
191 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
192 TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
193 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
194 TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
195 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
196 TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
197 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
198 TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
199 //TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
200 TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
201 //TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
202 TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
203 //TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
204 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
205 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
206 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
207 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
208 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
209 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
210 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
211 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
212 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
213 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
214 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
215 //TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
216 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
217 //TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
218 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
219 //TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
220 TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
221 //TESTCASE( strcmp( buffer, " 00000000002147483647" ) == 0 );
222 TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
223 //TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
224 TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
225 //TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
226 TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
227 //TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
228 TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
229 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
230 TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
231 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
232 TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
233 //TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
234 TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
235 //TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
236 TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
237 //TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
238 TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
239 //TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
240 TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
241 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
242 TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
243 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
244 TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
245 //TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
246 TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
247 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
248 TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
249 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
250 TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
251 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
252 TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
253 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
254 TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
255 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
256 TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
257 //TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
258 TESTCASE( testprintf( buffer, 100, "- %d", INT_MAX ) == 12 );
259 //TESTCASE( strcmp( buffer, "- 2147483647" ) == 0 );
260 TESTCASE( testprintf( buffer, 100, "- %d %% %d", INT_MAX, INT_MIN ) == 26 );
261 //TESTCASE( strcmp( buffer, "- 2147483647 % -2147483648" ) == 0 );
262 TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
263 //TESTCASE( strcmp( buffer, "x" ) == 0 );
264 TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
265 //TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
266 TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
267 //TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
270 TESTCASE( testprintf( buffer, 100, "123456%n789%n", &val1, &val2 ) == 9 );
271 //TESTCASE( strcmp( buffer, "123456789" ) == 0 );
272 TESTCASE( val1 == 6 );
273 TESTCASE( val2 == 9 );
275 TESTCASE( fclose( buffer ) == 0 );
276 char readbuffer[1000];
277 TESTCASE( ( buffer = fopen( "testfile", "r" ) ) != NULL );
278 TESTCASE( fread( readbuffer, 1, 1000, buffer ) == 985 );
279 TESTCASE( fclose( buffer ) == 0 );
280 TESTCASE( remove( "testfile" ) == 0 );
281 TESTCASE( memcmp( readbuffer, "-1281270-32768327670-214748364821474836470-214748364821474836470-922337203685477580892233720368547758070255255655356553542949672954294967295429496729542949672951844674407370955161518446744073709551615FFFFFFFF0XFFFFFFFFffffffff0xffffffff37777777777037777777777%.0#o-2147483648+2147483647+042949672954294967295-2147483648 2147483647 042949672954294967295-21474836482147483647-21474836482147483647-2147483648 2147483647 -2147483648 2147483647-21474836482147483647-21474836482147483647-21474836482147483647 -2147483648 2147483647 -21474836482147483647-21474836482147483647-214748364802147483647-02147483648002147483647-21474836482147483647-21474836482147483647-21474836482147483647 -2147483648 2147483647 00000000002147483647ffffffff0xffffffff0xffffffff-2147483648-2147483648-21474836480xffffffff0xffffffff0xffffffff214748364721474836472147483647+2147483647+2147483647+2147483647+2147483647+2147483647+2147483647- 2147483647- 2147483647 % -2147483648xabcdef0xdeadbeef123456789", 985 ) == 0 );