* get_variable_message_length: implemented a irix version, irix returns
authortwisti <none@none>
Tue, 12 Jul 2005 23:44:19 +0000 (23:44 +0000)
committertwisti <none@none>
Tue, 12 Jul 2005 23:44:19 +0000 (23:44 +0000)
  always -1 if the buffer is null

src/toolbox/util.c

index d74f704f0d3daa22b19c8e8eb4ed1abc59879c99..d251a2130ebc878e1844d0259e423f8a9b0f480f 100644 (file)
 
    Changes:
 
-   $Id: util.c 2979 2005-07-11 09:59:37Z twisti $
+   $Id: util.c 3019 2005-07-12 23:44:19Z twisti $
 
 */
 
 
+#include <assert.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <unistd.h>
 
 #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;
 }