~sircmpwn/rc-devel

interp: implement ~user v2 PROPOSED

Curtis Arthaud: 1
 interp: implement ~user

 1 files changed, 34 insertions(+), 4 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/rc-devel/patches/46233/mbox | git am -3
Learn more about email & git

[PATCH v2] interp: implement ~user Export this patch

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
It looks like this has the same problem.