The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 "JNIHelp.h" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * public static native String getEnvByName(String name) |
| 25 | * |
| 26 | * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint |
| 27 | * on the libc version.) |
| 28 | */ |
| 29 | static jstring java_getEnvByName(JNIEnv* env, jclass clazz, jstring nameStr) |
| 30 | { |
| 31 | jstring valueStr = NULL; |
| 32 | |
| 33 | if (nameStr != NULL) { |
| 34 | const char* name; |
| 35 | const char* val; |
| 36 | |
| 37 | name = (*env)->GetStringUTFChars(env, nameStr, NULL); |
| 38 | val = getenv(name); |
| 39 | if (val != NULL) |
| 40 | valueStr = (*env)->NewStringUTF(env, val); |
| 41 | |
| 42 | (*env)->ReleaseStringUTFChars(env, nameStr, name); |
| 43 | } else { |
| 44 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 45 | } |
| 46 | |
| 47 | return valueStr; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Pointer to complete environment, from Posix. |
| 52 | */ |
| 53 | extern char** environ; |
| 54 | |
| 55 | /* |
| 56 | * public static native String getEnvByIndex() |
| 57 | * |
| 58 | * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint |
| 59 | * on the libc version.) |
| 60 | */ |
| 61 | static jstring java_getEnvByIndex(JNIEnv* env, jclass clazz, jint index) |
| 62 | { |
| 63 | jstring valueStr = NULL; |
| 64 | |
| 65 | char* entry = environ[index]; |
| 66 | if (entry != NULL) { |
| 67 | valueStr = (*env)->NewStringUTF(env, entry); |
| 68 | } |
| 69 | |
| 70 | return valueStr; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * public static native String setFieldImpl() |
| 75 | * |
| 76 | * Sets a field via JNI. Used for the standard streams, which are r/o |
| 77 | * otherwise. |
| 78 | */ |
| 79 | static void java_setFieldImpl(JNIEnv* env, jclass clazz, jstring name, jstring sig, jobject object) |
| 80 | { |
| 81 | const char* fieldName = (*env)->GetStringUTFChars(env, name, NULL); |
| 82 | const char* fieldSig = (*env)->GetStringUTFChars(env, sig, NULL); |
| 83 | |
| 84 | jfieldID fieldID = (*env)->GetStaticFieldID(env, clazz, fieldName, fieldSig); |
| 85 | (*env)->SetStaticObjectField(env, clazz, fieldID, object); |
| 86 | |
| 87 | (*env)->ReleaseStringUTFChars(env, name, fieldName); |
| 88 | (*env)->ReleaseStringUTFChars(env, sig, fieldSig); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * JNI registration |
| 93 | */ |
| 94 | static JNINativeMethod gMethods[] = { |
| 95 | /* name, signature, funcPtr */ |
| 96 | { "getEnvByName", "(Ljava/lang/String;)Ljava/lang/String;", java_getEnvByName }, |
| 97 | { "getEnvByIndex", "(I)Ljava/lang/String;", java_getEnvByIndex }, |
| 98 | { "setFieldImpl", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V", java_setFieldImpl }, |
| 99 | }; |
| 100 | |
| 101 | int register_java_lang_System(JNIEnv* env) |
| 102 | { |
| 103 | return jniRegisterNativeMethods(env, "java/lang/System", |
| 104 | gMethods, NELEM(gMethods)); |
| 105 | } |
| 106 | |