modfetch/modules/os.c

70 lines
1.5 KiB
C
Raw Normal View History

2024-02-10 00:04:36 +01:00
/* OS - operating system module for modfetch
*
* author: jacekpoz
* 09 Feb 2024
*/
2024-02-09 12:05:06 +01:00
#include "../mod.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
2024-02-10 14:11:26 +01:00
static const semver _version = {
.major = 0,
.minor = 1,
.patch = 0,
};
semver version(void) { return _version; }
2024-02-10 14:51:50 +01:00
const char *name(void) { return "os"; }
2024-02-09 12:05:06 +01:00
2024-02-11 19:37:41 +01:00
uint8_t init(semver api_ver, char **config) {
2024-02-11 17:50:10 +01:00
if (!sveq(api_ver, API_VERSION)) {
return MFERR_APIVER;
}
2024-02-09 12:05:06 +01:00
(void)config;
2024-02-11 17:50:10 +01:00
return 0;
2024-02-09 12:05:06 +01:00
}
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;
}