0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A
4632N/A#ifdef __APPLE__
4632N/A#include <crt_externs.h>
4632N/A#define environ (*_NSGetEnviron())
4632N/A#endif
4632N/A
0N/AJNIEXPORT jobjectArray JNICALL
0N/AJava_java_lang_ProcessEnvironment_environ(JNIEnv *env, jclass ign)
0N/A{
0N/A /* This is one of the rare times it's more portable to declare an
0N/A * external symbol explicitly, rather than via a system header.
0N/A * The declaration is standardized as part of UNIX98, but there is
0N/A * no standard (not even de-facto) header file where the
0N/A * declaration is to be found. See:
0N/A * http://www.opengroup.org/onlinepubs/007908799/xbd/envvar.html */
4632N/A#ifndef __APPLE__
0N/A extern char ** environ; /* environ[i] looks like: VAR=VALUE\0 */
4632N/A#endif
0N/A
0N/A jsize count = 0;
0N/A jsize i, j;
0N/A jobjectArray result;
0N/A jclass byteArrCls = (*env)->FindClass(env, "[B");
0N/A
0N/A for (i = 0; environ[i]; i++) {
0N/A /* Ignore corrupted environment variables */
0N/A if (strchr(environ[i], '=') != NULL)
0N/A count++;
0N/A }
0N/A
0N/A result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);
0N/A if (result == NULL) return NULL;
0N/A
0N/A for (i = 0, j = 0; environ[i]; i++) {
0N/A const char * varEnd = strchr(environ[i], '=');
0N/A /* Ignore corrupted environment variables */
0N/A if (varEnd != NULL) {
0N/A jbyteArray var, val;
0N/A const char * valBeg = varEnd + 1;
0N/A jsize varLength = varEnd - environ[i];
0N/A jsize valLength = strlen(valBeg);
0N/A var = (*env)->NewByteArray(env, varLength);
0N/A if (var == NULL) return NULL;
0N/A val = (*env)->NewByteArray(env, valLength);
0N/A if (val == NULL) return NULL;
0N/A (*env)->SetByteArrayRegion(env, var, 0, varLength,
0N/A (jbyte*) environ[i]);
0N/A (*env)->SetByteArrayRegion(env, val, 0, valLength,
0N/A (jbyte*) valBeg);
0N/A (*env)->SetObjectArrayElement(env, result, 2*j , var);
0N/A (*env)->SetObjectArrayElement(env, result, 2*j+1, val);
0N/A (*env)->DeleteLocalRef(env, var);
0N/A (*env)->DeleteLocalRef(env, val);
0N/A j++;
0N/A }
0N/A }
0N/A
0N/A return result;
0N/A}