diff --git a/src/linux/linux.zig b/src/linux/linux.zig index 91569c3..4f0ccb2 100644 --- a/src/linux/linux.zig +++ b/src/linux/linux.zig @@ -4,3 +4,12 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 { const username = try std.process.getEnvVarOwned(allocator, "USER"); return username; } + +pub fn getHostname(allocator: std.mem.Allocator) ![]u8 { + var buf: [std.posix.HOST_NAME_MAX]u8 = undefined; + const hostnameEnv = try std.posix.gethostname(&buf); + + const hostname = try allocator.dupe(u8, hostnameEnv); + + return hostname; +} diff --git a/src/macos/macos.zig b/src/macos/macos.zig index 91569c3..4f0ccb2 100644 --- a/src/macos/macos.zig +++ b/src/macos/macos.zig @@ -4,3 +4,12 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 { const username = try std.process.getEnvVarOwned(allocator, "USER"); return username; } + +pub fn getHostname(allocator: std.mem.Allocator) ![]u8 { + var buf: [std.posix.HOST_NAME_MAX]u8 = undefined; + const hostnameEnv = try std.posix.gethostname(&buf); + + const hostname = try allocator.dupe(u8, hostnameEnv); + + return hostname; +} diff --git a/src/main.zig b/src/main.zig index c0d0cb6..b42d320 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,7 +10,12 @@ pub fn main() !void { const allocator = gpa.allocator(); const username = try os_module.getUsername(allocator); - try stdout.print("User: {s}\n", .{username}); try bw.flush(); + allocator.free(username); + + const hostname = try os_module.getHostname(allocator); + try stdout.print("Hostname: {s}\n", .{hostname}); + try bw.flush(); + allocator.free(hostname); } diff --git a/src/windows/windows.zig b/src/windows/windows.zig index 718107b..1777c26 100644 --- a/src/windows/windows.zig +++ b/src/windows/windows.zig @@ -4,3 +4,12 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 { const username = try std.process.getEnvVarOwned(allocator, "USERNAME"); return username; } + +pub fn getHostname(allocator: std.mem.Allocator) ![]u8 { + var buf: [std.posix.HOST_NAME_MAX]u8 = undefined; + const hostnameEnv = try std.posix.gethostname(&buf); + + const hostname = try allocator.dupe(u8, hostnameEnv); + + return hostname; +}