From 5a6560a685ea6d05d71916e65e696e40ca372ab1 Mon Sep 17 00:00:00 2001 From: twisti Date: Tue, 12 Jul 2005 23:44:19 +0000 Subject: [PATCH] * get_variable_message_length: implemented a irix version, irix returns always -1 if the buffer is null --- src/toolbox/util.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/toolbox/util.c b/src/toolbox/util.c index d74f704f0..d251a2130 100644 --- a/src/toolbox/util.c +++ b/src/toolbox/util.c @@ -28,16 +28,18 @@ Changes: - $Id: util.c 2979 2005-07-11 09:59:37Z twisti $ + $Id: util.c 3019 2005-07-12 23:44:19Z twisti $ */ +#include #include #include #include #include "types.h" + #include "mm/memory.h" #include "vm/exceptions.h" #include "vm/stringlocal.h" @@ -90,11 +92,43 @@ char *_Jv_getcwd(void) int get_variable_message_length(const char *fmt, va_list ap) { - int len; + int n; + + n = vsnprintf(NULL, 0, fmt, ap); + +#if defined(__IRIX__) + /* We know that IRIX returns -1 if the buffer is NULL */ + + if (n == -1) { + char *p, *np; + s4 size; + + size = 100; /* start with 100-bytes */ + + p = MNEW(char, size); - len = vsnprintf(NULL, 0, fmt, ap); + while (1) { + /* Try to print in the allocated space. */ + + n = vsnprintf(p, size, fmt, ap); + + /* If that worked, return the length. */ + if (n > -1 && n < size) + return n; + + /* Else try again with more space. */ + size *= 2; /* twice the old size */ + + if ((np = MREALLOC(p, char, size, size)) == NULL) { + assert(0); + } else { + p = np; + } + } + } +#endif - return len; + return n; } -- 2.25.1