@@ -149,6 +149,23 @@ std::vector<fs::path> filter(const std::vector<fs::path> &names,
149
149
return result;
150
150
}
151
151
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
+
152
169
bool has_magic (const std::string &pathname) {
153
170
static const auto magic_check = std::regex (" ([*?[])" );
154
171
return std::regex_search (pathname, magic_check);
@@ -256,7 +273,13 @@ std::vector<fs::path> glob(const std::string &pathname, bool recursive = false,
256
273
bool dironly = false ) {
257
274
std::vector<fs::path> result;
258
275
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
+
260
283
auto dirname = path.parent_path ();
261
284
const auto basename = path.filename ();
262
285
@@ -304,11 +327,11 @@ std::vector<fs::path> glob(const std::string &pathname, bool recursive = false,
304
327
305
328
for (auto &d : dirs) {
306
329
for (auto &name : glob_in_dir (d, basename, dironly)) {
330
+ fs::path subresult = name;
307
331
if (name.parent_path ().empty ()) {
308
- result.push_back (d / name);
309
- } else {
310
- result.push_back (name);
332
+ subresult = d / name;
311
333
}
334
+ result.push_back (subresult);
312
335
}
313
336
}
314
337
0 commit comments