* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / native / vm / VMSystemProperties.c
1 /* src/native/vm/VMSystemProperties.c - gnu/classpath/VMSystemProperties
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: VMSystemProperties.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/utsname.h>
41 #include <time.h>
42
43 #include "vm/types.h"
44
45 #include "mm/memory.h"
46 #include "native/jni.h"
47 #include "native/native.h"
48 #include "native/include/java_util_Properties.h"
49 #include "toolbox/logging.h"
50 #include "toolbox/util.h"
51 #include "vm/exceptions.h"
52 #include "vm/builtin.h"
53 #include "vm/loader.h"
54 #include "vm/options.h"
55 #include "vm/properties.h"
56 #include "vm/suck.h"
57
58
59 /*
60  * Class:     gnu/classpath/VMSystemProperties
61  * Method:    preInit
62  * Signature: (Ljava/util/Properties;)V
63  */
64 JNIEXPORT void JNICALL Java_gnu_classpath_VMSystemProperties_preInit(JNIEnv *env, jclass clazz, java_util_Properties *p)
65 {
66         char       *cwd;
67         char       *java_home;
68         char       *user;
69         char       *home;
70         char       *locale;
71         char       *lang;
72         char       *country;
73         struct utsname utsnamebuf;
74
75 #if !defined(ENABLE_STATICVM)
76         char *ld_library_path;
77         char *libpath;
78         s4    libpathlen;
79 #endif
80
81         if (!p) {
82                 exceptions_throw_nullpointerexception();
83                 return;
84         }
85
86         /* get properties from system */
87
88         cwd = _Jv_getcwd();
89         java_home = getenv("JAVA_HOME");
90         user = getenv("USER");
91         home = getenv("HOME");
92         uname(&utsnamebuf);
93
94         /* post-initialize the properties */
95
96         if (!properties_postinit(p))
97                 return;
98
99         properties_system_add("java.version", JAVA_VERSION);
100         properties_system_add("java.vendor", "GNU Classpath");
101         properties_system_add("java.vendor.url", "http://www.gnu.org/software/classpath/");
102         properties_system_add("java.home", java_home ? java_home : CACAO_PREFIX);
103         properties_system_add("java.vm.specification.version", "1.0");
104         properties_system_add("java.vm.specification.vendor", "Sun Microsystems Inc.");
105         properties_system_add("java.vm.specification.name", "Java Virtual Machine Specification");
106         properties_system_add("java.vm.version", VERSION);
107         properties_system_add("java.vm.vendor", "CACAO Team");
108         properties_system_add("java.vm.name", "CACAO");
109         properties_system_add("java.specification.version", "1.4");
110         properties_system_add("java.specification.vendor", "Sun Microsystems Inc.");
111         properties_system_add("java.specification.name", "Java Platform API Specification");
112         properties_system_add("java.class.version", CLASS_VERSION);
113         properties_system_add("java.class.path", classpath);
114
115         properties_system_add("java.runtime.version", VERSION);
116         properties_system_add("java.runtime.name", "CACAO");
117
118         /* Set bootclasspath properties. One for GNU classpath and the
119            other for compatibility with Sun (required by most
120            applications). */
121
122         properties_system_add("java.boot.class.path", bootclasspath);
123         properties_system_add("sun.boot.class.path", bootclasspath);
124
125 #if defined(ENABLE_STATICVM)
126         properties_system_add("gnu.classpath.boot.library.path", ".");
127         properties_system_add("java.library.path" , ".");
128 #else /* defined(ENABLE_STATICVM) */
129         /* fill gnu.classpath.boot.library.path with GNU classpath library
130        path */
131
132         libpathlen = strlen(CLASSPATH_LIBRARY_PATH) + strlen("0");
133
134         libpath = MNEW(char, libpathlen);
135
136         strcat(libpath, CLASSPATH_LIBRARY_PATH);
137
138         properties_system_add("gnu.classpath.boot.library.path", libpath);
139
140         MFREE(libpath, char, libpathlen);
141
142
143         /* now fill java.library.path */
144
145         ld_library_path = getenv("LD_LIBRARY_PATH");
146
147         if (ld_library_path) {
148                 libpathlen = strlen(".:") + strlen(ld_library_path) + strlen("0");
149
150                 libpath = MNEW(char, libpathlen);
151
152                 strcpy(libpath, ".:");
153                 strcat(libpath, ld_library_path);
154
155                 properties_system_add("java.library.path", libpath);
156
157                 MFREE(libpath, char, libpathlen);
158
159         } else {
160                 properties_system_add("java.library.path", "");
161         }
162 #endif /* defined(ENABLE_STATICVM) */
163
164         properties_system_add("java.io.tmpdir", "/tmp");
165
166 #if defined(ENABLE_INTRP)
167         if (opt_intrp) {
168                 /* XXX We don't support java.lang.Compiler */
169 /*              properties_system_add("java.compiler", "cacao.intrp"); */
170                 properties_system_add("java.vm.info", "interpreted mode");
171         } else
172 #endif
173         {
174                 /* XXX We don't support java.lang.Compiler */
175 /*              properties_system_add("java.compiler", "cacao.jit"); */
176                 properties_system_add("java.vm.info", "JIT mode");
177         }
178
179         properties_system_add("java.ext.dirs", "");
180
181 #if defined(DISABLE_GC)
182         /* When we disable the GC, we mmap the whole heap to a specific
183            address, so we can compare call traces. For this reason we have
184            to add the same properties on different machines, otherwise
185            more memory may be allocated (e.g. strlen("i386")
186            vs. strlen("alpha"). */
187
188         properties_system_add("os.arch", "unknown");
189         properties_system_add("os.name", "unknown");
190         properties_system_add("os.version", "unknown");
191 #else
192         /* We need to set the os.arch hardcoded to be compatible with SUN. */
193
194 #if defined(__I386__)
195         /* map all x86 architectures (i386, i486, i686) to i386 */
196
197         properties_system_add("os.arch", "i386");
198 #elif defined(__POWERPC__)
199         properties_system_add("os.arch", "ppc");
200 #elif defined(__X86_64__)
201         properties_system_add("os.arch", "amd64");
202 #else
203         /* default to what uname returns */
204
205         properties_system_add("os.arch", utsnamebuf.machine);
206 #endif
207
208         properties_system_add("os.name", utsnamebuf.sysname);
209         properties_system_add("os.version", utsnamebuf.release);
210 #endif
211
212         properties_system_add("file.separator", "/");
213         properties_system_add("path.separator", ":");
214         properties_system_add("line.separator", "\n");
215         properties_system_add("user.name", user ? user : "null");
216         properties_system_add("user.home", home ? home : "null");
217         properties_system_add("user.dir", cwd ? cwd : "null");
218
219         /* Are we little or big endian? */
220
221 #if defined(ENABLE_STATICVM)
222         /* This is just for debugging purposes and can cause troubles in
223        GNU Classpath. */
224
225         properties_system_add("gnu.cpu.endian", "unknown");
226 #else
227 # if WORDS_BIGENDIAN == 1
228         properties_system_add("gnu.cpu.endian", "big");
229 # else
230         properties_system_add("gnu.cpu.endian", "little");
231 # endif
232 #endif
233
234         /* get locale */
235
236         locale = getenv("LANG");
237
238         if (locale != NULL) {
239                 /* get the local stuff from the environment */
240
241                 if (strlen(locale) <= 2) {
242                         properties_system_add("user.language", locale);
243
244                 } else {
245                         if ((locale[2] == '_') && (strlen(locale) >= 5)) {
246                                 lang = MNEW(char, 3);
247                                 strncpy(lang, (char *) &locale[0], 2);
248                                 lang[2] = '\0';
249
250                                 country = MNEW(char, 3);
251                                 strncpy(country, (char *) &locale[3], 2);
252                                 country[2] = '\0';
253
254                                 properties_system_add("user.language", lang);
255                                 properties_system_add("user.country", country);
256                         }
257                 }
258
259         } else {
260                 /* if no default locale was specified, use `en_US' */
261
262                 properties_system_add("user.language", "en");
263                 properties_system_add("user.country", "US");
264         }
265         
266
267         /* Add remaining properties defined on commandline to the Java
268            system properties. */
269
270         properties_system_add_all();
271
272         /* clean up */
273
274         MFREE(cwd, char, 0);
275 }
276
277
278 /*
279  * These are local overrides for various environment variables in Emacs.
280  * Please do not remove this and leave it at the end of the file, where
281  * Emacs will automagically detect them.
282  * ---------------------------------------------------------------------
283  * Local variables:
284  * mode: c
285  * indent-tabs-mode: t
286  * c-basic-offset: 4
287  * tab-width: 4
288  * End:
289  */