src/windows/native/java/lang/ProcessEnvironment_md.c

Print this page


   1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #include <stdlib.h>
  26 
  27 #include "jni.h"
  28 #include "jni_util.h"
  29 #include <windows.h>
  30 
  31 static jstring
  32 environmentBlock9x(JNIEnv *env)
  33 {
  34     int i;
  35     jmethodID String_init_ID =
  36         (*env)->GetMethodID(env, JNU_ClassString(env), "<init>", "([B)V");
  37     jbyteArray bytes;
  38     jbyte *blockA = (jbyte *) GetEnvironmentStringsA();








  39     if (blockA == NULL) {
  40         /* Both GetEnvironmentStringsW and GetEnvironmentStringsA
  41          * failed.  Out of memory is our best guess.  */
  42         JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed");
  43         return NULL;
  44     }
  45 
  46     /* Don't search for "\0\0", since an empty environment block may
  47        legitimately consist of a single "\0". */
  48     for (i = 0; blockA[i];)
  49         while (blockA[i++])
  50             ;
  51 
  52     if ((bytes = (*env)->NewByteArray(env, i)) == NULL) return NULL;



  53     (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA);
  54     FreeEnvironmentStringsA(blockA);
  55     return (*env)->NewObject(env, JNU_ClassString(env),
  56                              String_init_ID, bytes);
  57 }
  58 
  59 /* Returns a Windows style environment block, discarding final trailing NUL */
  60 JNIEXPORT jstring JNICALL
  61 Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass)
  62 {
  63     int i;
  64     jstring envblock;
  65     jchar *blockW = (jchar *) GetEnvironmentStringsW();
  66     if (blockW == NULL)
  67         return environmentBlock9x(env);
  68 
  69     /* Don't search for "\u0000\u0000", since an empty environment
  70        block may legitimately consist of a single "\u0000".  */
  71     for (i = 0; blockW[i];)
  72         while (blockW[i++])
  73             ;
  74 
  75     envblock = (*env)->NewString(env, blockW, i);
   1 /*
   2  * Copyright (c) 2003, 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #include <stdlib.h>
  26 
  27 #include "jni.h"
  28 #include "jni_util.h"
  29 #include <windows.h>
  30 
  31 static jstring
  32 environmentBlock9x(JNIEnv *env)
  33 {
  34     int i;
  35     jmethodID String_init_ID;

  36     jbyteArray bytes;
  37     jbyte *blockA;
  38     jclass string_class;
  39 
  40     string_class= JNU_ClassString(env);
  41     CHECK_NULL_RETURN(string_class, NULL);
  42     String_init_ID =
  43         (*env)->GetMethodID(env, string_class, "<init>", "([B)V");
  44     CHECK_NULL_RETURN(String_init_ID);
  45     blockA = (jbyte *) GetEnvironmentStringsA();
  46     if (blockA == NULL) {
  47         /* Both GetEnvironmentStringsW and GetEnvironmentStringsA
  48          * failed.  Out of memory is our best guess.  */
  49         JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed");
  50         return NULL;
  51     }
  52 
  53     /* Don't search for "\0\0", since an empty environment block may
  54        legitimately consist of a single "\0". */
  55     for (i = 0; blockA[i];)
  56         while (blockA[i++])
  57             ;
  58 
  59     if ((bytes = (*env)->NewByteArray(env, i)) == NULL) {
  60         FreeEnvironmentStringsA(blockA);
  61         return NULL;
  62     }
  63     (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA);
  64     FreeEnvironmentStringsA(blockA);
  65     return (*env)->NewObject(env, string_class,
  66                              String_init_ID, bytes);
  67 }
  68 
  69 /* Returns a Windows style environment block, discarding final trailing NUL */
  70 JNIEXPORT jstring JNICALL
  71 Java_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass)
  72 {
  73     int i;
  74     jstring envblock;
  75     jchar *blockW = (jchar *) GetEnvironmentStringsW();
  76     if (blockW == NULL)
  77         return environmentBlock9x(env);
  78 
  79     /* Don't search for "\u0000\u0000", since an empty environment
  80        block may legitimately consist of a single "\u0000".  */
  81     for (i = 0; blockW[i];)
  82         while (blockW[i++])
  83             ;
  84 
  85     envblock = (*env)->NewString(env, blockW, i);