Skip to content

Commit

Permalink
utils: add expand_path (wordexp wrapper)
Browse files Browse the repository at this point in the history
For expanding command-line options for shells that don't do that by themselves.
  • Loading branch information
djcb committed Aug 2, 2023
1 parent c9f3709 commit d25d4b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/utils/mu-utils-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <glib.h>
#include <gio/gio.h>

#ifdef HAVE_WORDEXP_H
#include <wordexp.h>
#endif /*HAVE_WORDEXP_H*/

using namespace Mu;


Expand Down Expand Up @@ -179,6 +183,30 @@ Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome)
}


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

/*
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/mu-utils-file.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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<std::string> expand_path(const std::string& str);


/*
* for OSs with out support for direntry->d_type, like Solaris
*/
Expand Down

0 comments on commit d25d4b0

Please sign in to comment.