| ... | ... | @@ -14,16 +14,20 @@ pub const visibility: std.builtin.SymbolVisibility = if (linkage != .internal) |
| 14 | 14 | else |
| 15 | 15 | .default; |
| 16 | 16 | |
| 17 | | /// Checks whether the syscall has had an error, storing it in `std.c.errno` and returning -1. |
| 18 | | /// Otherwise returns the result. |
| 19 | | pub fn linuxErrno(r: usize) isize { |
| 20 | | const linux = std.os.linux; |
| 21 | | |
| 22 | | return switch (linux.errno(r)) { |
| 23 | | .SUCCESS => @bitCast(r), |
| 24 | | else => |err| blk: { |
| 25 | | std.c._errno().* = @intFromEnum(err); |
| 26 | | break :blk -1; |
| 17 | /// Given a low-level syscall return value, sets errno and returns `-1`, or on |
| 18 | /// success returns the result. |
| 19 | pub fn errno(syscall_return_value: usize) c_int { |
| 20 | return switch (builtin.os.tag) { |
| 21 | .linux => { |
| 22 | const signed: isize = @bitCast(syscall_return_value); |
| 23 | const casted: c_int = @intCast(signed); |
| 24 | if (casted < 0) { |
| 25 | @branchHint(.unlikely); |
| 26 | std.c._errno().* = -casted; |
| 27 | return -1; |
| 28 | } |
| 29 | return casted; |
| 27 | 30 | }, |
| 31 | else => comptime unreachable, |
| 28 | 32 | }; |
| 29 | 33 | } |