1
0
Fork 0
forked from poz/modfetch
modfetch/modules/os.c

54 lines
1.3 KiB
C
Raw Normal View History

2024-02-09 12:05:06 +01:00
#include "../mod.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
uint8_t version_major(void) { return 0; }
uint8_t version_minor(void) { return 1; }
uint8_t version_patch(void) { return 0; }
const char *module_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;
}