jdk/src/solaris/native/java/lang/ProcessEnvironment_md.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 12047 320a714614e9
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
Java_java_lang_ProcessEnvironment_environ(JNIEnv *env, jclass ign)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    /* This is one of the rare times it's more portable to declare an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * external symbol explicitly, rather than via a system header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * The declaration is standardized as part of UNIX98, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * no standard (not even de-facto) header file where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * declaration is to be found.  See:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * http://www.opengroup.org/onlinepubs/007908799/xbd/envvar.html */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extern char ** environ; /* environ[i] looks like: VAR=VALUE\0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    jsize count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    jsize i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    jobjectArray result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jclass byteArrCls = (*env)->FindClass(env, "[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    for (i = 0; environ[i]; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        /* Ignore corrupted environment variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (strchr(environ[i], '=') != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if (result == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    for (i = 0, j = 0; environ[i]; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        const char * varEnd = strchr(environ[i], '=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        /* Ignore corrupted environment variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (varEnd != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            jbyteArray var, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            const char * valBeg = varEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            jsize varLength = varEnd - environ[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            jsize valLength = strlen(valBeg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            var = (*env)->NewByteArray(env, varLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (var == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            val = (*env)->NewByteArray(env, valLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (val == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            (*env)->SetByteArrayRegion(env, var, 0, varLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                       (jbyte*) environ[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            (*env)->SetByteArrayRegion(env, val, 0, valLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                       (jbyte*) valBeg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            (*env)->SetObjectArrayElement(env, result, 2*j  , var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            (*env)->SetObjectArrayElement(env, result, 2*j+1, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            (*env)->DeleteLocalRef(env, var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            (*env)->DeleteLocalRef(env, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}