First submit for debug plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / net_sourceforge_phpdt_internal_debug_core_Environment.c
1 #ifdef WIN32
2 #  include "windows.h"
3 #else
4 #  include "string.h"
5 #  include "stdlib.h"
6 #endif
7 #define BUFFER 512
8
9 #include "net_sourceforge_phpdt_internal_debug_core_Environment.h"
10
11 JNIEXPORT jstring JNICALL Java_net_sourceforge_phpdt_internal_debug_core_Environment_getenv
12   (JNIEnv *env, jclass c, jstring jname)
13 {
14         // Retrieve the argument
15         char cname[BUFFER];
16         const char *str = (*env)->GetStringUTFChars(env, jname, (jboolean *)NULL);
17
18         strncpy(cname, str, BUFFER);
19         (*env)->ReleaseStringUTFChars(env, jname, str);
20
21         #ifdef WIN32
22                 char cvalue[BUFFER];
23                 int result = GetEnvironmentVariable(cname, cvalue, BUFFER);
24                 if (result == 0)
25                         return 0;
26                 else
27                         return (*env)->NewStringUTF(env, cvalue);
28         #else // UNIX
29                 char *cvalue = getenv(cname);
30                 if (cvalue == 0)
31                         return 0;
32                 else
33                         return (*env)->NewStringUTF(env, cvalue);
34         #endif
35 }
36
37 JNIEXPORT jstring JNICALL Java_net_sourceforge_phpdt_internal_debug_core_Environment_setenv
38   (JNIEnv *env, jclass c, jstring jname, jstring jvalue)
39 {
40         // Retrieve the arguments
41         char cname[BUFFER], cvalue[BUFFER];
42         const char *str = (*env)->GetStringUTFChars(env, jname, (jboolean *)NULL);
43
44         strncpy(cname, str, BUFFER);
45         (*env)->ReleaseStringUTFChars(env, jname, str);
46         str = (*env)->GetStringUTFChars(env, jvalue, (jboolean *)NULL);
47         strncpy(cvalue, str, BUFFER);
48         (*env)->ReleaseStringUTFChars(env, jvalue, str);
49
50         #ifdef WIN32
51                 SetEnvironmentVariable(cname, cvalue);
52         #else // UNIX
53                 char envbuf[BUFFER];
54                 strncpy(envbuf, cname, BUFFER);
55                 strncat(envbuf, "=", BUFFER-strlen(envbuf));
56                 strncat(envbuf, cvalue, BUFFER-strlen(envbuf));
57                 putenv(envbuf);
58         #endif
59         return 0;
60 }
61
62 #ifdef __LCC__
63
64 /**
65  * Valentin Valchev (Bulgaria, www.prosyst.com)
66  * This is the standart implementation of Java 2 OnLoad and OnUnload native
67  * library calls. This template defines them empty functions
68  */
69 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)   
70
71         return JNI_VERSION_1_2; 
72 }
73 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) 
74 {
75 }
76 #endif