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)
master
Nakidai 2024-07-04 02:59:09 +03:00
parent e3f577fe3d
commit 09c633e8ac
Signed by: nakidai
GPG Key ID: 18AD605FDA13FE5A
3 changed files with 21 additions and 33 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
mycfetch mycfetch
compile_commands.json
.cache/

View File

@ -1,10 +1,10 @@
#include "defs.h" #include "defs.h"
const int max_username_length = 128; static const int max_username_length = 128;
const int max_hostname_length = 128; static const int max_hostname_length = 128;
const int max_uptime_length = 40; static const int max_uptime_length = 40;
struct art_entry arts[] = { static struct art_entry arts[] = {
{ {
"cat", "cat",
{ {

View File

@ -1,23 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include "config.h" #include "config.h"
#include "defs.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) void getuptime(char *buffer, int max_length)
{ {
struct timespec uptime; struct timespec uptime;
@ -39,20 +28,11 @@ void getuptime(char *buffer, int max_length)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char **art = NULL; char **art = arts[0].art, *name = argv[1];
if (argc > 2) if (name)
{ for (int i = 0; i < sizeof(arts) / sizeof(*arts); ++i)
die(1, "usage: %s [artname]\n", argv[0]); if (!strcmp(arts[i].name, name))
} { art = arts[i].art; break; }
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;
}
struct utsname uname_buf; struct utsname uname_buf;
char hostname[max_hostname_length]; char hostname[max_hostname_length];
@ -64,9 +44,15 @@ int main(int argc, char **argv)
getuptime(uptime, max_uptime_length); getuptime(uptime, max_uptime_length);
uname(&uname_buf); uname(&uname_buf);
printf("%s %s@%s\n", art[0], username, hostname); printf(
printf("%s --\n", art[1]); "%s %s@%s\n"
printf("%s %s %s\n", art[2], uname_buf.sysname, uname_buf.release); "%s --\n"
printf("%s %s\n", art[3], uptime); "%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; return 0;
} }