blob: b2555c56c4644b4f8ce8099b187184d925158688 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26#include <stdlib.h>
27#include <string.h>
28#include "jni.h"
29#include "jni_util.h"
30
31JNIEXPORT jobjectArray JNICALL
32Java_java_lang_ProcessEnvironment_environ(JNIEnv *env, jclass ign)
33{
34 /* This is one of the rare times it's more portable to declare an
35 * external symbol explicitly, rather than via a system header.
36 * The declaration is standardized as part of UNIX98, but there is
37 * no standard (not even de-facto) header file where the
38 * declaration is to be found. See:
39 * http://www.opengroup.org/onlinepubs/007908799/xbd/envvar.html */
40 extern char ** environ; /* environ[i] looks like: VAR=VALUE\0 */
41
42 jsize count = 0;
43 jsize i, j;
44 jobjectArray result;
45 jclass byteArrCls = (*env)->FindClass(env, "[B");
46
47 for (i = 0; environ[i]; i++) {
48 /* Ignore corrupted environment variables */
49 if (strchr(environ[i], '=') != NULL)
50 count++;
51 }
52
53 result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);
54 if (result == NULL) return NULL;
55
56 for (i = 0, j = 0; environ[i]; i++) {
57 const char * varEnd = strchr(environ[i], '=');
58 /* Ignore corrupted environment variables */
59 if (varEnd != NULL) {
60 jbyteArray var, val;
61 const char * valBeg = varEnd + 1;
62 jsize varLength = varEnd - environ[i];
63 jsize valLength = strlen(valBeg);
64 var = (*env)->NewByteArray(env, varLength);
65 if (var == NULL) return NULL;
66 val = (*env)->NewByteArray(env, valLength);
67 if (val == NULL) return NULL;
68 (*env)->SetByteArrayRegion(env, var, 0, varLength,
69 (jbyte*) environ[i]);
70 (*env)->SetByteArrayRegion(env, val, 0, valLength,
71 (jbyte*) valBeg);
72 (*env)->SetObjectArrayElement(env, result, 2*j , var);
73 (*env)->SetObjectArrayElement(env, result, 2*j+1, val);
74 (*env)->DeleteLocalRef(env, var);
75 (*env)->DeleteLocalRef(env, val);
76 j++;
77 }
78 }
79
80 return result;
81}