1 /* 2 * Copyright (c) 2003, 2014 Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * 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 jbyteArray bytes; 37 jbyte *blockA; 38 jclass string_class; 39 40 string_class= JNU_ClassString(env); 41 CHECK_NULL_RETURN(string_class, NULL); 42 String_init_ID = 43 (*env)->GetMethodID(env, string_class, "<init>", "([B)V"); 44 CHECK_NULL_RETURN(String_init_ID); 45 blockA = (jbyte *) GetEnvironmentStringsA(); 46 if (blockA == NULL) { 47 /* Both GetEnvironmentStringsW and GetEnvironmentStringsA 48 * failed. Out of memory is our best guess. */ 49 JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed"); 50 return NULL; 51 } 52 53 /* Don't search for "\0\0", since an empty environment block may 54 legitimately consist of a single "\0". */ 55 for (i = 0; blockA[i];) 56 while (blockA[i++]) 57 ; 58 59 if ((bytes = (*env)->NewByteArray(env, i)) == NULL) { 60 FreeEnvironmentStringsA(blockA); 61 return NULL; 62 } 63 (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA); 64 FreeEnvironmentStringsA(blockA); 65 return (*env)->NewObject(env, string_class, 66 String_init_ID, bytes); 67 } 68 69 /* Returns a Windows style environment block, discarding final trailing NUL */ 70 JNIEXPORT jstring JNICALL 71 Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass) 72 { 73 int i; 74 jstring envblock; 75 jchar *blockW = (jchar *) GetEnvironmentStringsW(); 76 if (blockW == NULL) 77 return environmentBlock9x(env); 78 79 /* Don't search for "\u0000\u0000", since an empty environment 80 block may legitimately consist of a single "\u0000". */ 81 for (i = 0; blockW[i];) 82 while (blockW[i++]) 83 ; 84 85 envblock = (*env)->NewString(env, blockW, i); 86 FreeEnvironmentStringsW(blockW); 87 return envblock; 88 }