#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); 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, "PRETTY_NAME=\"", 5)) { os_name[strlen(os_name) - 2] = '\0'; snprintf(os_name, len, "%s", os_name + strlen("PRETTY_NAME=\"")); 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 / sizeof(char *); 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), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Debian, "\033[0;31m"); else if (strstr(fetch->os_name, "Void")) SetArt(fetch, sizeof(Void), "xbps-query -l | wc -l", Void, "\033[0;32m"); else if (strstr(fetch->os_name, "Alpine")) SetArt(fetch, sizeof(Alpine), "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), "pacman -Qq | wc -l", Arch, "\033[0;34m"); else if (strstr(fetch->os_name, "PlainOs")) SetArt(fetch, sizeof(PlainOs), NULL, PlainOs, "\033[37m"); else if (strstr(fetch->os_name, "OpenBSD")) SetArt(fetch, sizeof(OpenBSD), "/bin/ls -1 /var/db/pkg/ | wc -l | tr -d ' '", OpenBSD, "\033[1;33m"); else if (strstr(fetch->os_name, "Ubuntu")) SetArt(fetch, sizeof(Ubuntu), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Ubuntu, "\033[1;33m"); else if (!chdir("/system")) SetArt(fetch, sizeof(Android), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Android, "\033[32m"); else SetArt(fetch, sizeof(Unknow), NULL, Unknow, "\033[1;36m"); } int GetKernel(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.release); return 0; } int PrintOs(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.os_name); return 0; } int PrintColors(const char *title, const FETCH fetch) { printf("%s%s%s", fetch.color, title, fetch.font_color); 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); return 0; } int Blank(const char *title, const FETCH fetch) { UNUSED(title); UNUSED(fetch); return 0; } int GetUser(const char *title, const FETCH fetch) { UNUSED(fetch); struct passwd *pw = getpwuid(geteuid()); if (pw == 0) return 1; printf("%s%s%s%s", fetch.color, title, fetch.font_color, pw->pw_name); return 0; } int GetUptime(const char *title, const FETCH fetch) { #ifdef CLOCK struct timespec uptime; if (clock_gettime(CLOCK, &uptime) == -1) return 1; int days = uptime.tv_sec / 86400; int hours = uptime.tv_sec / 3600; int mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); if (days > 0) printf("%s%s%s%dd %dh %dm", fetch.color, title, fetch.font_color, days, hours, mins); else printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins); return 0; #endif return 1; } int GetArch(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.machine); return 0; } int GetShell(const char *title, const FETCH fetch) { char *shell = getenv("SHELL"); if (shell == NULL) return 1; char *splt = strrchr(shell, '/'); if (splt == NULL) return 1; printf("%s%s%s%s", fetch.color, title, fetch.font_color, splt + 1); return 0; } int GetPkg(const char *title, const FETCH fetch) { if (fetch.pkg_cmd == NULL) return 1; FILE *fp = popen(fetch.pkg_cmd, "r"); if (fp == NULL) return 1; printf("%s%s%s", fetch.color, title, fetch.font_color); int ch; while ((ch = getc(fp))) { if (ch == EOF || ch == '\n') break; putchar(ch); } pclose(fp); return 0; } int GetMem(const char *title, const FETCH fetch) { FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) return 1; unsigned int free = 0, cache = 0, total = 0, unused; if (fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\nBuffers: %u kB\nCached: %u kB", &total, &free, &unused, &unused, &cache) < 0) return 1; UNUSED(unused); printf("%s%s%s%umb / %umb", fetch.color, title, fetch.font_color, (free + cache) / 1024, total / 1024); fclose(fp); return 0; }