Finally fixed ram issue (no one noticed anyway), general optimization and now completely bash-free (except pkgman() obviously)

This commit is contained in:
TheDarkBug 2021-03-11 22:39:16 +01:00
parent 79f508562c
commit 218ade46a8
4 changed files with 110 additions and 89 deletions

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
uwufetch uwufetch
*.zip *.zip
*.vscode

View file

@ -1,15 +1,17 @@
NAME = uwufetch NAME = uwufetch
FILES = uwufetch.c FILES = uwufetch.c
FLAGS = -O3 -Wall -Wextra FLAGS = -O3
FLAGS_DEBUG = -Wall -Wextra
INSTALL_DIR = /usr/bin/ INSTALL_DIR = /usr/bin/
all: build install all: build install
build: uwufetch.c build: uwufetch.c
gcc $(FLAGS) -o $(NAME) $(FILES) gcc $(FLAGS) -o $(NAME) $(FILES)
debug: build install debug:
clear clear
./uwufetch gcc $(FLAGS_DEBUG) -o $(NAME) $(FILES)
./uwufetch -id asd
install: install:
sudo cp $(NAME) $(INSTALL_DIR)$(NAME) sudo cp $(NAME) $(INSTALL_DIR)$(NAME)

View file

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 76 KiB

View file

@ -19,7 +19,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/resource.h>
#include <sys/utsname.h> #include <sys/utsname.h>
// COLORS // COLORS
@ -36,11 +35,10 @@
#define PINK "\x1b[38;5;201m" #define PINK "\x1b[38;5;201m"
#define LPINK "\x1b[38;5;213m" #define LPINK "\x1b[38;5;213m"
struct rusage r_usage;
struct utsname sys_var; struct utsname sys_var;
struct sysinfo sys; struct sysinfo sys;
int ram_max, pkgs, a_i_flag = 0; int ram_max = 0, ram_free = 0, pkgs, a_i_flag = 0;
char user[32], host[253], shell[64], version_name[64], cpu_model[256], pkgman_name[64], image_name[32]; char user[32], host[256], shell[64], version_name[64], cpu_model[256], pkgman_name[64], image_name[32];
int pkgman(); int pkgman();
void get_info(); void get_info();
void list(); void list();
@ -123,74 +121,78 @@ int pkgman() { // this is just a function that returns the total of installed pa
void print_info() { // print collected info void print_info() { // print collected info
printf( "\033[9A\033[18C%s%s%s@%s\n" printf( "\033[9A\033[18C%s%s%s@%s\n"
"\033[17C %s%sOWOS %s%s\n" "\033[18C%s%sOWOS %s%s\n"
"\033[17C %s%sKERNEL %s%s %s\n" "\033[18C%s%sKERNEL %s%s %s\n"
"\033[17C %s%sCPUWU %s%s\n" "\033[18C%s%sCPUWU %s%s\n"
"\033[17C %s%sWAM %s%ldM/%iM\n" "\033[18C%s%sWAM %s%i MB/%i MB\n"
"\033[17C %s%sSHELL %s%s\n" "\033[18C%s%sSHELL %s%s\n"
"\033[17C %s%sPKGS %s%s%d %s\n" "\033[18C%s%sPKGS %s%s%d %s\n"
"\033[17C %s%sUWUPTIME %s%lid, %lih, %lim\n" "\033[18C%s%sUWUPTIME %s"/*"%lid, "*/"%lih, %lim\n"
"\033[17C %s%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\n", "\033[18C%s%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\u2587\u2587%s\n",
NORMAL, BOLD, user, host, NORMAL, BOLD, user, host,
NORMAL, BOLD, NORMAL, version_name, NORMAL, BOLD, NORMAL, version_name,
NORMAL, BOLD, NORMAL, sys_var.release, sys_var.machine, NORMAL, BOLD, NORMAL, sys_var.release, sys_var.machine,
NORMAL, BOLD, NORMAL, cpu_model, NORMAL, BOLD, NORMAL, cpu_model,
NORMAL, BOLD, NORMAL, r_usage.ru_maxrss, ram_max, NORMAL, BOLD, NORMAL, (ram_max - ram_free), ram_max,
NORMAL, BOLD, NORMAL, shell, NORMAL, BOLD, NORMAL, shell,
NORMAL, BOLD, NORMAL, NORMAL, pkgs, pkgman_name, NORMAL, BOLD, NORMAL, NORMAL, pkgs, pkgman_name,
NORMAL, BOLD, NORMAL, sys.uptime/60/60/24, sys.uptime/60/60%24, sys.uptime/60%60, NORMAL, BOLD, NORMAL, /*sys.uptime/60/60/24,*/ sys.uptime/60/60, sys.uptime/60%60,
BOLD, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, NORMAL); BOLD, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, NORMAL);
} }
void get_info() { // get all necessary info void get_info() { // get all necessary info
char line[256]; // var to scan file lines
// os version // os version
FILE *fos_rel = popen("cat /etc/os-release 2> /dev/null | awk '/^ID=/' | awk -F '=' '{print $2}'", "r"); FILE *os_release = fopen("/etc/os-release", "r");
fscanf(fos_rel,"%[^\n]", version_name); FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
fclose(fos_rel); if (os_release) { // get normal vars
while (fgets(line, sizeof(line), os_release)) {
if (strlen(version_name) < 1) { // handling unknown distribution sscanf(line, "\nID=%s", version_name);
if (strlen(version_name)) break;
}
while (fgets(line, sizeof(line), cpuinfo)) if (fscanf(cpuinfo, "model name : %[^with\n]", cpu_model)) break;
sprintf(user, "%s", getenv("USER"));
fclose(os_release);
} else { // try for android vars, or unknown system
DIR *system_app = opendir("/system/app/"); DIR *system_app = opendir("/system/app/");
DIR *system_priv_app = opendir("/system/priv-app/"); DIR *system_priv_app = opendir("/system/priv-app/");
if (system_app && system_priv_app) { // android if (system_app && system_priv_app) { // android
closedir(system_app); closedir(system_app);
closedir(system_priv_app); closedir(system_priv_app);
sprintf(version_name, "android"); sprintf(version_name, "android");
} else sprintf(version_name, "unknown"); // android vars
}
// user name, host name and shell
if (strcmp(version_name, "android") != 0) {
snprintf(user, 32, "%s", getenv("USER"));
// cpu (this is here and not near the ram for efficiency)
FILE *fcpu = popen("lscpu | grep 'Model name:' | cut -d ':' -f2 | sed 's/ //g' 2> /dev/null", "r");
fscanf(fcpu, "%[^\n]", cpu_model);
fclose(fcpu);
}
else if (strcmp(version_name, "android") == 0) { // android vars
FILE *whoami = popen("whoami", "r"); FILE *whoami = popen("whoami", "r");
fscanf(whoami, "%s", user); fscanf(whoami, "%s", user);
fclose(whoami); fclose(whoami);
FILE *fcpu = popen("cat /proc/cpuinfo | grep 'Hardware' | cut -d ':' -f2 | sed 's/ //g' 2> /dev/null", "r"); while (fgets(line, sizeof(line), cpuinfo)) if (fscanf(cpuinfo, "Hardware : %[^\n]", cpu_model)) break;
fscanf(fcpu, "%[^\n]", cpu_model); } else sprintf(version_name, "unknown");
fclose(fcpu);
} }
gethostname(host, 253); fclose(cpuinfo);
snprintf(shell, 16, "%s", getenv("SHELL")); gethostname(host, 256);
memmove(&shell[0], &shell[5], 16); sprintf(shell, "%s", getenv("SHELL"));
// system info // system resources
uname(&sys_var); uname(&sys_var);
sysinfo(&sys); sysinfo(&sys);
// ram // ram
ram_max = sys.totalram * sys.mem_unit / 1048576; FILE *meminfo = fopen("/proc/meminfo", "r");
getrusage(RUSAGE_SELF, &r_usage); while (fgets(line, sizeof(line), meminfo)) {
sscanf(line, "MemFree: %d kB", &ram_free);
sscanf(line, "MemTotal: %d kB", &ram_max);
if (ram_max > 0 && ram_free > 0) {
ram_max *= 0.001024;
ram_free *= 0.001024;
fclose(meminfo);
break;
}
}
pkgs = pkgman(); pkgs = pkgman();
} }
void list(char* arg) { // prints distribution list void list(char* arg) { // prints distribution list
/* distributions are listed by distribution branch /* distributions are listed by distribution branch
to make the output easier to understand by the user, to make the output easier to understand by the user.*/
also i didn't like the previous listing.*/
printf( "%s -d <options>\n" printf( "%s -d <options>\n"
" Available distributions:\n" " Available distributions:\n"
" %sArch linux %sbased:\n" " %sArch linux %sbased:\n"
@ -305,7 +307,6 @@ void print_ascii() { // prints logo (as ascii art) of the given system. distribu
} }
// BSD // BSD
else if (strcmp(version_name, "freebsd") == 0) { else if (strcmp(version_name, "freebsd") == 0) {
printf( "\032[1E\033[3C%s\n" printf( "\032[1E\033[3C%s\n"
" /\\,-'''''-,/\\\n" " /\\,-'''''-,/\\\n"
@ -325,21 +326,33 @@ void print_ascii() { // prints logo (as ascii art) of the given system. distribu
" /-________-\\ \n\n", YELLOW, RED, YELLOW, WHITE, YELLOW, LPINK, WHITE, LPINK, YELLOW); " /-________-\\ \n\n", YELLOW, RED, YELLOW, WHITE, YELLOW, LPINK, WHITE, LPINK, YELLOW);
} }
else printf("\033[0E\033[1C%s"
"TUX for generic\n"
" unix system\n"
" needs to be made.\n"
" If you are\n"
" reading this\n"
" please consider\n"
" contributing.\n\n", RED);
} }
void print_image() { // prints logo (as an image) of the given system. distributions listed alphabetically. void print_image() { // prints logo (as an image) of the given system. distributions listed alphabetically.
char command[256]; char command[256];
if (strlen(image_name) > 1) sprintf(command, "viu -t -w 18 -h 8 %s 2> /dev/null", image_name); if (strlen(image_name) > 1) sprintf(command, "viu -t -w 18 -h 8 %s 2> /dev/null", image_name);
else {
if (strcmp(version_name, "android") == 0) sprintf(command, "viu -t -w 18 -h 8 /data/data/com.termux/files/usr/lib/uwufetch/%s.png 2> /dev/null", version_name);
else sprintf(command, "viu -t -w 18 -h 8 /usr/lib/uwufetch/%s.png 2> /dev/null", version_name); else sprintf(command, "viu -t -w 18 -h 8 /usr/lib/uwufetch/%s.png 2> /dev/null", version_name);
if (system(command) != 0) { // if viu is not installed }
printf( "\033[1E\033[3C%s\n" if (system(command) != 0) { // if viu is not installed or the image is missing
printf( "\033[0E\033[3C%s\n"
" There was an\n" " There was an\n"
" error, maybe viu\n" " error: viu\n"
" is not installed.\n" " is not installed\n"
" or the image\n"
" is not fount\n"
" Read IMAGES.md\n" " Read IMAGES.md\n"
" for more info.\n\n", RED); " for more info.\n\n", RED);
} }
printf("\033[1E\033[0C\b");
} }
void usage(char* arg) { void usage(char* arg) {
@ -356,23 +369,29 @@ void usage(char* arg) {
void uwu_name() { // changes distro name to uwufied(?) name void uwu_name() { // changes distro name to uwufied(?) name
#define VERSION_TO_UWU(original, uwufied) if (strcmp(version_name, original) == 0) sprintf(version_name, "%s", uwufied) #define STRING_TO_UWU(original, uwufied) if (strcmp(version_name, original) == 0) sprintf(version_name, "%s", uwufied)
// linux // linux
VERSION_TO_UWU("arch", "Nyarch Linuwu"); STRING_TO_UWU("arch", "Nyarch Linuwu");
VERSION_TO_UWU("artix", "Nyartix Linuwu"); else STRING_TO_UWU("artix", "Nyartix Linuwu");
VERSION_TO_UWU("debian", "Debinyan"); else STRING_TO_UWU("debian", "Debinyan");
VERSION_TO_UWU("fedora", "Fedowa"); else STRING_TO_UWU("fedora", "Fedowa");
VERSION_TO_UWU("gentoo", "GentOwO"); else STRING_TO_UWU("gentoo", "GentOwO");
VERSION_TO_UWU("linuxmint", "LinUWU Miwint"); else STRING_TO_UWU("linuxmint", "LinUWU Miwint");
VERSION_TO_UWU("manjaro", "Myanjawo"); else STRING_TO_UWU("manjaro", "Myanjawo");
VERSION_TO_UWU("manjaro-arm", "Myanjawo AWM"); else STRING_TO_UWU("manjaro-arm", "Myanjawo AWM");
VERSION_TO_UWU("popos", "PopOwOS"); else STRING_TO_UWU("popos", "PopOwOS");
VERSION_TO_UWU("ubuntu", "Uwuntu"); else STRING_TO_UWU("ubuntu", "Uwuntu");
VERSION_TO_UWU("void", "OwOid"); else STRING_TO_UWU("void", "OwOid");
VERSION_TO_UWU("android", "Nyandroid"); // android at the end because it could be not considered as an actual distribution of gnu/linux else STRING_TO_UWU("android", "Nyandroid"); // android at the end because it could be not considered as an actual distribution of gnu/linux
// BSD // BSD
VERSION_TO_UWU("freebsd", "FweeBSD"); else STRING_TO_UWU("freebsd", "FweeBSD");
VERSION_TO_UWU("openbsd", "OwOpenBSD"); else STRING_TO_UWU("openbsd", "OwOpenBSD");
#undef VERSION_TO_UWU
else {
sprintf(version_name, "%s", "unknown");
if (a_i_flag == 1) print_image();
}
#undef STRING_TO_UWU
} }