Skip to content

Commit

Permalink
Add setenv and chdir methods to the CLibrary, fixes #196
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 10, 2021
1 parent abca999 commit 6d61a76
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/fusesource/jansi/internal/CLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,8 @@ public static class Termios {
public long c_ospeed;
}

public static native int setenv(String name, String value);

public static native int chdir(String path);

}
50 changes: 50 additions & 0 deletions src/main/native/jansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,53 @@ JNIEXPORT void JNICALL WINDOW_BUFFER_SIZE_RECORD_NATIVE(init)(JNIEnv *env, jclas
#endif
return;
}

#if defined(_WIN32) || defined(_WIN64)
JNIEXPORT jint JNICALL Kernel32_NATIVE(chdir)(JNIEnv *env, jstring path)
{
jint rc = 0;
const char *nativePath = (*env)->GetStringUTFChars(env, path, 0);
rc = (jint)SetCurrentDirectoryW(nativePath);
(*env)->ReleaseStringUTFChars(env, path, nativePath);
return rc;
}
#else
JNIEXPORT jint JNICALL CLibrary_NATIVE(chdir)(JNIEnv *env, jstring path)
{
jint rc = 0;
const char *nativePath = (*env)->GetStringUTFChars(env, path, 0);
rc = (jint)chdir(nativePath);
(*env)->ReleaseStringUTFChars(env, path, nativePath);
return rc;
}
#endif

#if defined(_WIN32) || defined(_WIN64)
JNIEXPORT jint JNICALL CLibrary_NATIVE(setenv)(JNIEnv *env, jstring name, jstring value)
{
jint rc = 0;
const char *nativeName = (*env)->GetStringUTFChars(env, name, 0);
const char *nativeValue = (*env)->GetStringUTFChars(env, value, 0);
rc = (jint)SetEnvironmentVariableW(nativeName, nativeValue);
(*env)->ReleaseStringUTFChars(env, name, nativeName);
(*env)->ReleaseStringUTFChars(env, value, nativeValue);
return rc;
}
#else
JNIEXPORT jint JNICALL CLibrary_NATIVE(setenv)(JNIEnv *env, jstring name, jstring value)
{
jint rc = 0;
const char *nativeName = (*env)->GetStringUTFChars(env, name, 0);
const char *nativeValue = (*env)->GetStringUTFChars(env, value, 0);
if (nativeName) {
if (nativeValue) {
rc = setenv(nativeName, nativeValue, 1);
} else {
rc = unsetenv(nativeName);
}
}
(*env)->ReleaseStringUTFChars(env, name, nativeName);
(*env)->ReleaseStringUTFChars(env, value, nativeValue);
return rc;
}
#endif
1 change: 1 addition & 0 deletions src/main/native/jansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sys/ioctl.h>
#include <pty.h>
#include <unistd.h>
#include <stdlib.h>

#include "inc_linux/jni.h"
#include "inc_linux/jni_md.h"
Expand Down
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.
Binary file not shown.

0 comments on commit 6d61a76

Please sign in to comment.