authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-24 19:47:18+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-24 19:47:18+01:00
log3ccf99c0bd5a681c56c456d1606b5eccfcf31fb7
tree61b893eec83afcb30605e90d1eb0bbb10cfb1670
parentc3f93be00cd516a561b32df4c68b5ed01e851645

std: Slim duplicate logic for some calls


3 files changed, 23 insertions(+), 54 deletions(-)

lib/std/c.zig-1
......@@ -74,7 +74,6 @@ pub extern "c" fn exit(code: c_int) noreturn;
7474pub extern "c" fn isatty(fd: fd_t) c_int;
7575pub extern "c" fn close(fd: fd_t) c_int;
7676pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
77pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int;
7877pub extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int;
7978pub extern "c" fn lseek(fd: fd_t, offset: off_t, whence: c_int) off_t;
8079pub extern "c" fn open(path: [*:0]const u8, oflag: c_uint, ...) c_int;
lib/std/c/darwin.zig+1
......@@ -13,6 +13,7 @@ pub extern "c" fn _dyld_get_image_vmaddr_slide(image_index: u32) usize;
1313pub extern "c" fn _dyld_get_image_name(image_index: u32) [*:0]const u8;
1414
1515pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) isize;
16pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int;
1617
1718pub extern "c" fn mach_absolute_time() u64;
1819pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void;
lib/std/os.zig+22-53
......@@ -2531,29 +2531,15 @@ pub const FStatError = error{
25312531
25322532pub fn fstat(fd: fd_t) FStatError!Stat {
25332533 var stat: Stat = undefined;
2534 if (comptime std.Target.current.isDarwin()) {
2535 switch (darwin.getErrno(darwin.@"fstat$INODE64"(fd, &stat))) {
2536 0 => return stat,
2537 EINVAL => unreachable,
2538 EBADF => unreachable, // Always a race condition.
2539 ENOMEM => return error.SystemResources,
2540 EACCES => return error.AccessDenied,
2541 else => |err| return unexpectedErrno(err),
2542 }
2543 }
25442534
2545 if (std.Target.current.os.tag == .netbsd) {
2546 switch (errno(system.__fstat50(fd, &stat))) {
2547 0 => return stat,
2548 EINVAL => unreachable,
2549 EBADF => unreachable, // Always a race condition.
2550 ENOMEM => return error.SystemResources,
2551 EACCES => return error.AccessDenied,
2552 else => |err| return unexpectedErrno(err),
2553 }
2554 }
2535 const symbol_name = if (comptime std.Target.current.isDarwin())
2536 "fstat$INODE64"
2537 else if (std.Target.current.os.tag == .netbsd)
2538 "__fstat50"
2539 else
2540 "fstat";
25552541
2556 switch (errno(system.fstat(fd, &stat))) {
2542 switch (errno(@field(system, symbol_name)(fd, &stat))) {
25572543 0 => return stat,
25582544 EINVAL => unreachable,
25592545 EBADF => unreachable, // Always a race condition.
......@@ -3413,16 +3399,12 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
34133399 return;
34143400 }
34153401
3416 if (std.Target.current.os.tag == .netbsd) {
3417 switch (errno(system.__clock_gettime50(clk_id, tp))) {
3418 0 => return,
3419 EFAULT => unreachable,
3420 EINVAL => return error.UnsupportedClock,
3421 else => |err| return unexpectedErrno(err),
3422 }
3423 }
3402 const symbol_name = if (std.Target.current.os.tag == .netbsd)
3403 "__clock_gettime50"
3404 else
3405 "clock_gettime";
34243406
3425 switch (errno(system.clock_gettime(clk_id, tp))) {
3407 switch (errno(@field(system, symbol_name)(clk_id, tp))) {
34263408 0 => return,
34273409 EFAULT => unreachable,
34283410 EINVAL => return error.UnsupportedClock,
......@@ -3444,16 +3426,12 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
34443426 return;
34453427 }
34463428
3447 if (std.Target.current.os.tag == .netbsd) {
3448 switch (errno(system.__clock_getres50(clk_id, res))) {
3449 0 => return,
3450 EFAULT => unreachable,
3451 EINVAL => return error.UnsupportedClock,
3452 else => |err| return unexpectedErrno(err),
3453 }
3454 }
3429 const symbol_name = if (std.Target.current.os.tag == .netbsd)
3430 "__clock_getres50"
3431 else
3432 "clock_getres";
34553433
3456 switch (errno(system.clock_getres(clk_id, res))) {
3434 switch (errno(@field(system, symbol_name)(clk_id, res))) {
34573435 0 => return,
34583436 EFAULT => unreachable,
34593437 EINVAL => return error.UnsupportedClock,
......@@ -3519,21 +3497,12 @@ pub const SigaltstackError = error{
35193497} || UnexpectedError;
35203498
35213499pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
3522 if (builtin.os.tag == .windows or builtin.os.tag == .uefi or builtin.os.tag == .wasi)
3523 @compileError("std.os.sigaltstack not available for this target");
3524
3525 if (std.Target.current.os.tag == .netbsd) {
3526 switch (errno(system.__sigaltstack14(ss, old_ss))) {
3527 0 => return,
3528 EFAULT => unreachable,
3529 EINVAL => unreachable,
3530 ENOMEM => return error.SizeTooSmall,
3531 EPERM => return error.PermissionDenied,
3532 else => |err| return unexpectedErrno(err),
3533 }
3534 }
3500 const symbol_name = if (std.Target.current.os.tag == .netbsd)
3501 "__sigaltstack14"
3502 else
3503 "sigaltstack";
35353504
3536 switch (errno(system.sigaltstack(ss, old_ss))) {
3505 switch (errno(@field(system, symbol_name)(ss, old_ss))) {
35373506 0 => return,
35383507 EFAULT => unreachable,
35393508 EINVAL => unreachable,