* Removed all Id tags.
[cacao.git] / src / toolbox / util.c
index fec3b2cfd666532c9d068cb916339ebd44f04c33..82acc8ff7f517543a2999cb0fd9af244295f7f15 100644 (file)
@@ -1,9 +1,9 @@
 /* src/toolbox/util.c - contains some utility functions
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Christian Thalinger
 
    Changes:
 
-   $Id: util.c 2194 2005-04-03 16:13:27Z twisti $
-
 */
 
 
+#include "config.h"
+
+#include <assert.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <unistd.h>
 
-#include "types.h"
+#include "vm/types.h"
+
 #include "mm/memory.h"
-#include "vm/exceptions.h"
+#include "vm/vm.h"
+
 
+/* _Jv_getcwd ******************************************************************
 
-/* getcwd **********************************************************************
+   Return the current working directory.
 
-   XXX
+   RETURN VALUE:
+       pointer to a char array allocated by MNEW, or
+          NULL if memory could not be allocated.
 
 *******************************************************************************/
 
@@ -66,8 +72,7 @@ char *_Jv_getcwd(void)
                /* too small buffer or a more serious problem */
 
                if (errno != ERANGE)
-                       throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                          strerror(errno));
+                       vm_abort("getcwd failed: %s", strerror(errno));
 
                /* double the buffer size */
 
@@ -87,16 +92,45 @@ char *_Jv_getcwd(void)
 
 *******************************************************************************/
 
-int get_variable_message_length(const char *fmt, ...)
+int get_variable_message_length(const char *fmt, va_list ap)
 {
-       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;
 
-       va_start(ap, fmt);
-       len = vsnprintf(NULL, 0, fmt, ap);
-       va_end(ap);
+               size = 100;                     /* start with 100-bytes               */
+
+               p = MNEW(char, size);
+
+               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;
 }