From 1ab6bf59a6722f816edce15574c2dfcaefeebd31 Mon Sep 17 00:00:00 2001 From: GasInfinity Date: Wed, 21 Jan 2026 12:02:08 +0100 Subject: [PATCH] feat(libzigc): add common linux errno syscall helper --- lib/c/common.zig | 14 ++++++++++++++ lib/c/sys/utsname.zig | 10 +--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/c/common.zig b/lib/c/common.zig index 4419b65cc6..09dc5a8c0c 100644 --- a/lib/c/common.zig +++ b/lib/c/common.zig @@ -13,3 +13,17 @@ pub const visibility: std.builtin.SymbolVisibility = if (linkage != .internal) .hidden else .default; + +/// Checks whether the syscall has had an error, storing it in `std.c.errno` and returning -1. +/// Otherwise returns the result. +pub fn linuxErrno(r: usize) isize { + const linux = std.os.linux; + + return switch (linux.errno(r)) { + .SUCCESS => @bitCast(r), + else => |err| blk: { + std.c._errno().* = @intFromEnum(err); + break :blk -1; + }, + }; +} diff --git a/lib/c/sys/utsname.zig b/lib/c/sys/utsname.zig index 4aab4124ef..06f849ba6d 100644 --- a/lib/c/sys/utsname.zig +++ b/lib/c/sys/utsname.zig @@ -13,15 +13,7 @@ comptime { } fn unameLinux(uts: *std.os.linux.utsname) callconv(.c) c_int { - const linux = std.os.linux; - - return switch (linux.errno(linux.uname(uts))) { - .SUCCESS => 0, - else => |err| blk: { - std.c._errno().* = @intFromEnum(err); - break :blk -1; - }, - }; + return @intCast(common.linuxErrno(std.os.linux.uname(uts))); } fn unameWasi(uts: *std.c.utsname) callconv(.c) c_int {