From 5267766b102d36e482586bee21a65cd295d9c885 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Sun, 12 Nov 2023 14:54:45 +0300 Subject: [PATCH] Refactor --- Makefile | 10 +++++++--- config.h | 29 +++++++++++++++++++++++++++++ info.c | 48 ------------------------------------------------ info.h | 3 --- main.c | 43 +++++++++++++++++++++++++++++++++++++++---- order.c | 37 ------------------------------------- 6 files changed, 75 insertions(+), 95 deletions(-) create mode 100644 config.h delete mode 100644 info.c delete mode 100644 info.h delete mode 100644 order.c diff --git a/Makefile b/Makefile index 47f46ab..a264e71 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,15 @@ -CC := gcc +CC := cc CFLAGS := -mycfetch: main.c info.c order.c - $(CC) main.c info.c -o mycfetch $(CFLAGS) +all: mycfetch + +mycfetch: main.c config.h + $(CC) main.c -o mycfetch $(CFLAGS) install: mycfetch cp mycfetch /usr/bin/mycfetch clean: rm mycfetch + +.PHONY: all install clean diff --git a/config.h b/config.h new file mode 100644 index 0000000..1e51588 --- /dev/null +++ b/config.h @@ -0,0 +1,29 @@ +const int max_username_length = 128; +const int max_hostname_length = 128; +const int max_uptime_length = 40; + +char *art[] = { +// FreeBSD + "\033[31m_ _\033[0m", + "\033[31m\\‾‾‾‾/\033[0m", + "\033[31m|^ww^|\033[0m", + "\033[31m\\____/\033[0m" + +// Gentoo +// "\x1B[38;5;135m /‾\\", +// "\x1B[38;5;135m( \x1B[38;5;183mo \x1B[38;5;135m\\", +// "\x1B[38;5;135m/ /", +// "\x1B[38;5;135m\\__/" + +// Arch +// " /\\ ", +// " /\\ \\ ", +// " / \\ ", +// "/__/\\__\\" + +// Just cute cat +// " ╱|、 ", +// "(˚ˎ。7 ", +// " |、˜〵 ", +// "じしˍ,)ノ" +}; diff --git a/info.c b/info.c deleted file mode 100644 index 9c929c9..0000000 --- a/info.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include -#include - -char *GetKernelVer(void) -{ - FILE *ptr; - char *uptime = malloc(50 * sizeof(char)); - for (int i = 0; i < 50; ++i) - uptime[i] = 0; - ptr = popen("/bin/uname -r", "r"); - if (ptr == NULL) - return "idk"; - fgets(uptime, 49, ptr); - pclose(ptr); - int i = 0; - for (i = 0; uptime[i] != 0; ++i); - uptime[i-1] = 0; - return uptime; -} - -char *GetCurrentUser(void) -{ - char *user = getenv("USER"); - char *host = malloc(25 * sizeof(char)); - char *out = malloc(50 * sizeof(char)); - gethostname(host, 25); - sprintf(out, "%s@%s", user, host); - return out; -} - -char *GetUptime(void) -{ - FILE *ptr; - char *uptime = malloc(50 * sizeof(char)); - for (int i = 0; i < 50; ++i) - uptime[i] = 0; - ptr = popen("uptime -p", "r"); - if (ptr == NULL) - return "idk"; - fgets(uptime, 49, ptr); - pclose(ptr); - int i = 0; - for (i = 0; uptime[i] != 0; ++i); - uptime[i-1] = 0; - return uptime+3; -} diff --git a/info.h b/info.h deleted file mode 100644 index 61a10bf..0000000 --- a/info.h +++ /dev/null @@ -1,3 +0,0 @@ -char *GetKernelVer(void); // takes kernel version from /proc/version -char *GetCurrentUser(void); // user@pc -char *GetUptime(void); // takes uptime from uptime -p \ No newline at end of file diff --git a/main.c b/main.c index 52be321..69443f8 100644 --- a/main.c +++ b/main.c @@ -1,11 +1,46 @@ #include -#include -#include +#include +#include +#include -#include "info.h" +#include "config.h" + +void getuptime(char *buffer, int max_length) +{ + struct timespec uptime; + clock_gettime(CLOCK_BOOTTIME, &uptime); + int days = uptime.tv_sec / 86400; + int hours = uptime.tv_sec / 3600 % 24; + int minutes = uptime.tv_sec / 60 % 60; + int seconds = uptime.tv_sec % 60; + + if (uptime.tv_sec < 60) + snprintf(buffer, max_length, "Up %d seconds", seconds); + else if (uptime.tv_sec < 3600) + snprintf(buffer, max_length, "Up %d minutes", minutes); + else if (uptime.tv_sec < 86400) + snprintf(buffer, max_length, "Up %d hours, %d minutes", hours, minutes); + else + snprintf(buffer, max_length, "Up %d days, %d hours, %d minutes", days, hours, minutes); +} int main(int argc, char **argv) { -#include "order.c" + struct utsname uname_buf; + struct timespec uptime_buf; + char hostname[max_hostname_length]; + char username[max_username_length]; + char uptime[max_uptime_length]; + + gethostname(hostname, max_hostname_length); + getlogin_r(username, max_username_length); + getuptime(uptime, max_uptime_length); + + uname(&uname_buf); + printf("%s %s@%s\n", art[0], username, hostname); + printf("%s --\n", art[1]); + printf("%s %s %s\n", art[2], uname_buf.sysname, uname_buf.release); + printf("%s %s\n", art[3], uptime); + // #include "order.c" return 0; } diff --git a/order.c b/order.c deleted file mode 100644 index fc928bf..0000000 --- a/order.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Printf needed info - * Don't include here any libraries -*/ - -// Gentoo - -// printf("\x1B[38;5;135m /‾\\  \x1B[38;5;255m%s\n", GetCurrentUser()); -// printf("\x1B[38;5;135m( \x1B[38;5;183mo \x1B[38;5;135m\\\n"); -// printf("\x1B[38;5;135m/ /  \x1B[38;5;255m%s\n", GetKernelVer()); -// printf("\x1B[38;5;135m\\__/  \x1B[38;5;255m%s\n", GetUptime()); - -printf(" /‾\\  %s\n", GetCurrentUser()); -printf("( o \\\n"); -printf("/ /  %s\n", GetKernelVer()); -printf("\\__/  %s\n", GetUptime()); - -// Arch - -// printf(" /\\  %s\n", GetCurrentUser()); -// printf(" /\\ \\\n"); -// printf(" / \\  %s\n", GetKernelVer()); -// printf("/__/\\__\\  %s\n", GetUptime()); - -// FreeBSD - -// printf("\033[31m_ _\033[0m  %s\n", GetCurrentUser()); -// printf("\033[31m\\‾‾‾‾/\033[0m\n"); -// printf("\033[31m|^ww^|\033[0m  %s\n", GetKernelVer()); -// printf("\033[31m\\____/\033[0m  %s\n", GetUptime()); - -// Just cat - -// printf(" ╱|、 %s\n", GetCurrentUser()); -// printf("(˚ˎ。7 \n"); -// printf(" |、˜〵  %s\n", GetKernelVer()); -// printf("じしˍ,)ノ %s\n", GetUptime());