From b1d3e36e2ea0d428dd333019c8346b6d4bcbc762 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Thu, 5 Mar 2026 10:03:58 -0600 Subject: [PATCH] windows: add GetComputerNameA so that hostname-related functions work --- src/os/hostname.zig | 37 +++++++++++++++++++++++++++++++------ src/os/windows.zig | 5 +++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/os/hostname.zig b/src/os/hostname.zig index f728a2455..af9148fbf 100644 --- a/src/os/hostname.zig +++ b/src/os/hostname.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const posix = std.posix; pub const LocalHostnameValidationError = error{ @@ -99,9 +100,21 @@ pub fn isLocal(hostname: []const u8) LocalHostnameValidationError!bool { if (std.mem.eql(u8, "localhost", hostname)) return true; // If hostname is not "localhost" it must match our hostname. - var buf: [posix.HOST_NAME_MAX]u8 = undefined; - const ourHostname = try posix.gethostname(&buf); - return std.mem.eql(u8, hostname, ourHostname); + switch (builtin.os.tag) { + .windows => { + const windows = @import("windows.zig"); + var buf: [256:0]u8 = undefined; + var nSize: windows.DWORD = buf.len; + if (windows.exp.kernel32.GetComputerNameA(&buf, &nSize) == 0) return false; + const ourHostname = buf[0..nSize]; + return std.mem.eql(u8, hostname, ourHostname); + }, + else => { + var buf: [posix.HOST_NAME_MAX]u8 = undefined; + const ourHostname = try posix.gethostname(&buf); + return std.mem.eql(u8, hostname, ourHostname); + }, + } } test "isLocal returns true when provided hostname is localhost" { @@ -109,9 +122,21 @@ test "isLocal returns true when provided hostname is localhost" { } test "isLocal returns true when hostname is local" { - var buf: [posix.HOST_NAME_MAX]u8 = undefined; - const localHostname = try posix.gethostname(&buf); - try std.testing.expect(try isLocal(localHostname)); + switch (builtin.os.tag) { + .windows => { + const windows = @import("windows.zig"); + var buf: [256:0]u8 = undefined; + var nSize: windows.DWORD = buf.len; + if (windows.exp.kernel32.GetComputerNameA(&buf, &nSize) == 0) return error.GetComputerNameFailed; + const localHostname = buf[0..nSize]; + try std.testing.expect(try isLocal(localHostname)); + }, + else => { + var buf: [posix.HOST_NAME_MAX]u8 = undefined; + const localHostname = try posix.gethostname(&buf); + try std.testing.expect(try isLocal(localHostname)); + }, + } } test "isLocal returns false when hostname is not local" { diff --git a/src/os/windows.zig b/src/os/windows.zig index 87d1b662e..e92a54537 100644 --- a/src/os/windows.zig +++ b/src/os/windows.zig @@ -99,6 +99,11 @@ pub const exp = struct { lpStartupInfo: *windows.STARTUPINFOW, lpProcessInformation: *windows.PROCESS_INFORMATION, ) callconv(.winapi) windows.BOOL; + /// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcomputernamea + pub extern "kernel32" fn GetComputerNameA( + lpBuffer: windows.LPSTR, + nSize: *windows.DWORD, + ) callconv(.winapi) windows.BOOL; }; pub const PROC_THREAD_ATTRIBUTE_NUMBER = 0x0000FFFF;