e7123d156c5887eab2f5e81c64a08051da95d48b
[cacao.git] / src / toolbox / util.c
1 /* src/toolbox/util.c - contains some utility functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: util.c 3216 2005-09-19 13:07:54Z twisti $
32
33 */
34
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40
41 #include "vm/types.h"
42
43 #include "mm/memory.h"
44 #include "vm/exceptions.h"
45 #include "vm/stringlocal.h"
46
47
48 /* getcwd **********************************************************************
49
50    XXX
51
52 *******************************************************************************/
53
54 char *_Jv_getcwd(void)
55 {
56         char *buf;
57         s4    size;
58
59         size = 1024;
60
61         buf = MNEW(char, size);
62
63         while (buf) {
64                 if (getcwd(buf, size) != NULL)
65                         return buf;
66
67                 MFREE(buf, char, size);
68
69                 /* too small buffer or a more serious problem */
70
71                 if (errno != ERANGE)
72                         throw_cacao_exception_exit(string_java_lang_InternalError,
73                                                                            strerror(errno));
74
75                 /* double the buffer size */
76
77                 size *= 2;
78
79                 buf = MNEW(char, size);
80         }
81
82         return NULL;
83 }
84
85
86 /* get_variable_message_length *************************************************
87
88    This function simluates the print of a variable message and
89    determines so the message length;
90
91 *******************************************************************************/
92
93 int get_variable_message_length(const char *fmt, va_list ap)
94 {
95         int n;
96
97         n = vsnprintf(NULL, 0, fmt, ap);
98
99 #if defined(__IRIX__)
100         /* We know that IRIX returns -1 if the buffer is NULL */
101
102         if (n == -1) {
103                 char *p, *np;
104                 s4    size;
105
106                 size = 100;                     /* start with 100-bytes               */
107
108                 p = MNEW(char, size);
109
110                 while (1) {
111                         /* Try to print in the allocated space. */
112
113                         n = vsnprintf(p, size, fmt, ap);
114
115                         /* If that worked, return the length. */
116                         if (n > -1 && n < size)
117                                 return n;
118
119                         /* Else try again with more space. */
120                         size *= 2;  /* twice the old size */
121
122                         if ((np = MREALLOC(p, char, size, size)) == NULL) {
123                                 assert(0);
124                         } else {
125                                 p = np;
126                         }
127                 }
128         }
129 #endif
130
131         return n;
132 }
133
134
135 /*
136  * These are local overrides for various environment variables in Emacs.
137  * Please do not remove this and leave it at the end of the file, where
138  * Emacs will automagically detect them.
139  * ---------------------------------------------------------------------
140  * Local variables:
141  * mode: c
142  * indent-tabs-mode: t
143  * c-basic-offset: 4
144  * tab-width: 4
145  * End:
146  */