src/java.base/windows/native/libjava/ProcessEnvironment_md.c
author igerasim
Wed, 21 Aug 2019 13:49:16 -0700
changeset 57826 bf4c808a4488
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211360: Change #if DEF to #if defined(DEF) Reviewed-by: bpb, vtewari
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, 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
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
static jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
environmentBlock9x(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    int i;
22641
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    35
    jmethodID String_init_ID;
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    36
    jbyteArray bytes;
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    37
    jbyte *blockA;
23556
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    38
    jclass string_class;
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    39
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    40
    string_class = JNU_ClassString(env);
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    41
    CHECK_NULL_RETURN(string_class, NULL);
22641
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    42
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    43
    String_init_ID =
23556
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    44
        (*env)->GetMethodID(env, string_class, "<init>", "([B)V");
22641
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    45
    CHECK_NULL_RETURN(String_init_ID, NULL);
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    46
e47f8892133e 8033372: Check jdk/src/share/native/java/lang for JNI pending exception issues
alanb
parents: 5506
diff changeset
    47
    blockA = (jbyte *) GetEnvironmentStringsA();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    if (blockA == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        /* Both GetEnvironmentStringsW and GetEnvironmentStringsA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
         * failed.  Out of memory is our best guess.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* Don't search for "\0\0", since an empty environment block may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
       legitimately consist of a single "\0". */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    for (i = 0; blockA[i];)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        while (blockA[i++])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
23556
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    61
    if ((bytes = (*env)->NewByteArray(env, i)) == NULL) {
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    62
        FreeEnvironmentStringsA(blockA);
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    63
        return NULL;
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    64
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    FreeEnvironmentStringsA(blockA);
23556
1726e34c34e8 8036603: Check jdk/src/windows/native/java/lang/ProcessEnvironment_md.c for JNI pending exceptions
msheppar
parents: 22641
diff changeset
    67
    return (*env)->NewObject(env, string_class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                             String_init_ID, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
/* Returns a Windows style environment block, discarding final trailing NUL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jstring envblock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jchar *blockW = (jchar *) GetEnvironmentStringsW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (blockW == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return environmentBlock9x(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* Don't search for "\u0000\u0000", since an empty environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
       block may legitimately consist of a single "\u0000".  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    for (i = 0; blockW[i];)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        while (blockW[i++])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    envblock = (*env)->NewString(env, blockW, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    FreeEnvironmentStringsW(blockW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    return envblock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}