std: Slim duplicate logic for some calls

master
LemonBoy 2020-03-24 19:47:18 +01:00
parent c3f93be00c
commit 3ccf99c0bd
3 changed files with 23 additions and 54 deletions

View File

@ -74,7 +74,6 @@ pub extern "c" fn exit(code: c_int) noreturn;
pub extern "c" fn isatty(fd: fd_t) c_int;
pub extern "c" fn close(fd: fd_t) c_int;
pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int;
pub extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int;
pub extern "c" fn lseek(fd: fd_t, offset: off_t, whence: c_int) off_t;
pub extern "c" fn open(path: [*:0]const u8, oflag: c_uint, ...) c_int;

View File

@ -13,6 +13,7 @@ pub extern "c" fn _dyld_get_image_vmaddr_slide(image_index: u32) usize;
pub extern "c" fn _dyld_get_image_name(image_index: u32) [*:0]const u8;
pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) isize;
pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int;
pub extern "c" fn mach_absolute_time() u64;
pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void;

View File

@ -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");
const symbol_name = if (std.Target.current.os.tag == .netbsd)
"__sigaltstack14"
else
"sigaltstack";
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),
}
}
switch (errno(system.sigaltstack(ss, old_ss))) {
switch (errno(@field(system, symbol_name)(ss, old_ss))) {
0 => return,
EFAULT => unreachable,
EINVAL => unreachable,