2023-11-02 15:00:45 +00:00
|
|
|
#include <pwd.h>
|
2023-11-02 14:51:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2023-12-16 16:04:59 +00:00
|
|
|
#include <stdint.h>
|
2023-11-02 14:51:10 +00:00
|
|
|
#include <stdlib.h>
|
2023-11-02 15:00:45 +00:00
|
|
|
#include <unistd.h>
|
2023-11-02 14:51:10 +00:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2023-12-16 15:58:30 +00:00
|
|
|
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);
|
2023-11-02 14:51:10 +00:00
|
|
|
|
|
|
|
fetch.font_color = FONT_COLOR;
|
|
|
|
GetArt(&fetch);
|
|
|
|
return fetch;
|
|
|
|
}
|
|
|
|
|
2023-12-15 19:24:22 +00:00
|
|
|
int GetOs(char *buf, size_t len) {
|
2023-11-02 14:51:10 +00:00
|
|
|
FILE *fp = fopen("/etc/os-release", "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2023-12-15 19:24:22 +00:00
|
|
|
while (fgets(buf, len, fp)) {
|
|
|
|
if (!strncmp(buf, "PRETTY_NAME=\"", 5)) {
|
|
|
|
buf[strlen(buf) - 2] = '\0';
|
2023-11-26 10:13:38 +00:00
|
|
|
fclose(fp);
|
2023-12-16 15:58:30 +00:00
|
|
|
|
|
|
|
char *tmp = strdup(buf);
|
|
|
|
if (tmp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
snprintf(buf, len, "%s", tmp + strlen("PRETTY_NAME=\""));
|
|
|
|
free(tmp);
|
|
|
|
|
2023-11-02 14:51:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:34:27 +00:00
|
|
|
void SetArt(FETCH *fetch, size_t size, char *pkg_cmd, char **logo, char *color) {
|
2023-11-26 10:28:41 +00:00
|
|
|
fetch->logo_size = size / sizeof(char *);
|
2023-11-02 15:34:27 +00:00
|
|
|
fetch->logo = logo;
|
|
|
|
fetch->pkg_cmd = pkg_cmd;
|
|
|
|
fetch->color = color;
|
|
|
|
}
|
|
|
|
|
2023-11-02 14:51:10 +00:00
|
|
|
void GetArt(FETCH *fetch) {
|
2023-11-02 15:34:27 +00:00
|
|
|
if (strstr(fetch->os_name, "Debian"))
|
2023-12-13 11:15:23 +00:00
|
|
|
SetArt(fetch, sizeof(Debian), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Debian, "\033[0;31m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "Void"))
|
2023-11-26 10:28:41 +00:00
|
|
|
SetArt(fetch, sizeof(Void), "xbps-query -l | wc -l", Void, "\033[0;32m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "Alpine"))
|
2023-11-26 10:28:41 +00:00
|
|
|
SetArt(fetch, sizeof(Alpine), "grep 'P:' /lib/apk/db/installed | wc -l", Alpine, "\033[1;34m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "Arch") || strstr(fetch->os_name, "Artix"))
|
2023-11-26 10:28:41 +00:00
|
|
|
SetArt(fetch, sizeof(Arch), "pacman -Qq | wc -l", Arch, "\033[0;34m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "PlainOs"))
|
2023-11-26 10:28:41 +00:00
|
|
|
SetArt(fetch, sizeof(PlainOs), NULL, PlainOs, "\033[37m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "OpenBSD"))
|
2023-11-26 10:28:41 +00:00
|
|
|
SetArt(fetch, sizeof(OpenBSD), "/bin/ls -1 /var/db/pkg/ | wc -l | tr -d ' '", OpenBSD, "\033[1;33m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
2023-12-13 11:15:23 +00:00
|
|
|
else if (strstr(fetch->os_name, "Ubuntu"))
|
2023-12-15 14:02:25 +00:00
|
|
|
SetArt(fetch, sizeof(Ubuntu), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Ubuntu, "\033[1;33m");
|
2023-12-13 11:15:23 +00:00
|
|
|
|
2023-11-02 15:34:27 +00:00
|
|
|
else if (!chdir("/system"))
|
2023-12-13 11:15:23 +00:00
|
|
|
SetArt(fetch, sizeof(Android), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Android, "\033[32m");
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else
|
2023-11-26 10:28:41 +00:00
|
|
|
SetArt(fetch, sizeof(Unknow), NULL, Unknow, "\033[1;36m");
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int GetKernel(const char *title, const FETCH fetch) {
|
2023-11-02 14:51:10 +00:00
|
|
|
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.release);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int PrintOs(const char *title, const FETCH fetch) {
|
2023-11-02 14:51:10 +00:00
|
|
|
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.os_name);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int PrintColors(const char *title, const FETCH fetch) {
|
2023-11-26 10:28:41 +00:00
|
|
|
printf("%s%s%s", fetch.color, title, fetch.font_color);
|
2023-11-02 14:51:10 +00:00
|
|
|
|
|
|
|
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);
|
2023-11-26 09:49:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int Blank(const char *title, const FETCH fetch) {
|
2023-12-26 19:01:04 +00:00
|
|
|
printf("%s%s%s", fetch.color, title, fetch.font_color);
|
2023-11-26 10:28:41 +00:00
|
|
|
return 0;
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
2023-11-02 15:00:45 +00:00
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int GetUser(const char *title, const FETCH fetch) {
|
2023-11-02 15:00:45 +00:00
|
|
|
UNUSED(fetch);
|
|
|
|
|
|
|
|
struct passwd *pw = getpwuid(geteuid());
|
2023-11-26 09:49:11 +00:00
|
|
|
if (pw == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
printf("%s%s%s%s", fetch.color, title, fetch.font_color, pw->pw_name);
|
|
|
|
return 0;
|
2023-11-02 15:00:45 +00:00
|
|
|
}
|
2023-11-02 15:19:43 +00:00
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int GetUptime(const char *title, const FETCH fetch) {
|
2023-11-02 15:19:43 +00:00
|
|
|
#ifdef CLOCK
|
|
|
|
struct timespec uptime;
|
2023-12-15 15:45:57 +00:00
|
|
|
if (clock_gettime(CLOCK, &uptime) == -1)
|
|
|
|
return 1;
|
2023-11-02 15:19:43 +00:00
|
|
|
|
2023-12-15 15:02:11 +00:00
|
|
|
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);
|
2023-11-02 15:19:43 +00:00
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
2023-11-02 15:19:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int GetArch(const char *title, const FETCH fetch) {
|
2023-11-02 15:19:43 +00:00
|
|
|
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.machine);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 15:19:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 09:49:11 +00:00
|
|
|
int GetShell(const char *title, const FETCH fetch) {
|
2023-11-02 15:19:43 +00:00
|
|
|
char *shell = getenv("SHELL");
|
|
|
|
if (shell == NULL)
|
2023-11-26 09:49:11 +00:00
|
|
|
return 1;
|
2023-11-02 15:19:43 +00:00
|
|
|
|
|
|
|
char *splt = strrchr(shell, '/');
|
2023-11-26 09:49:11 +00:00
|
|
|
if (splt == NULL)
|
|
|
|
return 1;
|
2023-11-02 15:19:43 +00:00
|
|
|
|
|
|
|
printf("%s%s%s%s", fetch.color, title, fetch.font_color, splt + 1);
|
2023-11-26 09:49:11 +00:00
|
|
|
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;
|
2023-11-02 15:19:43 +00:00
|
|
|
}
|
2023-12-15 14:02:25 +00:00
|
|
|
|
|
|
|
int GetMem(const char *title, const FETCH fetch) {
|
2023-12-15 15:02:11 +00:00
|
|
|
FILE *fp = fopen("/proc/meminfo", "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
2023-12-15 14:02:25 +00:00
|
|
|
|
2023-12-16 16:04:59 +00:00
|
|
|
off_t unused = 0, total = 0, available = 0;
|
2023-12-26 19:01:04 +00:00
|
|
|
if (fscanf(fp, "MemTotal: %lu kB\nMemFree: %lu kB\nMemAvailable: %lu kB", &total, &unused, &available) < 0)
|
2023-12-15 15:17:49 +00:00
|
|
|
return 1;
|
2023-12-15 14:02:25 +00:00
|
|
|
|
2023-12-15 15:17:49 +00:00
|
|
|
UNUSED(unused);
|
2023-12-26 19:01:04 +00:00
|
|
|
printf("%s%s%s%lumb / %lumb", fetch.color, title, fetch.font_color, (uintmax_t)(total - available) / 1024, (uintmax_t)total / 1024);
|
2023-12-15 15:02:11 +00:00
|
|
|
fclose(fp);
|
2023-12-15 14:02:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|