| author | |
| committer | |
| log | 989cd4233e14d592c19d458fee24ff0fa9fd9638 |
| tree | ffea8e823b772f49be1e9f74ddbc88e317db595f |
| parent | 8a15537c6e32a756d4bdd54723504174c61c8c6a |
| parent | 028011e59a486bf39db8c97e44a1bd79e09082ef |
| signature |
gethostname8 files changed, 61 insertions(+), 0 deletions(-)
std/c.zig+1| ... | ... | @@ -107,6 +107,7 @@ pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, |
| 107 | 107 | pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; |
| 108 | 108 | pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; |
| 109 | 109 | |
| 110 | pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int; | |
| 110 | 111 | pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int; |
| 111 | 112 | pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int; |
| 112 | 113 | pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int; |
std/os.zig+32| ... | ... | @@ -2688,6 +2688,38 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void { |
| 2688 | 2688 | } |
| 2689 | 2689 | } |
| 2690 | 2690 | |
| 2691 | pub const GetHostNameError = error{ | |
| 2692 | PermissionDenied, | |
| 2693 | Unexpected, | |
| 2694 | }; | |
| 2695 | ||
| 2696 | pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { | |
| 2697 | if (builtin.link_libc) { | |
| 2698 | switch (errno(system.gethostname(name_buffer, name_buffer.len))) { | |
| 2699 | 0 => return mem.toSlice(u8, name_buffer), | |
| 2700 | EFAULT => unreachable, | |
| 2701 | ENAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this | |
| 2702 | EPERM => return error.PermissionDenied, | |
| 2703 | else => |err| return unexpectedErrno(err), | |
| 2704 | } | |
| 2705 | } | |
| 2706 | if (linux.is_the_target) { | |
| 2707 | var uts: utsname = undefined; | |
| 2708 | switch (errno(system.uname(&uts))) { | |
| 2709 | 0 => { | |
| 2710 | const hostname = mem.toSlice(u8, &uts.nodename); | |
| 2711 | mem.copy(u8, name_buffer, hostname); | |
| 2712 | return name_buffer[0..hostname.len]; | |
| 2713 | }, | |
| 2714 | EFAULT => unreachable, | |
| 2715 | EPERM => return error.PermissionDenied, | |
| 2716 | else => |err| return unexpectedErrno(err), | |
| 2717 | } | |
| 2718 | } | |
| 2719 | ||
| 2720 | @compileError("TODO implement gethostname for this OS"); | |
| 2721 | } | |
| 2722 | ||
| 2691 | 2723 | test "" { |
| 2692 | 2724 | _ = @import("os/darwin.zig"); |
| 2693 | 2725 | _ = @import("os/freebsd.zig"); |
std/os/bits/darwin.zig+1| ... | ... | @@ -1175,3 +1175,4 @@ pub fn S_ISSOCK(m: u32) bool { |
| 1175 | 1175 | pub fn S_IWHT(m: u32) bool { |
| 1176 | 1176 | return m & S_IFMT == S_IFWHT; |
| 1177 | 1177 | } |
| 1178 | pub const HOST_NAME_MAX = 72; |
std/os/bits/freebsd.zig+2| ... | ... | @@ -935,3 +935,5 @@ pub fn S_ISSOCK(m: u32) bool { |
| 935 | 935 | pub fn S_IWHT(m: u32) bool { |
| 936 | 936 | return m & S_IFMT == S_IFWHT; |
| 937 | 937 | } |
| 938 | ||
| 939 | pub const HOST_NAME_MAX = 255; |
std/os/bits/linux.zig+10| ... | ... | @@ -1175,3 +1175,13 @@ pub const IORING_REGISTER_FILES = 2; |
| 1175 | 1175 | pub const IORING_UNREGISTER_FILES = 3; |
| 1176 | 1176 | pub const IORING_REGISTER_EVENTFD = 4; |
| 1177 | 1177 | pub const IORING_UNREGISTER_EVENTFD = 5; |
| 1178 | ||
| 1179 | pub const utsname = extern struct { | |
| 1180 | sysname: [65]u8, | |
| 1181 | nodename: [65]u8, | |
| 1182 | release: [65]u8, | |
| 1183 | version: [65]u8, | |
| 1184 | machine: [65]u8, | |
| 1185 | domainname: [65]u8, | |
| 1186 | }; | |
| 1187 | pub const HOST_NAME_MAX = 64; |
std/os/bits/netbsd.zig+2| ... | ... | @@ -819,3 +819,5 @@ pub fn S_ISSOCK(m: u32) bool { |
| 819 | 819 | pub fn S_IWHT(m: u32) bool { |
| 820 | 820 | return m & S_IFMT == S_IFWHT; |
| 821 | 821 | } |
| 822 | ||
| 823 | pub const HOST_NAME_MAX = 255; |
std/os/linux.zig+4| ... | ... | @@ -965,6 +965,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize { |
| 965 | 965 | return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss)); |
| 966 | 966 | } |
| 967 | 967 | |
| 968 | pub fn uname(uts: *utsname) usize { | |
| 969 | return syscall1(SYS_uname, @ptrToInt(uts)); | |
| 970 | } | |
| 971 | ||
| 968 | 972 | // XXX: This should be weak |
| 969 | 973 | extern const __ehdr_start: elf.Ehdr = undefined; |
| 970 | 974 |
std/os/test.zig+9| ... | ... | @@ -210,3 +210,12 @@ test "dl_iterate_phdr" { |
| 210 | 210 | expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0); |
| 211 | 211 | expect(counter != 0); |
| 212 | 212 | } |
| 213 | ||
| 214 | test "gethostname" { | |
| 215 | if (os.windows.is_the_target) | |
| 216 | return error.SkipZigTest; | |
| 217 | ||
| 218 | var buf: [os.HOST_NAME_MAX]u8 = undefined; | |
| 219 | const hostname = try os.gethostname(&buf); | |
| 220 | expect(hostname.len != 0); | |
| 221 | } |