diff --git a/src/factory.c b/src/factory.c index 356606b6..ca281c4d 100644 --- a/src/factory.c +++ b/src/factory.c @@ -57,7 +57,6 @@ char * Bound_script[10]; #include #include #include // for DBL_MAX -#include // chdir() #include //for INT_MIN #include // strncpy() @@ -2059,7 +2058,7 @@ int L_Run(lua_State* L) { if (!Directory_exists(path_element)) return luaL_error(L, "run: directory of script doesn't exist"); - chdir(path_element); + Change_directory(path_element); } Extract_filename(path_element, full_path); if (luaL_loadfile(L,path_element) != 0) @@ -2083,7 +2082,7 @@ int L_Run(lua_State* L) } nested_calls--; // restore directory - chdir(saved_directory); + Change_directory(saved_directory); return 0; } @@ -2323,7 +2322,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename) // This chdir is for the script's sake. Grafx2 itself will (try to) // not rely on what is the system's current directory. Extract_path(buf,Last_run_script); - chdir(buf); + Change_directory(buf); L = luaL_newstate(); // used to be lua_open() on Lua 5.1, deprecated on 5.2 diff --git a/src/filesel.c b/src/filesel.c index 89921110..686e9568 100644 --- a/src/filesel.c +++ b/src/filesel.c @@ -1518,7 +1518,7 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context Display_bookmark(bookmark_dropdown[temp],temp); } - chdir(context->File_directory); + Change_directory(context->File_directory); Get_current_directory(Selector->Directory,MAX_PATH_CHARACTERS); // Affichage des premiers fichiers visibles: @@ -2046,7 +2046,7 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context has_clicked_ok=0; // We must enter the directory - if (!chdir(Selector_filename)) + if (Change_directory(Selector_filename) == 0) { #if defined (__MINT__) static char path[1024]={0}; diff --git a/src/io.c b/src/io.c index 3d47063f..fa5d18d1 100644 --- a/src/io.c +++ b/src/io.c @@ -540,3 +540,12 @@ const char * Get_current_directory(char * buf, size_t size) return getcwd(buf, size); #endif } + +int Change_directory(const char * path) +{ +#if defined(__WIN32__) || defined(WIN32) + return (SetCurrentDirectoryA(path) ? 0 : -1); +#else + return chdir(path); +#endif +} diff --git a/src/io.h b/src/io.h index 125c69fa..fb1f260c 100644 --- a/src/io.h +++ b/src/io.h @@ -32,6 +32,7 @@ /// - opendir() /// - readdir() /// - getcwd() +/// - chdir() /// - Also, don't assume "/" or "\\", use PATH_SEPARATOR /// If you don't, you break another platform. ////////////////////////////////////////////////////////////////////////////// @@ -130,3 +131,7 @@ void Release_lock_file(const char *file_directory); /// /// Return the current directory, equivalent to getcwd() const char * Get_current_directory(char * buf, size_t size); + +/// +/// Change current directory. return 0 for success, -1 in case of error +int Change_directory(const char * path); diff --git a/src/loadsave.c b/src/loadsave.c index 363f1923..0d819db0 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -1723,7 +1723,7 @@ void Delete_safety_backups(void) For_each_file(Config_directory, Add_backup_file); - chdir(Config_directory); + Change_directory(Config_directory); for (element=Backups_main; element!=NULL; element=element->Next) { if(remove(element->String)) diff --git a/src/main.c b/src/main.c index 4efa10ad..15fc0657 100644 --- a/src/main.c +++ b/src/main.c @@ -73,7 +73,6 @@ #if defined(__WIN32__) #include #include - #define chdir(dir) SetCurrentDirectory(dir) #elif defined (__MINT__) #include #elif defined(__macosx__) @@ -489,12 +488,8 @@ int Init_program(int argc,char * argv[]) Set_data_directory(program_directory,Data_directory); // Choose directory for settings (read/write) Set_config_directory(program_directory,Config_directory); -#if defined(__MINT__) - strcpy(Main.selector.Directory,program_directory); -#else // On détermine le répertoire courant: - getcwd(Main.selector.Directory,MAX_PATH_CHARACTERS); -#endif + Get_current_directory(Main.selector.Directory,MAX_PATH_CHARACTERS); #ifdef ENABLE_FILENAMES_ICONV // Initialisation de iconv @@ -1005,7 +1000,7 @@ void Program_shutdown(void) } // On prend bien soin de passer dans le répertoire initial: - if (chdir(Initial_directory)!=-1) + if (Change_directory(Initial_directory)==0) { // On sauvegarde les données dans le .CFG et dans le .INI if (Config.Auto_save)