diff --git a/std/net.zig b/std/net.zig index be9d18056c..3a1656d14d 100644 --- a/std/net.zig +++ b/std/net.zig @@ -245,3 +245,11 @@ pub fn connectUnixSocket(path: []const u8) !std.fs.File { return std.fs.File.openHandle(sockfd); } + +pub const getHostName = os.gethostname; + +test "getHostName" { + var buf: [os.HOST_NAME_MAX]u8 = undefined; + const hostname = try getHostName(&buf); + expect(hostname.len != 0); +} diff --git a/std/os.zig b/std/os.zig index b32c8a0b7e..63e6338f81 100644 --- a/std/os.zig +++ b/std/os.zig @@ -2688,6 +2688,28 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void { } } +pub const GetHostNameError = error{Unexpected}; + +pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { + if (builtin.link_libc) { + @compileError("TODO implement gethostname when linking libc"); + } + if (linux.is_the_target) { + var uts: utsname = undefined; + switch (errno(system.uname(&uts))) { + 0 => { + const hostname = mem.toSlice(u8, &uts.nodename); + mem.copy(u8, name_buffer, hostname); + return name_buffer[0..hostname.len]; + }, + EFAULT => unreachable, + else => |err| return unexpectedErrno(err), + } + } + + @compileError("TODO implement gethostname for this OS"); +} + test "" { _ = @import("os/darwin.zig"); _ = @import("os/freebsd.zig"); diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index c489592c4b..939d203e8d 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1175,3 +1175,13 @@ pub const IORING_REGISTER_FILES = 2; pub const IORING_UNREGISTER_FILES = 3; pub const IORING_REGISTER_EVENTFD = 4; pub const IORING_UNREGISTER_EVENTFD = 5; + +pub const utsname = extern struct { + sysname: [65]u8, + nodename: [65]u8, + release: [65]u8, + version: [65]u8, + machine: [65]u8, + domainname: [65]u8, +}; +pub const HOST_NAME_MAX = 64; diff --git a/std/os/linux.zig b/std/os/linux.zig index 74c71ee2c4..47fc1841e9 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -965,6 +965,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize { return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss)); } +pub fn uname(uts: *utsname) usize { + return syscall1(SYS_uname, @ptrToInt(uts)); +} + // XXX: This should be weak extern const __ehdr_start: elf.Ehdr = undefined;