Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/unix/syshelp.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 1998, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#ifndef ARM_EMULATED
24
#include <unistd.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#endif
28
#if !defined(CHORUS) && !defined(ARM_EMULATED)
29
#include <utime.h>
30
#endif
31
#include <stdlib.h>
32
#include <string.h>
33
#ifndef CHORUS
34
#include <time.h>
35
#include <locale.h>
36
#endif
37
#if !defined(ARM_EMULATED) && !defined(CHORUS)
38
#include <langinfo.h>
39
#endif
40
#ifdef ARM_EMULATED
41
#define P_tmpdir "c:\\temp"
42
#endif
43
44
#if !defined(CHORUS)
45
/* If pwd.h is not supported, do not define J9PWENT */
46
#define J9PWENT
47
#include <pwd.h>
48
#endif
49
50
#include <dirent.h>
51
52
#include "jcl.h"
53
#include "jclprots.h"
54
#include "jclglob.h"
55
#include "j2sever.h"
56
57
58
59
#if defined(J9ZOS390)
60
#include "atoe.h"
61
#endif
62
63
64
/* JCL_J2SE */
65
#define JCL_J2SE
66
67
68
69
/* defineCodepageTable */
70
/* NULL separated list of code page aliases. The first name is */
71
/* the name of the System property, the names following before */
72
/* the NULL are the aliases. */
73
char* CodepageTable[] = {
74
"ISO8859_1",
75
/*[PR 96285]*/
76
NULL,
77
#if defined(AIXPPC)
78
/*[PR105613]*/
79
"IBM-943C",
80
"IBM-943",
81
NULL,
82
"IBM-950",
83
"big5",
84
NULL
85
#endif
86
};
87
88
89
/**
90
* Try to find the 'correct' unix temp directory, as taken from the man page for tmpnam.
91
* As a last resort, the '.' representing the current directory is returned.
92
*/
93
char * getTmpDir(JNIEnv *env, char**envSpace) {
94
PORT_ACCESS_FROM_ENV(env);
95
I_32 envSize;
96
if ((envSize = j9sysinfo_get_env("TMPDIR", NULL, 0))> 0) {
97
*envSpace = jclmem_allocate_memory(env,envSize); /* trailing null taken into account */
98
if(*envSpace==NULL) return ".";
99
j9sysinfo_get_env("TMPDIR", *envSpace, envSize);
100
if (j9file_attr(*envSpace) > -1)
101
return *envSpace;
102
/* directory was not there, free up memory and continue */
103
jclmem_free_memory(env,*envSpace);
104
*envSpace = NULL;
105
}
106
if (j9file_attr(P_tmpdir) > -1)
107
return P_tmpdir;
108
if (j9file_attr("/tmp") > -1)
109
return "/tmp";
110
return ".";
111
}
112
113
114
jobject getPlatformPropertyList(JNIEnv * env, const char *strings[], int propIndex)
115
{
116
PORT_ACCESS_FROM_ENV(env);
117
I_32 result = 0;
118
char *charResult = NULL, *envSpace = NULL;
119
jobject plist;
120
char userdir[EsMaxPath];
121
char home[EsMaxPath], *homeAlloc = NULL;
122
#if defined(J9PWENT)
123
struct passwd *pwentry = NULL;
124
#endif
125
126
/* Hard coded file/path separators and other values */
127
128
#if defined(J9ZOS390)
129
if (J2SE_VERSION_FROM_ENV(env)) {
130
strings[propIndex++] = "platform.notASCII";
131
strings[propIndex++] = "true";
132
133
strings[propIndex++] = "os.encoding";
134
strings[propIndex++] = "ISO8859_1";
135
}
136
#endif
137
138
strings[propIndex++] = "file.separator";
139
strings[propIndex++] = "/";
140
141
strings[propIndex++] = "line.separator";
142
strings[propIndex++] = "\n";
143
144
/* Get the directory where the executable was started */
145
strings[propIndex++] = "user.dir";
146
#ifndef ARM_EMULATED
147
charResult = getcwd(userdir, EsMaxPath);
148
#else
149
charResult = NULL;
150
#endif
151
if (charResult == NULL)
152
strings[propIndex++] = ".";
153
else
154
strings[propIndex++] = charResult;
155
156
strings[propIndex++] = "user.home";
157
charResult = NULL;
158
#if defined(J9ZOS390)
159
charResult = getenv("HOME");
160
if (charResult != NULL) {
161
strings[propIndex++] = charResult;
162
} else {
163
uid_t uid = geteuid();
164
if (uid != 0) {
165
struct passwd *userDescription = getpwuid(uid);
166
if (NULL != userDescription) {
167
charResult = userDescription->pw_dir;
168
strings[propIndex++] = charResult;
169
}
170
} else {
171
char *loginID = getlogin();
172
if (NULL != loginID) {
173
struct passwd *userDescription = getpwnam(loginID);
174
if (NULL != userDescription) {
175
charResult = userDescription->pw_dir;
176
strings[propIndex++] = charResult;
177
}
178
}
179
}
180
}
181
182
/* there exist situations where one of the above calls will fail. Fall through to the Unix solution for those cases */
183
#endif
184
#if defined(J9PWENT)
185
/*[PR 101939] user.home not set correctly when j9 invoked via execve(x,y,null) */
186
if (charResult == NULL){
187
pwentry = getpwuid(getuid());
188
if (pwentry) {
189
charResult = pwentry->pw_dir;
190
strings[propIndex++] = charResult;
191
}
192
}
193
#endif
194
195
if (charResult == NULL) {
196
result = j9sysinfo_get_env("HOME", home, sizeof(home));
197
if (!result && strlen(home) > 0) {
198
strings[propIndex++] = home;
199
} else {
200
if (result != -1) {
201
homeAlloc = j9mem_allocate_memory(result, J9MEM_CATEGORY_VM_JCL);
202
}
203
if (homeAlloc) {
204
result = j9sysinfo_get_env("HOME", homeAlloc, result);
205
}
206
if (homeAlloc && !result) {
207
strings[propIndex++] = homeAlloc;
208
} else {
209
strings[propIndex++] = ".";
210
}
211
}
212
}
213
214
/* Get the Temp Dir name */
215
strings[propIndex++] = "java.io.tmpdir";
216
strings[propIndex++] = getTmpDir(env, &envSpace);
217
218
if (JAVA_SPEC_VERSION < 12) {
219
/* Get the timezone */
220
strings[propIndex++] = "user.timezone";
221
strings[propIndex++] = "";
222
}
223
224
plist = createSystemPropertyList(env, strings, propIndex);
225
if (envSpace)
226
jclmem_free_memory(env,envSpace);
227
if (homeAlloc) jclmem_free_memory(env, homeAlloc);
228
return plist;
229
}
230
231
#undef J9PWENT
232
233
234
/**
235
* Turns a platform independent DLL name into a platform specific one.
236
*/
237
void mapLibraryToPlatformName(const char *inPath, char *outPath) {
238
#ifdef J9OS_I5
239
strcpy(outPath,inPath);
240
strcat(outPath, ".srvpgm");
241
#else
242
strcpy(outPath, "lib");
243
strcat(outPath,inPath);
244
#if defined(AIXPPC) && (JAVA_SPEC_VERSION <= 14)
245
strcat(outPath, ".a");
246
#else /* defined(AIXPPC) && (JAVA_SPEC_VERSION == 8) */
247
strcat(outPath, J9PORT_LIBRARY_SUFFIX);
248
#endif /* defined(AIXPPC) && (JAVA_SPEC_VERSION == 8) */
249
#endif
250
}
251
252
253
char *getPlatformFileEncoding(JNIEnv * env, char *codepageProp, int propSize, int encodingType)
254
{
255
#if defined(J9ZTPF)
256
return "ISO8859_1";
257
#endif /* defined(J9ZTPF) */
258
char *codepage = NULL;
259
int i = 0;
260
int nameIndex = 0;
261
#if defined(LINUX) || defined(OSX)
262
IDATA result = 0;
263
char langProp[24] = {0};
264
#if defined(LINUX)
265
char *ctype = NULL;
266
#endif /* defined(LINUX) */
267
PORT_ACCESS_FROM_ENV(env);
268
#endif /* defined(LINUX) || defined(OSX) */
269
270
/* Called with codepageProp == NULL to initialize the locale */
271
if (NULL == codepageProp) {
272
return NULL;
273
}
274
275
#if defined(LINUX)
276
/*[PR 104520] Return EUC_JP when LC_CTYPE is not set, and the LANG environment variable is "ja" */
277
ctype = setlocale(LC_CTYPE, NULL);
278
if ((NULL == ctype)
279
|| (0 == strcmp(ctype, "C"))
280
|| (0 == strcmp(ctype, "POSIX"))
281
) {
282
result = j9sysinfo_get_env("LANG", langProp, sizeof(langProp));
283
if ((0 == result) && (0 == strcmp(langProp, "ja"))) {
284
return "EUC-JP-LINUX";
285
}
286
}
287
#ifndef __ANDROID__
288
codepage = nl_langinfo(_NL_CTYPE_CODESET_NAME);
289
#else
290
codepage = "UTF-8";
291
#endif
292
#elif defined(ARM_EMULATED) || defined(CHORUS)
293
codepage = NULL;
294
#elif defined(OSX)
295
/* LC_ALL overwrites LC_CTYPE or LANG;
296
* LC_CTYPE applies to classification and conversion of characters, and to multibyte and wide characters;
297
* LANG specifies the locale to use except as overridden by LC_CTYPE
298
*/
299
codepage = "UTF-8";
300
result = j9sysinfo_get_env("LC_ALL", langProp, sizeof(langProp));
301
if (result >= 0) {
302
if ((0 == result) && (0 == strcmp(langProp, "C"))) {
303
/* LC_ALL is C */
304
codepage = "US-ASCII";
305
}
306
} else {
307
/* LC_ALL is not set */
308
result = j9sysinfo_get_env("LC_CTYPE", langProp, sizeof(langProp));
309
if (result >= 0) {
310
if ((0 == result) && (0 == strcmp(langProp, "C"))) {
311
/* LC_CTYPE is C */
312
codepage = "US-ASCII";
313
}
314
} else {
315
/* LC_CTYPE is not set */
316
result = j9sysinfo_get_env("LANG", langProp, sizeof(langProp));
317
if ((0 == result) && (0 == strcmp(langProp, "C"))) {
318
/* LANG is C */
319
codepage = "US-ASCII";
320
}
321
}
322
}
323
return codepage;
324
#else
325
codepage = nl_langinfo(CODESET);
326
#endif /* defined(LINUX) */
327
328
if (codepage == NULL || codepage[0] == '\0') {
329
codepage = "ISO8859_1";
330
} else {
331
if (!strcmp(codepage, "EUC-JP")) {
332
/*[PR CMVC 175994] file.encoding system property on ja_JP.eucjp */
333
codepage = "EUC-JP-LINUX";
334
} else {
335
strncpy(codepageProp, codepage, propSize);
336
codepageProp[propSize - 1] = '\0';
337
/*[PR 96285]*/
338
codepage = codepageProp;
339
nameIndex = 0;
340
for (i = 1; i < sizeof(CodepageTable) / sizeof(char *); i++) {
341
if (CodepageTable[i] == NULL) {
342
nameIndex = ++i;
343
continue;
344
}
345
if (!strcmp(CodepageTable[i], codepageProp)) {
346
codepage = CodepageTable[nameIndex];
347
break;
348
}
349
}
350
}
351
}
352
return codepage;
353
}
354
355