diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc index 3f5039a4..a22e5040 100644 --- a/lib/utils/mu-utils-file.cc +++ b/lib/utils/mu-utils-file.cc @@ -28,6 +28,10 @@ #include #include +#ifdef HAVE_WORDEXP_H +#include +#endif /*HAVE_WORDEXP_H*/ + using namespace Mu; @@ -309,6 +313,30 @@ Mu::run_command(std::initializer_list args) } +Result +Mu::expand_path(const std::string& str) +{ +#ifndef HAVE_WORDEXP_H + return Ok(std::string{str}); +#else + int res; + wordexp_t result; + memset(&result, 0, sizeof(result)); + + res = wordexp(str.c_str(), &result, 0); + if (res != 0 || result.we_wordc == 0) + return Err(Error::Code::File, "cannot expand '%s'; err=%d", str.c_str(), res); + + std::string expanded{result.we_wordv[0]}; + wordfree(&result); + + return Ok(std::move(expanded)); + +#endif /*HAVE_WORDEXP_H*/ +} + + + #ifdef BUILD_TESTS /* diff --git a/lib/utils/mu-utils-file.hh b/lib/utils/mu-utils-file.hh index c9d89146..9f429bd7 100644 --- a/lib/utils/mu-utils-file.hh +++ b/lib/utils/mu-utils-file.hh @@ -75,6 +75,16 @@ bool check_dir(const std::string& path, bool readable, bool writeable); */ std::string canonicalize_filename(const std::string& path, const std::string& relative_to); +/** + * Expand the filesystem path (as per wordexp(3)) + * + * @param str a filesystem path string + * + * @return the expanded string or some error + */ +Result expand_path(const std::string& str); + + /* * for OSs with out support for direntry->d_type, like Solaris */