63 lines
1.4 KiB
C
63 lines
1.4 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 _version = {
|
|
.major = 0,
|
|
.minor = 1,
|
|
.patch = 0,
|
|
};
|
|
|
|
semver version(void) { return _version; }
|
|
const char *name(void) { return "os"; }
|
|
|
|
void init(char **config) {
|
|
(void)config;
|
|
}
|
|
|
|
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;
|
|
}
|