for-each-ref: always check stat_tracking_info()'s return value
[git/mingw.git] / compat / snprintf.c
blob42ea1ac110813bbd16e77cfbc36f16e6a5e9ddb2
1 #include "../git-compat-util.h"
3 /*
4 * The size parameter specifies the available space, i.e. includes
5 * the trailing NUL byte; but Windows's vsnprintf uses the entire
6 * buffer and avoids the trailing NUL, should the buffer be exactly
7 * big enough for the result. Defining SNPRINTF_SIZE_CORR to 1 will
8 * therefore remove 1 byte from the reported buffer size, so we
9 * always have room for a trailing NUL byte.
11 #ifndef SNPRINTF_SIZE_CORR
12 #if defined(WIN32) && (!defined(__GNUC__) || __GNUC__ < 4)
13 #define SNPRINTF_SIZE_CORR 1
14 #else
15 #define SNPRINTF_SIZE_CORR 0
16 #endif
17 #endif
19 #undef vsnprintf
20 int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
22 va_list cp;
23 char *s;
24 int ret = -1;
26 if (maxsize > 0) {
27 va_copy(cp, ap);
28 ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, cp);
29 va_end(cp);
30 if (ret == maxsize-1)
31 ret = -1;
32 /* Windows does not NUL-terminate if result fills buffer */
33 str[maxsize-1] = 0;
35 if (ret != -1)
36 return ret;
38 s = NULL;
39 if (maxsize < 128)
40 maxsize = 128;
42 while (ret == -1) {
43 maxsize *= 4;
44 str = realloc(s, maxsize);
45 if (! str)
46 break;
47 s = str;
48 va_copy(cp, ap);
49 ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, cp);
50 va_end(cp);
51 if (ret == maxsize-1)
52 ret = -1;
54 free(s);
55 return ret;
58 int git_snprintf(char *str, size_t maxsize, const char *format, ...)
60 va_list ap;
61 int ret;
63 va_start(ap, format);
64 ret = git_vsnprintf(str, maxsize, format, ap);
65 va_end(ap);
67 return ret;