diff --git a/env.cpp b/env.cpp index c9c8bfd58..55d1032c4 100644 --- a/env.cpp +++ b/env.cpp @@ -507,6 +507,20 @@ int env_set_pwd() return 1; } +wcstring env_get_pwd_slash(void) +{ + env_var_t pwd = env_get_string(L"PWD"); + if (pwd.missing_or_empty()) + { + return L""; + } + if (! string_suffixes_string(L"/", pwd)) + { + pwd.push_back(L'/'); + } + return pwd; +} + /** Set up default values for various variables if not defined. */ diff --git a/env.h b/env.h index d594ef6a6..3525d4658 100644 --- a/env.h +++ b/env.h @@ -213,14 +213,12 @@ const char * const * env_export_arr(bool recalc); */ wcstring_list_t env_get_names(int flags); - - -/** - Update the PWD variable - directory -*/ +/** Update the PWD variable directory */ int env_set_pwd(); +/* Returns the PWD with a terminating slash */ +wcstring env_get_pwd_slash(); + class env_vars_snapshot_t { std::map vars; diff --git a/highlight.cpp b/highlight.cpp index a084d8c90..e9f8a9f42 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -1320,14 +1320,12 @@ void highlight_shell(const wcstring &buff, std::vector &color, size_t pos, std::fill(color.begin(), color.end(), -1); /* Do something sucky and get the current working directory on this background thread. This should really be passed in. Note that we also need this as a vector (of one directory). */ - const wcstring working_directory = get_working_directory(); + const wcstring working_directory = env_get_pwd_slash(); /* Tokenize the string */ tokenize(buff.c_str(), color, pos, error, working_directory, vars); - /* - Locate and syntax highlight cmdsubsts recursively - */ + /* Locate and syntax highlight cmdsubsts recursively */ wchar_t * const subbuff = wcsdup(buff.c_str()); wchar_t * subpos = subbuff; diff --git a/history.cpp b/history.cpp index af76caf3d..d2612a292 100644 --- a/history.cpp +++ b/history.cpp @@ -1639,7 +1639,7 @@ file_detection_context_t::file_detection_context_t(history_t *hist, const wcstri history(hist), command(cmd), when(time(NULL)), - working_directory(get_working_directory()) + working_directory(env_get_pwd_slash()) { } diff --git a/path.cpp b/path.cpp index 679e898ad..73cfc0171 100644 --- a/path.cpp +++ b/path.cpp @@ -446,18 +446,3 @@ bool paths_are_same_file(const wcstring &path1, const wcstring &path2) return false; } } - -wcstring get_working_directory(void) -{ - wcstring wd = L"./"; - wchar_t dir_path[4096]; - const wchar_t *cwd = wgetcwd(dir_path, 4096); - if (cwd) - { - wd = cwd; - /* Make sure the working directory ends with a slash */ - if (! wd.empty() && wd.at(wd.size() - 1) != L'/') - wd.push_back(L'/'); - } - return wd; -} diff --git a/path.h b/path.h index 74a931b65..a566501ef 100644 --- a/path.h +++ b/path.h @@ -78,7 +78,4 @@ bool path_is_valid(const wcstring &path, const wcstring &working_directory); /** Returns whether the two paths refer to the same file */ bool paths_are_same_file(const wcstring &path1, const wcstring &path2); -/* Returns the current working directory as returned by wgetcwd */ -wcstring get_working_directory(void); - #endif diff --git a/reader.cpp b/reader.cpp index 7978fb594..fae46011c 100644 --- a/reader.cpp +++ b/reader.cpp @@ -1275,7 +1275,7 @@ struct autosuggestion_context_t cursor_pos(pos), searcher(*history, term, HISTORY_SEARCH_TYPE_PREFIX), detector(history, term), - working_directory(get_working_directory()), + working_directory(env_get_pwd_slash()), vars(env_vars_snapshot_t::highlighting_keys), generation_count(s_generation_count), has_tried_reloading(false)