X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Ftoolbox%2Futil.c;h=548823e1e687726f71f42ab22613ced6c46f0d20;hb=51a8425f74379b089e1e3bdf752a1a16c65a3a09;hp=18dc97711d771766a0db0dc78f214008968ec52b;hpb=b1dfff49f1988d04767d9de747ff1addc1023c43;p=cacao.git diff --git a/src/toolbox/util.c b/src/toolbox/util.c index 18dc97711..548823e1e 100644 --- a/src/toolbox/util.c +++ b/src/toolbox/util.c @@ -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. @@ -19,33 +19,42 @@ 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 2463 2005-05-12 23:54:07Z twisti $ + $Id: util.c 5252 2006-08-18 13:07:21Z twisti $ */ +#include "config.h" + +#include #include #include #include -#include "types.h" +#include "vm/types.h" + #include "mm/memory.h" #include "vm/exceptions.h" #include "vm/stringlocal.h" +#include "vm/vm.h" -/* getcwd ********************************************************************** +/* _Jv_getcwd ****************************************************************** - XXX + 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 +76,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 +96,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; + + size = 100; /* start with 100-bytes */ - va_start(ap, fmt); - len = vsnprintf(NULL, 0, fmt, ap); - va_end(ap); + 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; }