-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower.c
91 lines (66 loc) · 1.67 KB
/
power.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "includes.h"
// Use config file
// to get path to
// POWER SUPPLY information
//
void find_info(char* bat_path) {
char buf[MAX_LEN] = {0};
char key[MAX_LEN] = {0};
char value[MAX_LEN] = {0};
char config_path[MAX_LEN] = {0};
char config_file_path[MAX_LEN * 2] = {0};
char* env_config;
char* home;
// First, looks if the
// environment variable: POWPRINTPATH
// is set
env_config = getenv("POWPRINTPATH");
if (env_config)
strcpy(config_path, env_config);
else {
home = getenv("HOME");
snprintf(config_path, sizeof config_path, "%s/%s", home, CONFIG_DEF_PATH);
}
snprintf(config_file_path, sizeof config_file_path, "%s/%s", config_path, CONFIG_FILE);
FILE* fp = fopen(config_file_path, "r");
if (fp == NULL) {
perror("configuration file ");
_exit(EXIT_FAILURE);
}
fgets(buf, sizeof buf, fp);
parse_line(buf, key, value);
if (strcmp(key, "PATH") == 0)
strcpy(bat_path, value);
}
//
// Reads the file in 'path' for information
// about the battery, and stores it in the
// 'info' struct
//
void get_info(struct power_info* info, char* path) {
char buf[LINE_LEN] = {0};
char key[MAX_LEN] = {0};
char value[MAX_LEN] = {0};
FILE* file = fopen(path, "r");
while (fgets(buf, sizeof buf, file) != NULL) {
parse_line(buf, key, value);
store_info(info, key, value);
}
}
//
// Prints final information
//
void print_info(struct power_info* info) {
printf("Power Information: STATUS:[%s] CAPACITY:[%s] HEALTH:[%s] TECH:[%s]\n",
info->status,
info->capacity,
info->health,
info->technology);
}
void power_print() {
char path[MAX_LEN] = {0};
struct power_info info = {0};
find_info(path);
get_info(&info, path);
print_info(&info);
}