kfetch/src/fetch.c

178 lines
4.2 KiB
C
Raw Normal View History

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>
#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);
}
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;
}
2023-11-02 15:34:27 +00:00
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;
}
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"))
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");
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-02 14:51:10 +00:00
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);
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-11-02 14:51:10 +00:00
UNUSED(title);
UNUSED(fetch);
2023-11-26 09:49:11 +00:00
return 1;
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
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);
printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins);
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
}