Skip to content

Commit

Permalink
Fix completely broken windows code for chdir/setenv
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 11, 2021
1 parent 9b34c64 commit 753a7f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/native/jansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,22 @@ JNIEXPORT void JNICALL WINDOW_BUFFER_SIZE_RECORD_NATIVE(init)(JNIEnv *env, jclas
}

#if defined(_WIN32) || defined(_WIN64)
JNIEXPORT jint JNICALL Kernel32_NATIVE(chdir)(JNIEnv *env, jstring path)
wchar_t* java_to_wchar(JNIEnv* env, jstring string) {
jsize len = (*env)->GetStringLength(env, string);
wchar_t* str = (wchar_t*) malloc(sizeof(wchar_t) * (len + 1));
(*env)->GetStringRegion(env, string, 0, len, (jchar*) str);
str[len] = L'\0';
return str;
}
#endif

#if defined(_WIN32) || defined(_WIN64)
JNIEXPORT jint JNICALL CLibrary_NATIVE(chdir)(JNIEnv *env, jstring path)
{
jint rc = 0;
const char *nativePath = (*env)->GetStringUTFChars(env, path, 0);
const char *nativePath = path != NULL ? java_to_wchar(env, path) : NULL;
rc = (jint)SetCurrentDirectoryW(nativePath);
(*env)->ReleaseStringUTFChars(env, path, nativePath);
if (nativePath) free(nativePath);
return rc;
}
#else
Expand All @@ -602,11 +612,11 @@ JNIEXPORT jint JNICALL CLibrary_NATIVE(chdir)(JNIEnv *env, jstring path)
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);
const char *nativeName = name != NULL ? java_to_wchar(env, name) : NULL;
const char *nativeValue = value != NULL ? java_to_wchar(env, value) : NULL;
rc = (jint)SetEnvironmentVariableW(nativeName, nativeValue);
(*env)->ReleaseStringUTFChars(env, name, nativeName);
(*env)->ReleaseStringUTFChars(env, value, nativeValue);
if (nativeName) free(nativeName);
if (nativeValue) free(nativeValue);
return rc;
}
#else
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 753a7f1

Please sign in to comment.