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,...@@ -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/net.zig-8
...@@ -245,11 +245,3 @@ pub fn connectUnixSocket(path: []const u8) !std.fs.File {...@@ -245,11 +245,3 @@ pub fn connectUnixSocket(path: []const u8) !std.fs.File {
245245
246 return std.fs.File.openHandle(sockfd);246 return std.fs.File.openHandle(sockfd);
247}247}
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 {...@@ -2688,11 +2688,20 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
2688 }2688 }
2689}2689}
26902690
2691pub const GetHostNameError = error{Unexpected};2691pub const GetHostNameError = error{
2692 PermissionDenied,
2693 Unexpected,
2694};
26922695
2693pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {2696pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
2694 if (builtin.link_libc) {2697 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 }
2696 }2705 }
2697 if (linux.is_the_target) {2706 if (linux.is_the_target) {
2698 var uts: utsname = undefined;2707 var uts: utsname = undefined;
...@@ -2703,6 +2712,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {...@@ -2703,6 +2712,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
2703 return name_buffer[0..hostname.len];2712 return name_buffer[0..hostname.len];
2704 },2713 },
2705 EFAULT => unreachable,2714 EFAULT => unreachable,
2715 EPERM => return error.PermissionDenied,
2706 else => |err| return unexpectedErrno(err),2716 else => |err| return unexpectedErrno(err),
2707 }2717 }
2708 }2718 }
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/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/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}