feat: get os pretty name
Browse files
+39 −12 src/main.c
| @@ -1,25 +1,52 @@ | |||
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | #include <sys/utsname.h> | 2 | #include <sys/utsname.h> |
| 3 | +#include <string.h> | ||
| 3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 4 | 5 | ||
| 5 | static const char asciiArt[] = | 6 | static const char asciiArt[] = |
| 6 | -" \n" | 7 | +" \n" |
| 7 | -" ██████ \n" | 8 | +" ██████ \n" |
| 8 | -" ██████████ \n" | 9 | +" ██████████ \n" |
| 9 | -" ███ ███ \n" | 10 | +" ███ ███ \n" |
| 10 | -" ███ \n" | 11 | +" ███ \n" |
| 11 | -" ████████ \n" | 12 | +" ████████ \n" |
| 12 | -" █████████ \n" | 13 | +" █████████ \n" |
| 13 | -" ███ \n" | 14 | +" ███ \n" |
| 14 | -" ███ ███ \n" | 15 | +" ███ ███ \n" |
| 15 | -" ██████████ \n" | 16 | +" ██████████ \n" |
| 16 | -" ██████ \n" | 17 | +" ██████ \n" |
| 17 | " "; | 18 | " "; |
| 18 | 19 | ||
| 19 | void printAscii(void) { | 20 | void printAscii(void) { |
| 20 | printf("%s\n\n", asciiArt); | 21 | printf("%s\n\n", asciiArt); |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 24 | +void printOS(void) { | ||
| 25 | + FILE *fp = fopen("/etc/os-release", "r"); | ||
| 26 | + if (!fp) { | ||
| 27 | + printf("OS: Generic Linux\n"); | ||
| 28 | + return; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + char line[256]; | ||
| 32 | + while (fgets(line, sizeof(line), fp)) { | ||
| 33 | + if(strncmp(line, "PRETTY_NAME=", 12) == 0) { | ||
| 34 | + char *name = line + 12; | ||
| 35 | + if (*name == '"') name++; | ||
| 36 | + size_t len = strlen(name); | ||
| 37 | + if (len > 0 && name[len - 1] == '\n') name[--len] = '\0'; | ||
| 38 | + if (len > 0 && name[len - 1] == '"') name[--len] = '\0'; | ||
| 39 | + | ||
| 40 | + printf("OS: %s\n", name); | ||
| 41 | + fclose(fp); | ||
| 42 | + return; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + } | ||
| 46 | + fclose(fp); | ||
| 47 | + printf("OS: Generic Linux\n"); | ||
| 48 | +} | ||
| 49 | + | ||
| 23 | int printStats(void) { | 50 | int printStats(void) { |
| 24 | 51 | ||
| 25 | char *wm = getenv("XDG_SESSION_DESKTOP"); | 52 | char *wm = getenv("XDG_SESSION_DESKTOP"); |
| @@ -32,7 +59,7 @@ int printStats(void) { | |||
| 32 | perror("uname"); | 59 | perror("uname"); |
| 33 | return 1; | 60 | return 1; |
| 34 | } | 61 | } |
| 35 | - printf("OS: %s\n", info.sysname); | 62 | + printOS(); |
| 36 | printf("Kernel: %s\n", info.release); | 63 | printf("Kernel: %s\n", info.release); |
| 37 | printf("Host: %s\n", info.nodename); | 64 | printf("Host: %s\n", info.nodename); |
| 38 | printf("WM: %s\n", wm ? wm : "Unknown"); | 65 | printf("WM: %s\n", wm ? wm : "Unknown"); |