2zw

fix crash in openbsd

ef7fd4d1a15a2a6627217d26b79c9d9b60007ec8

SM <seb.michalk@gmail.com>

2026-01-09 20:47:03 +0000

 .gitignore  |  1 +
 src/bar.zig | 26 ++++++++++++++------------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index f52e419..01a7d1a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 zig-cache/
 zig-out/
+.zig-cache/
 TAGS
 .dirlocals
diff --git a/src/bar.zig b/src/bar.zig
index 70e0eaa..19908da 100644
--- a/src/bar.zig
+++ b/src/bar.zig
@@ -488,20 +488,22 @@ fn readbat_linux() !u8 {
 }
 
 fn readbat_openbsd(alloc: std.mem.Allocator) !u8 {
-    var child = std.process.Child.init(&.{ "sysctl", "-n", "hw.sensors.acpibat0.raw0" }, alloc);
-    child.stdout_behavior = .Pipe;
-    child.stderr_behavior = .Ignore;
-    try child.spawn();
-
-    const stdout_file = child.stdout orelse return error.MissingStdout;
-    defer stdout_file.close();
+    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 buf: [64]u8 = undefined;
-    const read_bytes = try stdout_file.readAll(&buf);
-    _ = try child.wait();
-    if (read_bytes == 0) return error.EndOfStream;
+    const term = run_res.term;
+    switch (term) {
+        .Exited => |code| if (code != 0) return error.ChildExitedWithError,
+        else => return error.ChildExitedWithError,
+    }
 
-    const trimmed = std.mem.trim(u8, buf[0..read_bytes], &std.ascii.whitespace);
+    if (run_res.stdout.len == 0) return error.EndOfStream;
+    const trimmed = std.mem.trim(u8, run_res.stdout, &std.ascii.whitespace);
 
     // Happy path: sysctl prints "XX.XX%" or "XX%".
     if (std.mem.indexOfScalar(u8, trimmed, '%')) |percent_idx| {