aboutsummaryrefslogtreecommitdiff
path: root/lib/std/os.zig
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-03-24 19:47:18 +0100
committerLemonBoy <thatlemon@gmail.com>2020-03-24 19:47:18 +0100
commit3ccf99c0bd5a681c56c456d1606b5eccfcf31fb7 (patch)
tree61b893eec83afcb30605e90d1eb0bbb10cfb1670 /lib/std/os.zig
parentc3f93be00cd516a561b32df4c68b5ed01e851645 (diff)
downloadzig-3ccf99c0bd5a681c56c456d1606b5eccfcf31fb7.tar.gz
zig-3ccf99c0bd5a681c56c456d1606b5eccfcf31fb7.zip
std: Slim duplicate logic for some calls
Diffstat (limited to 'lib/std/os.zig')
-rw-r--r--lib/std/os.zig75
1 files changed, 22 insertions, 53 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 4e25b254ea..dc032badea 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -2531,29 +2531,15 @@ pub const FStatError = error{
pub fn fstat(fd: fd_t) FStatError!Stat {
var stat: Stat = undefined;
- if (comptime std.Target.current.isDarwin()) {
- switch (darwin.getErrno(darwin.@"fstat$INODE64"(fd, &stat))) {
- 0 => return stat,
- EINVAL => unreachable,
- EBADF => unreachable, // Always a race condition.
- ENOMEM => return error.SystemResources,
- EACCES => return error.AccessDenied,
- else => |err| return unexpectedErrno(err),
- }
- }
- if (std.Target.current.os.tag == .netbsd) {
- switch (errno(system.__fstat50(fd, &stat))) {
- 0 => return stat,
- EINVAL => unreachable,
- EBADF => unreachable, // Always a race condition.
- ENOMEM => return error.SystemResources,
- EACCES => return error.AccessDenied,
- else => |err| return unexpectedErrno(err),
- }
- }
+ const symbol_name = if (comptime std.Target.current.isDarwin())
+ "fstat$INODE64"
+ else if (std.Target.current.os.tag == .netbsd)
+ "__fstat50"
+ else
+ "fstat";
- switch (errno(system.fstat(fd, &stat))) {
+ switch (errno(@field(system, symbol_name)(fd, &stat))) {
0 => return stat,
EINVAL => unreachable,
EBADF => unreachable, // Always a race condition.
@@ -3413,16 +3399,12 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
return;
}
- if (std.Target.current.os.tag == .netbsd) {
- switch (errno(system.__clock_gettime50(clk_id, tp))) {
- 0 => return,
- EFAULT => unreachable,
- EINVAL => return error.UnsupportedClock,
- else => |err| return unexpectedErrno(err),
- }
- }
+ const symbol_name = if (std.Target.current.os.tag == .netbsd)
+ "__clock_gettime50"
+ else
+ "clock_gettime";
- switch (errno(system.clock_gettime(clk_id, tp))) {
+ switch (errno(@field(system, symbol_name)(clk_id, tp))) {
0 => return,
EFAULT => unreachable,
EINVAL => return error.UnsupportedClock,
@@ -3444,16 +3426,12 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
return;
}
- if (std.Target.current.os.tag == .netbsd) {
- switch (errno(system.__clock_getres50(clk_id, res))) {
- 0 => return,
- EFAULT => unreachable,
- EINVAL => return error.UnsupportedClock,
- else => |err| return unexpectedErrno(err),
- }
- }
+ const symbol_name = if (std.Target.current.os.tag == .netbsd)
+ "__clock_getres50"
+ else
+ "clock_getres";
- switch (errno(system.clock_getres(clk_id, res))) {
+ switch (errno(@field(system, symbol_name)(clk_id, res))) {
0 => return,
EFAULT => unreachable,
EINVAL => return error.UnsupportedClock,
@@ -3519,21 +3497,12 @@ pub const SigaltstackError = error{
} || UnexpectedError;
pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
- if (builtin.os.tag == .windows or builtin.os.tag == .uefi or builtin.os.tag == .wasi)
- @compileError("std.os.sigaltstack not available for this target");
-
- if (std.Target.current.os.tag == .netbsd) {
- switch (errno(system.__sigaltstack14(ss, old_ss))) {
- 0 => return,
- EFAULT => unreachable,
- EINVAL => unreachable,
- ENOMEM => return error.SizeTooSmall,
- EPERM => return error.PermissionDenied,
- else => |err| return unexpectedErrno(err),
- }
- }
+ const symbol_name = if (std.Target.current.os.tag == .netbsd)
+ "__sigaltstack14"
+ else
+ "sigaltstack";
- switch (errno(system.sigaltstack(ss, old_ss))) {
+ switch (errno(@field(system, symbol_name)(ss, old_ss))) {
0 => return,
EFAULT => unreachable,
EINVAL => unreachable,