batterystatus.c - sites - public wiki contents of suckless.org | |
git clone git://git.suckless.org/sites | |
Log | |
Files | |
Refs | |
--- | |
batterystatus.c (1218B) | |
--- | |
1 #define BATT_NOW "/sys/class/power_supply/BAT0/charge_now" | |
2 #define BATT_FULL "/sys/class/power_supply/BAT0/charge_full" | |
3 #define BATT_STATUS "/sys/class/power_supply/BAT0/status" | |
4 | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <string.h> | |
8 #include <errno.h> | |
9 | |
10 char * | |
11 smprintf(char *fmt, ...) | |
12 { | |
13 va_list fmtargs; | |
14 char *buf = NULL; | |
15 | |
16 va_start(fmtargs, fmt); | |
17 if (vasprintf(&buf, fmt, fmtargs) == -1){ | |
18 fprintf(stderr, "malloc vasprintf\n"); | |
19 exit(1); | |
20 } | |
21 va_end(fmtargs); | |
22 | |
23 return buf; | |
24 } | |
25 | |
26 char * | |
27 getbattery(){ | |
28 long lnum1, lnum2 = 0; | |
29 char *status = malloc(sizeof(char)*12); | |
30 char s = '?'; | |
31 FILE *fp = NULL; | |
32 if ((fp = fopen(BATT_NOW, "r"))) { | |
33 fscanf(fp, "%ld\n", &lnum1); | |
34 fclose(fp); | |
35 fp = fopen(BATT_FULL, "r"); | |
36 fscanf(fp, "%ld\n", &lnum2); | |
37 fclose(fp); | |
38 fp = fopen(BATT_STATUS, "r"); | |
39 fscanf(fp, "%s\n", status); | |
40 fclose(fp); | |
41 if (strcmp(status,"Charging") == 0) | |
42 s = '+'; | |
43 if (strcmp(status,"Discharging") == 0) | |
44 s = '-'; | |
45 if (strcmp(status,"Full") == 0) | |
46 s = '='; | |
47 return smprintf("%c%ld%%", s,(lnum1/(lnum2/100))); | |
48 } | |
49 else return smprintf(""); | |
50 } | |
51 | |
52 | |
53 |