]> git.klezlab.it Git - klez/bat.git/commitdiff
initial
authorFederico klez Culloca <klez@klezlab.it>
Sat, 13 Jun 2026 19:40:57 +0000 (21:40 +0200)
committerFederico klez Culloca <klez@klezlab.it>
Sat, 13 Jun 2026 19:40:57 +0000 (21:40 +0200)
Makefile [new file with mode: 0644]
bat.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..a7204f2
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,11 @@
+bat : bat.c
+               cc -o bat bat.o -std=c23
+
+install : bat
+               cp bat ~/bin/bat
+
+clean :
+               rm bat
+
+check : bat.c
+               cppcheck bat.c --std=c23 --check-level=exhaustive
diff --git a/bat.c b/bat.c
new file mode 100644 (file)
index 0000000..849a897
--- /dev/null
+++ b/bat.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <limits.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <machine/apmvar.h>
+
+int main(void)
+{
+       const int apmf = open("/dev/apm", O_RDONLY);
+
+       if (apmf == -1) {
+               goto print_error;
+       }
+
+       struct apm_power_info api;
+
+       if (ioctl(apmf, APM_IOC_GETPOWER, &api) == 0) {
+               if ( close(apmf) != 0 ) {
+                       goto print_error;
+               }
+
+               const char* ac_state = api.ac_state == APM_AC_OFF ? "BAT" : "AC";
+
+               printf("%s - %hhu%%"
+                               , ac_state
+                               , api.battery_life);
+
+               if (api.minutes_left != UINT_MAX) {
+                       printf(" - %u min", api.minutes_left);
+               }
+
+        printf("\n");
+
+               return EXIT_SUCCESS;
+       }
+
+print_error:
+       fprintf(stderr, "Error: %s", strerror(errno));
+       return EXIT_FAILURE;
+}