fordon_greeman / sfetch

Branch: 6c9a4d9
Switch branches/tags
main
theidioticdev feat: the great nix purge 6c9a4d9 2026-09-25
95 lines · 2.9 KB Raw Blame History
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 printAscii(void) {
21 #if ENABLE_ASCII
22 static const char asciiArt[] =
23 " ████████ \n"
24 " ██████████ \n"
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 printOS(void) {
36 #ifdef __linux__
37 FILE *fp = fopen("/etc/os-release", "r");
38 if (!fp) {
39 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
40 return;
41 }
42 char line[512];
43 while (fgets(line, sizeof(line), fp)) {
44 if(strncmp(line, "PRETTY_NAME=", 12) == 0) {
45 char *name = line + 12;
46 if (*name == '"') name++;
47 size_t len = strlen(name);
48 if (len > 0 && name[len - 1] == '\n') name[--len] = '\0';
49 if (len > 0 && name[len - 1] == '"') name[--len] = '\0';
50 printf(C_CYAN "OS: " C_RESET "%s\n", name);
51 fclose(fp);
52 return;
53 }
54 }
55 fclose(fp);
56 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
57 #else
58 struct utsname otherOS;
59 if (uname(&otherOS) != 0) {
60 perror("uname");
61 return;
62 }
63 printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname);
64 #endif
65 }
66 int printStats(void) {
67 char *wm = getenv("XDG_SESSION_DESKTOP");
68 if (!wm) {
69 wm = getenv("DESKTOP_SESSION");
70 }
71 char *session = getenv("XDG_SESSION_TYPE");
72 struct utsname info;
73 if (uname(&info) != 0) {
74 perror("uname");
75 return 1;
76 }
77 printOS();
78 printf(C_GREEN "Kernel: " C_RESET "%s %s\n",info.sysname, info.release);
79 printf(C_BLUE "Host: " C_RESET "%s\n", info.nodename);
80 printf(C_RED "WM: " C_RESET "%s (%s)\n", wm ? wm : "unknown / tty", session ? session : "unknown");
81 return 0;
82 }
83 int main(int argc, char **argv) {
84 for (int i = 1; i < argc; i++) {
85 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
86 printf("sfetch-%s\n", version);
87 return 0;
88 }
89 fprintf(stderr, "unknown argument: %s\nusage: %s [-v]\n", argv[i], argv[0]);
90 return 1;
91 }
92 printAscii();
93 printStats();
94 return 0;
95 }