2024-06-07 11:41:51 +00:00
|
|
|
#define _FETCH_C
|
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>
|
2024-01-03 11:10:52 +00:00
|
|
|
#include <limits.h>
|
2023-11-02 14:51:10 +00:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include "fetch.h"
|
2024-06-07 11:41:51 +00:00
|
|
|
#include "structs.h"
|
2023-11-02 14:51:10 +00:00
|
|
|
#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
|
|
|
|
2024-01-03 10:18:43 +00:00
|
|
|
GetArt(&fetch, 1);
|
2023-11-02 14:51:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-03 10:18:43 +00:00
|
|
|
void SetArt(FETCH *fetch, size_t size, char *pkg_cmd, char **logo, char *color, int flag) {
|
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;
|
2024-01-03 10:18:43 +00:00
|
|
|
|
|
|
|
if (flag)
|
|
|
|
fetch->pkg_cmd = pkg_cmd;
|
|
|
|
|
2023-11-02 15:34:27 +00:00
|
|
|
fetch->color = color;
|
|
|
|
}
|
|
|
|
|
2024-01-03 10:18:43 +00:00
|
|
|
void GetArt(FETCH *fetch, int flag) {
|
2023-11-02 15:34:27 +00:00
|
|
|
if (strstr(fetch->os_name, "Debian"))
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Debian), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Debian, "\033[0;31m", flag);
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "Void"))
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Void), "xbps-query -l | wc -l", Void, "\033[0;32m", flag);
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "Alpine"))
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Alpine), "grep 'P:' /lib/apk/db/installed | wc -l", Alpine, "\033[1;34m", flag);
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else if (strstr(fetch->os_name, "Arch") || strstr(fetch->os_name, "Artix"))
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Arch), "pacman -Qq | wc -l", Arch, "\033[0;34m", flag);
|
2023-11-02 15:34:27 +00:00
|
|
|
|
2023-12-13 11:15:23 +00:00
|
|
|
else if (strstr(fetch->os_name, "Ubuntu"))
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Ubuntu), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Ubuntu, "\033[1;33m", flag);
|
2023-12-13 11:15:23 +00:00
|
|
|
|
2024-01-15 18:37:52 +00:00
|
|
|
else if (strstr(fetch->os_name, "Mint"))
|
|
|
|
SetArt(fetch, sizeof(Lmint), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Lmint, "\033[1;32m", flag);
|
|
|
|
|
2024-06-07 13:14:43 +00:00
|
|
|
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", flag);
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
else if (strstr(fetch->os_name, "Manana"))
|
|
|
|
SetArt(fetch, sizeof(Manana), "pacman -Qq | wc -l", Manana, "\033[1;32m", flag);
|
|
|
|
|
2024-01-03 14:08:38 +00:00
|
|
|
else if (!chdir("/system") && !strcmp(fetch->os_name, "Linux"))
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Android), "dpkg -l | tail -n+6 | wc -l | tr -d ' '", Android, "\033[32m", flag);
|
2023-11-02 15:34:27 +00:00
|
|
|
|
|
|
|
else
|
2024-01-03 10:18:43 +00:00
|
|
|
SetArt(fetch, sizeof(Unknow), NULL, Unknow, "\033[1;36m", flag);
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetKernel(struct par par, const FETCH fetch) {
|
|
|
|
printf("%s%s", FONT_COLOR, fetch.uts.release);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int PrintOs(struct par par, const FETCH fetch) {
|
|
|
|
printf("%s%s", FONT_COLOR, fetch.os_name);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int Blank(struct par par, const FETCH fetch) {
|
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
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetUser(struct par par, 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;
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%s", FONT_COLOR, pw->pw_name);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 15:00:45 +00:00
|
|
|
}
|
2023-11-02 15:19:43 +00:00
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetUptime(struct par par, 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)
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%d days", FONT_COLOR, days);
|
2023-12-15 15:02:11 +00:00
|
|
|
|
|
|
|
else
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%dh %dm", 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
|
|
|
}
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetArch(struct par par, const FETCH fetch) {
|
|
|
|
printf("%s%s", FONT_COLOR, fetch.uts.machine);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
2023-11-02 15:19:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetShell(struct par par, 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
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%s", FONT_COLOR, splt + 1);
|
2023-11-26 09:49:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetPkg(struct par par, const FETCH fetch) {
|
2023-11-26 09:49:11 +00:00
|
|
|
if (fetch.pkg_cmd == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
FILE *fp = popen(fetch.pkg_cmd, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s", FONT_COLOR);
|
2023-11-26 09:49:11 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetMem(struct par par, 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
|
|
|
|
2024-01-05 20:07:26 +00:00
|
|
|
off_t free = 0, total = 0, cached = 0, buffer = 0, unused = 0;
|
2024-06-07 11:41:51 +00:00
|
|
|
if (fscanf(fp, "MemTotal: %jd kB\nMemFree: %jd kB\nMemAvailable: %jd kB\nBuffers: %jd kB\nCached: %jd kB", &total, &free, &unused, &buffer, &cached) < 0)
|
2023-12-15 15:17:49 +00:00
|
|
|
return 1;
|
2023-12-15 14:02:25 +00:00
|
|
|
|
2024-01-03 15:46:17 +00:00
|
|
|
fclose(fp);
|
2023-12-15 14:02:25 +00:00
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%lumb / %lumb", FONT_COLOR, (uintmax_t)(total - (cached + free + buffer)) / 1024, (uintmax_t)total / 1024);
|
2023-12-15 14:02:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2023-12-28 13:21:23 +00:00
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetModel(struct par par, const FETCH fetch) {
|
2023-12-28 13:21:23 +00:00
|
|
|
FILE *fp = NULL;
|
|
|
|
for (size_t i = 0; i < sizeof(MODELS) / sizeof(char *); i++) {
|
|
|
|
fp = fopen(MODELS[i], "r");
|
|
|
|
if (fp != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
char model_buffer[MODEL_BUFF_SIZE + 1];
|
|
|
|
char *ptr = fgets(model_buffer, sizeof(model_buffer), fp);
|
2023-12-29 11:50:38 +00:00
|
|
|
if (ptr != NULL) {
|
|
|
|
|
|
|
|
/* del \n */
|
|
|
|
char *ptr = strchr(model_buffer, '\n');
|
|
|
|
if (ptr != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%.25s", FONT_COLOR, model_buffer);
|
2023-12-29 11:50:38 +00:00
|
|
|
}
|
2023-12-28 13:21:23 +00:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-03 11:10:52 +00:00
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetHostname(struct par par, const FETCH fetch) {
|
2024-01-03 11:10:52 +00:00
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
|
|
|
if (gethostname(hostname, sizeof(hostname)) < 0)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%s", FONT_COLOR, hostname);
|
2024-01-03 11:10:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-01-03 12:09:07 +00:00
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int GetAVG(struct par par, const FETCH fetch) {
|
2024-01-03 12:09:07 +00:00
|
|
|
#ifndef __ANDROID__
|
|
|
|
double avg[3] = {0, 0, 0};
|
|
|
|
|
|
|
|
if (getloadavg(avg, sizeof(avg) / sizeof(avg[0])) < 0)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s%.2f %.2f %.2f", FONT_COLOR, avg[0], avg[1], avg[2]);
|
|
|
|
|
2024-01-03 12:09:07 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2024-01-03 14:46:33 +00:00
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
int Execute(struct par par, const FETCH fetch) {
|
|
|
|
if (par.str2 == NULL)
|
2024-01-03 14:46:33 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
FILE *fp = popen(par.str2, "r");
|
2024-01-15 18:37:52 +00:00
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-07 11:41:51 +00:00
|
|
|
printf("%s", FONT_COLOR);
|
2024-01-15 18:37:52 +00:00
|
|
|
|
|
|
|
int c;
|
|
|
|
while ((c = getc(fp))) {
|
|
|
|
if (c == EOF || c == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
putchar(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
pclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|