authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-11 21:21:00-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-22 11:48:53-04:00
loge63daca92e959495be347525415ef1e520b01f44
treeafd49b1f9e2c9fdd2b4b39272a376cd57c9a28f2
parent8a15537c6e32a756d4bdd54723504174c61c8c6a
signaturelock-open Commit is signed but in an unrecognized format.

linux implementation of std.net.getHostName


4 files changed, 44 insertions(+), 0 deletions(-)

std/net.zig+8
......@@ -245,3 +245,11 @@ 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+22
......@@ -2688,6 +2688,28 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
26882688 }
26892689}
26902690
2691pub const GetHostNameError = error{Unexpected};
2692
2693pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
2694 if (builtin.link_libc) {
2695 @compileError("TODO implement gethostname when linking libc");
2696 }
2697 if (linux.is_the_target) {
2698 var uts: utsname = undefined;
2699 switch (errno(system.uname(&uts))) {
2700 0 => {
2701 const hostname = mem.toSlice(u8, &uts.nodename);
2702 mem.copy(u8, name_buffer, hostname);
2703 return name_buffer[0..hostname.len];
2704 },
2705 EFAULT => unreachable,
2706 else => |err| return unexpectedErrno(err),
2707 }
2708 }
2709
2710 @compileError("TODO implement gethostname for this OS");
2711}
2712
26912713test "" {
26922714 _ = @import("os/darwin.zig");
26932715 _ = @import("os/freebsd.zig");
std/os/bits/linux.zig+10
......@@ -1175,3 +1175,13 @@ pub const IORING_REGISTER_FILES = 2;
11751175pub const IORING_UNREGISTER_FILES = 3;
11761176pub const IORING_REGISTER_EVENTFD = 4;
11771177pub 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/linux.zig+4
......@@ -965,6 +965,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize {
965965 return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss));
966966}
967967
968pub fn uname(uts: *utsname) usize {
969 return syscall1(SYS_uname, @ptrToInt(uts));
970}
971
968972// XXX: This should be weak
969973extern const __ehdr_start: elf.Ehdr = undefined;
970974