From: Scott Worley Date: Fri, 24 Feb 2023 03:02:44 +0000 (-0800) Subject: Make filenames from dir name X-Git-Url: http://git.scottworley.com/batteryviewer/commitdiff_plain/ef0963b824205103ed0d3dab50934f72f5923c5c?hp=7281755a19f8bbeb03e11ef0ff5407df6bf59e1b Make filenames from dir name --- diff --git a/batteryviewer.c b/batteryviewer.c index 540db8e..9f1dedd 100644 --- a/batteryviewer.c +++ b/batteryviewer.c @@ -1,11 +1,27 @@ +// For vasprintf +#define _GNU_SOURCE + #include "chart.h" #include #include #include #include +#include #include #include +char *sasprintf(const char *fmt, ...) { + char *result; + va_list args; + va_start(args, fmt); + if (vasprintf(&result, fmt, args) == -1) { + fprintf(stderr, "Can't allocate memory!?\n"); + abort(); + } + va_end(args); + return result; +} + struct State { char *voltage_filename; char *current_filename; @@ -79,9 +95,10 @@ static void activate(GtkApplication *app, gpointer user_data) { } int main(int argc, char **argv) { + char *battery_dir = "/sys/class/power_supply/BAT0"; struct State state = { - .voltage_filename = "/sys/class/power_supply/BAT0/voltage_now", - .current_filename = "/sys/class/power_supply/BAT0/current_now", + .voltage_filename = sasprintf("%s/voltage_now", battery_dir), + .current_filename = sasprintf("%s/current_now", battery_dir), }; GtkApplication *app = gtk_application_new("com.scottworley.batteryviewer",