authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-22 15:36:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-22 16:07:21-04:00
log028011e59a486bf39db8c97e44a1bd79e09082ef
treeffea8e823b772f49be1e9f74ddbc88e317db595f
parente63daca92e959495be347525415ef1e520b01f44
signaturelock-open Commit is signed but in an unrecognized format.

libc implementation of gethostname


7 files changed, 27 insertions(+), 10 deletions(-)

std/c.zig+1
......@@ -107,6 +107,7 @@ pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void,
107107pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
108108pub 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;
110111pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
111112pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
112113pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;
std/net.zig-8
......@@ -245,11 +245,3 @@ pub fn connectUnixSocket(path: []const u8) !std.fs.File {
245245
246246 return std.fs.File.openHandle(sockfd);
247247}
248
249pub const getHostName = os.gethostname;
250
251test "getHostName" {
252 var buf: [os.HOST_NAME_MAX]u8 = undefined;
253 const hostname = try getHostName(&buf);
254 expect(hostname.len != 0);
255}
std/os.zig+12-2
......@@ -2688,11 +2688,20 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
26882688 }
26892689}
26902690
2691pub const GetHostNameError = error{Unexpected};
2691pub const GetHostNameError = error{
2692 PermissionDenied,
2693 Unexpected,
2694};
26922695
26932696pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
26942697 if (builtin.link_libc) {
2695 @compileError("TODO implement gethostname when linking 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 }
26962705 }
26972706 if (linux.is_the_target) {
26982707 var uts: utsname = undefined;
......@@ -2703,6 +2712,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
27032712 return name_buffer[0..hostname.len];
27042713 },
27052714 EFAULT => unreachable,
2715 EPERM => return error.PermissionDenied,
27062716 else => |err| return unexpectedErrno(err),
27072717 }
27082718 }
std/os/bits/darwin.zig+1
......@@ -1175,3 +1175,4 @@ pub fn S_ISSOCK(m: u32) bool {
11751175pub fn S_IWHT(m: u32) bool {
11761176 return m & S_IFMT == S_IFWHT;
11771177}
1178pub const HOST_NAME_MAX = 72;
std/os/bits/freebsd.zig+2
......@@ -935,3 +935,5 @@ pub fn S_ISSOCK(m: u32) bool {
935935pub fn S_IWHT(m: u32) bool {
936936 return m & S_IFMT == S_IFWHT;
937937}
938
939pub const HOST_NAME_MAX = 255;
std/os/bits/netbsd.zig+2
......@@ -819,3 +819,5 @@ pub fn S_ISSOCK(m: u32) bool {
819819pub fn S_IWHT(m: u32) bool {
820820 return m & S_IFMT == S_IFWHT;
821821}
822
823pub const HOST_NAME_MAX = 255;
std/os/test.zig+9
......@@ -210,3 +210,12 @@ test "dl_iterate_phdr" {
210210 expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
211211 expect(counter != 0);
212212}
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}