2 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
33 #error "The macro ARCHPROPNAME has not been defined"
35 #include <sys/utsname.h> /* For os_name and os_version */
36 #include <langinfo.h> /* For nl_langinfo */
39 #include <sys/types.h>
41 #include <sys/param.h>
45 #include "locale_str.h"
46 #include "java_props.h"
49 #define CODESET _NL_CTYPE_CODESET_NAME
51 #ifdef ALT_CODESET_KEY
52 #define CODESET ALT_CODESET_KEY
56 /* Take an array of string pairs (map of key->value) and a string (key).
57 * Examine each pair in the map to see if the first string (key) matches the
58 * string. If so, store the second string of the pair (value) in the value and
59 * return 1. Otherwise do nothing and return 0. The end of the map is
60 * indicated by an empty string at the start of a pair (key of "").
63 mapLookup(char* map[], const char* key, char** value) {
65 for (i = 0; strcmp(map[i], ""); i += 2){
66 if (!strcmp(key, map[i])){
74 /* This function sets an environment variable using envstring.
75 * The format of envstring is "name=value".
76 * If the name has already existed, it will append value to the name.
79 setPathEnvironment(char *envstring)
81 char name[20], *value, *current;
83 value = strchr(envstring, '='); /* locate name and value separator */
86 return; /* not a valid environment setting */
88 /* copy first part as environment name */
89 strncpy(name, envstring, value - envstring);
90 name[value-envstring] = '\0';
92 value++; /* set value point to value of the envstring */
94 current = getenv(name);
96 if (! strstr(current, value)) {
97 /* value is not found in current environment, append it */
98 char *temp = malloc(strlen(envstring) + strlen(current) + 2);
101 strcat(temp, current);
106 /* else the value has already been set, do nothing */
109 /* environment variable is not found */
115 #define P_tmpdir "/var/tmp"
118 /* This function gets called very early, before VM_CALLS are setup.
119 * Do not use any of the VM_CALLS entries!!!
122 GetJavaProperties(JNIEnv *env)
124 static java_props_t sprops = {0};
125 char *v; /* tmp var */
127 if (sprops.user_dir) {
132 sprops.tmp_dir = P_tmpdir;
134 /* Printing properties */
135 sprops.printerJob = "sun.print.PSPrinterJob";
137 /* patches/service packs installed */
138 sprops.patch_level = "unknown";
140 /* Java 2D properties */
141 sprops.graphics_env = "sun.awt.X11GraphicsEnvironment";
142 sprops.awt_toolkit = NULL;
144 /* This is used only for debugging of font problems. */
145 v = getenv("JAVA2D_FONTPATH");
146 sprops.font_dir = v ? v : NULL;
149 /* supported instruction sets */
152 sysinfo(SI_ISALIST, list, sizeof(list));
153 sprops.cpu_isalist = strdup(list);
156 sprops.cpu_isalist = NULL;
159 /* endianness of platform */
161 unsigned int endianTest = 0xff000000;
162 if (((char*)(&endianTest))[0] != 0)
163 sprops.cpu_endian = "big";
165 sprops.cpu_endian = "little";
172 sprops.os_name = strdup(name.sysname);
173 sprops.os_version = strdup(name.release);
175 sprops.os_arch = ARCHPROPNAME;
177 if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) {
178 sprops.desktop = "gnome";
181 sprops.desktop = NULL;
185 /* Determine the language, country, variant, and encoding from the host,
186 * and store these in the user.language, user.country, user.variant and
187 * file.encoding system properties. */
190 lc = setlocale(LC_CTYPE, "");
194 * 'lc == null' means system doesn't support user's environment
197 setlocale(LC_ALL, "C");
198 sprops.language = "en";
199 sprops.encoding = "ISO8859-1";
200 sprops.sun_jnu_encoding = sprops.encoding;
203 if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) {
210 * locale string format in Solaris is
211 * <language name>_<country name>.<encoding name>@<variant name>
212 * <country name>, <encoding name>, and <variant name> are optional.
215 char *language = NULL, *country = NULL, *variant = NULL,
217 char *std_language = NULL, *std_country = NULL, *std_variant = NULL,
218 *std_encoding = NULL;
219 char *p, encoding_variant[64];
224 * Workaround for Solaris bug 4201684: Xlib doesn't like @euro
225 * locales. Since we don't depend on the libc @euro behavior,
226 * we just remove the qualifier.
227 * On Linux, the bug doesn't occur; on the other hand, @euro
228 * is needed there because it's a shortcut that also determines
229 * the encoding - without it, we wouldn't get ISO-8859-15.
230 * Therefore, this code section is Solaris-specific.
232 lc = strdup(lc); /* keep a copy, setlocale trashes original. */
234 p = strstr(temp, "@euro");
237 setlocale(LC_ALL, temp);
242 /* Parse the language, country, encoding, and variant from the
243 * locale. Any of the elements may be missing, but they must occur
244 * in the order language_country.encoding@variant, and must be
245 * preceded by their delimiter (except for language).
247 * If the locale name (without .encoding@variant, if any) matches
248 * any of the names in the locale_aliases list, map it to the
249 * corresponding full locale name. Most of the entries in the
250 * locale_aliases list are locales that include a language name but
251 * no country name, and this facility is used to map each language
252 * to a default country if that's possible. It's also used to map
253 * the Solaris locale aliases to their proper Java locale IDs.
255 if ((p = strchr(temp, '.')) != NULL) {
256 strcpy(encoding_variant, p); /* Copy the leading '.' */
258 } else if ((p = strchr(temp, '@')) != NULL) {
259 strcpy(encoding_variant, p); /* Copy the leading '@' */
262 *encoding_variant = '\0';
265 if (mapLookup(locale_aliases, temp, &p)) {
270 if ((country = strchr(temp, '_')) != NULL) {
274 p = encoding_variant;
275 if ((encoding = strchr(p, '.')) != NULL) {
276 p[encoding++ - p] = '\0';
279 if ((variant = strchr(p, '@')) != NULL) {
280 p[variant++ - p] = '\0';
283 /* Normalize the language name */
285 if (language != NULL) {
286 mapLookup(language_names, language, &std_language);
288 sprops.language = std_language;
290 /* Normalize the country name */
291 if (country != NULL) {
292 std_country = country;
293 mapLookup(country_names, country, &std_country);
294 sprops.country = strdup(std_country);
297 /* Normalize the variant name. Note that we only use
298 * variants listed in the mapping array; others are ignored. */
299 if (variant != NULL) {
300 mapLookup(variant_names, variant, &std_variant);
301 sprops.variant = std_variant;
304 /* Normalize the encoding name. Note that we IGNORE the string
305 * 'encoding' extracted from the locale name above. Instead, we use the
306 * more reliable method of calling nl_langinfo(CODESET). This function
307 * returns an empty string if no encoding is set for the given locale
308 * (e.g., the C or POSIX locales); we use the default ISO 8859-1
309 * converter for such locales.
312 /* OK, not so reliable - nl_langinfo() gives wrong answers on
313 * Euro locales, in particular. */
314 if (strcmp(p, "ISO8859-15") == 0)
317 p = nl_langinfo(CODESET);
319 /* Convert the bare "646" used on Solaris to a proper IANA name */
320 if (strcmp(p, "646") == 0)
323 /* return same result nl_langinfo would return for en_UK,
324 * in order to use optimizations. */
325 std_encoding = (*p != '\0') ? p : "ISO8859-1";
330 * Remap the encoding string to a different value for japanese
331 * locales on linux so that customized converters are used instead
332 * of the default converter for "EUC-JP". The customized converters
333 * omit support for the JIS0212 encoding which is not supported by
334 * the variant of "EUC-JP" encoding used on linux
336 if (strcmp(p, "EUC-JP") == 0) {
337 std_encoding = "EUC-JP-LINUX";
340 /* For Solaris use customized vendor defined character
341 * customized EUC-JP converter
343 if (strcmp(p,"eucJP") == 0) {
344 std_encoding = "eucJP-open";
349 * Remap the encoding string to Big5_Solaris which augments
350 * the default converter for Solaris Big5 locales to include
351 * seven additional ideographic characters beyond those included
352 * in the Java "Big5" converter.
354 if (strcmp(p, "Big5") == 0) {
355 std_encoding = "Big5_Solaris";
358 sprops.encoding = std_encoding;
359 sprops.sun_jnu_encoding = sprops.encoding;
364 #if __BYTE_ORDER == __LITTLE_ENDIAN
365 sprops.unicode_encoding = "UnicodeLittle";
367 sprops.unicode_encoding = "UnicodeBig";
370 sprops.unicode_encoding = "UnicodeBig";
373 /* user properties */
375 struct passwd *pwent = getpwuid(getuid());
376 sprops.user_name = pwent ? strdup(pwent->pw_name) : "?";
377 sprops.user_home = pwent ? strdup(pwent->pw_dir) : "?";
383 * We defer setting up timezone until it's actually necessary.
384 * Refer to TimeZone.getDefault(). However, the system
385 * property is necessary to be able to be set by the command
386 * line interface -D. Here temporarily set a null string to
389 tzset(); /* for compatibility */
390 sprops.timezone = "";
393 /* Current directory */
395 char buf[MAXPATHLEN];
397 if (getcwd(buf, sizeof(buf)) == NULL)
398 JNU_ThrowByName(env, "java/lang/Error",
399 "Properties init: Could not determine current working directory.");
401 sprops.user_dir = strdup(buf);
404 sprops.file_separator = "/";
405 sprops.path_separator = ":";
406 sprops.line_separator = "\n";
408 /* Append CDE message and resource search path to NLSPATH and
409 * XFILESEARCHPATH, in order to pick localized message for
410 * FileSelectionDialog window (Bug 4173641).
412 setPathEnvironment("NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat");
413 setPathEnvironment("XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt");