summaryrefslogtreecommitdiff
path: root/core/fs.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/fs.zig')
-rw-r--r--core/fs.zig18
1 files changed, 12 insertions, 6 deletions
diff --git a/core/fs.zig b/core/fs.zig
index 333588ce..f58e108b 100644
--- a/core/fs.zig
+++ b/core/fs.zig
@@ -405,15 +405,21 @@ pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 {
}
fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 {
- return expandHomeFrom(path, out, std.c.getenv("HOME"));
+ return expandHomeFrom(path, out, processHome());
}
-/// expandHome with HOME given (null: unset), so a test need not change the
-/// process's environment, which a child spawned later reads.
-fn expandHomeFrom(path: []const u8, out: *[4096]u8, env_home: ?[*:0]const u8) []const u8 {
+/// This process's home: $HOME, else its passwd entry's; null for none.
+pub fn processHome() ?[]const u8 {
+ return homeDir(std.c.getenv("HOME"));
+}
+
+/// expandHome with the home given (null: none, `~` left as typed): a
+/// session's (Pardes.expandHome), or this process's. `~user` is that
+/// user's passwd entry, as the session that recorded found it.
+pub fn expandHomeFrom(path: []const u8, out: *[4096]u8, given: ?[]const u8) []const u8 {
if (path.len == 0 or path[0] != '~') return path;
const end = std.mem.indexOfAny(u8, path, "/:") orelse path.len;
- const home = (if (end == 1) homeDir(env_home) else userHome(path[1..end])) orelse return path;
+ const home = (if (end == 1) given else pardes.tape.value(.env, userHome, .{path[1..end]})) orelse return path;
const trimmed = std.mem.trimEnd(u8, home, "/");
const rest = path[end..];
if (trimmed.len == 0 and (rest.len == 0 or rest[0] != '/')) return std.fmt.bufPrint(out, "/{s}", .{rest}) catch path;
@@ -1468,7 +1474,7 @@ test "a leading ~ is home, from HOME or else the passwd entry" {
// HOME unset: the passwd entry's home.
const pw = std.c.getpwuid(std.c.getuid()) orelse return error.SkipZigTest;
var from_passwd: [4096]u8 = undefined;
- try std.testing.expectEqualStrings(try std.fmt.bufPrint(&from_passwd, "{s}/y", .{std.mem.trimEnd(u8, std.mem.span(pw.dir.?), "/")}), expandHomeFrom("~/y", &out, null));
+ try std.testing.expectEqualStrings(try std.fmt.bufPrint(&from_passwd, "{s}/y", .{std.mem.trimEnd(u8, std.mem.span(pw.dir.?), "/")}), expandHomeFrom("~/y", &out, homeDir(null)));
}
test "Restore prefers default directory then falls back to original path" {