summaryrefslogtreecommitdiff
path: root/src/util.c
blob: 1f6fd414b275fcf74ee9339a5b24af12346bba0b (plain)
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 "util.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>

gpointer
cmumble_find_by_id(GList *list, gsize member_offset, guint id)
{
	gpointer el = NULL;
	GList *l;

	for (l = list; l; l = l->next) {
		if (G_STRUCT_MEMBER(uint32_t, l->data, member_offset) == id) {
			el = l->data;
			break;
		}
	}

	return el;
}

gchar *
cmumble_get_os_name(void)
{
	FILE *f;
	char *line = NULL;
	char *os = NULL, *value, *end;
	size_t key_len = strlen("PRETTY_NAME=");
	size_t n;

	f = fopen("/etc/os-release", "r");
	if (f == NULL)
		return NULL;

	while (getline(&line, &n, f) != -1) {
		if (strncmp("PRETTY_NAME=", line, key_len) == 0 && (n - key_len) > 1) {
			value = &line[key_len];
			if (strlen(value) == 0)
				continue;

			end = &value[strlen(value) - 1];
			if (*end == '\n')
				*end-- = '\0';

			if (strlen(value) < 2)
				continue;

			if (value[0] == '"')
				value++;

			if (*end == '"')
				*end = '\0';

			os = strdup(value);
			goto out;
		}
	}

out:
	free(line);
	fclose(f);
	return os;
}