sfetch.c wasn't visible
Browse files
+111 −0 sfetch.c new file mode 100755
| @@ -0,0 +1,111 @@ | |||
| 1 | +#include <stdio.h> | ||
| 2 | +#include <sys/utsname.h> | ||
| 3 | +#include <string.h> | ||
| 4 | +#include <unistd.h> | ||
| 5 | +#include <stdlib.h> | ||
| 6 | + | ||
| 7 | +#define C_RESET "\033[0m" | ||
| 8 | +#define C_BOLD "\033[1m" | ||
| 9 | +#define C_BLUE "\033[1;34m" | ||
| 10 | +#define C_YELLOW "\033[1;33m" | ||
| 11 | +#define C_CYAN "\033[1;36m" | ||
| 12 | +#define C_GREEN "\033[1;32m" | ||
| 13 | +#define C_RED "\033[1;31m" | ||
| 14 | +#define C_PURPLE "\033[1;35m" | ||
| 15 | + | ||
| 16 | +#define ENABLE_ASCII 1 | ||
| 17 | + | ||
| 18 | +const char *version = "0.1.5"; | ||
| 19 | + | ||
| 20 | +void | ||
| 21 | +printAscii(void) | ||
| 22 | +{ | ||
| 23 | +#if ENABLE_ASCII | ||
| 24 | + static const char asciiArt[] = | ||
| 25 | + " ___ \n" | ||
| 26 | + " (.. | \n" | ||
| 27 | + " (<> | \n" | ||
| 28 | + " / __ \\ \n" | ||
| 29 | + " ( / \\ /| \n" | ||
| 30 | + " _/\\ __)/_) \n" | ||
| 31 | + " \\/-____\\/ "; | ||
| 32 | + printf(C_YELLOW "%s" C_RESET "\n\n", asciiArt); | ||
| 33 | +#endif | ||
| 34 | +} | ||
| 35 | +void | ||
| 36 | +printOS(void) | ||
| 37 | +{ | ||
| 38 | +#ifdef __linux__ | ||
| 39 | + FILE *fp = fopen("/etc/os-release", "r"); | ||
| 40 | + if (!fp) | ||
| 41 | + { | ||
| 42 | + printf(C_CYAN "OS: " C_RESET "Generic Linux\n"); | ||
| 43 | + return; | ||
| 44 | + } | ||
| 45 | + char line[512]; | ||
| 46 | + while (fgets(line, sizeof(line), fp)) | ||
| 47 | + { | ||
| 48 | + if(strncmp(line, "PRETTY_NAME=", 12) == 0) | ||
| 49 | + { | ||
| 50 | + char *name = line + 12; | ||
| 51 | + if (*name == '"') name++; | ||
| 52 | + size_t len = strlen(name); | ||
| 53 | + if (len > 0 && name[len - 1] == '\n') name[--len] = '\0'; | ||
| 54 | + if (len > 0 && name[len - 1] == '"') name[--len] = '\0'; | ||
| 55 | + printf(C_CYAN "OS: " C_RESET "%s\n", name); | ||
| 56 | + fclose(fp); | ||
| 57 | + return; | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + fclose(fp); | ||
| 61 | + printf(C_CYAN "OS: " C_RESET "Generic Linux\n"); | ||
| 62 | +#else | ||
| 63 | + struct utsname otherOS; | ||
| 64 | + if (uname(&otherOS) != 0) | ||
| 65 | + { | ||
| 66 | + perror("uname"); | ||
| 67 | + return; | ||
| 68 | + } | ||
| 69 | + printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname); | ||
| 70 | +#endif | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +int | ||
| 74 | +printStats(void) | ||
| 75 | +{ | ||
| 76 | + char *wm = getenv("XDG_CURRENT_DESKTOP"); | ||
| 77 | + if (!wm) | ||
| 78 | + { | ||
| 79 | + wm = getenv("DESKTOP_SESSION"); | ||
| 80 | + } | ||
| 81 | + char *session = getenv("XDG_SESSION_TYPE"); | ||
| 82 | + struct utsname info; | ||
| 83 | + if (uname(&info) != 0) | ||
| 84 | + { | ||
| 85 | + perror("uname"); | ||
| 86 | + return 1; | ||
| 87 | + } | ||
| 88 | + printOS(); | ||
| 89 | + printf(C_GREEN "Kernel: " C_RESET "%s %s\n",info.sysname, info.release); | ||
| 90 | + printf(C_BLUE "Host: " C_RESET "%s\n", info.nodename); | ||
| 91 | + printf(C_RED "WM: " C_RESET "%s (%s)\n", wm ? wm : "unknown / tty", session ? session : "unknown"); | ||
| 92 | + return 0; | ||
| 93 | +} | ||
| 94 | + | ||
| 95 | +int | ||
| 96 | +main(int argc, char **argv) | ||
| 97 | +{ | ||
| 98 | + for (int i = 1; i < argc; i++) | ||
| 99 | + { | ||
| 100 | + if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) | ||
| 101 | + { | ||
| 102 | + printf("sfetch-%s\n", version); | ||
| 103 | + return 0; | ||
| 104 | + } | ||
| 105 | + fprintf(stderr, "unknown argument: %s\nusage: %s [-v]\n", argv[i], argv[0]); | ||
| 106 | + return 1; | ||
| 107 | + } | ||
| 108 | + printAscii(); | ||
| 109 | + printStats(); | ||
| 110 | + return 0; | ||
| 111 | +} | ||