OSDN Git Service

958fdcbaf18ee5799416a7417e9c253b01d9fb67
[pf3gnuchains/gcc-fork.git] / libjava / gnu / classpath / natSystemProperties.cc
1 // natSystemProperties.cc - Implementation of native side of
2 // SystemProperties class.
3
4 /* Copyright (C) 2005, 2006  Free Software Foundation
5
6    This file is part of libgcj.
7
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
10 details.  */
11
12 #include <config.h>
13 #include <platform.h>
14
15 #include <stdlib.h>
16 #include <errno.h>
17
18 #ifdef HAVE_PWD_H
19 #include <pwd.h>
20 #endif
21
22 #ifdef HAVE_UNAME
23 #include <sys/utsname.h>
24 #endif
25
26 #ifdef HAVE_LOCALE_H
27 #include <locale.h>
28 #endif
29
30 #ifdef HAVE_LANGINFO_H
31 #include <langinfo.h>
32 #endif
33
34 #include <gcj/cni.h>
35 #include <jvm.h>
36 #include <java-props.h>
37 #include <gnu/classpath/SystemProperties.h>
38 #include <java/lang/String.h>
39 #include <jni.h>
40
41 char *_Jv_Module_Load_Path = NULL;
42
43 #ifdef USE_LTDL
44 #include <ltdl.h>
45
46 void
47 _Jv_SetDLLSearchPath (const char *path)
48 {
49   _Jv_Module_Load_Path = strdup (path);
50 }
51
52 #else
53
54 void
55 _Jv_SetDLLSearchPath (const char *)
56 {
57   // Nothing.
58 }
59
60 #endif /* USE_LTDL */
61
62 #if ! defined (DEFAULT_FILE_ENCODING) && defined (HAVE_ICONV) \
63     && defined (HAVE_NL_LANGINFO)
64
65 static const char *
66 file_encoding ()
67 {
68   setlocale (LC_CTYPE, "");
69   const char *e = nl_langinfo (CODESET);
70   if (e == NULL || *e == '\0')
71     e = "8859_1";
72   return e;
73 }
74
75 #define DEFAULT_FILE_ENCODING file_encoding ()
76
77 #endif
78
79 #ifndef DEFAULT_FILE_ENCODING
80 #define DEFAULT_FILE_ENCODING "8859_1"
81 #endif
82
83 static const char *default_file_encoding = DEFAULT_FILE_ENCODING;
84
85 #if defined(HAVE_GETPWUID_R) && defined(_POSIX_PTHREAD_SEMANTICS)
86 /* Use overload resolution to find out the signature of getpwuid_r.  */
87
88   /* This is Posix getpwuid_r.  */
89 template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
90 static inline int
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)
96 {
97   return getpwuid_r (user_id, pwd_r, buf_r, len_r, pwd_entry);
98 }
99
100 /* This is used on HPUX 10.20 */
101 template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
102 static inline int
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)
107 {
108   return getpwuid_r (user_id, pwd_r, buf_r, len_r);
109 }
110
111 /* This is used on IRIX 5.2.  */
112 template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
113 static inline int
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)
118 {
119   *pwd_entry = getpwuid_r (user_id, pwd_r, buf_r, len_r);
120   return (*pwd_entry == NULL) ? errno : 0;
121 }
122 #endif
123
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.
129 static char*
130 PrependVersionedLibdir (::java::lang::String* libpath)
131 {
132   char* retval = 0;
133
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);
137   val[total] = '\0';
138   retval = _Jv_PrependVersionedLibdir (val);
139   JvFree (val);
140
141   return retval;
142 }
143
144 void
145 gnu::classpath::SystemProperties::insertSystemProperties (java::util::Properties *newprops)
146 {
147   // A convenience define.
148 #define SET(Prop,Val) \
149         newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
150
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", "48.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.");
169
170   char value[100];
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);
179
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);
192
193   SET ("file.encoding", default_file_encoding);
194
195 #ifdef HAVE_UNAME
196   struct utsname u;
197   if (! uname (&u))
198     {
199       SET ("os.name", u.sysname);
200       SET ("os.version", u.release);
201
202       // Normalize x86 architecture names to "i386" (except on Windows, which 
203       // is handled in win32.cc).
204       if (u.machine[0] == 'i'
205           && u.machine[1] != 0
206           && u.machine[2] == '8'
207           && u.machine[3] == '6'
208           && u.machine[4] == 0)
209         SET ("os.arch", "i386");
210       else
211         SET ("os.arch", u.machine);
212     }
213   else
214     {
215       SET ("os.name", "unknown");
216       SET ("os.arch", "unknown");
217       SET ("os.version", "unknown");
218     }
219 #endif /* HAVE_UNAME */
220
221 #ifndef NO_GETUID
222 #ifdef HAVE_PWD_H
223   uid_t user_id = getuid ();
224   struct passwd *pwd_entry;
225
226 #if defined(HAVE_GETPWUID_R) && defined(_POSIX_PTHREAD_SEMANTICS)
227   struct passwd pwd_r;
228   size_t len_r = 200;
229   char *buf_r = (char *) _Jv_AllocBytes (len_r);
230
231   while (buf_r != NULL)
232     {
233       int r = getpwuid_adaptor (getpwuid_r, user_id, &pwd_r,
234                                 buf_r, len_r, &pwd_entry);
235       if (r == 0)
236         break;
237       else if (r != ERANGE)
238         {
239           pwd_entry = NULL;
240           break;
241         }
242       len_r *= 2;
243       buf_r = (char *) _Jv_AllocBytes (len_r);
244     }
245 #else
246   pwd_entry = getpwuid (user_id);
247 #endif /* HAVE_GETPWUID_R */
248
249   if (pwd_entry != NULL)
250     {
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);
254     }
255 #endif /* HAVE_PWD_H */
256 #endif /* NO_GETUID */
257
258 #ifdef HAVE_GETCWD
259 #ifdef HAVE_UNISTD_H
260   /* Use getcwd to set "user.dir". */
261   int buflen = 250;
262   char *buffer = (char *) malloc (buflen);
263   while (buffer != NULL)
264     {
265       if (getcwd (buffer, buflen) != NULL)
266         {
267           SET ("user.dir", buffer);
268           break;
269         }
270       if (errno != ERANGE)
271         break;
272       buflen = 2 * buflen;
273       buffer = (char *) realloc (buffer, buflen);
274     }
275   if (buffer != NULL)
276     free (buffer);
277 #endif /* HAVE_UNISTD_H */
278 #endif /* HAVE_GETCWD */
279
280   // Set user locale properties based on setlocale()
281 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
282   // We let the user choose the locale.  However, since Java differs
283   // from POSIX, we arbitrarily pick LC_MESSAGES as determining the
284   // Java locale.  We can't use LC_ALL because it might return a full
285   // list of all the settings.  If we don't have LC_MESSAGES then we
286   // just default to `en_US'.
287   setlocale (LC_ALL, "");
288   char *locale = setlocale (LC_MESSAGES, "");
289   if (locale && strlen (locale) >= 2)
290     {
291       char buf[3];
292       buf[2] = '\0';
293       // copy the first two chars to user.language
294       strncpy (buf, locale, 2);
295       SET ("user.language", buf);
296       // if the next char is a '_', copy the two after that to user.region
297       locale += 2;
298       if (locale[0] == '_')
299         {
300           locale++;
301           strncpy (buf, locale, 2);
302           SET ("user.region", buf);
303         }
304     }
305   else
306 #endif /* HAVE_SETLOCALE and HAVE_LC_MESSAGES */
307     {
308       SET ("user.language", "en");
309       SET ("user.region", "US");
310     }  
311
312   // Set the java extension directories property if it has not yet been
313   // specified.
314   ::java::lang::String *extdirs = newprops->getProperty(JvNewStringLatin1("java.ext.dirs"));
315   if (! extdirs)
316     SET ("java.ext.dirs", JAVA_EXT_DIRS);
317
318   // The endorsed directories that libgcj knows about by default.
319   // This is a way to get other jars into the boot class loader
320   // without overriding java.endorsed.dirs.
321   SET ("gnu.gcj.runtime.endorsed.dirs", GCJ_ENDORSED_DIRS);
322
323   // The path to libgcj's boot classes
324   SET ("sun.boot.class.path", BOOT_CLASS_PATH);
325
326   // If there is a default system database, set it.
327   SET ("gnu.gcj.precompiled.db.path", LIBGCJ_DEFAULT_DATABASE);
328
329   // Set some properties according to whatever was compiled in with
330   // `-D'.  Important: after this point, the only properties that
331   // should be set are those which either the user cannot meaningfully
332   // override, or which augment whatever value the user has provided.
333   for (int i = 0; i < _Jv_Properties_Count; ++i)
334     {
335       const char *s, *p;
336       // Find the `='.
337       for (s = p = _Jv_Compiler_Properties[i]; *s && *s != '='; ++s)
338         ;
339       jstring name = JvNewStringLatin1 (p, s - p);
340       jstring val = JvNewStringLatin1 (*s == '=' ? s + 1 : s);
341       newprops->put (name, val);
342     }
343
344   // Set the system properties from the user's environment.
345 #ifndef DISABLE_GETENV_PROPERTIES
346   if (_Jv_Environment_Properties)
347     {
348       size_t i = 0;
349
350       while (_Jv_Environment_Properties[i].key)
351         {
352           SET (_Jv_Environment_Properties[i].key, 
353                _Jv_Environment_Properties[i].value);
354           i++;
355         }
356     }
357 #endif
358
359   // The name used to invoke this process (argv[0] in C).
360   SET ("gnu.gcj.progname", _Jv_GetSafeArg (0));
361
362   // Allow platform specific settings and overrides.
363   _Jv_platform_initProperties (newprops);
364
365   // If java.library.path is set, tell libltdl so we search the new
366   // directories as well.  FIXME: does this work properly on Windows?
367   ::java::lang::String *path = newprops->getProperty(JvNewStringLatin1("java.library.path"));
368   if (path)
369     {
370       // Prepend GCJ_VERSIONED_LIBDIR to the module load path so that
371       // libgcj will find its own JNI libraries, like libgtkpeer.so.
372       char* val = PrependVersionedLibdir (path);
373       _Jv_SetDLLSearchPath (val);
374       _Jv_Free (val);
375     }
376   else
377     {
378       // Set a value for user code to see.
379 #ifdef USE_LTDL
380       char *libpath = getenv (LTDL_SHLIBPATH_VAR);
381       char* val = _Jv_PrependVersionedLibdir (libpath);
382       SET ("java.library.path", val);
383       _Jv_SetDLLSearchPath (val);
384       _Jv_Free (val);
385 #else
386       SET ("java.library.path", "");
387 #endif
388     }
389
390   // If java.class.path is still not set then set it according to the
391   // CLASSPATH environment variable if given.  See gij.cc main () and
392   // prims.cc _Jv_CreateJavaVM () for all the ways this could have
393   // been set much earlier.
394   // If CLASSPATH isn't set or if the path is empty fall back to "."
395   path = newprops->getProperty(JvNewStringLatin1("java.class.path"));
396   if (!path)
397     {
398       char *classpath = getenv("CLASSPATH");
399       if (classpath && classpath[0] != 0)
400         {
401           path = JvNewStringLatin1 (classpath);
402           newprops->put(JvNewStringLatin1 ("java.class.path"), path);
403         }
404     }
405
406   if (!path || path->length() == 0)
407     SET ("java.class.path", ".");
408 }
409
410 jboolean
411 gnu::classpath::SystemProperties::isWordsBigEndian (void)
412 {
413   union
414   {
415     long lval;
416     char cval;
417   } u;
418
419   u.lval = 1;
420   return u.cval == 0;
421 }
422