Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/solaris/native/java/lang/java_props_md.c
48795 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#if defined(__linux__) || defined(_ALLBSD_SOURCE)26#include <stdio.h>27#include <ctype.h>28#endif29#include <pwd.h>30#include <locale.h>31#ifndef ARCHPROPNAME32#error "The macro ARCHPROPNAME has not been defined"33#endif34#include <sys/utsname.h> /* For os_name and os_version */35#include <langinfo.h> /* For nl_langinfo */36#include <stdlib.h>37#include <string.h>38#include <sys/types.h>39#include <unistd.h>40#include <sys/param.h>41#include <time.h>42#include <errno.h>4344#ifdef MACOSX45#include "java_props_macosx.h"46#endif4748#if defined(_ALLBSD_SOURCE)49#if !defined(P_tmpdir)50#include <paths.h>51#define P_tmpdir _PATH_VARTMP52#endif53#endif5455#include "locale_str.h"56#include "java_props.h"5758#if !defined(_ALLBSD_SOURCE)59#ifdef __linux__60#ifndef CODESET61#define CODESET _NL_CTYPE_CODESET_NAME62#endif63#else64#ifdef ALT_CODESET_KEY65#define CODESET ALT_CODESET_KEY66#endif67#endif68#endif /* !_ALLBSD_SOURCE */6970#ifdef JAVASE_EMBEDDED71#include <dlfcn.h>72#include <sys/stat.h>73#endif7475/* Take an array of string pairs (map of key->value) and a string (key).76* Examine each pair in the map to see if the first string (key) matches the77* string. If so, store the second string of the pair (value) in the value and78* return 1. Otherwise do nothing and return 0. The end of the map is79* indicated by an empty string at the start of a pair (key of "").80*/81static int82mapLookup(char* map[], const char* key, char** value) {83int i;84for (i = 0; strcmp(map[i], ""); i += 2){85if (!strcmp(key, map[i])){86*value = map[i + 1];87return 1;88}89}90return 0;91}9293#ifndef P_tmpdir94#define P_tmpdir "/var/tmp"95#endif9697static int ParseLocale(JNIEnv* env, int cat, char ** std_language, char ** std_script,98char ** std_country, char ** std_variant, char ** std_encoding) {99char *temp = NULL;100char *language = NULL, *country = NULL, *variant = NULL,101*encoding = NULL;102char *p, *encoding_variant, *old_temp, *old_ev;103char *lc;104105/* Query the locale set for the category */106107#ifdef MACOSX108lc = setupMacOSXLocale(cat); // malloc'd memory, need to free109#else110lc = setlocale(cat, NULL);111#endif112113#ifndef __linux__114if (lc == NULL) {115return 0;116}117118temp = malloc(strlen(lc) + 1);119if (temp == NULL) {120#ifdef MACOSX121free(lc); // malloced memory122#endif123JNU_ThrowOutOfMemoryError(env, NULL);124return 0;125}126127if (cat == LC_CTYPE) {128/*129* Workaround for Solaris bug 4201684: Xlib doesn't like @euro130* locales. Since we don't depend on the libc @euro behavior,131* we just remove the qualifier.132* On Linux, the bug doesn't occur; on the other hand, @euro133* is needed there because it's a shortcut that also determines134* the encoding - without it, we wouldn't get ISO-8859-15.135* Therefore, this code section is Solaris-specific.136*/137strcpy(temp, lc);138p = strstr(temp, "@euro");139if (p != NULL) {140*p = '\0';141setlocale(LC_ALL, temp);142}143}144#else145if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) {146lc = "en_US";147}148149temp = malloc(strlen(lc) + 1);150if (temp == NULL) {151JNU_ThrowOutOfMemoryError(env, NULL);152return 0;153}154155#endif156157/*158* locale string format in Solaris is159* <language name>_<country name>.<encoding name>@<variant name>160* <country name>, <encoding name>, and <variant name> are optional.161*/162163strcpy(temp, lc);164#ifdef MACOSX165free(lc); // malloced memory166#endif167/* Parse the language, country, encoding, and variant from the168* locale. Any of the elements may be missing, but they must occur169* in the order language_country.encoding@variant, and must be170* preceded by their delimiter (except for language).171*172* If the locale name (without .encoding@variant, if any) matches173* any of the names in the locale_aliases list, map it to the174* corresponding full locale name. Most of the entries in the175* locale_aliases list are locales that include a language name but176* no country name, and this facility is used to map each language177* to a default country if that's possible. It's also used to map178* the Solaris locale aliases to their proper Java locale IDs.179*/180181encoding_variant = malloc(strlen(temp)+1);182if (encoding_variant == NULL) {183free(temp);184JNU_ThrowOutOfMemoryError(env, NULL);185return 0;186}187188if ((p = strchr(temp, '.')) != NULL) {189strcpy(encoding_variant, p); /* Copy the leading '.' */190*p = '\0';191} else if ((p = strchr(temp, '@')) != NULL) {192strcpy(encoding_variant, p); /* Copy the leading '@' */193*p = '\0';194} else {195*encoding_variant = '\0';196}197198if (mapLookup(locale_aliases, temp, &p)) {199old_temp = temp;200temp = realloc(temp, strlen(p)+1);201if (temp == NULL) {202free(old_temp);203free(encoding_variant);204JNU_ThrowOutOfMemoryError(env, NULL);205return 0;206}207strcpy(temp, p);208old_ev = encoding_variant;209encoding_variant = realloc(encoding_variant, strlen(temp)+1);210if (encoding_variant == NULL) {211free(old_ev);212free(temp);213JNU_ThrowOutOfMemoryError(env, NULL);214return 0;215}216// check the "encoding_variant" again, if any.217if ((p = strchr(temp, '.')) != NULL) {218strcpy(encoding_variant, p); /* Copy the leading '.' */219*p = '\0';220} else if ((p = strchr(temp, '@')) != NULL) {221strcpy(encoding_variant, p); /* Copy the leading '@' */222*p = '\0';223}224}225226language = temp;227if ((country = strchr(temp, '_')) != NULL) {228*country++ = '\0';229}230231p = encoding_variant;232if ((encoding = strchr(p, '.')) != NULL) {233p[encoding++ - p] = '\0';234p = encoding;235}236if ((variant = strchr(p, '@')) != NULL) {237p[variant++ - p] = '\0';238}239240/* Normalize the language name */241if (std_language != NULL) {242*std_language = "en";243if (language != NULL && mapLookup(language_names, language, std_language) == 0) {244*std_language = malloc(strlen(language)+1);245strcpy(*std_language, language);246}247}248249/* Normalize the country name */250if (std_country != NULL && country != NULL) {251if (mapLookup(country_names, country, std_country) == 0) {252*std_country = malloc(strlen(country)+1);253strcpy(*std_country, country);254}255}256257/* Normalize the script and variant name. Note that we only use258* variants listed in the mapping array; others are ignored.259*/260if (variant != NULL) {261if (std_script != NULL) {262mapLookup(script_names, variant, std_script);263}264265if (std_variant != NULL) {266mapLookup(variant_names, variant, std_variant);267}268}269270/* Normalize the encoding name. Note that we IGNORE the string271* 'encoding' extracted from the locale name above. Instead, we use the272* more reliable method of calling nl_langinfo(CODESET). This function273* returns an empty string if no encoding is set for the given locale274* (e.g., the C or POSIX locales); we use the default ISO 8859-1275* converter for such locales.276*/277if (std_encoding != NULL) {278/* OK, not so reliable - nl_langinfo() gives wrong answers on279* Euro locales, in particular. */280if (strcmp(p, "ISO8859-15") == 0)281p = "ISO8859-15";282else283p = nl_langinfo(CODESET);284285/* Convert the bare "646" used on Solaris to a proper IANA name */286if (strcmp(p, "646") == 0)287p = "ISO646-US";288289/* return same result nl_langinfo would return for en_UK,290* in order to use optimizations. */291*std_encoding = (*p != '\0') ? p : "ISO8859-1";292293#ifdef __linux__294/*295* Remap the encoding string to a different value for japanese296* locales on linux so that customized converters are used instead297* of the default converter for "EUC-JP". The customized converters298* omit support for the JIS0212 encoding which is not supported by299* the variant of "EUC-JP" encoding used on linux300*/301if (strcmp(p, "EUC-JP") == 0) {302*std_encoding = "EUC-JP-LINUX";303}304#else305if (strcmp(p,"eucJP") == 0) {306/* For Solaris use customized vendor defined character307* customized EUC-JP converter308*/309*std_encoding = "eucJP-open";310} else if (strcmp(p, "Big5") == 0 || strcmp(p, "BIG5") == 0) {311/*312* Remap the encoding string to Big5_Solaris which augments313* the default converter for Solaris Big5 locales to include314* seven additional ideographic characters beyond those included315* in the Java "Big5" converter.316*/317*std_encoding = "Big5_Solaris";318} else if (strcmp(p, "Big5-HKSCS") == 0) {319/*320* Solaris uses HKSCS2001321*/322*std_encoding = "Big5-HKSCS-2001";323}324#endif325#ifdef MACOSX326/*327* For the case on MacOS X where encoding is set to US-ASCII, but we328* don't have any encoding hints from LANG/LC_ALL/LC_CTYPE, use UTF-8329* instead.330*331* The contents of ASCII files will still be read and displayed332* correctly, but so will files containing UTF-8 characters beyond the333* standard ASCII range.334*335* Specifically, this allows apps launched by double-clicking a .jar336* file to correctly read UTF-8 files using the default encoding (see337* 8011194).338*/339if (strcmp(p,"US-ASCII") == 0 && getenv("LANG") == NULL &&340getenv("LC_ALL") == NULL && getenv("LC_CTYPE") == NULL) {341*std_encoding = "UTF-8";342}343#endif344}345346free(temp);347free(encoding_variant);348349return 1;350}351352#ifdef JAVASE_EMBEDDED353/* Determine the default embedded toolkit based on whether libawt_xawt354* exists in the JRE. This can still be overridden by -Dawt.toolkit=XXX355*/356static char* getEmbeddedToolkit() {357Dl_info dlinfo;358char buf[MAXPATHLEN];359int32_t len;360char *p;361struct stat statbuf;362363/* Get address of this library and the directory containing it. */364dladdr((void *)getEmbeddedToolkit, &dlinfo);365realpath((char *)dlinfo.dli_fname, buf);366len = strlen(buf);367p = strrchr(buf, '/');368/* Default AWT Toolkit on Linux and Solaris is XAWT (libawt_xawt.so). */369strncpy(p, "/libawt_xawt.so", MAXPATHLEN-len-1);370/* Check if it exists */371if (stat(buf, &statbuf) == -1 && errno == ENOENT) {372/* No - this is a reduced-headless-jre so use special HToolkit */373return "sun.awt.HToolkit";374}375else {376/* Yes - this is a headful JRE so fallback to SE defaults */377return NULL;378}379}380#endif381382/* This function gets called very early, before VM_CALLS are setup.383* Do not use any of the VM_CALLS entries!!!384*/385java_props_t *386GetJavaProperties(JNIEnv *env)387{388static java_props_t sprops;389char *v; /* tmp var */390391if (sprops.user_dir) {392return &sprops;393}394395/* tmp dir */396sprops.tmp_dir = P_tmpdir;397#ifdef MACOSX398/* darwin has a per-user temp dir */399static char tmp_path[PATH_MAX];400int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, tmp_path, PATH_MAX);401if (pathSize > 0 && pathSize <= PATH_MAX) {402sprops.tmp_dir = tmp_path;403}404#endif /* MACOSX */405406/* Printing properties */407#ifdef MACOSX408sprops.printerJob = "sun.lwawt.macosx.CPrinterJob";409#else410sprops.printerJob = "sun.print.PSPrinterJob";411#endif412413/* patches/service packs installed */414sprops.patch_level = "unknown";415416/* Java 2D/AWT properties */417#ifdef MACOSX418// Always the same GraphicsEnvironment and Toolkit on Mac OS X419sprops.graphics_env = "sun.awt.CGraphicsEnvironment";420sprops.awt_toolkit = "sun.lwawt.macosx.LWCToolkit";421422// check if we're in a GUI login session and set java.awt.headless=true if not423sprops.awt_headless = isInAquaSession() ? NULL : "true";424#else425sprops.graphics_env = "sun.awt.X11GraphicsEnvironment";426#ifdef JAVASE_EMBEDDED427sprops.awt_toolkit = getEmbeddedToolkit();428if (sprops.awt_toolkit == NULL) // default as below429#endif430sprops.awt_toolkit = "sun.awt.X11.XToolkit";431#endif432433/* This is used only for debugging of font problems. */434v = getenv("JAVA2D_FONTPATH");435sprops.font_dir = v ? v : NULL;436437#ifdef SI_ISALIST438/* supported instruction sets */439{440char list[258];441sysinfo(SI_ISALIST, list, sizeof(list));442sprops.cpu_isalist = strdup(list);443}444#else445sprops.cpu_isalist = NULL;446#endif447448/* endianness of platform */449{450unsigned int endianTest = 0xff000000;451if (((char*)(&endianTest))[0] != 0)452sprops.cpu_endian = "big";453else454sprops.cpu_endian = "little";455}456457/* os properties */458{459#ifdef MACOSX460setOSNameAndVersion(&sprops);461#else462struct utsname name;463uname(&name);464sprops.os_name = strdup(name.sysname);465#ifdef _AIX466{467char *os_version = malloc(strlen(name.version) +468strlen(name.release) + 2);469if (os_version != NULL) {470strcpy(os_version, name.version);471strcat(os_version, ".");472strcat(os_version, name.release);473}474sprops.os_version = os_version;475}476#else477sprops.os_version = strdup(name.release);478#endif /* _AIX */479#endif /* MACOSX */480481sprops.os_arch = ARCHPROPNAME;482483if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) {484sprops.desktop = "gnome";485}486else {487sprops.desktop = NULL;488}489}490491/* ABI property (optional) */492#ifdef JDK_ARCH_ABI_PROP_NAME493sprops.sun_arch_abi = JDK_ARCH_ABI_PROP_NAME;494#endif495496/* Determine the language, country, variant, and encoding from the host,497* and store these in the user.language, user.country, user.variant and498* file.encoding system properties. */499setlocale(LC_ALL, "");500if (ParseLocale(env, LC_CTYPE,501&(sprops.format_language),502&(sprops.format_script),503&(sprops.format_country),504&(sprops.format_variant),505&(sprops.encoding))) {506ParseLocale(env, LC_MESSAGES,507&(sprops.language),508&(sprops.script),509&(sprops.country),510&(sprops.variant),511NULL);512} else {513sprops.language = "en";514sprops.encoding = "ISO8859-1";515}516sprops.display_language = sprops.language;517sprops.display_script = sprops.script;518sprops.display_country = sprops.country;519sprops.display_variant = sprops.variant;520521/* ParseLocale failed with OOME */522JNU_CHECK_EXCEPTION_RETURN(env, NULL);523524#ifdef MACOSX525sprops.sun_jnu_encoding = "UTF-8";526#else527sprops.sun_jnu_encoding = sprops.encoding;528#endif529530#ifdef _ALLBSD_SOURCE531#if BYTE_ORDER == _LITTLE_ENDIAN532sprops.unicode_encoding = "UnicodeLittle";533#else534sprops.unicode_encoding = "UnicodeBig";535#endif536#else /* !_ALLBSD_SOURCE */537#ifdef __linux__538#if __BYTE_ORDER == __LITTLE_ENDIAN539sprops.unicode_encoding = "UnicodeLittle";540#else541sprops.unicode_encoding = "UnicodeBig";542#endif543#else544sprops.unicode_encoding = "UnicodeBig";545#endif546#endif /* _ALLBSD_SOURCE */547548/* user properties */549{550struct passwd *pwent = getpwuid(getuid());551sprops.user_name = pwent ? strdup(pwent->pw_name) : "?";552#ifdef MACOSX553setUserHome(&sprops);554#else555sprops.user_home = pwent ? strdup(pwent->pw_dir) : NULL;556#endif557if (sprops.user_home == NULL) {558sprops.user_home = "?";559}560}561562/* User TIMEZONE */563{564/*565* We defer setting up timezone until it's actually necessary.566* Refer to TimeZone.getDefault(). However, the system567* property is necessary to be able to be set by the command568* line interface -D. Here temporarily set a null string to569* timezone.570*/571tzset(); /* for compatibility */572sprops.timezone = "";573}574575/* Current directory */576{577char buf[MAXPATHLEN];578errno = 0;579if (getcwd(buf, sizeof(buf)) == NULL)580JNU_ThrowByName(env, "java/lang/Error",581"Properties init: Could not determine current working directory.");582else583sprops.user_dir = strdup(buf);584}585586sprops.file_separator = "/";587sprops.path_separator = ":";588sprops.line_separator = "\n";589590#ifdef MACOSX591setProxyProperties(&sprops);592#endif593594return &sprops;595}596597jstring598GetStringPlatform(JNIEnv *env, nchar* cstr)599{600return JNU_NewStringPlatform(env, cstr);601}602603604