]>
Commit | Line | Data |
---|---|---|
6f248ce1 TL |
1 | #define _GNU_SOURCE |
2 | ||
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
6 | #include <unistd.h> | |
7 | #include <sys/utsname.h> | |
8 | #include <fnmatch.h> | |
9 | #include <err.h> | |
10 | ||
11 | #include "archcommon.h" | |
12 | ||
13 | bool variable_match(const char *iface, const char *variable, const char *pattern) { | |
14 | // Map platform-independent variables to sysfs names | |
15 | if(!strcasecmp(variable, "mac")) | |
16 | variable = "address"; | |
17 | ||
18 | // Open the corresponding sysfs file | |
19 | char *filename = NULL; | |
20 | if(asprintf(&filename, "/sys/class/net/%s/%s", iface, variable) == -1 || !filename) | |
21 | errx(1, "asprintf"); | |
22 | ||
23 | // Shortcut: * tests for file presence | |
24 | if(!strcmp(pattern, "*")) | |
25 | return access(filename, F_OK); | |
26 | ||
27 | FILE *f = fopen(filename, "r"); | |
28 | if(!f) | |
29 | return false; | |
30 | ||
31 | // Match against any line | |
32 | char buf[1024]; | |
33 | bool found = false; | |
34 | while(fgets(buf, sizeof buf, f)) { | |
35 | // strip newline | |
36 | size_t len = strlen(buf); | |
37 | if(len && buf[len - 1] == '\n') | |
38 | buf[len - 1] = 0; | |
39 | ||
40 | if(fnmatch(pattern, buf, FNM_EXTMATCH) == 0) { | |
41 | found = true; | |
42 | break; | |
43 | } | |
44 | } | |
45 | ||
46 | fclose(f); | |
47 | ||
48 | return found; | |
49 | } |