diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/os/homedir.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/os/homedir.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/os/homedir.zig | 92 |
1 files changed, 44 insertions, 48 deletions
diff --git a/vendor/libghostty-vt/src/os/homedir.zig b/vendor/libghostty-vt/src/os/homedir.zig index 9e0420f4..8da9640a 100644 --- a/vendor/libghostty-vt/src/os/homedir.zig +++ b/vendor/libghostty-vt/src/os/homedir.zig @@ -1,7 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); const passwd = @import("passwd.zig"); -const posix = std.posix; const objc = @import("objc"); const Error = error{ @@ -11,10 +10,10 @@ const Error = error{ /// Determine the home directory for the currently executing user. This /// is generally an expensive process so the value should be cached. -pub inline fn home(buf: []u8) !?[]const u8 { +pub inline fn home(io: std.Io, environ_map: *const std.process.Environ.Map, buf: []u8) !?[]const u8 { return switch (builtin.os.tag) { - .linux, .freebsd, .macos => try homeUnix(buf), - .windows => try homeWindows(buf), + .linux, .freebsd, .macos => try homeUnix(io, environ_map, buf), + .windows => homeWindows(environ_map, buf) catch return error.BufferTooSmall, // iOS doesn't have a user-writable home directory .ios => null, @@ -23,9 +22,9 @@ pub inline fn home(buf: []u8) !?[]const u8 { }; } -fn homeUnix(buf: []u8) !?[]const u8 { +fn homeUnix(io: std.Io, environ_map: *const std.process.Environ.Map, buf: []u8) !?[]const u8 { // First: if we have a HOME env var, then we use that. - if (posix.getenv("HOME")) |result| { + if (environ_map.get("HOME")) |result| { if (buf.len < result.len) return Error.BufferTooSmall; @memcpy(buf[0..result.len], result); return buf[0..result.len]; @@ -60,13 +59,14 @@ fn homeUnix(buf: []u8) !?[]const u8 { // If all else fails, have the shell tell us... fba.reset(); - const run = try std.process.Child.run(.{ - .allocator = fba.allocator(), + const run = try std.process.run(fba.allocator(), io, .{ .argv = &[_][]const u8{ "/bin/sh", "-c", "cd && pwd" }, - .max_output_bytes = fba.buffer.len / 2, + .environ_map = environ_map, + .stdout_limit = .limited(fba.buffer.len / 2), + .stderr_limit = .limited(fba.buffer.len / 2), }); - if (run.term == .Exited and run.term.Exited == 0) { + if (run.term == .exited and run.term.exited == 0) { const result = trimSpace(run.stdout); if (buf.len < result.len) return Error.BufferTooSmall; @memcpy(buf[0..result.len], result); @@ -76,33 +76,11 @@ fn homeUnix(buf: []u8) !?[]const u8 { return null; } -fn homeWindows(buf: []u8) !?[]const u8 { - const drive_len = blk: { - var fba_instance = std.heap.FixedBufferAllocator.init(buf); - const fba = fba_instance.allocator(); - const drive = std.process.getEnvVarOwned(fba, "HOMEDRIVE") catch |err| switch (err) { - error.OutOfMemory => return Error.BufferTooSmall, - error.InvalidWtf8, error.EnvironmentVariableNotFound => return null, - }; - // could shift the contents if this ever happens - if (drive.ptr != buf.ptr) @panic("codebug"); - break :blk drive.len; - }; - - const path_len = blk: { - const path_buf = buf[drive_len..]; - var fba_instance = std.heap.FixedBufferAllocator.init(buf[drive_len..]); - const fba = fba_instance.allocator(); - const homepath = std.process.getEnvVarOwned(fba, "HOMEPATH") catch |err| switch (err) { - error.OutOfMemory => return Error.BufferTooSmall, - error.InvalidWtf8, error.EnvironmentVariableNotFound => return null, - }; - // could shift the contents if this ever happens - if (homepath.ptr != path_buf.ptr) @panic("codebug"); - break :blk homepath.len; - }; - - return buf[0 .. drive_len + path_len]; +fn homeWindows(environ_map: *const std.process.Environ.Map, buf: []u8) !?[]const u8 { + var writer: std.Io.Writer = .fixed(buf); + _ = try writer.write(environ_map.get("HOMEDRIVE") orelse return null); + _ = try writer.write(environ_map.get("HOMEPATH") orelse return null); + return writer.buffered(); } fn trimSpace(input: []const u8) []const u8 { @@ -119,9 +97,14 @@ pub const ExpandError = error{ /// /// Errors if `home` fails or if the size of the expanded path is larger /// than `buf.len`. -pub fn expandHome(path: []const u8, buf: []u8) ExpandError![]const u8 { +pub fn expandHome( + io: std.Io, + environ_map: *const std.process.Environ.Map, + path: []const u8, + buf: []u8, +) ExpandError![]const u8 { return switch (builtin.os.tag) { - .linux, .freebsd, .macos => try expandHomeUnix(path, buf), + .linux, .freebsd, .macos => try expandHomeUnix(io, environ_map, path, buf), // `~/` is not an idiom generally used on Windows .windows => return path, @@ -133,9 +116,14 @@ pub fn expandHome(path: []const u8, buf: []u8) ExpandError![]const u8 { }; } -fn expandHomeUnix(path: []const u8, buf: []u8) ExpandError![]const u8 { +fn expandHomeUnix( + io: std.Io, + environ_map: *const std.process.Environ.Map, + path: []const u8, + buf: []u8, +) ExpandError![]const u8 { if (!std.mem.startsWith(u8, path, "~/")) return path; - const home_dir: []const u8 = if (home(buf)) |home_| + const home_dir: []const u8 = if (home(io, environ_map, buf)) |home_| home_ orelse return error.HomeDetectionFailed else |_| return error.HomeDetectionFailed; @@ -152,28 +140,33 @@ test "expandHomeUnix" { if (builtin.os.tag == .windows) return error.SkipZigTest; const testing = std.testing; + const io = testing.io; const allocator = testing.allocator; + var environ_map = try testing.environ.createMap(testing.allocator); + defer environ_map.deinit(); var buf: [std.fs.max_path_bytes]u8 = undefined; - const home_dir = try expandHomeUnix("~/", &buf); + const home_dir = try expandHomeUnix(io, &environ_map, "~/", &buf); // Joining the home directory `~` with the path `/` // the result should end with a separator here. (e.g. `/home/user/`) try testing.expect(home_dir[home_dir.len - 1] == std.fs.path.sep); - const downloads = try expandHomeUnix("~/Downloads/shader.glsl", &buf); + const downloads = try expandHomeUnix(io, &environ_map, "~/Downloads/shader.glsl", &buf); const expected_downloads = try std.mem.concat(allocator, u8, &[_][]const u8{ home_dir, "Downloads/shader.glsl" }); defer allocator.free(expected_downloads); try testing.expectEqualStrings(expected_downloads, downloads); - try testing.expectEqualStrings("~", try expandHomeUnix("~", &buf)); - try testing.expectEqualStrings("~abc/", try expandHomeUnix("~abc/", &buf)); - try testing.expectEqualStrings("/home/user", try expandHomeUnix("/home/user", &buf)); - try testing.expectEqualStrings("", try expandHomeUnix("", &buf)); + try testing.expectEqualStrings("~", try expandHomeUnix(io, &environ_map, "~", &buf)); + try testing.expectEqualStrings("~abc/", try expandHomeUnix(io, &environ_map, "~abc/", &buf)); + try testing.expectEqualStrings("/home/user", try expandHomeUnix(io, &environ_map, "/home/user", &buf)); + try testing.expectEqualStrings("", try expandHomeUnix(io, &environ_map, "", &buf)); // Expect an error if the buffer is large enough to hold the home directory, // but not the expanded path var small_buf = try allocator.alloc(u8, home_dir.len); defer allocator.free(small_buf); try testing.expectError(error.BufferTooSmall, expandHomeUnix( + io, + &environ_map, "~/Downloads", small_buf[0..], )); @@ -181,9 +174,12 @@ test "expandHomeUnix" { test { const testing = std.testing; + const io = testing.io; + var environ_map = try testing.environ.createMap(testing.allocator); + defer environ_map.deinit(); var buf: [1024]u8 = undefined; - const result = try home(&buf); + const result = try home(io, &environ_map, &buf); try testing.expect(result != null); try testing.expect(result.?.len > 0); } |
