From fa8332dddda854602048126c981a4654f98057b5 Mon Sep 17 00:00:00 2001 From: urly3 Date: Mon, 24 Aug 2026 15:30:46 +0200 Subject: [PATCH] feature: getActiveTerminal for FreeBSD (#1047) ## What are the changes about? Implements getActiveTerminal for FreeBSD via fstat ## What existing issue(s) does this resolve? #1046 ## Pre-requisites - [ x ] I have tested & confirmed the changes work locally - [ x ] I have read and fully adhere to the rules set in the contributing guidelines found in `CONTRIBUTING.md` Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/1047 Reviewed-by: AnErrupTion --- ly-core/src/interop.zig | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/ly-core/src/interop.zig b/ly-core/src/interop.zig index d894485..b269513 100644 --- a/ly-core/src/interop.zig +++ b/ly-core/src/interop.zig @@ -246,8 +246,29 @@ fn PlatformStruct() type { if (result != 0) return error.SetUserUidFailed; } + // FreeBSD implementation using sysctlbyname + // https://man.freebsd.org/cgi/man.cgi?sysctlbyname pub fn getActiveTtyImpl(_: std.mem.Allocator, _: std.Io, _: bool) !u8 { - return error.FeatureUnimplemented; + const tty_fd = std.posix.system.open("/dev/tty", .{}); + if (tty_fd < 0) return error.NoTtyFound; + defer _ = std.posix.system.close(tty_fd); + + var tty_stat: std.posix.Stat = undefined; + if (std.posix.system.fstat(tty_fd, &tty_stat) < 0) return error.FstatTty; + + var buf: ["ttyvx".len:0]u8 = undefined; + var len: usize = buf.len + 1; + + if (std.posix.system.sysctlbyname("kern.devname", buf[0..].ptr, &len, &tty_stat.rdev, @sizeOf(u64)) < 0) + return error.SysctlTty; + + const dev_name = buf[0 .. len - 1]; + + if (std.mem.startsWith(u8, dev_name, "ttyv")) { + return try std.fmt.parseInt(u8, dev_name[dev_name.len - 1 ..], 16); + } + + return error.NoTtyFound; } pub fn getUserIdRange(_: std.mem.Allocator, _: std.Io, _: []const u8) !UidRange {