zig/std/os.zig

40 lines
1.1 KiB
Zig
Raw Normal View History

const system = switch(@compileVar("os")) {
linux => @import("linux.zig"),
darwin => @import("darwin.zig"),
else => @compileError("Unsupported OS"),
};
const errno = @import("errno.zig");
2016-02-04 08:00:54 +00:00
pub error Unexpected;
2016-08-18 03:11:04 +00:00
pub fn getRandomBytes(buf: []u8) -> %void {
while (true) {
const ret = switch (@compileVar("os")) {
linux => system.getrandom(buf.ptr, buf.len, 0),
darwin => system.getrandom(buf.ptr, buf.len),
else => @compileError("unsupported os"),
};
const err = system.getErrno(ret);
if (err > 0) {
return switch (err) {
errno.EINVAL => @unreachable(),
errno.EFAULT => @unreachable(),
errno.EINTR => continue,
else => error.Unexpected,
}
}
return;
2016-02-04 08:00:54 +00:00
}
}
2016-04-18 23:42:56 +00:00
pub coldcc fn abort() -> unreachable {
2016-08-18 03:11:04 +00:00
switch (@compileVar("os")) {
linux, darwin => {
system.raise(system.SIGABRT);
system.raise(system.SIGKILL);
2016-08-18 03:11:04 +00:00
while (true) {}
},
else => @compileError("unsupported os"),
2016-08-18 03:11:04 +00:00
}
2016-04-18 23:42:56 +00:00
}