summaryrefslogtreecommitdiff
path: root/lib/utils/mu-utils-file.cc
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2023-08-02 20:43:45 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2023-08-03 22:47:27 +0300
commit111e48efa35a8112311ec3745f4bb5ddbb7731e0 (patch)
treeadbb572cf733cbb5c53ca5db1f39a78cc59343c0 /lib/utils/mu-utils-file.cc
parentb91272aca2604be4d4902e8ccd95cafa3c5d88fd (diff)
utils: add expand_path (wordexp wrapper)
For expanding command-line options for shells that don't do that by themselves.
Diffstat (limited to 'lib/utils/mu-utils-file.cc')
-rw-r--r--lib/utils/mu-utils-file.cc28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc
index 3f5039a..a22e504 100644
--- a/lib/utils/mu-utils-file.cc
+++ b/lib/utils/mu-utils-file.cc
@@ -28,6 +28,10 @@
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
+#ifdef HAVE_WORDEXP_H
+#include <wordexp.h>
+#endif /*HAVE_WORDEXP_H*/
+
using namespace Mu;
@@ -309,6 +313,30 @@ Mu::run_command(std::initializer_list<std::string> args)
}
+Result<std::string>
+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
/*