submodule: Use cat instead of echo to avoid DOS line-endings
[git/dscho.git] / compat / snprintf.c
blobe1e0e7543d9414726122c121b7909bf73809a81a
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 char *s;
23 int ret = -1;
25 if (maxsize > 0) {
26 ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
27 if (ret == maxsize-1)
28 ret = -1;
29 /* Windows does not NUL-terminate if result fills buffer */
30 str[maxsize-1] = 0;
32 if (ret != -1)
33 return ret;
35 s = NULL;
36 if (maxsize < 128)
37 maxsize = 128;
39 while (ret == -1) {
40 maxsize *= 4;
41 str = realloc(s, maxsize);
42 if (! str)
43 break;
44 s = str;
45 ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
46 if (ret == maxsize-1)
47 ret = -1;
49 free(s);
50 return ret;
53 int git_snprintf(char *str, size_t maxsize, const char *format, ...)
55 va_list ap;
56 int ret;
58 va_start(ap, format);
59 ret = git_vsnprintf(str, maxsize, format, ap);
60 va_end(ap);
62 return ret;