1 // natSystemProperties.cc - Implementation of native side of
2 // SystemProperties class.
4 /* Copyright (C) 2005, 2006 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
23 #include <sys/utsname.h>
30 #ifdef HAVE_LANGINFO_H
36 #include <java-props.h>
37 #include <gnu/classpath/SystemProperties.h>
38 #include <java/lang/String.h>
41 char *_Jv_Module_Load_Path
= NULL
;
47 _Jv_SetDLLSearchPath (const char *path
)
49 _Jv_Module_Load_Path
= strdup (path
);
55 _Jv_SetDLLSearchPath (const char *)
62 #if ! defined (DEFAULT_FILE_ENCODING) && defined (HAVE_ICONV) \
63 && defined (HAVE_NL_LANGINFO)
68 setlocale (LC_CTYPE
, "");
69 const char *e
= nl_langinfo (CODESET
);
70 if (e
== NULL
|| *e
== '\0')
75 #define DEFAULT_FILE_ENCODING file_encoding ()
79 #ifndef DEFAULT_FILE_ENCODING
80 #define DEFAULT_FILE_ENCODING "8859_1"
83 static const char *default_file_encoding
= DEFAULT_FILE_ENCODING
;
85 #if defined(HAVE_GETPWUID_R) && defined(_POSIX_PTHREAD_SEMANTICS)
86 /* Use overload resolution to find out the signature of getpwuid_r. */
88 /* This is Posix getpwuid_r. */
89 template <typename T_uid
, typename T_passwd
, typename T_buf
, typename T_len
>
91 getpwuid_adaptor(int (*getpwuid_r
)(T_uid user_id
, T_passwd
*pwd_r
,
92 T_buf
*buf_r
, T_len len_r
,
93 T_passwd
**pwd_entry_ptr
),
94 uid_t user_id
, struct passwd
*pwd_r
,
95 char *buf_r
, size_t len_r
, struct passwd
**pwd_entry
)
97 return getpwuid_r (user_id
, pwd_r
, buf_r
, len_r
, pwd_entry
);
100 /* This is used on HPUX 10.20 */
101 template <typename T_uid
, typename T_passwd
, typename T_buf
, typename T_len
>
103 getpwuid_adaptor(int (*getpwuid_r
)(T_uid user_id
, T_passwd
*pwd_r
,
104 T_buf
*buf_r
, T_len len_r
),
105 uid_t user_id
, struct passwd
*pwd_r
,
106 char *buf_r
, size_t len_r
, struct passwd
**pwd_entry
)
108 return getpwuid_r (user_id
, pwd_r
, buf_r
, len_r
);
111 /* This is used on IRIX 5.2. */
112 template <typename T_uid
, typename T_passwd
, typename T_buf
, typename T_len
>
114 getpwuid_adaptor(T_passwd
* (*getpwuid_r
)(T_uid user_id
, T_passwd
*pwd_r
,
115 T_buf
*buf_r
, T_len len_r
),
116 uid_t user_id
, struct passwd
*pwd_r
,
117 char *buf_r
, size_t len_r
, struct passwd
**pwd_entry
)
119 *pwd_entry
= getpwuid_r (user_id
, pwd_r
, buf_r
, len_r
);
120 return (*pwd_entry
== NULL
) ? errno
: 0;
124 // Prepend GCJ_VERSIONED_LIBDIR to a module search path stored in a
125 // Java string, if the path is not already prefixed by
126 // GCJ_VERSIONED_LIBDIR. Return a newly JvMalloc'd char buffer. The
127 // result should be freed using JvFree. See
128 // _Jv_PrependVersionedLibdir in prims.cc.
130 PrependVersionedLibdir (::java::lang::String
* libpath
)
134 // Extract a C char array from libpath.
135 char* val
= (char*) _Jv_Malloc (JvGetStringUTFLength (libpath
) + 1);
136 jsize total
= JvGetStringUTFRegion (libpath
, 0, libpath
->length(), val
);
138 retval
= _Jv_PrependVersionedLibdir (val
);
145 gnu::classpath::SystemProperties::insertSystemProperties (::java::util::Properties
*newprops
)
147 // A convenience define.
148 #define SET(Prop,Val) \
149 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
151 // A mixture of the Java Product Versioning Specification
152 // (introduced in 1.2), and earlier versioning properties. Some
153 // programs rely on seeing values that they expect, so we claim to
154 // be a 1.4-ish VM for their sake.
155 SET ("java.version", JV_VERSION
);
156 SET ("java.runtime.version", JV_VERSION
);
157 SET ("java.vendor", "Free Software Foundation, Inc.");
158 SET ("java.vendor.url", "http://gcc.gnu.org/java/");
159 SET ("java.class.version", "49.0");
160 SET ("java.vm.specification.version", "1.0");
161 SET ("java.vm.specification.name", "Java(tm) Virtual Machine Specification");
162 SET ("java.vm.specification.vendor", "Sun Microsystems Inc.");
163 SET ("java.vm.version", __VERSION__
);
164 SET ("java.vm.vendor", "Free Software Foundation, Inc.");
165 SET ("java.vm.name", "GNU libgcj");
166 SET ("java.specification.version", JV_API_VERSION
);
167 SET ("java.specification.name", "Java(tm) Platform API Specification");
168 SET ("java.specification.vendor", "Sun Microsystems Inc.");
171 #define NAME "GNU libgcj "
172 strcpy (value
, NAME
);
173 strncpy (value
+ sizeof (NAME
) - 1, __VERSION__
,
174 sizeof(value
) - sizeof(NAME
));
175 value
[sizeof (value
) - 1] = '\0';
176 jstring version
= JvNewStringLatin1 (value
);
177 newprops
->put (JvNewStringLatin1 ("java.fullversion"), version
);
178 newprops
->put (JvNewStringLatin1 ("java.vm.info"), version
);
180 // This definition is rather arbitrary: we choose $(prefix). In
181 // part we do this because most people specify only --prefix and
182 // nothing else when installing gcj. Plus, people are free to
183 // redefine `java.home' with `-D' if necessary.
184 SET ("java.home", JAVA_HOME
);
185 SET ("gnu.classpath.home", PREFIX
);
186 // This is set to $(toolexeclibdir) because we use this to find
187 // .security files at runtime.
188 char val2
[sizeof ("file://") + sizeof (TOOLEXECLIBDIR
) + 1];
189 strcpy (val2
, "file://");
190 strcat (val2
, TOOLEXECLIBDIR
);
191 SET ("gnu.classpath.home.url", val2
);
193 SET ("file.encoding", default_file_encoding
);
199 SET ("os.name", u
.sysname
);
200 SET ("os.version", u
.release
);
202 // Normalize x86 architecture names to "i386" (except on Windows, which
203 // is handled in win32.cc).
204 if (u
.machine
[0] == 'i'
206 && u
.machine
[2] == '8'
207 && u
.machine
[3] == '6'
208 && u
.machine
[4] == 0)
209 SET ("os.arch", "i386");
211 SET ("os.arch", u
.machine
);
215 SET ("os.name", "unknown");
216 SET ("os.arch", "unknown");
217 SET ("os.version", "unknown");
219 #endif /* HAVE_UNAME */
223 uid_t user_id
= getuid ();
224 struct passwd
*pwd_entry
;
226 #if defined(HAVE_GETPWUID_R) && defined(_POSIX_PTHREAD_SEMANTICS)
229 char *buf_r
= (char *) _Jv_AllocBytes (len_r
);
231 while (buf_r
!= NULL
)
233 int r
= getpwuid_adaptor (getpwuid_r
, user_id
, &pwd_r
,
234 buf_r
, len_r
, &pwd_entry
);
237 else if (r
!= ERANGE
)
243 buf_r
= (char *) _Jv_AllocBytes (len_r
);
246 pwd_entry
= getpwuid (user_id
);
247 #endif /* HAVE_GETPWUID_R */
249 if (pwd_entry
!= NULL
)
251 SET ("user.name", pwd_entry
->pw_name
);
252 SET ("user.home", pwd_entry
->pw_dir
);
253 SET ("gnu.gcj.user.realname", pwd_entry
->pw_gecos
);
255 #endif /* HAVE_PWD_H */
256 #endif /* NO_GETUID */
260 /* Use getcwd to set "user.dir". */
262 char *buffer
= (char *) malloc (buflen
);
263 while (buffer
!= NULL
)
265 if (getcwd (buffer
, buflen
) != NULL
)
267 SET ("user.dir", buffer
);
273 char *orig_buf
= buffer
;
274 buffer
= (char *) realloc (buffer
, buflen
);
280 #endif /* HAVE_UNISTD_H */
281 #endif /* HAVE_GETCWD */
283 // Set user locale properties based on setlocale()
284 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
285 // We let the user choose the locale. However, since Java differs
286 // from POSIX, we arbitrarily pick LC_MESSAGES as determining the
287 // Java locale. We can't use LC_ALL because it might return a full
288 // list of all the settings. If we don't have LC_MESSAGES then we
289 // just default to `en_US'.
290 setlocale (LC_ALL
, "");
291 char *locale
= setlocale (LC_MESSAGES
, "");
292 if (locale
&& strlen (locale
) >= 2)
296 // copy the first two chars to user.language
297 strncpy (buf
, locale
, 2);
298 SET ("user.language", buf
);
299 // if the next char is a '_', copy the two after that to user.region
301 if (locale
[0] == '_')
304 strncpy (buf
, locale
, 2);
305 SET ("user.region", buf
);
309 #endif /* HAVE_SETLOCALE and HAVE_LC_MESSAGES */
311 SET ("user.language", "en");
312 SET ("user.region", "US");
315 // Set the java extension directories property if it has not yet been
317 ::java::lang::String
*extdirs
= newprops
->getProperty(JvNewStringLatin1("java.ext.dirs"));
319 SET ("java.ext.dirs", JAVA_EXT_DIRS
);
321 // The endorsed directories that libgcj knows about by default.
322 // This is a way to get other jars into the boot class loader
323 // without overriding java.endorsed.dirs.
324 SET ("gnu.gcj.runtime.endorsed.dirs", GCJ_ENDORSED_DIRS
);
326 // The path to libgcj's boot classes
327 SET ("sun.boot.class.path", BOOT_CLASS_PATH
);
329 // If there is a default system database, set it.
330 SET ("gnu.gcj.precompiled.db.path", LIBGCJ_DEFAULT_DATABASE
);
332 // Set some properties according to whatever was compiled in with
333 // `-D'. Important: after this point, the only properties that
334 // should be set are those which either the user cannot meaningfully
335 // override, or which augment whatever value the user has provided.
336 for (int i
= 0; i
< _Jv_Properties_Count
; ++i
)
340 for (s
= p
= _Jv_Compiler_Properties
[i
]; *s
&& *s
!= '='; ++s
)
342 jstring name
= JvNewStringLatin1 (p
, s
- p
);
343 jstring val
= JvNewStringLatin1 (*s
== '=' ? s
+ 1 : s
);
344 newprops
->put (name
, val
);
347 // Set the system properties from the user's environment.
348 #ifndef DISABLE_GETENV_PROPERTIES
349 if (_Jv_Environment_Properties
)
353 while (_Jv_Environment_Properties
[i
].key
)
355 SET (_Jv_Environment_Properties
[i
].key
,
356 _Jv_Environment_Properties
[i
].value
);
362 // The name used to invoke this process (argv[0] in C).
363 SET ("gnu.gcj.progname", _Jv_GetSafeArg (0));
365 // Allow platform specific settings and overrides.
366 _Jv_platform_initProperties (newprops
);
368 // If java.library.path is set, tell libltdl so we search the new
369 // directories as well.
370 ::java::lang::String
*path
= newprops
->getProperty(JvNewStringLatin1("java.library.path"));
373 // Prepend GCJ_VERSIONED_LIBDIR to the module load path so that
374 // libgcj will find its own JNI libraries, like libgtkpeer.so.
375 char* val
= PrependVersionedLibdir (path
);
376 _Jv_SetDLLSearchPath (val
);
381 // Set a value for user code to see.
383 char *libpath
= getenv (LTDL_SHLIBPATH_VAR
);
384 char* val
= _Jv_PrependVersionedLibdir (libpath
);
385 SET ("java.library.path", val
);
386 _Jv_SetDLLSearchPath (val
);
389 SET ("java.library.path", "");
393 // If java.class.path is still not set then set it according to the
394 // CLASSPATH environment variable if given. See gij.cc main () and
395 // prims.cc _Jv_CreateJavaVM () for all the ways this could have
396 // been set much earlier.
397 // If CLASSPATH isn't set or if the path is empty fall back to "."
398 path
= newprops
->getProperty(JvNewStringLatin1("java.class.path"));
401 char *classpath
= getenv("CLASSPATH");
402 if (classpath
&& classpath
[0] != 0)
404 path
= JvNewStringLatin1 (classpath
);
405 newprops
->put(JvNewStringLatin1 ("java.class.path"), path
);
409 if (!path
|| path
->length() == 0)
410 SET ("java.class.path", ".");
414 gnu::classpath::SystemProperties::isWordsBigEndian (void)