diff --git a/src/dialogs.cpp b/src/dialogs.cpp index f0ef662..6d1e90f 100644 --- a/src/dialogs.cpp +++ b/src/dialogs.cpp @@ -1,4 +1,5 @@ #include "dialogs.hpp" +#include "filesystem.hpp" #include Dialog::Message::Message(const std::string &text, std::function &&on_cancel, bool show_progress_bar) : Gtk::Window(Gtk::WindowType::WINDOW_POPUP) { @@ -84,9 +85,8 @@ std::string Dialog::gtk_dialog(const boost::filesystem::path &path, const std::s else if(!path.empty()) gtk_file_chooser_set_current_folder(reinterpret_cast(dialog.gobj()), path.string().c_str()); else { - boost::system::error_code ec; - auto current_path = boost::filesystem::current_path(ec); - if(!ec) + auto current_path = filesystem::get_current_path(); + if(!current_path.empty()) gtk_file_chooser_set_current_folder(reinterpret_cast(dialog.gobj()), current_path.string().c_str()); } diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 9a6f07d..fdee257 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -1,4 +1,5 @@ #include "filesystem.hpp" +#include "process.hpp" #include "utility.hpp" #include #include @@ -81,6 +82,33 @@ std::string filesystem::unescape_argument(const std::string &argument) { return unescaped; } +boost::filesystem::path filesystem::get_current_path() noexcept { + static boost::filesystem::path current_path; + if(!current_path.empty()) + return current_path; +#ifdef _WIN32 + boost::system::error_code ec; + auto path = boost::filesystem::current_path(ec); + if(!ec) { + current_path = std::move(path); + return current_path; + } + return boost::filesystem::path(); +#else + std::string path; + TinyProcessLib::Process process("pwd", "", [&path](const char *buffer, size_t length) { + path += std::string(buffer, length); + }); + if(process.get_exit_status() == 0) { + if(!path.empty() && path.back() == '\n') + path.pop_back(); + current_path = boost::filesystem::path(path); + return current_path; + } + return boost::filesystem::path(); +#endif +} + boost::filesystem::path filesystem::get_home_path() noexcept { static boost::filesystem::path home_path; if(!home_path.empty()) diff --git a/src/filesystem.hpp b/src/filesystem.hpp index 3056315..a09710c 100644 --- a/src/filesystem.hpp +++ b/src/filesystem.hpp @@ -16,6 +16,9 @@ public: static std::string escape_argument(const std::string &argument); static std::string unescape_argument(const std::string &argument); + /// Does not resolve symbolic links. Returns empty path on failure + static boost::filesystem::path get_current_path() noexcept; + /// Returns empty path on failure static boost::filesystem::path get_home_path() noexcept; /// Replaces home path with ~ static boost::filesystem::path get_short_path(const boost::filesystem::path &path) noexcept; diff --git a/src/juci.cpp b/src/juci.cpp index 41d31f1..733a43b 100644 --- a/src/juci.cpp +++ b/src/juci.cpp @@ -24,13 +24,12 @@ int Application::on_command_line(const Glib::RefPtr char **argv = cmd->get_arguments(argc); ctx.parse(argc, argv); if(argc >= 2) { - boost::system::error_code current_path_ec; - auto current_path = boost::filesystem::current_path(current_path_ec); - if(current_path_ec) + auto current_path = filesystem::get_current_path(); + if(current_path.empty()) errors.emplace_back("\e[31mError\e[m: could not find current path\n"); for(int c = 1; c < argc; c++) { boost::filesystem::path path(argv[c]); - if(path.is_relative() && !current_path_ec) + if(path.is_relative() && !current_path.empty()) path = current_path / path; boost::system::error_code ec; if(boost::filesystem::exists(path, ec)) { diff --git a/src/project.cpp b/src/project.cpp index ddc319f..799b89f 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -35,11 +35,8 @@ boost::filesystem::path Project::get_preferably_view_folder() { else if(!Directories::get().path.empty()) return Directories::get().path; else { - boost::system::error_code ec; - auto current_path = boost::filesystem::current_path(ec); - if(ec) - return boost::filesystem::path(); - return current_path; + auto current_path = filesystem::get_current_path(); + return !current_path.empty() ? current_path : boost::filesystem::path(); } } @@ -49,11 +46,8 @@ boost::filesystem::path Project::get_preferably_directory_folder() { else if(auto view = Notebook::get().get_current_view()) return view->file_path.parent_path(); else { - boost::system::error_code ec; - auto current_path = boost::filesystem::current_path(ec); - if(ec) - return boost::filesystem::path(); - return current_path; + auto current_path = filesystem::get_current_path(); + return !current_path.empty() ? current_path : boost::filesystem::path(); } }