Signed-off-by: Curtis Arthaud <uku82@gmx.fr>
---
interp/value.ha | 38 ++++++++++++++++++++++++++++++++++----
1 file changed, 34 insertions(+), 4 deletions(-)
diff --git a/interp/value.ha b/interp/value.ha
index 1ac8653..4ab2372 100644
--- a/interp/value.ha
+++ b/interp/value.ha
@@ -9,6 +9,8 @@ use path;
use rt;
use strconv;
use strings;
+use unix::passwd;
+use unix;
// A value object.
// TODO: s/[]value/[]str/g
@@ -97,11 +99,39 @@ export fn expand(state: *state, val: *ast::value, glob: bool) (value | error) =
};
fn expand_tilde(pattern: str) str = {
- const home = os::getenv("HOME") as str;
let buf = path::init(pattern)!;
- path::popprefix(&buf, "~")!;
- path::prepend(&buf, home)!;
- return strings::dup(path::string(&buf));
+
+ let iter = path::iter(&buf);
+ const prefix = match (path::nextiter(&iter)) {
+ case let s: str =>
+ yield s;
+ case =>
+ abort();
+ };
+ if (prefix == "~") {
+ const pw = passwd::getuid(unix::getuid()) as passwd::pwent;
+ defer passwd::pwent_finish(&pw);
+ const home = strings::dup(pw.homedir);
+ path::popprefix(&buf, "~")!;
+ path::prepend(&buf, home)!;
+ } else {
+ const user = strings::dup(strings::ltrim(prefix, '~'));
+ match (passwd::getuser(user)) {
+ case void =>
+ void;
+ case =>
+ path::popprefix(&buf, prefix)!;
+ path::prepend(&buf, user)!;
+ path::prepend(&buf, "/home")!;
+ };
+ };
+
+ const s = path::string(&buf);
+ if (strings::hassuffix(pattern, '/')) {
+ return strings::join("", s, "/");
+ } else {
+ return strings::dup(s);
+ };
};
fn expand_glob(pattern: str) (value | error) = {
--
2.42.0