dovecot-1.1: Support systems with non-C99 vsnprintf().

dovecot at dovecot.org dovecot at dovecot.org
Fri Jun 13 00:36:29 EEST 2008


details:   http://hg.dovecot.org/dovecot-1.1/rev/2adf8302551e
changeset: 7650:2adf8302551e
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Jun 13 00:28:18 2008 +0300
description:
Support systems with non-C99 vsnprintf().

diffstat:

3 files changed, 46 insertions(+), 3 deletions(-)
configure.in     |    5 ++---
src/lib/compat.c |   38 ++++++++++++++++++++++++++++++++++++++
src/lib/compat.h |    6 ++++++

diffs (78 lines):

diff -r 2e712b753ae3 -r 2adf8302551e configure.in
--- 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 @@ AC_CACHE_CHECK([for C99 vsnprintf()],i_c
     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 ***
diff -r 2e712b753ae3 -r 2adf8302551e src/lib/compat.c
--- 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 @@ unsigned long long int my_strtoll(const 
 #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
diff -r 2e712b753ae3 -r 2adf8302551e src/lib/compat.h
--- 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
@@ -191,6 +191,12 @@ unsigned long long int my_strtoull(const
 #ifndef HAVE_STRTOLL
 #  define strtoll my_strtoll
 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,


More information about the dovecot-cvs mailing list