Path: blob/master/src/java.base/windows/native/libjava/ProcessEnvironment_md.c
41119 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include <stdlib.h>2526#include "jni.h"27#include "jni_util.h"28#include <windows.h>2930static jstring31environmentBlock9x(JNIEnv *env)32{33int i;34jmethodID String_init_ID;35jbyteArray bytes;36jbyte *blockA;37jclass string_class;3839string_class = JNU_ClassString(env);40CHECK_NULL_RETURN(string_class, NULL);4142String_init_ID =43(*env)->GetMethodID(env, string_class, "<init>", "([B)V");44CHECK_NULL_RETURN(String_init_ID, NULL);4546blockA = (jbyte *) GetEnvironmentStringsA();47if (blockA == NULL) {48/* Both GetEnvironmentStringsW and GetEnvironmentStringsA49* failed. Out of memory is our best guess. */50JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed");51return NULL;52}5354/* Don't search for "\0\0", since an empty environment block may55legitimately consist of a single "\0". */56for (i = 0; blockA[i];)57while (blockA[i++])58;5960if ((bytes = (*env)->NewByteArray(env, i)) == NULL) {61FreeEnvironmentStringsA(blockA);62return NULL;63}64(*env)->SetByteArrayRegion(env, bytes, 0, i, blockA);65FreeEnvironmentStringsA(blockA);66return (*env)->NewObject(env, string_class,67String_init_ID, bytes);68}6970/* Returns a Windows style environment block, discarding final trailing NUL */71JNIEXPORT jstring JNICALL72Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass)73{74int i;75jstring envblock;76jchar *blockW = (jchar *) GetEnvironmentStringsW();77if (blockW == NULL)78return environmentBlock9x(env);7980/* Don't search for "\u0000\u0000", since an empty environment81block may legitimately consist of a single "\u0000". */82for (i = 0; blockW[i];)83while (blockW[i++])84;8586envblock = (*env)->NewString(env, blockW, i);87FreeEnvironmentStringsW(blockW);88return envblock;89}909192