ProcessEnvironment_md.c revision 10444:f08705540498
1193323Sed/* 2193323Sed * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4193323Sed * 5193323Sed * This code is free software; you can redistribute it and/or modify it 6193323Sed * under the terms of the GNU General Public License version 2 only, as 7193323Sed * published by the Free Software Foundation. Oracle designates this 8193323Sed * particular file as subject to the "Classpath" exception as provided 9193323Sed * by Oracle in the LICENSE file that accompanied this code. 10193323Sed * 11193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT 12193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13193323Sed * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14193323Sed * version 2 for more details (a copy is included in the LICENSE file that 15193323Sed * accompanied this code). 16193323Sed * 17193323Sed * You should have received a copy of the GNU General Public License version 18193323Sed * 2 along with this work; if not, write to the Free Software Foundation, 19193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20193323Sed * 21193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22193323Sed * or visit www.oracle.com if you need additional information or have any 23314564Sdim * questions. 24314564Sdim */ 25321369Sdim#include <stdlib.h> 26314564Sdim 27249423Sdim#include "jni.h" 28249423Sdim#include "jni_util.h" 29314564Sdim#include <windows.h> 30314564Sdim 31314564Sdimstatic jstring 32249423SdimenvironmentBlock9x(JNIEnv *env) 33198090Srdivacky{ 34193323Sed int i; 35218893Sdim jmethodID String_init_ID; 36321369Sdim jbyteArray bytes; 37314564Sdim jbyte *blockA; 38314564Sdim jclass string_class; 39249423Sdim 40193323Sed string_class = JNU_ClassString(env); 41314564Sdim CHECK_NULL_RETURN(string_class, NULL); 42193323Sed 43249423Sdim String_init_ID = 44193323Sed (*env)->GetMethodID(env, string_class, "<init>", "([B)V"); 45314564Sdim CHECK_NULL_RETURN(String_init_ID, NULL); 46314564Sdim 47314564Sdim blockA = (jbyte *) GetEnvironmentStringsA(); 48193323Sed if (blockA == NULL) { 49193323Sed /* Both GetEnvironmentStringsW and GetEnvironmentStringsA 50193323Sed * failed. Out of memory is our best guess. */ 51193323Sed JNU_ThrowOutOfMemoryError(env, "GetEnvironmentStrings failed"); 52193323Sed return NULL; 53193323Sed } 54193323Sed 55193323Sed /* Don't search for "\0\0", since an empty environment block may 56193323Sed legitimately consist of a single "\0". */ 57193323Sed for (i = 0; blockA[i];) 58193323Sed while (blockA[i++]) 59193323Sed ; 60193323Sed 61194710Sed if ((bytes = (*env)->NewByteArray(env, i)) == NULL) { 62194710Sed FreeEnvironmentStringsA(blockA); 63288943Sdim return NULL; 64193323Sed } 65280031Sdim (*env)->SetByteArrayRegion(env, bytes, 0, i, blockA); 66193323Sed FreeEnvironmentStringsA(blockA); 67193323Sed return (*env)->NewObject(env, string_class, 68198090Srdivacky String_init_ID, bytes); 69193323Sed} 70193323Sed 71193323Sed/* Returns a Windows style environment block, discarding final trailing NUL */ 72193323SedJNIEXPORT jstring JNICALL 73193323SedJava_java_lang_ProcessEnvironment_environmentBlock(JNIEnv *env, jclass klass) 74226633Sdim{ 75193323Sed int i; 76193323Sed jstring envblock; 77193323Sed jchar *blockW = (jchar *) GetEnvironmentStringsW(); 78193323Sed if (blockW == NULL) 79193323Sed return environmentBlock9x(env); 80193323Sed 81193323Sed /* Don't search for "\u0000\u0000", since an empty environment 82193323Sed block may legitimately consist of a single "\u0000". */ 83193323Sed for (i = 0; blockW[i];) 84193323Sed while (blockW[i++]) 85193323Sed ; 86193323Sed 87193323Sed envblock = (*env)->NewString(env, blockW, i); 88193323Sed FreeEnvironmentStringsW(blockW); 89193323Sed return envblock; 90193323Sed} 91193323Sed