#include #include #include #include #include #include #include #include "fetch.h" #include "config.h" #include "logo.h" #define UNUSED(x) ((void)x) FETCH Init(void) { FETCH fetch; if (uname(&fetch.uts) < 0) { fprintf(stderr, "kfetch: %s\n", strerror(errno)); exit(1); } memset(fetch.os_name, '\0', sizeof(fetch.os_name)); if (GetOs(fetch.os_name, sizeof(fetch.os_name))) snprintf(fetch.os_name, sizeof(fetch.os_name), "%s", fetch.uts.sysname); else snprintf(fetch.os_name, sizeof(fetch.os_name), "%s", fetch.os_name + strlen("NAME=\"")); fetch.font_color = FONT_COLOR; GetArt(&fetch); return fetch; } int GetOs(char *os_name, size_t len) { FILE *fp = fopen("/etc/os-release", "r"); if (fp == NULL) return 1; while (fgets(os_name, len, fp)) { if (!strncmp(os_name, "NAME=", 5)) { os_name[strlen(os_name) - 2] = '\0'; fclose(fp); return 0; } } fclose(fp); return 1; } void SetArt(FETCH *fetch, size_t size, char *pkg_cmd, char **logo, char *color) { fetch->logo_size = size; fetch->logo = logo; fetch->pkg_cmd = pkg_cmd; fetch->color = color; } void GetArt(FETCH *fetch) { if (strstr(fetch->os_name, "Debian")) SetArt(fetch, sizeof(Debian) / sizeof(char *), "dpkg -l | tail -n+6 | wc -l", Debian, "\033[0;31m"); else if (strstr(fetch->os_name, "Void")) SetArt(fetch, sizeof(Void) / sizeof(char *), "xbps-query -l | wc -l", Void, "\033[0;32m"); else if (strstr(fetch->os_name, "Alpine")) SetArt(fetch, sizeof(Alpine) / sizeof(char *), "grep 'P:' /lib/apk/db/installed | wc -l", Alpine, "\033[1;34m"); else if (strstr(fetch->os_name, "Arch") || strstr(fetch->os_name, "Artix")) SetArt(fetch, sizeof(Arch) / sizeof(char *), "pacman -Qq | wc -l", Arch, "\033[0;34m"); else if (strstr(fetch->os_name, "PlainOs")) SetArt(fetch, sizeof(PlainOs) / sizeof(char *), NULL, PlainOs, "\033[37m"); else if (strstr(fetch->os_name, "OpenBSD")) SetArt(fetch, sizeof(OpenBSD) / sizeof(char *), "/bin/ls -1 /var/db/pkg/ | wc -l | tr -d ' '", OpenBSD, "\033[1;33m"); else if (!chdir("/system")) SetArt(fetch, sizeof(Android) / sizeof(char *), "dpkg -l | tail -n+6 | wc -l", Android, "\033[32m"); else SetArt(fetch, sizeof(Unknow) / sizeof(char *), NULL, Unknow, "\033[1;36m"); } void GetKernel(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.release); } void PrintOs(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.os_name); } void PrintColors(const char *title, const FETCH fetch) { UNUSED(title); UNUSED(fetch); for (int i = 1; i < 7; i++) printf("\033[1;3%dm%s\033[0m \033[0;3%dm%s\033[0m ", i, PC_CHAR, i, PC_CHAR); } void Blank(const char *title, const FETCH fetch) { UNUSED(title); UNUSED(fetch); printf(" "); } void GetUser(const char *title, const FETCH fetch) { UNUSED(fetch); struct passwd *pw = getpwuid(geteuid()); printf("%s%s%s%s", fetch.color, title, fetch.font_color, (pw != 0) ? pw->pw_name : "none"); } void GetUptime(const char *title, const FETCH fetch) { int hours = 0; int mins = 0; #ifdef CLOCK struct timespec uptime; clock_gettime(CLOCK, &uptime); hours = uptime.tv_sec / 3600; mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); #endif printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins); } void GetArch(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.machine); } void GetShell(const char *title, const FETCH fetch) { char *shell = getenv("SHELL"); if (shell == NULL) printf("%s%s%s%s", fetch.color, title, fetch.font_color, "none"); char *splt = strrchr(shell, '/'); if (splt == NULL) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, "none"); return; } printf("%s%s%s%s", fetch.color, title, fetch.font_color, splt + 1); }