Place __stdcall between return value and function name
[git/dscho.git] / compat / snprintf.c
blob47b2b8a55ec05a6f9d626278c67c31cab1b740e3
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 expects the
6 * number of characters to write without the trailing NUL.
7 */
8 #ifndef SNPRINTF_SIZE_CORR
9 #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ < 4 || defined(_MSC_VER)
10 #define SNPRINTF_SIZE_CORR 1
11 #else
12 #define SNPRINTF_SIZE_CORR 0
13 #endif
14 #endif
16 #undef vsnprintf
18 #if defined(_MSC_VER)
19 #define vsnprintf _vsnprintf
20 #endif
22 int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
24 char *s;
25 int ret = -1;
27 if (maxsize > 0) {
28 ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
29 if (ret == maxsize-1)
30 ret = -1;
31 /* Windows does not NUL-terminate if result fills buffer */
32 str[maxsize-1] = 0;
34 if (ret != -1)
35 return ret;
37 s = NULL;
38 if (maxsize < 128)
39 maxsize = 128;
41 while (ret == -1) {
42 maxsize *= 4;
43 str = realloc(s, maxsize);
44 if (! str)
45 break;
46 s = str;
47 ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
48 if (ret == maxsize-1)
49 ret = -1;
51 free(s);
52 return ret;
55 int git_snprintf(char *str, size_t maxsize, const char *format, ...)
57 va_list ap;
58 int ret;
60 va_start(ap, format);
61 ret = git_vsnprintf(str, maxsize, format, ap);
62 va_end(ap);
64 return ret;