From 3237ada9b5fa9ec510917bc1b6b7fad0b5f10cc7 Mon Sep 17 00:00:00 2001 From: 8nlight <8nlight@disroot.org> Date: Tue, 1 Aug 2023 22:41:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BETA!!!!!! --- LICENSE | 9 +++ Makefile | 3 + README.md | 3 + fetch.c | 34 ++++++++++ funcs.h | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 241 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 fetch.c create mode 100644 funcs.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c6ee65e --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +CFLAGS?= -s -flto -Os -pedantic +all: + cc fetch.c $(CFLAGS) -o kfetch diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e82d5a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# kfetch + +Get information about system \ No newline at end of file diff --git a/fetch.c b/fetch.c new file mode 100644 index 0000000..3425e08 --- /dev/null +++ b/fetch.c @@ -0,0 +1,34 @@ +#include +#include "funcs.h" + +int main(void) { + Init(); + void (*FUNCS[])(void) = {PrintOs, GetKernel, GetShell, GetUptime, GetUser, GetPkgs, Blank, PrintColors}; + + 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) + for (size_t j = 0; j < strlen(Logo.art[0]); j++) + printf(" "); + + + FUNCS[i](); + printf("\n"); + } + + + //If art is larger than buffer size + for (size_t j = i; j < Logo.size; j++) + printf("%s\n", Logo.art[j]); + + //Clean and close + printf("\n\033[0m"); + if (Os != NULL) + free(Os); + + return 0; +} diff --git a/funcs.h b/funcs.h new file mode 100644 index 0000000..40140b8 --- /dev/null +++ b/funcs.h @@ -0,0 +1,192 @@ +//Base funcs +#include +#include +#include +#include +#include +#include +#include +#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 + +//Varables +typedef struct { + size_t size; + char *color; + char *pkg_cmd; + char **art; +} LOGO; + +char *Os; +char *Color; +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); +void Blank(void); +LOGO GetArt(void); + + +void Init(void) { + Os = GetOs(); + + Logo = GetArt(); + Color = Logo.color; + if (uname(&Uts) < 0) { + fprintf(stderr, "WTF, uname dont work\n"); + exit(1); + } +} + + +void GetShell(void) { + char *shell = getenv("SHELL"); + if (shell == NULL) { + printf("%s SHELL \033[37mNothing\033[0m", Color); + return; + } + + char *splt = strrchr(shell, '/'); + if (splt != NULL) + printf("%s SHELL \033[37m%s\033[0m", Color, splt + 1); +} + + +void GetKernel(void) { + printf("%s KERNEL \033[37m%s\033[0m", Color, Uts.release); +} + + +void PrintOs(void) { + if (Os == NULL) { + printf("%s OS \033[37mUnknow\033[0m", Color); + return; + } + + printf("%s OS \033[37m%s\033[0m", Color, Os + strlen("PRETTY_NAME= ")); +} + + +void GetUptime(void) { + #ifdef CLOCK + struct timespec uptime; + clock_gettime(CLOCK, &uptime); + + int UptimeH = uptime.tv_sec / 3600; + int UptimeM = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); + + printf("%s UPTIME \033[37m%dh %dm\033[0m", Color, UptimeH, UptimeM); + + #else + printf("%s UPTIME \033[37m0h 0m\033[0m", Color); + + #endif +} + + +void GetUser(void) { + struct passwd *pw = getpwuid(geteuid()); + printf("%s USER \033[37m%s\033[0m", Color, pw->pw_name); +} + + +void GetPkgs(void) { + if (Logo.pkg_cmd == NULL) { + printf("%s PKGS \033[37m0", Color); + return; + } + + FILE *fp = popen(Logo.pkg_cmd, "r"); + printf("%s PKGS \033[37m", 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'; + + fclose(fp); + return line; + } + } + } + + return NULL; +} + + +void PrintColors(void) { + printf(" "); + for (int i = 0; i < 7; i++) + printf("\033[1;3%dm$ \033[0m", i); +} + +void Blank(void) { + return; +} + +LOGO GetArt(void) { + LOGO art; + + //Else + art.size = sizeof(Debian) / sizeof(char *); + art.pkg_cmd = NULL; + art.art = Debian; + art.color = "\033[0;31m"; + if (Os == NULL) + return art; + + else 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"; + } + + 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"; + } + + return art; +}