This commit is contained in:
Your Name 2023-11-02 17:51:10 +03:00
parent b14ca9e430
commit c65640c4f6
12 changed files with 241 additions and 296 deletions

View File

@ -1,9 +0,0 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,4 +1,5 @@
CFLAGS?= -s -flto -Os -pedantic
CFLAGS?=-s -Os -pedantic -Wall -Wextra
CC?=cc
all:
$(CC) fetch.c $(CFLAGS) -o kfetch
$(CC) src/*.c -Iinclude -I. $(CFLAGS) -okfetch

View File

@ -1,3 +0,0 @@
# kfetch
Get information about system

View File

@ -1,17 +1,22 @@
#ifndef CONFIG_H
#define CONFIG_H
#ifndef _CONFIG_H
#define _CONFIG_H
void (*FUNCS[])(void) = {
PrintOs,
GetAvg,
GetKernel,
GetShell,
GetUptime,
GetUser,
GetPkgs,
GetArch,
Blank,
PrintColors
#include "fetch.h"
typedef struct {
char *title;
void (*func)(const char *title, const FETCH fetch);
} PARAMETR;
/* Config, EDIT THERE */
#define FONT_COLOR "\033[37m"
#define PC_CHAR "%"
static PARAMETR CONFIG[] = {
{"Krnl ", GetKernel},
{"Os ", PrintOs},
{" ", Blank},
{" ", PrintColors}
};
#endif

33
fetch.c
View File

@ -1,33 +0,0 @@
#include <stdio.h>
#include "funcs.h"
#include "config.h"
int main(void) {
Init();
size_t i;
for (i = 0; i < sizeof(FUNCS) / sizeof(void *); i++) {
if (i < Logo.size)
printf("%s", Logo.art[i]);
//If art is less than buffer size
else if (i >= Logo.size)
printf("%s", Logo.art[Logo.size - 1]);
FUNCS[i]();
printf("\033[0m\n");
}
//If art is larger than buffer size
for (; i < Logo.size; i++)
printf("%s\n", Logo.art[i]);
//Clean and close
printf("\n\033[0m");
if (Os != NULL)
free(Os);
return 0;
}

222
funcs.h
View File

@ -1,222 +0,0 @@
//Base funcs
#ifndef FUNCS_H
#define FUNCS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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);
void PrintColors(void);
void GetArch(void);
void Blank(void);
char *GetOs(void);
void SetArt(LOGO *art, size_t size, char *pkg_cmd, char **logo, char *color);
LOGO GetArt(void);
void Init(void) {
if (uname(&Uts) < 0) {
fprintf(stderr, "kfetch: uaname() failed: %s\n", strerror(errno));
exit(1);
}
Os = GetOs();
Logo = GetArt();
}
void GetShell(void) {
char *shell = getenv("SHELL");
if (shell == NULL) {
printf("%sSHELL \033[0;37mnone\033[0m", Logo.color);
return;
}
char *splt = strrchr(shell, '/');
if (splt != NULL)
printf("%sSHELL \033[0;37m%s\033[0m", Logo.color, splt + 1);
}
void GetKernel(void) {
printf("%sKERNEL \033[0;37m%s\033[0m", Logo.color, Uts.release);
}
void PrintOs(void) {
if (Os == NULL) {
printf("%sOS \033[0;37m%s\033[0m", Logo.color, Uts.sysname);
return;
}
printf("%sOS \033[0;37m%s\033[0m", Logo.color, Os + strlen("NAME= "));
}
void GetUptime(void) {
int UptimeH = 0;
int UptimeM = 0;
#ifdef CLOCK
struct timespec uptime;
clock_gettime(CLOCK, &uptime);
UptimeH = uptime.tv_sec / 3600;
UptimeM = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60);
#endif
printf("%sUPTIME \033[0;37m%dh %dm\033[0m", Logo.color, UptimeH, UptimeM);
}
void GetUser(void) {
struct passwd *pw = getpwuid(geteuid());
printf("%sUSER \033[0;37m%s\033[0m", Logo.color, (pw != 0) ? pw->pw_name : "none");
}
void GetPkgs(void) {
if (Logo.pkg_cmd == NULL) {
printf("%sPKGS \033[0;37m0", Logo.color);
return;
}
FILE *fp = popen(Logo.pkg_cmd, "r");
if (fp == NULL) {
printf("%sPKGS \033[0;37m0", Logo.color);
return;
}
printf("%sPKGS \033[0;37m", Logo.color);
char ch;
while ((ch = fgetc(fp))) {
if (feof(fp) || ch == '\n')
break;
printf("%c", ch);
}
pclose(fp);
}
void PrintColors(void) {
for (int i = 1; i < 7; i++)
printf("\033[1;3%dm● \033[0;3%dm● \033[0m", i, i);
}
void GetArch(void) {
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
printf("%sLOAGAVG \033[0;37m%.2f %.2f %.2f", Logo.color, avg[0], avg[1], avg[2]);
}
void Blank(void) {
return;
}
char *GetOs(void) {
FILE *fp = fopen("/etc/os-release", "r");
if (fp != NULL) {
size_t len = 0;
char *line = NULL;
while (getline(&line, &len, fp) != EOF) {
if (!strncmp(line, "NAME=", 5)) {
line[strlen(line) - 2] = '\0';
fclose(fp);
//free in main()
return line;
}
}
//EOF
free(line);
}
return NULL;
}
void SetArt(LOGO *art, size_t size, char *pkg_cmd, char **logo, char *color) {
art->size = size;
art->pkg_cmd = pkg_cmd;
art->art = logo;
art->color = color;
}
LOGO GetArt(void) {
LOGO art;
if (Os != NULL) {
if (strstr(Os, "Debian"))
SetArt(&art, sizeof(Debian) / sizeof(char *), "dpkg -l | tail -n+6 | wc -l", Debian, "\033[0;31m");
else if (strstr(Os, "Void"))
SetArt(&art, sizeof(Void) / sizeof(char *), "xbps-query -l | wc -l", Void, "\033[0;32m");
else if (strstr(Os, "Alpine"))
SetArt(&art, sizeof(Alpine) / sizeof(char *), "grep 'P:' /lib/apk/db/installed | wc -l", Alpine, "\033[1;34m");
else if (strstr(Os, "Arch") || strstr(Os, "Artix"))
SetArt(&art, sizeof(Arch) / sizeof(char *), "pacman -Qq | wc -l", Arch, "\033[0;34m");
else if (strstr(Os, "PlainOs"))
SetArt(&art, sizeof(PlainOs) / sizeof(char *), NULL, PlainOs, "\033[37m");
}
else if (strstr(Uts.sysname, "OpenBSD"))
SetArt(&art, sizeof(OpenBSD) / sizeof(char *), "/bin/ls -1 /var/db/pkg/ | wc -l | tr -d ' '", OpenBSD, "\033[1;33m");
else if (!chdir("/system"))
SetArt(&art, sizeof(Android) / sizeof(char *), "dpkg -l | tail -n+6 | wc -l", Android, "\033[32m");
else
SetArt(&art, sizeof(Unknow) / sizeof(char *), NULL, Unknow, "\033[1;36m");
return art;
}
#endif

View File

@ -1,12 +0,0 @@
Edit FUNCS in fetch.c
GetShell
GetAvg
GetKernel
PrintOs
GetUptime
GetUser
GetPkgs
PrintColors
GetArch
Blank

34
include/fetch.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef _FUNCS_H
#define _FUNCS_H
#include <time.h>
#include <sys/utsname.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 {
char os_name[128];
struct utsname uts;
char *pkg_cmd;
char *color;
char *font_color;
size_t logo_size;
char **logo;
} FETCH;
FETCH Init(void);
int GetOs(char *os_name, size_t len);
void GetArt(FETCH *fetch);
void GetKernel(const char *title, const FETCH fetch);
void PrintOs(const char *title, const FETCH fetch);
void PrintColors(const char *title, const FETCH fetch);
void Blank(const char *title, const FETCH fetch);
#endif

View File

@ -1,5 +1,5 @@
#ifndef LOGOS_H
#define LOGOS_H
#ifndef _LOGOS_H
#define _LOGOS_H
char *Debian[] = {
"\033[31m _____ ",

84
include/systems.h Normal file
View File

@ -0,0 +1,84 @@
#ifndef _LOGOS_H
#define _LOGOS_H
char *Debian[] = {
"\033[31m _____ ",
"\033[31m / __ \\ ",
"\033[31m| / | ",
"\033[31m| \\___` ",
"\033[31m -_ ",
"\033[31m --_ ",
" "
};
char *Void[] = {
"\033[32m _\033[36m________ ",
"\033[32m / \\\033[36m\\ _ \\ ",
"\033[32m| /\\\033[36m\\ \\ | ",
"\033[32m| ( (\033[36m_) ) | ",
"\033[32m| \\_ \\\033[36m\\/ | ",
"\033[32m \\______\\\033[36m\\_/ ",
" "
};
char *Alpine[] = {
"\033[34m /\\ /\\ ",
"\033[34m / \\ \\ ",
"\033[34m / \\ \\ ",
"\033[34m / \\ \\ ",
"\033[34m/ \\ \\ ",
"\033[34m \\ ",
" "
};
char *Arch[] = {
"\033[1;34m /\\ ",
"\033[1;34m / \\ ",
"\033[1;34m /\\ \\ ",
"\033[1;34m / \\ ",
"\033[1;34m / ,, \\ ",
"\033[1;34m / | | -\\ ",
"\033[1;34m/_-'' ''-_\\",
" "
};
char *Android[] = {
"\033[32m ;, ,; ",
"\033[32m ';,.-----.,;' ",
"\033[32m ,' ', ",
"\033[32m / O O \\ ",
"\033[32m| | ",
"\033[32m'-----------------' ",
" ",
};
char *OpenBSD[] = {
"\033[1;33m _____ ",
"\033[1;33m \\- -/ ",
"\033[1;33m \\_/ \\ ",
"\033[1;33m \\ \033[0mO O\033[1;33m | ",
"\033[1;33m |_ < ) 3 ) ",
"\033[1;33m / \\ / ",
"\033[1;33m /-_____-\\ ",
" "
};
char *PlainOs[] = {
" /U /U ",
"( . .) ",
"*(()()) ",
" "
};
char *Unknow[] = {
"\033[1;36m ___ ",
"\033[1;36m (\033[0m..\033[1;36m | ",
"\033[1;36m (\033[33m<> \033[1;36m| ",
"\033[1;36m / \033[0m__\033[1;36m \\ ",
"\033[1;36m ( \033[0m/ \\\033[1;36m /| ",
"\033[33m_\033[1;36m/ \033[0m\\__/\033[1;36m/_) ",
"\033[1;36m\\/\033[1;36m-____\033[33m\\/ ",
" "
};
#endif

76
src/fetch.c Normal file
View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#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;
}
void GetArt(FETCH *fetch) {
fetch->color = "\033[36m";
fetch->logo = Unknow;
fetch->logo_size = sizeof(Unknow) / sizeof(char *);
}
void GetKernel(const char *title, const FETCH fetch) {
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.release);
}
void PrintOs(const char *title, const FETCH fetch) {
printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.os_name);
}
void PrintColors(const char *title, const FETCH fetch) {
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);
}
void Blank(const char *title, const FETCH fetch) {
UNUSED(title);
UNUSED(fetch);
printf("");
}

24
src/main.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include "fetch.h"
#include "config.h"
int main(void) {
FETCH fetch = Init();
size_t i;
for (i = 0; i < sizeof(CONFIG) / sizeof(PARAMETR); i++) {
if (i < fetch.logo_size)
printf("%s", fetch.logo[i]);
if (i >= fetch.logo_size)
printf("%s", fetch.logo[fetch.logo_size - 1]);
CONFIG[i].func(CONFIG[i].title, fetch);
putchar('\n');
}
for (; i < fetch.logo_size; i++)
printf("%s\n", fetch.logo[i]);
printf("\n\033[0m");
return 0;
}