aboutsummaryrefslogtreecommitdiffstats
path: root/linux/Battery.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2020-08-27 07:48:10 +0200
committerDaniel Lange <DLange@git.local>2020-08-27 07:48:10 +0200
commitf3147ea2d1598914c2db53e8cfb34c8ff81e2ff4 (patch)
tree3ee82b2af2ab3d38b6e4b07f3994516aac72f742 /linux/Battery.c
parentdf568a576f7b44ac5a2b9b7222c7f39d9932f626 (diff)
downloaddebian_htop-f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4.tar.gz
debian_htop-f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4.tar.bz2
debian_htop-f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4.zip
New upstream version 3.0.0upstream/3.0.0
Diffstat (limited to 'linux/Battery.c')
-rw-r--r--linux/Battery.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/linux/Battery.c b/linux/Battery.c
index aedacab..4014a50 100644
--- a/linux/Battery.c
+++ b/linux/Battery.c
@@ -64,7 +64,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
}
char* line = NULL;
- for (unsigned short int i = 0; i < lineNum; i++) {
+ for (unsigned short int j = 0; j < lineNum; j++) {
free(line);
line = String_readLine(file);
if (!line) break;
@@ -78,7 +78,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
const unsigned long int foundNum = atoi(foundNumStr);
free(foundNumStr);
free(line);
-
+
total += foundNum;
}
@@ -175,7 +175,7 @@ static inline ssize_t xread(int fd, void *buf, size_t count) {
}
static void Battery_getSysData(double* level, ACPresence* isOnAC) {
-
+
*level = 0;
*isOnAC = AC_ERROR;
@@ -193,17 +193,27 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
char* entryName = (char *) dirEntry->d_name;
const char filePath[50];
- if (entryName[0] == 'B' && entryName[1] == 'A' && entryName[2] == 'T') {
-
+ xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/type", entryName);
+ int fd1 = open(filePath, O_RDONLY);
+ if (fd1 == -1)
+ continue;
+
+ char type[8];
+ ssize_t typelen = xread(fd1, type, 7);
+ close(fd1);
+ if (typelen < 1)
+ continue;
+
+ if (type[0] == 'B' && type[1] == 'a' && type[2] == 't') {
xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
- int fd = open(filePath, O_RDONLY);
- if (fd == -1) {
+ int fd2 = open(filePath, O_RDONLY);
+ if (fd2 == -1) {
closedir(dir);
return;
}
char buffer[1024];
- ssize_t buflen = xread(fd, buffer, 1023);
- close(fd);
+ ssize_t buflen = xread(fd2, buffer, 1023);
+ close(fd2);
if (buflen < 1) {
closedir(dir);
return;
@@ -247,20 +257,20 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
if (*isOnAC != AC_ERROR) {
continue;
}
-
+
xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
- int fd = open(filePath, O_RDONLY);
- if (fd == -1) {
+ int fd3 = open(filePath, O_RDONLY);
+ if (fd3 == -1) {
closedir(dir);
return;
}
char buffer[2] = "";
for(;;) {
- ssize_t res = read(fd, buffer, 1);
+ ssize_t res = read(fd3, buffer, 1);
if (res == -1 && errno == EINTR) continue;
break;
}
- close(fd);
+ close(fd3);
if (buffer[0] == '0') {
*isOnAC = AC_ABSENT;
} else if (buffer[0] == '1') {

© 2014-2024 Faster IT GmbH | imprint | privacy policy