zig/lib/c/common.zig
Andrew Kelley a2ea36a517 zig libc: modify errno helper to eliminate @intCast
The vast majority of libc functions return `c_int` for the return value,
when setting errno. This utility function is for those cases.

Other cases can hand-roll the logic, or additional helpers can be added.
2026-01-24 20:18:18 +01:00

33 lines
1 KiB
Zig

const builtin = @import("builtin");
const std = @import("std");
pub const linkage: std.builtin.GlobalLinkage = if (builtin.is_test)
.internal
else
.strong;
/// Determines the symbol's visibility to other objects.
/// For WebAssembly this allows the symbol to be resolved to other modules, but will not
/// export it to the host runtime.
pub const visibility: std.builtin.SymbolVisibility = if (linkage != .internal)
.hidden
else
.default;
/// Given a low-level syscall return value, sets errno and returns `-1`, or on
/// success returns the result.
pub fn errno(syscall_return_value: usize) c_int {
return switch (builtin.os.tag) {
.linux => {
const signed: isize = @bitCast(syscall_return_value);
const casted: c_int = @intCast(signed);
if (casted < 0) {
@branchHint(.unlikely);
std.c._errno().* = -casted;
return -1;
}
return casted;
},
else => comptime unreachable,
};
}