diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc index 1b8786cd..a8be5220 100644 --- a/lib/utils/mu-utils-file.cc +++ b/lib/utils/mu-utils-file.cc @@ -352,18 +352,21 @@ Mu::play (const std::string& path) Result -Mu::expand_path(const std::string& str) +expand_path_real(const std::string& str) { #ifndef HAVE_WORDEXP_H return Ok(std::string{str}); #else int res; - wordexp_t result; - memset(&result, 0, sizeof(result)); + wordexp_t 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); + if (res != 0) + return Err(Error::Code::File, "cannot expand {}; err={}", str, res); + else if (auto&n = result.we_wordc; n != 1) { + wordfree(&result); + return Err(Error::Code::File, "expected 1 expansions, but got {} for {}", n, str); + } std::string expanded{result.we_wordv[0]}; wordfree(&result); @@ -374,6 +377,18 @@ Mu::expand_path(const std::string& str) } +Result +Mu::expand_path(const std::string& str) +{ + if (auto&& res{expand_path_real(str)}; res) + return res; + + // failed... try quoting. + auto qstr{to_string_gchar(g_shell_quote(str.c_str()))}; + return expand_path_real(qstr); +} + + #ifdef BUILD_TESTS