From 09c633e8ac0aaff32f13ced0068364edb8df5bcb Mon Sep 17 00:00:00 2001 From: Nakidai Date: Thu, 4 Jul 2024 02:59:09 +0300 Subject: [PATCH] Rewrite code a little - Make variables declared in header static - Simplify art selection, now ignore invalid argument count or art name - Remove `die` function (was needed by previous art selection) --- .gitignore | 2 ++ config.h | 8 ++++---- mycfetch.c | 44 +++++++++++++++----------------------------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 88cfc38..0a82235 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ mycfetch +compile_commands.json +.cache/ diff --git a/config.h b/config.h index 7d6570c..09d0f76 100644 --- a/config.h +++ b/config.h @@ -1,10 +1,10 @@ #include "defs.h" -const int max_username_length = 128; -const int max_hostname_length = 128; -const int max_uptime_length = 40; +static const int max_username_length = 128; +static const int max_hostname_length = 128; +static const int max_uptime_length = 40; -struct art_entry arts[] = { +static struct art_entry arts[] = { { "cat", { diff --git a/mycfetch.c b/mycfetch.c index 61c3f66..25efad7 100644 --- a/mycfetch.c +++ b/mycfetch.c @@ -1,23 +1,12 @@ #include -#include #include #include #include -#include #include #include "config.h" #include "defs.h" -void die(int code, char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - exit(code); -} - void getuptime(char *buffer, int max_length) { struct timespec uptime; @@ -39,20 +28,11 @@ void getuptime(char *buffer, int max_length) int main(int argc, char **argv) { - char **art = NULL; - if (argc > 2) - { - die(1, "usage: %s [artname]\n", argv[0]); - } - else if (argc == 2) - { - for (int i = 0; i < sizeof(arts)/sizeof(struct art_entry); ++i) - if (!strcmp(arts[i].name, argv[1])) { art = arts[i].art; break; } - if (!art) die(1, "Art %s not found\n", argv[1]); - } else - { - art = arts[0].art; - } + char **art = arts[0].art, *name = argv[1]; + if (name) + for (int i = 0; i < sizeof(arts) / sizeof(*arts); ++i) + if (!strcmp(arts[i].name, name)) + { art = arts[i].art; break; } struct utsname uname_buf; char hostname[max_hostname_length]; @@ -64,9 +44,15 @@ int main(int argc, char **argv) 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); + printf( + "%s %s@%s\n" + "%s --\n" + "%s %s %s\n" + "%s %s\n", + art[0], username, hostname, + art[1], + art[2], uname_buf.sysname, uname_buf.release, + art[3], uptime + ); return 0; }