2zw

fix unreachable code on OpenBSD

b26fc21c5b78ec4e4af76b293ebc2e116aeec6a6

SM <seb.michalk@gmail.com>

2026-01-09 21:04:49 +0000

 src/bar.zig | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/bar.zig b/src/bar.zig
index 19908da..a4f4106 100644
--- a/src/bar.zig
+++ b/src/bar.zig
@@ -488,22 +488,20 @@ fn readbat_linux() !u8 {
 }
 
 fn readbat_openbsd(alloc: std.mem.Allocator) !u8 {
-    const run_res = try std.process.Child.run(.{
-        .allocator = alloc,
-        .argv = &.{ "sysctl", "-n", "hw.sensors.acpibat0.raw0" },
-        .max_output_bytes = 256,
-    });
-    defer alloc.free(run_res.stdout);
-    defer alloc.free(run_res.stderr);
+    var child = std.process.Child.init(&.{ "sysctl", "-n", "hw.sensors.acpibat0.raw0" }, alloc);
+    child.stdout_behavior = .Pipe;
+    child.stderr_behavior = .Ignore;
+    try child.spawn();
+    defer _ = child.kill() catch {};
 
-    const term = run_res.term;
-    switch (term) {
-        .Exited => |code| if (code != 0) return error.ChildExitedWithError,
-        else => return error.ChildExitedWithError,
-    }
+    const stdout_file = child.stdout orelse return error.MissingStdout;
+    defer stdout_file.close();
 
-    if (run_res.stdout.len == 0) return error.EndOfStream;
-    const trimmed = std.mem.trim(u8, run_res.stdout, &std.ascii.whitespace);
+    var buf: [128]u8 = undefined;
+    const read_bytes = try stdout_file.readAll(&buf);
+    if (read_bytes == 0) return error.EndOfStream;
+
+    const trimmed = std.mem.trim(u8, buf[0..read_bytes], &std.ascii.whitespace);
 
     // Happy path: sysctl prints "XX.XX%" or "XX%".
     if (std.mem.indexOfScalar(u8, trimmed, '%')) |percent_idx| {
@@ -522,7 +520,6 @@ fn parsePercent(text: []const u8) !u8 {
     return @as(u8, @intCast(rounded));
 }
 
-
 var g_bar: ?*Bar = null;
 
 pub fn setGlobalBar(bar_ptr: ?*Bar) void {