X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Ftoolbox%2Futil.c;h=4adb970f5f00d0c0ffa97d1a37f8d8a6a13eedb0;hb=b307ed446ce8893052cd4774fcb2298cf4d7a4db;hp=18dc97711d771766a0db0dc78f214008968ec52b;hpb=b1dfff49f1988d04767d9de747ff1addc1023c43;p=cacao.git diff --git a/src/toolbox/util.c b/src/toolbox/util.c index 18dc97711..4adb970f5 100644 --- a/src/toolbox/util.c +++ b/src/toolbox/util.c @@ -1,9 +1,7 @@ /* 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, 2008 + CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO This file is part of CACAO. @@ -19,33 +17,32 @@ 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. - - Contact: cacao@complang.tuwien.ac.at - - Authors: Christian Thalinger - - Changes: - - $Id: util.c 2463 2005-05-12 23:54:07Z twisti $ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ +#include "config.h" + +#include #include #include #include -#include "types.h" -#include "mm/memory.h" -#include "vm/exceptions.h" -#include "vm/stringlocal.h" +#include "vm/types.h" +#include "mm/memory.hpp" +#include "vm/vm.hpp" -/* getcwd ********************************************************************** - XXX +/* _Jv_getcwd ****************************************************************** + + Return the current working directory. + + RETURN VALUE: + pointer to a char array allocated by MNEW, or + NULL if memory could not be allocated. *******************************************************************************/ @@ -67,8 +64,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 */ @@ -88,16 +84,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 */ - va_start(ap, fmt); - len = vsnprintf(NULL, 0, fmt, ap); - va_end(ap); + if (n == -1) { + char *p, *np; + s4 size; + + 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; }