#ifndef ARM_EMULATED
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#if !defined(CHORUS) && !defined(ARM_EMULATED)
#include <utime.h>
#endif
#include <stdlib.h>
#include <string.h>
#ifndef CHORUS
#include <time.h>
#include <locale.h>
#endif
#if !defined(ARM_EMULATED) && !defined(CHORUS)
#include <langinfo.h>
#endif
#ifdef ARM_EMULATED
#define P_tmpdir "c:\\temp"
#endif
#if !defined(CHORUS)
#define J9PWENT
#include <pwd.h>
#endif
#include <dirent.h>
#include "jcl.h"
#include "jclprots.h"
#include "jclglob.h"
#include "j2sever.h"
#if defined(J9ZOS390)
#include "atoe.h"
#endif
#define JCL_J2SE
char* CodepageTable[] = {
"ISO8859_1",
NULL,
#if defined(AIXPPC)
"IBM-943C",
"IBM-943",
NULL,
"IBM-950",
"big5",
NULL
#endif
};
char * getTmpDir(JNIEnv *env, char**envSpace) {
PORT_ACCESS_FROM_ENV(env);
I_32 envSize;
if ((envSize = j9sysinfo_get_env("TMPDIR", NULL, 0))> 0) {
*envSpace = jclmem_allocate_memory(env,envSize);
if(*envSpace==NULL) return ".";
j9sysinfo_get_env("TMPDIR", *envSpace, envSize);
if (j9file_attr(*envSpace) > -1)
return *envSpace;
jclmem_free_memory(env,*envSpace);
*envSpace = NULL;
}
if (j9file_attr(P_tmpdir) > -1)
return P_tmpdir;
if (j9file_attr("/tmp") > -1)
return "/tmp";
return ".";
}
jobject getPlatformPropertyList(JNIEnv * env, const char *strings[], int propIndex)
{
PORT_ACCESS_FROM_ENV(env);
I_32 result = 0;
char *charResult = NULL, *envSpace = NULL;
jobject plist;
char userdir[EsMaxPath];
char home[EsMaxPath], *homeAlloc = NULL;
#if defined(J9PWENT)
struct passwd *pwentry = NULL;
#endif
#if defined(J9ZOS390)
if (J2SE_VERSION_FROM_ENV(env)) {
strings[propIndex++] = "platform.notASCII";
strings[propIndex++] = "true";
strings[propIndex++] = "os.encoding";
strings[propIndex++] = "ISO8859_1";
}
#endif
strings[propIndex++] = "file.separator";
strings[propIndex++] = "/";
strings[propIndex++] = "line.separator";
strings[propIndex++] = "\n";
strings[propIndex++] = "user.dir";
#ifndef ARM_EMULATED
charResult = getcwd(userdir, EsMaxPath);
#else
charResult = NULL;
#endif
if (charResult == NULL)
strings[propIndex++] = ".";
else
strings[propIndex++] = charResult;
strings[propIndex++] = "user.home";
charResult = NULL;
#if defined(J9ZOS390)
charResult = getenv("HOME");
if (charResult != NULL) {
strings[propIndex++] = charResult;
} else {
uid_t uid = geteuid();
if (uid != 0) {
struct passwd *userDescription = getpwuid(uid);
if (NULL != userDescription) {
charResult = userDescription->pw_dir;
strings[propIndex++] = charResult;
}
} else {
char *loginID = getlogin();
if (NULL != loginID) {
struct passwd *userDescription = getpwnam(loginID);
if (NULL != userDescription) {
charResult = userDescription->pw_dir;
strings[propIndex++] = charResult;
}
}
}
}
#endif
#if defined(J9PWENT)
if (charResult == NULL){
pwentry = getpwuid(getuid());
if (pwentry) {
charResult = pwentry->pw_dir;
strings[propIndex++] = charResult;
}
}
#endif
if (charResult == NULL) {
result = j9sysinfo_get_env("HOME", home, sizeof(home));
if (!result && strlen(home) > 0) {
strings[propIndex++] = home;
} else {
if (result != -1) {
homeAlloc = j9mem_allocate_memory(result, J9MEM_CATEGORY_VM_JCL);
}
if (homeAlloc) {
result = j9sysinfo_get_env("HOME", homeAlloc, result);
}
if (homeAlloc && !result) {
strings[propIndex++] = homeAlloc;
} else {
strings[propIndex++] = ".";
}
}
}
strings[propIndex++] = "java.io.tmpdir";
strings[propIndex++] = getTmpDir(env, &envSpace);
if (JAVA_SPEC_VERSION < 12) {
strings[propIndex++] = "user.timezone";
strings[propIndex++] = "";
}
plist = createSystemPropertyList(env, strings, propIndex);
if (envSpace)
jclmem_free_memory(env,envSpace);
if (homeAlloc) jclmem_free_memory(env, homeAlloc);
return plist;
}
#undef J9PWENT
void mapLibraryToPlatformName(const char *inPath, char *outPath) {
#ifdef J9OS_I5
strcpy(outPath,inPath);
strcat(outPath, ".srvpgm");
#else
strcpy(outPath, "lib");
strcat(outPath,inPath);
#if defined(AIXPPC) && (JAVA_SPEC_VERSION <= 14)
strcat(outPath, ".a");
#else
strcat(outPath, J9PORT_LIBRARY_SUFFIX);
#endif
#endif
}
char *getPlatformFileEncoding(JNIEnv * env, char *codepageProp, int propSize, int encodingType)
{
#if defined(J9ZTPF)
return "ISO8859_1";
#endif
char *codepage = NULL;
int i = 0;
int nameIndex = 0;
#if defined(LINUX) || defined(OSX)
IDATA result = 0;
char langProp[24] = {0};
#if defined(LINUX)
char *ctype = NULL;
#endif
PORT_ACCESS_FROM_ENV(env);
#endif
if (NULL == codepageProp) {
return NULL;
}
#if defined(LINUX)
ctype = setlocale(LC_CTYPE, NULL);
if ((NULL == ctype)
|| (0 == strcmp(ctype, "C"))
|| (0 == strcmp(ctype, "POSIX"))
) {
result = j9sysinfo_get_env("LANG", langProp, sizeof(langProp));
if ((0 == result) && (0 == strcmp(langProp, "ja"))) {
return "EUC-JP-LINUX";
}
}
#ifndef __ANDROID__
codepage = nl_langinfo(_NL_CTYPE_CODESET_NAME);
#else
codepage = "UTF-8";
#endif
#elif defined(ARM_EMULATED) || defined(CHORUS)
codepage = NULL;
#elif defined(OSX)
codepage = "UTF-8";
result = j9sysinfo_get_env("LC_ALL", langProp, sizeof(langProp));
if (result >= 0) {
if ((0 == result) && (0 == strcmp(langProp, "C"))) {
codepage = "US-ASCII";
}
} else {
result = j9sysinfo_get_env("LC_CTYPE", langProp, sizeof(langProp));
if (result >= 0) {
if ((0 == result) && (0 == strcmp(langProp, "C"))) {
codepage = "US-ASCII";
}
} else {
result = j9sysinfo_get_env("LANG", langProp, sizeof(langProp));
if ((0 == result) && (0 == strcmp(langProp, "C"))) {
codepage = "US-ASCII";
}
}
}
return codepage;
#else
codepage = nl_langinfo(CODESET);
#endif
if (codepage == NULL || codepage[0] == '\0') {
codepage = "ISO8859_1";
} else {
if (!strcmp(codepage, "EUC-JP")) {
codepage = "EUC-JP-LINUX";
} else {
strncpy(codepageProp, codepage, propSize);
codepageProp[propSize - 1] = '\0';
codepage = codepageProp;
nameIndex = 0;
for (i = 1; i < sizeof(CodepageTable) / sizeof(char *); i++) {
if (CodepageTable[i] == NULL) {
nameIndex = ++i;
continue;
}
if (!strcmp(CodepageTable[i], codepageProp)) {
codepage = CodepageTable[nameIndex];
break;
}
}
}
}
return codepage;
}