more crimes

This commit is contained in:
ptrcnull 2023-11-13 03:32:18 +01:00
parent d483eb1bcd
commit 4e69ce3cc5
Signed by untrusted user: ptrcnull
GPG key ID: 411F7B30801DD9CA

View file

@ -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);
}