• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2003, 2004, 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 
26 #include <stdlib.h>
27 #include <string.h>
28 #include "jni.h"
29 #include "jni_util.h"
30 
31 #ifdef __APPLE__
32 #include <crt_externs.h>
33 #define environ (*_NSGetEnviron())
34 #endif
35 #include <nativehelper/JNIHelp.h>
36 
37 #define NATIVE_METHOD(className, functionName, signature) \
38 { #functionName, signature, (void*)(className ## _ ## functionName) }
39 
40 JNIEXPORT jobjectArray JNICALL
ProcessEnvironment_environ(JNIEnv * env,jclass ign)41 ProcessEnvironment_environ(JNIEnv *env, jclass ign)
42 {
43     /* This is one of the rare times it's more portable to declare an
44      * external symbol explicitly, rather than via a system header.
45      * The declaration is standardized as part of UNIX98, but there is
46      * no standard (not even de-facto) header file where the
47      * declaration is to be found.  See:
48      * http://www.opengroup.org/onlinepubs/007908799/xbd/envvar.html */
49 #ifndef __APPLE__
50     extern char ** environ; /* environ[i] looks like: VAR=VALUE\0 */
51 #endif
52 
53     jsize count = 0;
54     jsize i, j;
55     jobjectArray result;
56     jclass byteArrCls = (*env)->FindClass(env, "[B");
57 
58     for (i = 0; environ[i]; i++) {
59         /* Ignore corrupted environment variables */
60         if (strchr(environ[i], '=') != NULL)
61             count++;
62     }
63 
64     result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);
65     if (result == NULL) return NULL;
66 
67     for (i = 0, j = 0; environ[i]; i++) {
68         const char * varEnd = strchr(environ[i], '=');
69         /* Ignore corrupted environment variables */
70         if (varEnd != NULL) {
71             jbyteArray var, val;
72             const char * valBeg = varEnd + 1;
73             jsize varLength = varEnd - environ[i];
74             jsize valLength = strlen(valBeg);
75             var = (*env)->NewByteArray(env, varLength);
76             if (var == NULL) return NULL;
77             val = (*env)->NewByteArray(env, valLength);
78             if (val == NULL) return NULL;
79             (*env)->SetByteArrayRegion(env, var, 0, varLength,
80                                        (jbyte*) environ[i]);
81             (*env)->SetByteArrayRegion(env, val, 0, valLength,
82                                        (jbyte*) valBeg);
83             (*env)->SetObjectArrayElement(env, result, 2*j  , var);
84             (*env)->SetObjectArrayElement(env, result, 2*j+1, val);
85             (*env)->DeleteLocalRef(env, var);
86             (*env)->DeleteLocalRef(env, val);
87             j++;
88         }
89     }
90 
91     return result;
92 }
93 
94 static JNINativeMethod gMethods[] = {
95   NATIVE_METHOD(ProcessEnvironment, environ, "()[[B"),
96 };
97 
register_java_lang_ProcessEnvironment(JNIEnv * env)98 void register_java_lang_ProcessEnvironment(JNIEnv* env) {
99   jniRegisterNativeMethods(env, "java/lang/ProcessEnvironment", gMethods, NELEM(gMethods));
100 }
101