comparison util.c @ 1265:a344885e7c79

Fix info_message/warning/error functions to deal with limitations of vsnprintf on Windows
author Michael Pavone <pavone@retrodev.com>
date Sat, 04 Mar 2017 19:02:53 -0800
parents 4490c9c12272
children 5905593d6828
comparison
equal deleted inserted replaced
1264:555f2ac537e7 1265:a344885e7c79
190 { 190 {
191 va_list args; 191 va_list args;
192 va_start(args, format); 192 va_start(args, format);
193 if (!headless) { 193 if (!headless) {
194 //take a guess at the final size 194 //take a guess at the final size
195 size_t size = strlen(format) * 2; 195 int32_t size = strlen(format) * 2;
196 char *buf = malloc(size); 196 char *buf = malloc(size);
197 size_t actual = vsnprintf(buf, size, format, args); 197 int32_t actual = vsnprintf(buf, size, format, args);
198 if (actual >= size) { 198 if (actual >= size || actual < 0) {
199 actual++; 199 if (actual < 0) {
200 //seems on windows, vsnprintf is returning -1 when the buffer is too small
201 //since we don't know the proper size, a generous multiplier will hopefully suffice
202 actual = size * 4;
203 } else {
204 actual++;
205 }
200 free(buf); 206 free(buf);
201 buf = malloc(actual); 207 buf = malloc(actual);
202 va_end(args); 208 va_end(args);
203 va_start(args, format); 209 va_start(args, format);
204 vsnprintf(buf, actual, format, args); 210 vsnprintf(buf, actual, format, args);
220 #ifndef _WIN32 226 #ifndef _WIN32
221 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) { 227 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) {
222 warning_printf(format, args); 228 warning_printf(format, args);
223 } else { 229 } else {
224 #endif 230 #endif
225 size_t size = strlen(format) * 2; 231 int32_t size = strlen(format) * 2;
226 char *buf = malloc(size); 232 char *buf = malloc(size);
227 size_t actual = vsnprintf(buf, size, format, args); 233 int32_t actual = vsnprintf(buf, size, format, args);
228 if (actual >= size) { 234 if (actual >= size || actual < 0) {
229 actual++; 235 if (actual < 0) {
236 //seems on windows, vsnprintf is returning -1 when the buffer is too small
237 //since we don't know the proper size, a generous multiplier will hopefully suffice
238 actual = size * 4;
239 } else {
240 actual++;
241 }
230 free(buf); 242 free(buf);
231 buf = malloc(actual); 243 buf = malloc(actual);
232 va_end(args); 244 va_end(args);
233 va_start(args, format); 245 va_start(args, format);
234 vsnprintf(buf, actual, format, args); 246 vsnprintf(buf, actual, format, args);
249 #ifndef _WIN32 261 #ifndef _WIN32
250 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) { 262 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) {
251 info_printf(format, args); 263 info_printf(format, args);
252 } else { 264 } else {
253 #endif 265 #endif
254 size_t size = strlen(format) * 2; 266 int32_t size = strlen(format) * 2;
255 char *buf = malloc(size); 267 char *buf = malloc(size);
256 size_t actual = vsnprintf(buf, size, format, args); 268 int32_t actual = vsnprintf(buf, size, format, args);
257 if (actual >= size) { 269 if (actual >= size || actual < 0) {
258 actual++; 270 if (actual < 0) {
271 //seems on windows, vsnprintf is returning -1 when the buffer is too small
272 //since we don't know the proper size, a generous multiplier will hopefully suffice
273 actual = size * 4;
274 } else {
275 actual++;
276 }
259 free(buf); 277 free(buf);
260 buf = malloc(actual); 278 buf = malloc(actual);
261 va_end(args); 279 va_end(args);
262 va_start(args, format); 280 va_start(args, format);
263 vsnprintf(buf, actual, format, args); 281 vsnprintf(buf, actual, format, args);