mycfetch/mycfetch.c

90 lines
2.4 KiB
C
Raw Normal View History

2023-06-10 17:56:49 +03:00
#include <stdio.h>
2023-11-15 20:56:56 +03:00
#include <stdlib.h>
2023-11-12 14:54:45 +03:00
#include <time.h>
2023-11-15 20:56:56 +03:00
#include <stdarg.h>
#include <string.h>
2023-06-10 17:56:49 +03:00
#ifdef _WIN32
#include <wdm.h>
#include <winsock.h>
#include <winbase.h>
#else
#include <sys/utsname.h>
#include <unistd.h>
#endif /* _WIN32 */
2023-11-12 14:54:45 +03:00
#include "config.h"
2023-11-15 20:56:56 +03:00
#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);
}
2023-11-12 14:54:45 +03:00
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);
}
2023-06-10 17:56:49 +03:00
int main(int argc, char **argv)
{
2023-11-15 20:56:56 +03:00
char **art = NULL;
2023-11-19 17:14:42 +03:00
if (argc > 2)
2023-11-15 20:56:56 +03:00
{
2023-11-19 17:14:42 +03:00
die(1, "usage: %s [artname]\n", argv[0]);
}
else if (argc == 2)
{
for (int i = 0; i < sizeof(arts)/sizeof(struct art_entry); ++i)
2023-11-15 20:56:56 +03:00
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;
}
2023-11-19 17:14:42 +03:00
char hostname[max_hostname_length];
char username[max_username_length];
char uptime[max_uptime_length];
#ifdef _WIN32
RTL_OSVERSIONINFOW version_buffer = {.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOW)};
RtlGetVersion(&version_buffer);
GetUserNameA(username, max_username_length);
#else
struct utsname uname_buf;
2023-11-19 17:14:42 +03:00
getlogin_r(username, max_username_length);
2023-11-12 14:54:45 +03:00
uname(&uname_buf);
#endif
gethostname(hostname, max_hostname_length);
getuptime(uptime, max_uptime_length);
2023-11-19 17:14:42 +03:00
2023-11-12 14:54:45 +03:00
printf("%s %s@%s\n", art[0], username, hostname);
printf("%s --\n", art[1]);
#ifdef _WIN32
printf("%s NT %u.%u %s\n", art[2], version_buffer.dwMajorVersion, version_buffer.dwMinorVersion, version_buffer.szCSDVersion);
#else
2023-11-12 14:54:45 +03:00
printf("%s %s %s\n", art[2], uname_buf.sysname, uname_buf.release);
#endif
2023-11-12 14:54:45 +03:00
printf("%s %s\n", art[3], uptime);
2023-11-15 20:56:56 +03:00
return 0;
2023-06-10 17:56:49 +03:00
}