Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/lang/ProcessEnvironment_md.c
32287 views
/*1* Copyright (c) 2003, 2013, 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*/2425#include <stdlib.h>26#include <string.h>27#include "jni.h"28#include "jni_util.h"2930#ifdef __APPLE__31#include <crt_externs.h>32#define environ (*_NSGetEnviron())33#else34/* This is one of the rare times it's more portable to declare an35* external symbol explicitly, rather than via a system header.36* The declaration is standardized as part of UNIX98, but there is37* no standard (not even de-facto) header file where the38* declaration is to be found. See:39* http://www.opengroup.org/onlinepubs/009695399/functions/environ.html40* http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html41*42* "All identifiers in this volume of IEEE Std 1003.1-2001, except43* environ, are defined in at least one of the headers" (!)44*/45extern char **environ;46#endif4748JNIEXPORT jobjectArray JNICALL49Java_java_lang_ProcessEnvironment_environ(JNIEnv *env, jclass ign)50{51jsize count = 0;52jsize i, j;53jobjectArray result;54jclass byteArrCls = (*env)->FindClass(env, "[B");55CHECK_NULL_RETURN(byteArrCls, NULL);5657for (i = 0; environ[i]; i++) {58/* Ignore corrupted environment variables */59if (strchr(environ[i], '=') != NULL)60count++;61}6263result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);64CHECK_NULL_RETURN(result, NULL);6566for (i = 0, j = 0; environ[i]; i++) {67const char * varEnd = strchr(environ[i], '=');68/* Ignore corrupted environment variables */69if (varEnd != NULL) {70jbyteArray var, val;71const char * valBeg = varEnd + 1;72jsize varLength = varEnd - environ[i];73jsize valLength = strlen(valBeg);74var = (*env)->NewByteArray(env, varLength);75CHECK_NULL_RETURN(var, NULL);76val = (*env)->NewByteArray(env, valLength);77CHECK_NULL_RETURN(val, NULL);78(*env)->SetByteArrayRegion(env, var, 0, varLength,79(jbyte*) environ[i]);80(*env)->SetByteArrayRegion(env, val, 0, valLength,81(jbyte*) valBeg);82(*env)->SetObjectArrayElement(env, result, 2*j , var);83(*env)->SetObjectArrayElement(env, result, 2*j+1, val);84(*env)->DeleteLocalRef(env, var);85(*env)->DeleteLocalRef(env, val);86j++;87}88}8990return result;91}929394