blob: d02481c8b56285a3bdee861db422e13d7825f9cc [file] [log] [blame]
Elliott Hughes94367e02009-10-23 18:10:44 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "AndroidSystemNatives.h"
18#include "JNIHelp.h"
19
20#include <stdlib.h>
21#include <string.h>
22
23
24/*
25 * public static native String getEnvByName(String name)
26 *
27 * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint
28 * on the libc version.)
29 */
30static jstring java_getEnvByName(JNIEnv* env, jclass, jstring nameStr) {
31 jstring valueStr = NULL;
32
33 if (nameStr != NULL) {
34 const char* name = env->GetStringUTFChars(nameStr, NULL);
35 const char* val = getenv(name);
36 if (val != NULL) {
37 valueStr = env->NewStringUTF(val);
38 }
39 env->ReleaseStringUTFChars(nameStr, name);
40 } else {
41 jniThrowException(env, "java/lang/NullPointerException", NULL);
42 }
43
44 return valueStr;
45}
46
47/*
48 * Pointer to complete environment, from Posix.
49 */
50extern char** environ;
51
52/*
53 * public static native String getEnvByIndex()
54 *
55 * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint
56 * on the libc version.)
57 */
58static jstring java_getEnvByIndex(JNIEnv* env, jclass, jint index) {
59 jstring valueStr = NULL;
60
61 char* entry = environ[index];
62 if (entry != NULL) {
63 valueStr = env->NewStringUTF(entry);
64 }
65
66 return valueStr;
67}
68
69/*
70 * public static native String setFieldImpl()
71 *
72 * Sets a field via JNI. Used for the standard streams, which are r/o
73 * otherwise.
74 */
75static void java_setFieldImpl(JNIEnv* env, jclass clazz,
76 jstring name, jstring sig, jobject object) {
77 const char* fieldName = env->GetStringUTFChars(name, NULL);
78 const char* fieldSig = env->GetStringUTFChars(sig, NULL);
79
80 jfieldID fieldID = env->GetStaticFieldID(clazz, fieldName, fieldSig);
81 env->SetStaticObjectField(clazz, fieldID, object);
82
83 env->ReleaseStringUTFChars(name, fieldName);
84 env->ReleaseStringUTFChars(sig, fieldSig);
85}
86
87/*
88 * JNI registration
89 */
90static JNINativeMethod gMethods[] = {
91 /* name, signature, funcPtr */
92 { "getEnvByName", "(Ljava/lang/String;)Ljava/lang/String;", (void*) java_getEnvByName },
93 { "getEnvByIndex", "(I)Ljava/lang/String;", (void*) java_getEnvByIndex },
94 { "setFieldImpl", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V", (void*) java_setFieldImpl },
95};
96
97int register_java_lang_System(JNIEnv* env) {
98 return jniRegisterNativeMethods(env, "java/lang/System",
99 gMethods, NELEM(gMethods));
100}