kfetch/funcs.h

219 lines
4.0 KiB
C
Raw Normal View History

//Base funcs
2023-10-18 20:55:28 +00:00
#ifndef FUNCS_H
#define FUNCS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2023-10-18 20:55:28 +00:00
#include <errno.h>
#include <pwd.h>
#include <time.h>
#include <sys/utsname.h>
#include "logos.h"
#if defined(CLOCK_BOOTTIME)
#define CLOCK CLOCK_BOOTTIME
#elif defined(CLOCK_UPTIME)
#define CLOCK CLOCK_UPTIME
#elif defined(__APPLE__)
#define CLOCK CLOCK_MONOTONIC
#endif
typedef struct {
size_t size;
char *color;
char *pkg_cmd;
char **art;
} LOGO;
char *Os;
LOGO Logo;
struct utsname Uts;
//Funcs
void Init(void);
void GetShell(void);
void GetKernel(void);
void PrintOs(void);
void GetUptime(void);
void GetUser(void);
void GetPkgs(void);
char *GetOs(void);
void PrintColors(void);
2023-08-20 06:54:50 +00:00
void GetArch(void);
void Blank(void);
LOGO GetArt(void);
void Init(void) {
Os = GetOs();
Logo = GetArt();
if (uname(&Uts) < 0) {
2023-10-18 20:55:28 +00:00
fprintf(stderr, "kfetch: uaname() failed: %s\n", strerror(errno));
exit(1);
}
}
void GetShell(void) {
char *shell = getenv("SHELL");
if (shell == NULL) {
2023-10-18 20:55:28 +00:00
printf("%sSHELL \033[0;37mNothing\033[0m", Logo.color);
return;
}
char *splt = strrchr(shell, '/');
if (splt != NULL)
2023-10-18 20:55:28 +00:00
printf("%sSHELL \033[0;37m%s\033[0m", Logo.color, splt + 1);
}
void GetKernel(void) {
2023-10-18 20:55:28 +00:00
printf("%sKERNEL \033[0;37m%s\033[0m", Logo.color, Uts.release);
}
void PrintOs(void) {
if (Os == NULL) {
2023-10-18 20:55:28 +00:00
printf("%sOS \033[0;37m%s\033[0m", Logo.color, Uts.sysname);
return;
}
2023-10-18 20:55:28 +00:00
printf("%sOS \033[0;37m%s\033[0m", Logo.color, Os + strlen("PRETTY_NAME= "));
}
void GetUptime(void) {
2023-10-18 20:55:28 +00:00
int UptimeH = 0;
int UptimeM = 0;
#ifdef CLOCK
struct timespec uptime;
clock_gettime(CLOCK, &uptime);
2023-10-18 20:55:28 +00:00
UptimeH = uptime.tv_sec / 3600;
UptimeM = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60);
2023-10-18 20:55:28 +00:00
#endif
2023-10-18 20:55:28 +00:00
printf("%sUPTIME \033[0;37m%dh %dm\033[0m", Logo.color, UptimeH, UptimeM);
}
void GetUser(void) {
struct passwd *pw = getpwuid(geteuid());
2023-10-18 20:55:28 +00:00
printf("%sUSER \033[0;37m%s\033[0m", Logo.color, pw->pw_name);
}
void GetPkgs(void) {
if (Logo.pkg_cmd == NULL) {
2023-10-18 20:55:28 +00:00
printf("%sPKGS \033[0;37m0", Logo.color);
return;
}
FILE *fp = popen(Logo.pkg_cmd, "r");
2023-10-18 20:55:28 +00:00
printf("%sPKGS \033[0;37m", Logo.color);
char ch;
while ((ch = fgetc(fp)) != '\n')
printf("%c", ch);
pclose(fp);
}
char *GetOs(void) {
FILE *fp = fopen("/etc/os-release", "r");
if (fp == NULL)
return NULL;
else {
size_t len = 1;
char *line = malloc(sizeof(char) * len);
while (getline(&line, &len, fp) != EOF) {
if (strstr(line, "PRETTY_NAME=") != NULL) {
line[strlen(line) - 2] = '\0';
2023-08-20 21:03:19 +00:00
fclose(fp);
2023-08-20 06:54:50 +00:00
//Need free (in main())
return line;
}
}
2023-08-20 18:59:40 +00:00
//EOF
free(line);
}
return NULL;
}
void PrintColors(void) {
for (int i = 0; i < 7; i++)
2023-08-20 06:54:50 +00:00
printf("\033[1;3%dm● \033[0;3%dm● \033[0m", i, i);
}
2023-08-20 06:54:50 +00:00
void GetArch(void) {
2023-10-18 20:55:28 +00:00
printf("%sARCH \033[0;37m%s", Logo.color, Uts.machine);
}
void GetAvg(void) {
double avg[3] = {0, 0, 0};
#ifndef __ANDROID__
if (getloadavg(avg, sizeof(avg) / sizeof(avg[0])) < 0) {
fprintf(stderr, "kfetch: getloadavg() failed\n");
return;
}
#endif
2023-10-18 21:16:57 +00:00
printf("%sLOAGAVG \033[0;37m%.2f %.2f %.2f", Logo.color, avg[0], avg[1], avg[2]);
2023-08-20 06:54:50 +00:00
}
void Blank(void) {
return;
}
LOGO GetArt(void) {
LOGO art;
2023-08-20 18:59:40 +00:00
if (Os != NULL) {
if (strstr(Os, "Debian")) {
art.size = sizeof(Debian) / sizeof(char *);
art.pkg_cmd = "dpkg -l | tail -n+6 | wc -l";
art.art = Debian;
art.color = "\033[0;31m";
}
else if (strstr(Os, "Void")) {
art.size = sizeof(Void) / sizeof(char *);
art.pkg_cmd = "xbps-query -l | wc -l";
art.art = Void;
art.color = "\033[0;32m";
}
2023-08-20 18:59:40 +00:00
else if (strstr(Os, "Alpine")) {
art.size = sizeof(Alpine) / sizeof(char *);
art.pkg_cmd = "grep 'P:' /lib/apk/db/installed | wc -l";
art.art = Alpine;
art.color = "\033[1;34m";
}
else if (strstr(Os, "Arch") || strstr(Os, "Artix")) {
art.size = sizeof(Arch) / sizeof(char *);
art.pkg_cmd = "pacman -Qq | wc -l";
2023-08-20 21:03:19 +00:00
art.art = Arch;
2023-10-07 16:14:22 +00:00
art.color = "\033[0;34m";
2023-08-20 21:03:19 +00:00
}
}
2023-08-20 18:59:40 +00:00
else {
art.size = sizeof(Unknow) / sizeof(char *);
art.pkg_cmd = NULL;
art.art = Unknow;
art.color = "\033[1;36m";
}
return art;
}
2023-08-20 18:59:40 +00:00
2023-10-18 20:55:28 +00:00
#endif