authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-22 18:19:37-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-09-22 18:19:37-04:00
log989cd4233e14d592c19d458fee24ff0fa9fd9638
treeffea8e823b772f49be1e9f74ddbc88e317db595f
parent8a15537c6e32a756d4bdd54723504174c61c8c6a
parent028011e59a486bf39db8c97e44a1bd79e09082ef
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3298 from ziglang/gethostname

gethostname

8 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,6 +107,7 @@ pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void,
107pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;107pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
108pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;108pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
109109
110pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
110pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;111pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
111pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;112pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
112pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;113pub 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,6 +2688,38 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
2688 }2688 }
2689}2689}
26902690
2691pub const GetHostNameError = error{
2692 PermissionDenied,
2693 Unexpected,
2694};
2695
2696pub 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
2691test "" {2723test "" {
2692 _ = @import("os/darwin.zig");2724 _ = @import("os/darwin.zig");
2693 _ = @import("os/freebsd.zig");2725 _ = @import("os/freebsd.zig");
std/os/bits/darwin.zig+1
...@@ -1175,3 +1175,4 @@ pub fn S_ISSOCK(m: u32) bool {...@@ -1175,3 +1175,4 @@ pub fn S_ISSOCK(m: u32) bool {
1175pub fn S_IWHT(m: u32) bool {1175pub fn S_IWHT(m: u32) bool {
1176 return m & S_IFMT == S_IFWHT;1176 return m & S_IFMT == S_IFWHT;
1177}1177}
1178pub const HOST_NAME_MAX = 72;
std/os/bits/freebsd.zig+2
...@@ -935,3 +935,5 @@ pub fn S_ISSOCK(m: u32) bool {...@@ -935,3 +935,5 @@ pub fn S_ISSOCK(m: u32) bool {
935pub fn S_IWHT(m: u32) bool {935pub fn S_IWHT(m: u32) bool {
936 return m & S_IFMT == S_IFWHT;936 return m & S_IFMT == S_IFWHT;
937}937}
938
939pub const HOST_NAME_MAX = 255;
std/os/bits/linux.zig+10
...@@ -1175,3 +1175,13 @@ pub const IORING_REGISTER_FILES = 2;...@@ -1175,3 +1175,13 @@ pub const IORING_REGISTER_FILES = 2;
1175pub const IORING_UNREGISTER_FILES = 3;1175pub const IORING_UNREGISTER_FILES = 3;
1176pub const IORING_REGISTER_EVENTFD = 4;1176pub const IORING_REGISTER_EVENTFD = 4;
1177pub const IORING_UNREGISTER_EVENTFD = 5;1177pub const IORING_UNREGISTER_EVENTFD = 5;
1178
1179pub 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};
1187pub const HOST_NAME_MAX = 64;
std/os/bits/netbsd.zig+2
...@@ -819,3 +819,5 @@ pub fn S_ISSOCK(m: u32) bool {...@@ -819,3 +819,5 @@ pub fn S_ISSOCK(m: u32) bool {
819pub fn S_IWHT(m: u32) bool {819pub fn S_IWHT(m: u32) bool {
820 return m & S_IFMT == S_IFWHT;820 return m & S_IFMT == S_IFWHT;
821}821}
822
823pub 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,6 +965,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize {
965 return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss));965 return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss));
966}966}
967967
968pub fn uname(uts: *utsname) usize {
969 return syscall1(SYS_uname, @ptrToInt(uts));
970}
971
968// XXX: This should be weak972// XXX: This should be weak
969extern const __ehdr_start: elf.Ehdr = undefined;973extern const __ehdr_start: elf.Ehdr = undefined;
970974
std/os/test.zig+9
...@@ -210,3 +210,12 @@ test "dl_iterate_phdr" {...@@ -210,3 +210,12 @@ test "dl_iterate_phdr" {
210 expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);210 expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
211 expect(counter != 0);211 expect(counter != 0);
212}212}
213
214test "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}