J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include "jni.h" |
| 28 | #include "jni_util.h" |
| 29 | #include <windows.h> |
| 30 | |
| 31 | static jstring |
| 32 | environmentBlock9x(JNIEnv *env) |
| 33 | { |
| 34 | int i; |
| 35 | jmethodID String_init_ID = |
| 36 | (*env)->GetMethodID(env, JNU_ClassString(env), "<init>", "([B)V"); |
| 37 | jbyteArray bytes; |
| 38 | jbyte *blockA = (jbyte *) GetEnvironmentStringsA(); |
| 39 | if (blockA == NULL) { |
| 40 | /* Both GetEnvironmentStringsW and GetEnvironmentStringsA |
| 41 | * failed. Out of memory is our best guess. */ |
| 42 | JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed"); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | /* Don't search for "\0\0", since an empty environment block may |
| 47 | legitimately consist of a single "\0". */ |
| 48 | for (i = 0; blockA[i];) |
| 49 | while (blockA[i++]) |
| 50 | ; |
| 51 | |
| 52 | if ((bytes = (*env)->NewByteArray(env, i)) == NULL) return NULL; |
| 53 | (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA); |
| 54 | FreeEnvironmentStringsA(blockA); |
| 55 | return (*env)->NewObject(env, JNU_ClassString(env), |
| 56 | String_init_ID, bytes); |
| 57 | } |
| 58 | |
| 59 | /* Returns a Windows style environment block, discarding final trailing NUL */ |
| 60 | JNIEXPORT jstring JNICALL |
| 61 | Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass) |
| 62 | { |
| 63 | int i; |
| 64 | jstring envblock; |
| 65 | jchar *blockW = (jchar *) GetEnvironmentStringsW(); |
| 66 | if (blockW == NULL) |
| 67 | return environmentBlock9x(env); |
| 68 | |
| 69 | /* Don't search for "\u0000\u0000", since an empty environment |
| 70 | block may legitimately consist of a single "\u0000". */ |
| 71 | for (i = 0; blockW[i];) |
| 72 | while (blockW[i++]) |
| 73 | ; |
| 74 | |
| 75 | envblock = (*env)->NewString(env, blockW, i); |
| 76 | FreeEnvironmentStringsW(blockW); |
| 77 | return envblock; |
| 78 | } |