feat: add hostname info

This commit is contained in:
utox39
2025-02-23 22:21:41 +01:00
parent 9e5f5ef3cf
commit e7af5f4d28
4 changed files with 33 additions and 1 deletions

View File

@@ -4,3 +4,12 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
const username = try std.process.getEnvVarOwned(allocator, "USER"); const username = try std.process.getEnvVarOwned(allocator, "USER");
return 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;
}

View File

@@ -4,3 +4,12 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
const username = try std.process.getEnvVarOwned(allocator, "USER"); const username = try std.process.getEnvVarOwned(allocator, "USER");
return 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;
}

View File

@@ -10,7 +10,12 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
const username = try os_module.getUsername(allocator); const username = try os_module.getUsername(allocator);
try stdout.print("User: {s}\n", .{username}); try stdout.print("User: {s}\n", .{username});
try bw.flush(); 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);
} }

View File

@@ -4,3 +4,12 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
const username = try std.process.getEnvVarOwned(allocator, "USERNAME"); const username = try std.process.getEnvVarOwned(allocator, "USERNAME");
return 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;
}