File: archlinux.c

package info (click to toggle)
ifupdown 0.8.44
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 952 kB
  • sloc: ansic: 2,781; sh: 899; perl: 513; makefile: 90
file content (49 lines) | stat: -rw-r--r-- 1,034 bytes parent folder | download | duplicates (4)
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
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <fnmatch.h>
#include <err.h>

#include "archcommon.h"

bool variable_match(const char *iface, const char *variable, const char *pattern) {
	// Map platform-independent variables to sysfs names
	if(!strcasecmp(variable, "mac"))
		variable = "address";

	// Open the corresponding sysfs file
	char *filename = NULL;
	if(asprintf(&filename, "/sys/class/net/%s/%s", iface, variable) == -1 || !filename)
		errx(1, "asprintf");

	// Shortcut: * tests for file presence
	if(!strcmp(pattern, "*"))
		return access(filename, F_OK);

	FILE *f = fopen(filename, "r");
	if(!f)
		return false;

	// Match against any line
	char buf[1024];
	bool found = false;
	while(fgets(buf, sizeof buf, f)) {
		// strip newline
		size_t len = strlen(buf);
		if(len && buf[len - 1] == '\n')
			buf[len - 1] = 0;

		if(fnmatch(pattern, buf, FNM_EXTMATCH) == 0) {
			found = true;
			break;
		}
	}

	fclose(f);

	return found;
}