Commit 3640303ce1

Andrew Kelley <andrew@ziglang.org>
2019-05-27 23:28:59
freebsd fixes
1 parent af4b8eb
Changed files (4)
std/os/bits/freebsd.zig
@@ -1,3 +1,6 @@
+const std = @import("../../std.zig");
+const maxInt = std.math.maxInt;
+
 pub const fd_t = c_int;
 pub const pid_t = c_int;
 
std/c.zig
@@ -72,7 +72,7 @@ pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
 pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
 pub extern "c" fn rmdir(path: [*]const u8) c_int;
 pub extern "c" fn getenv(name: [*]const u8) ?[*]u8;
-pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
+pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
 pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
 pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
 
std/os.zig
@@ -2071,6 +2071,7 @@ pub fn pipe2(flags: u32) PipeError![2]fd_t {
 pub const SysCtlError = error{
     PermissionDenied,
     SystemResources,
+    NameTooLong,
     Unexpected,
 };
 
@@ -2081,7 +2082,8 @@ pub fn sysctl(
     newp: ?*c_void,
     newlen: usize,
 ) SysCtlError!void {
-    switch (errno(system.sysctl(name.ptr, name.len, oldp, oldlenp, newp, newlen))) {
+    const name_len = math.cast(c_uint, name.len) catch return error.NameTooLong;
+    switch (errno(system.sysctl(name.ptr, name_len, oldp, oldlenp, newp, newlen))) {
         0 => return,
         EFAULT => unreachable,
         EPERM => return error.PermissionDenied,
std/thread.zig
@@ -340,7 +340,10 @@ pub const Thread = struct {
         var count: c_int = undefined;
         var count_len: usize = @sizeOf(c_int);
         const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu";
-        try os.sysctlbynameC(name, @ptrCast(*c_void, &count), &count_len, null, 0);
+        os.sysctlbynameC(name, &count, &count_len, null, 0) catch |err| switch (err) {
+            error.NameTooLong => unreachable,
+            else => |e| return e,
+        };
         return @intCast(usize, count);
     }
 };