@@ -149,6 +149,23 @@ std::vector<fs::path> filter(const std::vector<fs::path> &names,
149149 return result;
150150}
151151
152+ fs::path expand_tilde (fs::path path) {
153+ if (path.empty ()) return path;
154+
155+ const char * home = std::getenv (" HOME" );
156+ if (home == nullptr ) {
157+ throw std::invalid_argument (" error: Unable to expand `~` - HOME environment variable not set." );
158+ }
159+
160+ std::string s = path.string ();
161+ if (s[0 ] == ' ~' ) {
162+ s = std::string (home) + s.substr (1 , s.size () - 1 );
163+ return fs::path (s);
164+ } else {
165+ return path;
166+ }
167+ }
168+
152169bool has_magic (const std::string &pathname) {
153170 static const auto magic_check = std::regex (" ([*?[])" );
154171 return std::regex_search (pathname, magic_check);
@@ -256,7 +273,13 @@ std::vector<fs::path> glob(const std::string &pathname, bool recursive = false,
256273 bool dironly = false ) {
257274 std::vector<fs::path> result;
258275
259- const auto path = fs::path (pathname);
276+ auto path = fs::path (pathname);
277+
278+ if (pathname[0 ] == ' ~' ) {
279+ // expand tilde
280+ path = expand_tilde (path);
281+ }
282+
260283 auto dirname = path.parent_path ();
261284 const auto basename = path.filename ();
262285
@@ -304,11 +327,11 @@ std::vector<fs::path> glob(const std::string &pathname, bool recursive = false,
304327
305328 for (auto &d : dirs) {
306329 for (auto &name : glob_in_dir (d, basename, dironly)) {
330+ fs::path subresult = name;
307331 if (name.parent_path ().empty ()) {
308- result.push_back (d / name);
309- } else {
310- result.push_back (name);
332+ subresult = d / name;
311333 }
334+ result.push_back (subresult);
312335 }
313336 }
314337
0 commit comments