4adb970f5f00d0c0ffa97d1a37f8d8a6a13eedb0
[cacao.git] / src / toolbox / util.c
1 /* src/toolbox/util.c - contains some utility functions
2
3    Copyright (C) 1996-2005, 2006, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <unistd.h>
32
33 #include "vm/types.h"
34
35 #include "mm/memory.hpp"
36 #include "vm/vm.hpp"
37
38
39 /* _Jv_getcwd ******************************************************************
40
41    Return the current working directory.
42
43    RETURN VALUE:
44        pointer to a char array allocated by MNEW, or
45            NULL if memory could not be allocated.
46
47 *******************************************************************************/
48
49 char *_Jv_getcwd(void)
50 {
51         char *buf;
52         s4    size;
53
54         size = 1024;
55
56         buf = MNEW(char, size);
57
58         while (buf) {
59                 if (getcwd(buf, size) != NULL)
60                         return buf;
61
62                 MFREE(buf, char, size);
63
64                 /* too small buffer or a more serious problem */
65
66                 if (errno != ERANGE)
67                         vm_abort("getcwd failed: %s", strerror(errno));
68
69                 /* double the buffer size */
70
71                 size *= 2;
72
73                 buf = MNEW(char, size);
74         }
75
76         return NULL;
77 }
78
79
80 /* get_variable_message_length *************************************************
81
82    This function simluates the print of a variable message and
83    determines so the message length;
84
85 *******************************************************************************/
86
87 int get_variable_message_length(const char *fmt, va_list ap)
88 {
89         int n;
90
91         n = vsnprintf(NULL, 0, fmt, ap);
92
93 #if defined(__IRIX__)
94         /* We know that IRIX returns -1 if the buffer is NULL */
95
96         if (n == -1) {
97                 char *p, *np;
98                 s4    size;
99
100                 size = 100;                     /* start with 100-bytes               */
101
102                 p = MNEW(char, size);
103
104                 while (1) {
105                         /* Try to print in the allocated space. */
106
107                         n = vsnprintf(p, size, fmt, ap);
108
109                         /* If that worked, return the length. */
110                         if (n > -1 && n < size)
111                                 return n;
112
113                         /* Else try again with more space. */
114                         size *= 2;  /* twice the old size */
115
116                         if ((np = MREALLOC(p, char, size, size)) == NULL) {
117                                 assert(0);
118                         } else {
119                                 p = np;
120                         }
121                 }
122         }
123 #endif
124
125         return n;
126 }
127
128
129 /*
130  * These are local overrides for various environment variables in Emacs.
131  * Please do not remove this and leave it at the end of the file, where
132  * Emacs will automagically detect them.
133  * ---------------------------------------------------------------------
134  * Local variables:
135  * mode: c
136  * indent-tabs-mode: t
137  * c-basic-offset: 4
138  * tab-width: 4
139  * End:
140  */