-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys.c
227 lines (176 loc) · 6.09 KB
/
sys.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include "sys.h"
#include "main.h"
#if __APPLE__
// required for determining root path (apple only)
#include <libproc.h>
#endif
void read_battery_info__ubuntu(battery_t *battery, FILE *batteryinfo);
void read_battery_info__macos(battery_t *battery, FILE *batteryinfo);
// function to change the working directory to that of the codebase's root
// (useful for later file writing with relative paths)
void set_working_dir(void) {
// determine the absolute path of the codebase's root
char *path_buffer = malloc(MAX_BUFFER_SIZE);
ssize_t sz;
// determine path of current process (dependent on OS)
#if __APPLE__
sz = proc_pidpath(getpid(), path_buffer, MAX_BUFFER_SIZE);
#elif __linux__
sz = readlink("/proc/self/exe", path_buffer, MAX_BUFFER_SIZE);
#else
perror("Unsupported operating system.");
exit(EXIT_FAILURE);
#endif
printf("[%s]\n", path_buffer);
// Find final directory separator of path
while (sz > 0 && path_buffer[sz-1] != '/') { --sz; }
// Truncate string to directory name only
path_buffer[sz] = '\0';
// change directory to path
if (chdir(path_buffer) != 0) {
perror("Failed to change working directory to codebase root");
exit(EXIT_FAILURE);
}
printf("[%s]\n", path_buffer);
// printing current working directory
char s[100];
printf("wd = %s\n", getcwd(s, 100));
}
op_sys_t determine_os(void) {
// determine operating system by compiler flags
#if __APPLE__
printf("Apple OS detected.\n");
// apple operating system
return MACOS;
#elif __linux__
printf("Linux OS detected.\n");
// linux operating system
return LINUX;
#else
perror("Unsupported operating system.");
exit(EXIT_FAILURE);
#endif
}
void read_battery_info(battery_t *battery, op_sys_t op_sys) {
int info_status;
void (*read_battery_info__func)(battery_t *battery, FILE *batteryinfo);
if (op_sys == LINUX) {
info_status = system(BATTERY_INFO__LINUX);
read_battery_info__func = read_battery_info__ubuntu;
} else if (op_sys == MACOS) {
info_status = system(BATTERY_INFO__MACOS);
read_battery_info__func = read_battery_info__macos;
} else {
perror("Unsupported operating sytem.");
exit(EXIT_FAILURE);
}
if (info_status != SYSTEM_SUCCESS) {
perror("Failed to execute BATTERY_INFO command");
exit(EXIT_FAILURE);
}
FILE *batteryinfo = fopen(BATTERY_INFO_PATH, "r");
if (batteryinfo == NULL) {
perror("Failed to read battery information file");
exit(EXIT_FAILURE);
}
read_battery_info__func(battery, batteryinfo);
// if file is empty then device does not have battery/access to battery failed
if (ftell(batteryinfo) == 0) {
perror("Failed to access battery or device does not have a battery");
exit(EXIT_FAILURE);
}
if (fclose(batteryinfo) != 0) {
perror("Failed to close battery information file");
exit(EXIT_FAILURE);
}
}
static void update_battery(battery_t *battery, state_t state, int percentage) {
if (percentage < 0 || percentage > 100) {
perror("Invalid percentage passed as parameter to update battery");
exit(EXIT_FAILURE);
}
if (state != CHARGING && state != DISCHARGING) {
perror("Invalid state passed as parameter to update battery");
exit(EXIT_FAILURE);
}
battery->state = state;
battery->percentage = percentage;
}
static char *skip_white_space(char *str) {
while(*str == ' '){str++;}
return str;
}
///////////////////////// LINUX /////////////////////////
void read_battery_info__ubuntu(battery_t *battery, FILE *batteryinfo) {
char buff[MAX_BUFFER_SIZE + 1];
char status[MAX_BUFFER_SIZE + 1];
int percentage = -1;
while (fgets(buff, MAX_BUFFER_SIZE, batteryinfo)) {
// get ptr to label of this line
char *label_ptr = skip_white_space(buff);
strtok(label_ptr, ":");
// get following token (the information) up to the newline character
char *info_ptr;
info_ptr = strtok(NULL, "\n");
info_ptr = skip_white_space(info_ptr);
if (strcmp(label_ptr, "state") == 0) {
strcpy(status, info_ptr);
} else {
percentage = strtol(info_ptr, NULL, 10);
}
}
if (percentage == -1) {
perror("Failed to parse percentage in read_battery_info__linux");
exit(EXIT_FAILURE);
}
// initiliase battery struct using information from system
if (strcmp(status, "fully-charged") == 0 || strcmp(status, "charging") == 0) {
update_battery(battery, CHARGING, percentage);
} else if (strcmp(status, "discharging") == 0) {
update_battery(battery, DISCHARGING, percentage);
} else {
perror("Could not read battery status");
exit(EXIT_FAILURE);
}
}
///////////////////////// macOS /////////////////////////
void read_battery_info__macos(battery_t *battery, FILE *batteryinfo) {
char buff[MAX_BUFFER_SIZE + 1];
int percentage = -1;
char status[MAX_BUFFER_SIZE + 1];
// disregard first line of file, store second line (which will contain
// the relevant data into buff
fgets(buff, MAX_BUFFER_SIZE, batteryinfo);
fgets(buff, MAX_BUFFER_SIZE, batteryinfo);
char *str_ptr = buff;
for (; *str_ptr; str_ptr++) {
if (*str_ptr == '%') {
// % symbol reached, so read the 3 preceding non-whitespace chars
// as the charge percentage of the battery
percentage = strtol(skip_white_space(str_ptr - 3), NULL, 10);
break;
}
}
if (percentage == -1) {
perror("Failed to parse percentage in read_battery_info__macos");
exit(EXIT_FAILURE);
}
// (strptr + 2) represents start of state field, which contains leading
// whitespace and is terminated by a ';'
str_ptr += 2;
strtok(str_ptr, ";");
strcpy(status, skip_white_space(str_ptr));
// initiliase battery struct using information from system
if (strcmp(status, "fully-charged") == 0 || strcmp(status, "charged") == 0 || strcmp(status, "charging") == 0) {
update_battery(battery, CHARGING, percentage);
} else {
// battery is either "discharging" or "AC attached; not charging"
// so assume battery is discharging
update_battery(battery, DISCHARGING, percentage);
}
}