Skip to content

Commit 6d61a76

Browse files
committedFeb 10, 2021
Add setenv and chdir methods to the CLibrary, fixes #196
1 parent abca999 commit 6d61a76

File tree

15 files changed

+55
-0
lines changed

15 files changed

+55
-0
lines changed
 

‎src/main/java/org/fusesource/jansi/internal/CLibrary.java

+4
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,8 @@ public static class Termios {
164164
public long c_ospeed;
165165
}
166166

167+
public static native int setenv(String name, String value);
168+
169+
public static native int chdir(String path);
170+
167171
}

‎src/main/native/jansi.c

+50
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,53 @@ JNIEXPORT void JNICALL WINDOW_BUFFER_SIZE_RECORD_NATIVE(init)(JNIEnv *env, jclas
577577
#endif
578578
return;
579579
}
580+
581+
#if defined(_WIN32) || defined(_WIN64)
582+
JNIEXPORT jint JNICALL Kernel32_NATIVE(chdir)(JNIEnv *env, jstring path)
583+
{
584+
jint rc = 0;
585+
const char *nativePath = (*env)->GetStringUTFChars(env, path, 0);
586+
rc = (jint)SetCurrentDirectoryW(nativePath);
587+
(*env)->ReleaseStringUTFChars(env, path, nativePath);
588+
return rc;
589+
}
590+
#else
591+
JNIEXPORT jint JNICALL CLibrary_NATIVE(chdir)(JNIEnv *env, jstring path)
592+
{
593+
jint rc = 0;
594+
const char *nativePath = (*env)->GetStringUTFChars(env, path, 0);
595+
rc = (jint)chdir(nativePath);
596+
(*env)->ReleaseStringUTFChars(env, path, nativePath);
597+
return rc;
598+
}
599+
#endif
600+
601+
#if defined(_WIN32) || defined(_WIN64)
602+
JNIEXPORT jint JNICALL CLibrary_NATIVE(setenv)(JNIEnv *env, jstring name, jstring value)
603+
{
604+
jint rc = 0;
605+
const char *nativeName = (*env)->GetStringUTFChars(env, name, 0);
606+
const char *nativeValue = (*env)->GetStringUTFChars(env, value, 0);
607+
rc = (jint)SetEnvironmentVariableW(nativeName, nativeValue);
608+
(*env)->ReleaseStringUTFChars(env, name, nativeName);
609+
(*env)->ReleaseStringUTFChars(env, value, nativeValue);
610+
return rc;
611+
}
612+
#else
613+
JNIEXPORT jint JNICALL CLibrary_NATIVE(setenv)(JNIEnv *env, jstring name, jstring value)
614+
{
615+
jint rc = 0;
616+
const char *nativeName = (*env)->GetStringUTFChars(env, name, 0);
617+
const char *nativeValue = (*env)->GetStringUTFChars(env, value, 0);
618+
if (nativeName) {
619+
if (nativeValue) {
620+
rc = setenv(nativeName, nativeValue, 1);
621+
} else {
622+
rc = unsetenv(nativeName);
623+
}
624+
}
625+
(*env)->ReleaseStringUTFChars(env, name, nativeName);
626+
(*env)->ReleaseStringUTFChars(env, value, nativeValue);
627+
return rc;
628+
}
629+
#endif

‎src/main/native/jansi.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <sys/ioctl.h>
3030
#include <pty.h>
3131
#include <unistd.h>
32+
#include <stdlib.h>
3233

3334
#include "inc_linux/jni.h"
3435
#include "inc_linux/jni_md.h"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.