From 4e69ce3cc57c0a73c6196bc882d9198898da5c18 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Mon, 13 Nov 2023 03:32:18 +0100 Subject: [PATCH] more crimes --- src/main.zig | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/main.zig b/src/main.zig index 5ccbbb3..748dbf8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -18,19 +18,13 @@ pub fn main() !void { std.process.exit(1); } - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - defer _ = gpa.deinit(); - const allocator = gpa.allocator(); - - const args = try std.process.argsAlloc(allocator); - defer std.process.argsFree(allocator, args); - var target: u8 = 15; var battery: []const u8 = "BAT0"; + var argsIter = std.process.args(); + var parse_mode = ParseMode.none; - for (args, 0..) |arg, i| { - _ = i; + while (argsIter.next()) |arg| { switch (parse_mode) { ParseMode.none => { if (std.mem.eql(u8, arg, "--help")) { @@ -72,20 +66,23 @@ pub fn main() !void { } var sent = false; - const batteryPath = try std.fmt.allocPrint(allocator, "/sys/class/power_supply/{s}/capacity", .{battery}); + var batBuf: [256]u8 = undefined; + var capBuf: [8]u8 = undefined; + + const batteryPath = try std.fmt.bufPrint(&batBuf, "/sys/class/power_supply/{s}/capacity", .{battery}); while (true) { const capacityFile = std.fs.openFileAbsolute(batteryPath, .{}) catch |err| { std.log.err("open {s}: {}", .{ batteryPath, err }); std.process.exit(1); }; - var capacityStr = std.ArrayList(u8).init(allocator); - try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null); + _ = try capacityFile.reader().readAll(&capBuf); capacityFile.close(); + var splat = std.mem.splitScalar(u8, &capBuf, '\n'); - // std.log.info("battery: {s}", .{capacityStr.items}); + // std.log.info("battery: {s}", .{splat.first()}); - const capacity = try std.fmt.parseInt(u8, capacityStr.items, 10); + const capacity = try std.fmt.parseInt(u8, splat.first(), 10); // std.log.info("battery (but parsed): {}", .{capacity}); if (capacity <= target) { @@ -108,7 +105,6 @@ pub fn main() !void { } else { sent = false; } - capacityStr.deinit(); std.time.sleep(10 * 1e9); }