-
Notifications
You must be signed in to change notification settings - Fork 0
/
batcommon.c
52 lines (45 loc) · 880 Bytes
/
batcommon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* common battery routines */
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "batcommon.h"
int
batinit(char **full, char **now, const char *path)
{
*full = malloc(strlen(path) + sizeof("/energy_full"));
if(*full == NULL)
return -1;
*now = malloc(strlen(path) + sizeof("/energy_now"));
if(*now == NULL){
free(*full);
return -1;
}
strcpy(*full, path);
strcat(*full, "/energy_full");
strcpy(*now, path);
strcat(*now, "/energy_now");
return 0;
}
unsigned long long
readull(const char *file)
{
int fd;
char buf[22];
ssize_t r;
unsigned long long ret;
if((fd = open(file, O_RDONLY)) == -1)
return 0;
r = read(fd, buf, sizeof(buf));
close(fd);
if(r < 1)
return 0;
errno = 0;
ret = strtoull(buf, NULL, 10);
if(errno)
return 0;
return ret;
}