Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/java/lang/ProcessEnvironment_md.c
32287 views
/*1* Copyright (c) 2003, 2014, 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);41String_init_ID =42(*env)->GetMethodID(env, string_class, "<init>", "([B)V");43CHECK_NULL_RETURN(String_init_ID, NULL);44blockA = (jbyte *) GetEnvironmentStringsA();45if (blockA == NULL) {46/* Both GetEnvironmentStringsW and GetEnvironmentStringsA47* failed. Out of memory is our best guess. */48JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed");49return NULL;50}5152/* Don't search for "\0\0", since an empty environment block may53legitimately consist of a single "\0". */54for (i = 0; blockA[i];)55while (blockA[i++])56;5758if ((bytes = (*env)->NewByteArray(env, i)) == NULL) {59FreeEnvironmentStringsA(blockA);60return NULL;61}62(*env)->SetByteArrayRegion(env, bytes, 0, i, blockA);63FreeEnvironmentStringsA(blockA);64return (*env)->NewObject(env, string_class,65String_init_ID, bytes);66}6768/* Returns a Windows style environment block, discarding final trailing NUL */69JNIEXPORT jstring JNICALL70Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass)71{72int i;73jstring envblock;74jchar *blockW = (jchar *) GetEnvironmentStringsW();75if (blockW == NULL)76return environmentBlock9x(env);7778/* Don't search for "\u0000\u0000", since an empty environment79block may legitimately consist of a single "\u0000". */80for (i = 0; blockW[i];)81while (blockW[i++])82;8384envblock = (*env)->NewString(env, blockW, i);85FreeEnvironmentStringsW(blockW);86return envblock;87}888990