From 7a3f0a55d9a481f260935f26426ee4508d44cb18 Mon Sep 17 00:00:00 2001 From: Greg V Date: Wed, 17 Oct 2018 18:01:00 +0300 Subject: [PATCH] Add freebsd to more things --- std/debug/index.zig | 2 ++ std/heap.zig | 6 +++--- std/os/file.zig | 6 +++--- std/os/index.zig | 6 +++--- std/os/path.zig | 11 ++++++++++- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/std/debug/index.zig b/std/debug/index.zig index e6d8fe3fc6..cabeba14f7 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -1078,6 +1078,8 @@ pub const DebugInfo = switch (builtin.os) { self.elf.close(); } }, + builtin.Os.freebsd => struct.{ + }, else => @compileError("Unsupported OS"), }; diff --git a/std/heap.zig b/std/heap.zig index 8a68a7b457..34addef094 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -69,7 +69,7 @@ pub const DirectAllocator = struct.{ const self = @fieldParentPtr(DirectAllocator, "allocator", allocator); switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios, Os.freebsd => { const p = os.posix; const alloc_size = if (alignment <= os.page_size) n else n + alignment; const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0); @@ -120,7 +120,7 @@ pub const DirectAllocator = struct.{ const self = @fieldParentPtr(DirectAllocator, "allocator", allocator); switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios, Os.freebsd => { if (new_size <= old_mem.len) { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; @@ -165,7 +165,7 @@ pub const DirectAllocator = struct.{ const self = @fieldParentPtr(DirectAllocator, "allocator", allocator); switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios, Os.freebsd => { _ = os.posix.munmap(@ptrToInt(bytes.ptr), bytes.len); }, Os.windows => { diff --git a/std/os/file.zig b/std/os/file.zig index 293d637d19..4c21cc9b5a 100644 --- a/std/os/file.zig +++ b/std/os/file.zig @@ -229,7 +229,7 @@ pub const File = struct.{ pub fn seekForward(self: File, amount: isize) !void { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios, Os.freebsd => { const result = posix.lseek(self.handle, amount, posix.SEEK_CUR); const err = posix.getErrno(result); if (err > 0) { @@ -260,7 +260,7 @@ pub const File = struct.{ pub fn seekTo(self: File, pos: usize) !void { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios, Os.freebsd => { const ipos = try math.cast(isize, pos); const result = posix.lseek(self.handle, ipos, posix.SEEK_SET); const err = posix.getErrno(result); @@ -294,7 +294,7 @@ pub const File = struct.{ pub fn getPos(self: File) !usize { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => { + Os.linux, Os.macosx, Os.ios, Os.freebsd => { const result = posix.lseek(self.handle, 0, posix.SEEK_CUR); const err = posix.getErrno(result); if (err > 0) { diff --git a/std/os/index.zig b/std/os/index.zig index ba3771c15e..16bc571a83 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); const Os = builtin.Os; const is_windows = builtin.os == Os.windows; const is_posix = switch (builtin.os) { - builtin.Os.linux, builtin.Os.macosx => true, + builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd => true, else => false, }; const os = @This(); @@ -42,7 +42,7 @@ pub const time = @import("time.zig"); pub const page_size = 4 * 1024; pub const MAX_PATH_BYTES = switch (builtin.os) { - Os.linux, Os.macosx, Os.ios => posix.PATH_MAX, + Os.linux, Os.macosx, Os.ios, Os.freebsd => posix.PATH_MAX, // Each UTF-16LE character may be expanded to 3 UTF-8 bytes. // If it would require 4 UTF-8 bytes, then there would be a surrogate // pair in the UTF-16LE, and we (over)account 3 bytes for it that way. @@ -103,7 +103,7 @@ const math = std.math; /// library implementation. pub fn getRandomBytes(buf: []u8) !void { switch (builtin.os) { - Os.linux => while (true) { + Os.linux, Os.freebsd => while (true) { // TODO check libc version and potentially call c.getrandom. // See #397 const errno = posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)); diff --git a/std/os/path.zig b/std/os/path.zig index 8096cdd0d2..e0d8030bcc 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -1161,7 +1161,7 @@ pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealErro const pathname_w = try windows_util.cStrToPrefixedFileW(pathname); return realW(out_buffer, pathname_w); }, - Os.macosx, Os.ios, Os.freebsd => { + Os.macosx, Os.ios => { // TODO instead of calling the libc function here, port the implementation to Zig const err = posix.getErrno(posix.realpath(pathname, out_buffer)); switch (err) { @@ -1188,6 +1188,15 @@ pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealErro return os.readLinkC(out_buffer, proc_path.ptr); }, + Os.freebsd => { // XXX requires fdescfs + const fd = try os.posixOpenC(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); + defer os.close(fd); + + var buf: ["/dev/fd/-2147483648".len]u8 = undefined; + const proc_path = fmt.bufPrint(buf[0..], "/dev/fd/{}\x00", fd) catch unreachable; + + return os.readLinkC(out_buffer, proc_path.ptr); + }, else => @compileError("TODO implement os.path.real for " ++ @tagName(builtin.os)), } }