1/* 2 * Copyright (c) 2003, 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 31static jstring 32environmentBlock9x(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 43 String_init_ID = 44 (*env)->GetMethodID(env, string_class, "<init>", "([B)V"); 45 CHECK_NULL_RETURN(String_init_ID, NULL); 46 47 blockA = (jbyte *) GetEnvironmentStringsA(); 48 if (blockA == NULL) { 49 /* Both GetEnvironmentStringsW and GetEnvironmentStringsA 50 * failed. Out of memory is our best guess. */ 51 JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed"); 52 return NULL; 53 } 54 55 /* Don't search for "\0\0", since an empty environment block may 56 legitimately consist of a single "\0". */ 57 for (i = 0; blockA[i];) 58 while (blockA[i++]) 59 ; 60 61 if ((bytes = (*env)->NewByteArray(env, i)) == NULL) { 62 FreeEnvironmentStringsA(blockA); 63 return NULL; 64 } 65 (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA); 66 FreeEnvironmentStringsA(blockA); 67 return (*env)->NewObject(env, string_class, 68 String_init_ID, bytes); 69} 70 71/* Returns a Windows style environment block, discarding final trailing NUL */ 72JNIEXPORT jstring JNICALL 73Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass) 74{ 75 int i; 76 jstring envblock; 77 jchar *blockW = (jchar *) GetEnvironmentStringsW(); 78 if (blockW == NULL) 79 return environmentBlock9x(env); 80 81 /* Don't search for "\u0000\u0000", since an empty environment 82 block may legitimately consist of a single "\u0000". */ 83 for (i = 0; blockW[i];) 84 while (blockW[i++]) 85 ; 86 87 envblock = (*env)->NewString(env, blockW, i); 88 FreeEnvironmentStringsW(blockW); 89 return envblock; 90} 91