-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiman.c
64 lines (54 loc) · 1.4 KB
/
wifiman.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <regex.h>
#define WIFI_INTERFACE_PATTERN "^wlp[0-9]+s[0-9]+|^wlan[0-9]+"
int wifi_is_enabled() {
struct ifaddrs *ifaddr, *ifa;
regex_t regex;
int result;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
if (regcomp(®ex, WIFI_INTERFACE_PATTERN, REG_EXTENDED) != 0) {
printf("Failed to compile regex.\n");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_name != NULL && regexec(®ex, ifa->ifa_name, 0, NULL, 0) == 0) {
freeifaddrs(ifaddr);
regfree(®ex);
return 1;
}
}
freeifaddrs(ifaddr);
regfree(®ex);
return 0;
}
void write_to_file(const char* filename, const char* str) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file\n");
return;
}
fprintf(file, "%s", str);
fclose(file);
}
void enable_wifi() {
write_to_file("/sys/module/wlan/parameters/fwpath", "");
usleep(100000);
write_to_file("/sys/module/wlan/parameters/fwpath", "sta");
}
int main() {
while (1) {
if (!wifi_is_enabled()) {
printf("dead, re-enabling");
enable_wifi();
}
sleep(1);
}
return 0;
}