Mercurial > dovecot > original-hg > dovecot-1.2
changeset 7850:7f9f7fc02359 HEAD
Support systems with non-C99 vsnprintf().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 13 Jun 2008 00:28:18 +0300 |
parents | 9a00bd31ca99 |
children | 560611fa3d68 |
files | configure.in src/lib/compat.c src/lib/compat.h |
diffstat | 3 files changed, 46 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/configure.in Fri Jun 13 00:27:24 2008 +0300 +++ b/configure.in Fri Jun 13 00:28:18 2008 +0300 @@ -1373,11 +1373,10 @@ return f("hello %s%d", "world", 1); }]])], [i_cv_c99_vsnprintf=yes], - [i_cv_c99_vsnprintf=no], - []) + [i_cv_c99_vsnprintf=no]) ]) if test $i_cv_c99_vsnprintf = no; then - AC_ERROR([You don't appear to have C99 compatible vsnprintf() call]) + AC_DEFINE(HAVE_OLD_VSNPRINTF,, Define if you don't have C99 compatible vsnprintf() call) fi dnl ***
--- a/src/lib/compat.c Fri Jun 13 00:27:24 2008 +0300 +++ b/src/lib/compat.c Fri Jun 13 00:28:18 2008 +0300 @@ -238,3 +238,41 @@ #endif } #endif + +#ifdef HAVE_OLD_VSNPRINTF +#undef vsnprintf +int my_vsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + size_t tmp_size; + char *tmp; + int ret; + + /* On overflow HP-UX returns -1, IRIX and Tru64 return size-1. */ + ret = vsnprintf(str, size, format, ap); + if (ret >= 0 && (size_t)ret+1 != size) + return ret; + + /* see if data stack has enough available space for it */ + tmp_size = t_get_bytes_available(); + if (tmp_size > size) { + tmp = t_buffer_get(tmp_size); + ret = vsnprintf(tmp, tmp_size, format, ap); + if (ret >= 0 && (size_t)ret+1 != size) + return ret; + } else { + tmp_size = size; + } + + /* try to allocate enough memory to get it to fit. */ + do { + tmp_size = nearest_power(tmp_size+1); + tmp = i_malloc(tmp_size); + ret = vsnprintf(tmp, tmp_size, format, ap); + i_free(tmp); + if (ret >= 0 && (size_t)ret+1 != size) + return ret; + } while (tmp_size < 1024*1024); + + i_panic("my_vsnprintf(): Output string too big"); +} +#endif
--- a/src/lib/compat.h Fri Jun 13 00:27:24 2008 +0300 +++ b/src/lib/compat.h Fri Jun 13 00:28:18 2008 +0300 @@ -193,6 +193,12 @@ unsigned long long int my_strtoll(const char *nptr, char **endptr, int base); #endif +#ifdef HAVE_OLD_VSNPRINTF +# include <stdio.h> +# define vsnprintf my_vsnprintf +int my_vsnprintf(char *str, size_t size, const char *format, va_list ap); +#endif + /* ctype.h isn't safe with signed chars, use our own instead if really needed */ #define i_toupper(x) ((char) toupper((int) (unsigned char) (x)))