Skip to content

Instantly share code, notes, and snippets.

@rightson
Last active December 4, 2017 16:42
Show Gist options
  • Save rightson/c644b9886af80b1e7b16948549239e17 to your computer and use it in GitHub Desktop.
Save rightson/c644b9886af80b1e7b16948549239e17 to your computer and use it in GitHub Desktop.
A rough and dirty way to expand string containing path using GNU wordexp
// ref: https://linux.die.net/man/3/wordexp
#include <iostream>
#include <string>
#include <wordexp.h>
using namespace std;
std::string expandPath(const std::string &str) {
wordexp_t p;
char** w;
wordexp(str.c_str(), &p, 0);
w = p.we_wordv;
std::string ret;
if (p.we_wordc > 0) {
ret = w[0];
}
wordfree(&p);
return ret;
}
int main() {
std::string path = expandPath("~rightson/bin");
cout << path << endl;
// output: /home/rightson/bin
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment