zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit c2325053a86f4350e20ea3b476c7efeb942d8438 (tree)
parent 9458620e18cb89b71cd0ad2755b9a7ae4f63e846
Author: Luna <git@l4.pm>
Date:   Fri,  8 Nov 2019 21:44:17 -0300

add Address.parseUnix and Address.format support for AF_UNIX

Diffstat:
Mlib/std/net.zig | 17++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/std/net.zig b/lib/std/net.zig @@ -14,7 +14,7 @@ pub const Address = extern union { any: os.sockaddr, in: os.sockaddr_in, in6: os.sockaddr_in6, - unix: os.sockaddr_un, + un: os.sockaddr_un, // TODO this crashed the compiler //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0); @@ -40,6 +40,18 @@ pub const Address = extern union { return error.InvalidIPAddressFormat; } + pub fn parseUnix(path: []const u8) !Address { + var sock_addr = os.sockaddr_un{ + .family = os.AF_UNIX, + .path = undefined, + }; + + if (path.len > sock_addr.path.len) return error.NameTooLong; + mem.copy(u8, &sock_addr.path, path); + + return Address{ .un = sock_addr }; + } + pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address { switch (family) { os.AF_INET => return parseIp4(name, port), @@ -315,6 +327,9 @@ pub const Address = extern union { } try std.fmt.format(context, Errors, output, "]:{}", port); }, + os.AF_UNIX => { + try std.fmt.format(context, Errors, output, "{}", self.un.path); + }, else => unreachable, } }