@@ -579,61 +579,70 @@ JNIEXPORT void JNICALL WINDOW_BUFFER_SIZE_RECORD_NATIVE(init)(JNIEnv *env, jclas
579579}
580580
581581#if defined(_WIN32 ) || defined(_WIN64 )
582+
582583wchar_t * java_to_wchar (JNIEnv * env , jstring string ) {
583584 jsize len = (* env )-> GetStringLength (env , string );
584585 wchar_t * str = (wchar_t * ) malloc (sizeof (wchar_t ) * (len + 1 ));
585586 (* env )-> GetStringRegion (env , string , 0 , len , (jchar * ) str );
586587 str [len ] = L'\0' ;
587588 return str ;
588589}
589- #endif
590590
591- #if defined(_WIN32 ) || defined(_WIN64 )
592- JNIEXPORT jint JNICALL CLibrary_NATIVE (chdir )(JNIEnv * env , jstring path )
591+ JNIEXPORT jint JNICALL CLibrary_NATIVE (chdir )(JNIEnv * env , jobject thiz , jstring path )
593592{
594593 jint rc = 0 ;
595- const char * nativePath = path != NULL ? java_to_wchar (env , path ) : NULL ;
596- rc = (jint )SetCurrentDirectoryW (nativePath );
594+ wchar_t * nativePath = path != NULL ? java_to_wchar (env , path ) : NULL ;
595+ rc = (jint ) SetCurrentDirectoryW (nativePath );
597596 if (nativePath ) free (nativePath );
598597 return rc ;
599598}
600- #else
601- JNIEXPORT jint JNICALL CLibrary_NATIVE (chdir )(JNIEnv * env , jstring path )
602- {
603- jint rc = 0 ;
604- const char * nativePath = (* env )-> GetStringUTFChars (env , path , 0 );
605- rc = (jint )chdir (nativePath );
606- (* env )-> ReleaseStringUTFChars (env , path , nativePath );
607- return rc ;
608- }
609- #endif
610599
611- #if defined(_WIN32 ) || defined(_WIN64 )
612- JNIEXPORT jint JNICALL CLibrary_NATIVE (setenv )(JNIEnv * env , jstring name , jstring value )
600+ JNIEXPORT jint JNICALL CLibrary_NATIVE (setenv )(JNIEnv * env , jobject thiz , jstring name , jstring value )
613601{
614602 jint rc = 0 ;
615- const char * nativeName = name != NULL ? java_to_wchar (env , name ) : NULL ;
616- const char * nativeValue = value != NULL ? java_to_wchar (env , value ) : NULL ;
617- rc = (jint )SetEnvironmentVariableW (nativeName , nativeValue );
603+ wchar_t * nativeName = name != NULL ? java_to_wchar (env , name ) : NULL ;
604+ wchar_t * nativeValue = value != NULL ? java_to_wchar (env , value ) : NULL ;
605+ rc = (jint ) SetEnvironmentVariableW (nativeName , nativeValue );
618606 if (nativeName ) free (nativeName );
619607 if (nativeValue ) free (nativeValue );
620608 return rc ;
621609}
610+
622611#else
623- JNIEXPORT jint JNICALL CLibrary_NATIVE (setenv )(JNIEnv * env , jstring name , jstring value )
612+
613+ char * java_to_char (JNIEnv * env , jstring string ) {
614+ size_t len = (* env )-> GetStringLength (env , string );
615+ size_t bytes = (* env )-> GetStringUTFLength (env , string );
616+ char * chars = (char * ) malloc (bytes + 1 );
617+ (* env )-> GetStringUTFRegion (env , string , 0 , len , chars );
618+ chars [bytes ] = 0 ;
619+ return chars ;
620+ }
621+
622+ JNIEXPORT jint JNICALL CLibrary_NATIVE (chdir )(JNIEnv * env , jobject thiz , jstring path )
624623{
625624 jint rc = 0 ;
626- const char * nativeName = (* env )-> GetStringUTFChars (env , name , 0 );
627- const char * nativeValue = (* env )-> GetStringUTFChars (env , value , 0 );
628- if (nativeName ) {
629- if (nativeValue ) {
625+ char * nativePath = java_to_char (env , path );
626+ rc = (jint ) chdir (nativePath );
627+ free (nativePath );
628+ return rc ;
629+ }
630+
631+ JNIEXPORT jint JNICALL CLibrary_NATIVE (setenv )(JNIEnv * env , jobject thiz , jstring name , jstring value )
632+ {
633+ jint rc = 0 ;
634+ if (name ) {
635+ char * nativeName = java_to_char (env , name );
636+ if (value ) {
637+ char * nativeValue = java_to_char (env , value );
630638 rc = setenv (nativeName , nativeValue , 1 );
639+ free (nativeValue );
631640 } else {
632641 rc = unsetenv (nativeName );
633642 }
643+ free (nativeName );
634644 }
635- (* env )-> ReleaseStringUTFChars (env , name , nativeName );
636- (* env )-> ReleaseStringUTFChars (env , value , nativeValue );
637645 return rc ;
638646}
647+
639648#endif
0 commit comments