parent
9242a48152
commit
69df0bd3fd
@ -1,10 +1,83 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <sys/types.h> |
||||
#include <sys/stat.h> |
||||
#include <fcntl.h> |
||||
#include <dirent.h> |
||||
#include <poll.h> |
||||
#include <errno.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
|
||||
char *bat_power_path = "/sys/class/power_supply/BAT1/energy_now"; |
||||
char *bat_capacity_path = "/sys/class/power_supply/BAT1/capacity"; |
||||
//char *bat_power_path = "/sys/class/power_supply/BAT1/energy_now";
|
||||
//char *bat_capacity_path = "/sys/class/power_supply/BAT1/capacity";
|
||||
|
||||
const char *bat_base_path = "/sys/class/power_supply/"; |
||||
|
||||
void usage (void) |
||||
{ |
||||
printf("usage: bmon [-w]\n"); |
||||
} |
||||
|
||||
int main (int argc, char *argv[]) |
||||
{ |
||||
int wflag = 0; |
||||
int opc; |
||||
while ((opc = getopt(argc, argv, "w")) != -1) { |
||||
switch (opc) { |
||||
case 'w': |
||||
wflag = 1; |
||||
break; |
||||
default: /* '?' */ |
||||
usage(); |
||||
exit(1); |
||||
break;
|
||||
} |
||||
|
||||
} |
||||
|
||||
DIR *bdir = NULL; |
||||
if (!(bdir = opendir(bat_base_path))) |
||||
perror("error opening base directory"); |
||||
|
||||
struct dirent *ent = NULL; |
||||
int *fd_list = NULL; |
||||
int fd_num = 0; |
||||
for (;;) { |
||||
errno = 0; |
||||
if (!(ent = readdir(bdir))) { |
||||
if (errno) |
||||
perror("error reading base directory"); |
||||
else break; |
||||
} |
||||
|
||||
if (!strstr(ent->d_name, "BAT1")) |
||||
continue; |
||||
|
||||
int fd; |
||||
char path[sizeof(bat_base_path) + 256 + 64]; |
||||
strcpy(path, bat_base_path); |
||||
strcat(path, ent->d_name); |
||||
if (wflag) |
||||
strcat(path, "/energy_now"); |
||||
else strcat(path, "/capacity"); |
||||
|
||||
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) != -1) { |
||||
int *tmp = NULL; |
||||
tmp = realloc(fd_list, sizeof(int) * (fd_num + 1)); |
||||
if (!tmp) |
||||
perror("failed realloc"); |
||||
fd_list = tmp; |
||||
fd_list[fd_num] = fd; |
||||
fd_num++; |
||||
} else printf("err: %s\n", strerror(errno)); |
||||
|
||||
} |
||||
closedir(bdir); |
||||
if (!fd_num) |
||||
printf("no files were opened\n"); |
||||
|
||||
for (int i = 0; i < fd_num; i++) |
||||
close(fd_list[i]); |
||||
return 0; |
||||
} |
||||
|
Loading…
Reference in new issue