75 lines
1.5 KiB
C
75 lines
1.5 KiB
C
/* OS - operating system module for modfetch
|
|
*
|
|
* author: jacekpoz
|
|
* 09 Feb 2024
|
|
*/
|
|
|
|
#include "../mod.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
|
|
static const semver api_ver = {
|
|
.major = 0,
|
|
.minor = 1,
|
|
.patch = 0,
|
|
};
|
|
|
|
static const semver _version = {
|
|
.major = 0,
|
|
.minor = 1,
|
|
.patch = 0,
|
|
};
|
|
|
|
semver version(void) { return _version; }
|
|
const char *name(void) { return "os"; }
|
|
|
|
uint8_t init(char **config) {
|
|
if (!sveq(api_ver, API_VERSION)) {
|
|
return MFERR_APIVER;
|
|
}
|
|
|
|
(void)config;
|
|
|
|
return 0;
|
|
}
|
|
|
|
const char *get(void) {
|
|
char *ret;
|
|
|
|
FILE *os_release = fopen("/etc/os-release", "r");
|
|
|
|
if (os_release == NULL) {
|
|
fprintf(stderr, "error: failed opening /etc/os-release\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
size_t len = 0;
|
|
ssize_t read;
|
|
char *line = NULL;
|
|
|
|
char *name = malloc(1024 * sizeof(char));
|
|
const char *name_label = "PRETTY_NAME=";
|
|
const size_t name_label_len = strlen(name_label);
|
|
|
|
while ((read = getline(&line, &len, os_release)) != -1) {
|
|
if (strncmp(line, name_label, name_label_len) != 0) {
|
|
continue;
|
|
}
|
|
// + 1 because of "
|
|
line += name_label_len + 1;
|
|
const size_t new_len = strlen(line);
|
|
// the ending "
|
|
line[new_len - 2] = '\0';
|
|
name = line;
|
|
break;
|
|
}
|
|
|
|
if (asprintf(&ret, "OS: %s", name) < 0) {
|
|
fprintf(stderr, "error: failed formatting string (this shouldn't happen)");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
return ret;
|
|
}
|