From 142a6b4b6a87837af7dfba0e08f75127ca4dbafb Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 24 Apr 2020 00:25:16 +0200 Subject: [PATCH 01/37] Add function to replace substrings --- paleofetch.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index a70537c..38ad6aa 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -87,6 +87,17 @@ void remove_substring(char *str, const char* substring, size_t len) { while(*(sub+(++i)) != '\0'); } +void replace_substring(char *str, const char *sub_str, const char *repl_str, size_t sub_len, size_t repl_len) { + char buffer[BUF_SIZE]; + char *start = strstr(str, sub_str); + if (start == NULL) return; // substring not found + int start_index = start - str; + + strcpy(buffer, str); + strncpy(start, repl_str, repl_len); + strcpy(start + repl_len, buffer + start_index + sub_len); +} + char *get_title() { // reduce the maximum size for these, so that we don't over-fill the title string char hostname[BUF_SIZE / 3]; @@ -370,6 +381,8 @@ char *get_cpu() { remove_substring(cpu_model, cpu_remove[i].substring, cpu_remove[i].length); } + replace_substring(cpu_model, "Core2", "Core 2", 5, 6); + char *cpu = malloc(BUF_SIZE); snprintf(cpu, BUF_SIZE, "%s (%d) @ %.1fGHz", cpu_model, num_cores, freq); free(cpu_model); From 00613632ad7c4546a1783f8b83212fb976a178d4 Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 24 Apr 2020 21:05:07 +0200 Subject: [PATCH 02/37] Add configurable way to replace strings of CPU and GPU model --- config.h | 4 ++-- paleofetch.c | 24 ++++++++++++++++-------- paleofetch.h | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/config.h b/config.h index 8ab5233..80d9fed 100644 --- a/config.h +++ b/config.h @@ -24,7 +24,7 @@ { "", get_colors2, false }, \ }; -#define CPU_REMOVE \ +#define CPU_CONFIG \ { \ REMOVE("(R)"), \ REMOVE("(TM)"), \ @@ -36,7 +36,7 @@ REMOVE("CPU"), \ }; -#define GPU_REMOVE \ +#define GPU_CONFIG \ { \ REMOVE("Corporation"), \ }; diff --git a/paleofetch.c b/paleofetch.c index 38ad6aa..41ac7ec 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -34,11 +34,13 @@ struct conf { typedef struct { char *substring; + char *repl_str; size_t length; + size_t repl_len; } STRING_REMOVE; -STRING_REMOVE cpu_remove[] = CPU_REMOVE; -STRING_REMOVE gpu_remove[] = GPU_REMOVE; +STRING_REMOVE cpu_config[] = CPU_CONFIG; +STRING_REMOVE gpu_config[] = GPU_CONFIG; Display *display; struct utsname uname_info; @@ -377,12 +379,14 @@ char *get_cpu() { fclose(cpufreq); /* remove unneeded information */ - for (int i = 0; i < COUNT(cpu_remove); ++i) { - remove_substring(cpu_model, cpu_remove[i].substring, cpu_remove[i].length); + for (int i = 0; i < COUNT(cpu_config); ++i) { + if (cpu_config[i].repl_str == NULL) { + remove_substring(cpu_model, cpu_config[i].substring, cpu_config[i].length); + } else { + replace_substring(cpu_model, cpu_config[i].substring, cpu_config[i].repl_str, cpu_config[i].length, cpu_config[i].repl_len); + } } - replace_substring(cpu_model, "Core2", "Core 2", 5, 6); - char *cpu = malloc(BUF_SIZE); snprintf(cpu, BUF_SIZE, "%s (%d) @ %.1fGHz", cpu_model, num_cores, freq); free(cpu_model); @@ -426,8 +430,12 @@ char *find_gpu(int index) { pci_cleanup(pacc); /* remove unneeded information */ - for (int i = 0; i < COUNT(gpu_remove); ++i) { - remove_substring(gpu, gpu_remove[i].substring, gpu_remove[i].length); + for (int i = 0; i < COUNT(gpu_config); ++i) { + if (gpu_config[i].repl_str == NULL) { + remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length); + } else { + replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len); + } } truncate_spaces(gpu); diff --git a/paleofetch.h b/paleofetch.h index 59fe62e..f831e69 100644 --- a/paleofetch.h +++ b/paleofetch.h @@ -20,4 +20,5 @@ char *get_title(), *spacer(); #define SPACER {"", spacer, false}, -#define REMOVE(A) { (A), sizeof(A) - 1} +#define REMOVE(A) { (A), NULL, sizeof(A) - 1 , 0 } +#define REPLACE(A, B) { (A), (B), sizeof(A) - 1, sizeof(B) - 1 } From c0e59cc6b59a37687cde47457afb47d7cc4cd0bb Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 24 Apr 2020 21:23:11 +0200 Subject: [PATCH 03/37] Improve function for substring replacement --- paleofetch.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 41ac7ec..399e9a9 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -89,15 +89,20 @@ void remove_substring(char *str, const char* substring, size_t len) { while(*(sub+(++i)) != '\0'); } +/* + * Replaces the first sub_len characters of sub_str from str + * with the first repl_len characters of repl_str + * This can be dangerous if repl_str is bigger than sub_str + * as no checking is done if str is big enough + */ void replace_substring(char *str, const char *sub_str, const char *repl_str, size_t sub_len, size_t repl_len) { char buffer[BUF_SIZE]; char *start = strstr(str, sub_str); if (start == NULL) return; // substring not found - int start_index = start - str; - strcpy(buffer, str); + strcpy(buffer, start + sub_len); strncpy(start, repl_str, repl_len); - strcpy(start + repl_len, buffer + start_index + sub_len); + strcpy(start + repl_len, buffer); } char *get_title() { @@ -105,7 +110,6 @@ char *get_title() { char hostname[BUF_SIZE / 3]; status = gethostname(hostname, BUF_SIZE / 3); halt_and_catch_fire("unable to retrieve host name"); - char username[BUF_SIZE / 3]; status = getlogin_r(username, BUF_SIZE / 3); halt_and_catch_fire("unable to retrieve login name"); From 011d98ef0e08abb5e983733595280db411ee600f Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 24 Apr 2020 21:34:02 +0200 Subject: [PATCH 04/37] Rename struct for string modification to reflect new function --- paleofetch.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 399e9a9..0033d39 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -37,10 +37,10 @@ typedef struct { char *repl_str; size_t length; size_t repl_len; -} STRING_REMOVE; +} STRING_MODIFY; -STRING_REMOVE cpu_config[] = CPU_CONFIG; -STRING_REMOVE gpu_config[] = GPU_CONFIG; +STRING_MODIFY cpu_config[] = CPU_CONFIG; +STRING_MODIFY gpu_config[] = GPU_CONFIG; Display *display; struct utsname uname_info; From ad0b6685f5d1229e896e8fa5da5d849a410a6b68 Mon Sep 17 00:00:00 2001 From: dwzg Date: Sun, 26 Apr 2020 00:56:32 +0200 Subject: [PATCH 05/37] Add fallback method for CPU frequency --- paleofetch.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 1a7a0a3..b7880c9 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -350,28 +350,43 @@ char *get_cpu() { FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); - if (cpufreq == NULL) { - status = -1; - halt_and_catch_fire("Unable to open cpufreq"); - } - line = NULL; - if (getline(&line, &len, cpufreq) != -1) { - sscanf(line, "%d", &cpu_freq); - cpu_freq /= 1000; // convert kHz to MHz - freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double - while (cpu_freq % 10 == 0) { - --prec; - cpu_freq /= 10; + + if (cpufreq != NULL) { + + if (getline(&line, &len, cpufreq) != -1) { + sscanf(line, "%d", &cpu_freq); + cpu_freq /= 1000; // convert kHz to MHz + } else { + fclose(cpufreq); + free(line); + goto cpufreq_fallback; } - if (prec == 0) prec = 1; // we don't want zero decimal places } else { - freq = 0.0; // cpuinfo_max_freq not available? +cpufreq_fallback: + cpufreq = fopen("/proc/cpuinfo", "r"); /* read from cpu info */ + if(cpuinfo == NULL) { + status = -1; + halt_and_catch_fire("Unable to open cpuinfo"); + } + + while (getline(&line, &len, cpufreq) != -1) { + if (sscanf(line, "cpu MHz : %lf", &freq) > 0) break; + } + + cpu_freq = (int) freq; } - + free(line); fclose(cpufreq); + + freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double + while (cpu_freq % 10 == 0) { + --prec; + cpu_freq /= 1; + } + if (prec == 0) prec = 1; // we don't want zero decimal places /* remove unneeded information */ for (int i = 0; i < COUNT(cpu_remove); ++i) { From f8fd070b672e80b7b9a1cb8beec9dfb1ff83e8ac Mon Sep 17 00:00:00 2001 From: dwzg Date: Tue, 28 Apr 2020 21:37:50 +0200 Subject: [PATCH 06/37] Fix check of wrong file pointer --- paleofetch.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index b7880c9..07abb5d 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -359,14 +359,14 @@ char *get_cpu() { sscanf(line, "%d", &cpu_freq); cpu_freq /= 1000; // convert kHz to MHz } else { - fclose(cpufreq); - free(line); + fclose(cpufreq); + free(line); goto cpufreq_fallback; } } else { cpufreq_fallback: cpufreq = fopen("/proc/cpuinfo", "r"); /* read from cpu info */ - if(cpuinfo == NULL) { + if (cpufreq == NULL) { status = -1; halt_and_catch_fire("Unable to open cpuinfo"); } From de9a199bd7818c82c6456270385d1ed64c9767af Mon Sep 17 00:00:00 2001 From: dwzg Date: Tue, 28 Apr 2020 21:46:26 +0200 Subject: [PATCH 07/37] Fix bug in frequency precision detection code --- paleofetch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paleofetch.c b/paleofetch.c index 44b2962..e691716 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -387,7 +387,7 @@ static char *get_cpu() { freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double while (cpu_freq % 10 == 0) { --prec; - cpu_freq /= 1; + cpu_freq /= 10; } if (prec == 0) prec = 1; // we don't want zero decimal places From d554a74458fcf5cf951eaa0b3f41768fb813e13d Mon Sep 17 00:00:00 2001 From: dwzg Date: Tue, 28 Apr 2020 21:49:40 +0200 Subject: [PATCH 08/37] Remove unnecessary newlines --- paleofetch.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index e691716..f8d4e61 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -352,12 +352,9 @@ static char *get_cpu() { fclose(cpuinfo); FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); - line = NULL; - if (cpufreq != NULL) { - if (getline(&line, &len, cpufreq) != -1) { sscanf(line, "%d", &cpu_freq); cpu_freq /= 1000; // convert kHz to MHz From 02120c749b4dd2725d7760279caedde0ebd26357 Mon Sep 17 00:00:00 2001 From: dwzg Date: Tue, 28 Apr 2020 22:27:25 +0200 Subject: [PATCH 09/37] Return stolen newline --- paleofetch.c | 1 + 1 file changed, 1 insertion(+) diff --git a/paleofetch.c b/paleofetch.c index 9310558..5c81699 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -112,6 +112,7 @@ static char *get_title() { char hostname[BUF_SIZE / 3]; status = gethostname(hostname, BUF_SIZE / 3); halt_and_catch_fire("unable to retrieve host name"); + char username[BUF_SIZE / 3]; status = getlogin_r(username, BUF_SIZE / 3); halt_and_catch_fire("unable to retrieve login name"); From 6b4532119471af8a8a97ff6835d106ff5027bfa9 Mon Sep 17 00:00:00 2001 From: dwzg Date: Sun, 26 Apr 2020 14:17:45 +0200 Subject: [PATCH 10/37] Add ability to switch CPU frequency unit to MHz for < 1 GHz --- paleofetch.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/paleofetch.c b/paleofetch.c index f8d4e61..1c947b9 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -343,6 +343,7 @@ static char *get_cpu() { size_t len; /* unused */ int num_cores = 0, cpu_freq, prec = 3; double freq; + char freq_unit[4]; /* read the model name into cpu_model, and increment num_cores every time model name is found */ while(getline(&line, &len, cpuinfo) != -1) { @@ -388,13 +389,20 @@ static char *get_cpu() { } if (prec == 0) prec = 1; // we don't want zero decimal places + if (freq < 1.0) { + strcpy(freq_unit, "MHz"); + prec = 0; + } else { + strcpy(freq_unit, "GHz"); + } + /* remove unneeded information */ for (int i = 0; i < COUNT(cpu_remove); ++i) { remove_substring(cpu_model, cpu_remove[i].substring, cpu_remove[i].length); } char *cpu = malloc(BUF_SIZE); - snprintf(cpu, BUF_SIZE, "%s (%d) @ %.*fGHz", cpu_model, num_cores, prec, freq); + snprintf(cpu, BUF_SIZE, "%s (%d) @ %.*f%s", cpu_model, num_cores, prec, freq, freq_unit); free(cpu_model); truncate_spaces(cpu); From b3c1ae50431b81120d9b36479e03d40dff5bd855 Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 1 May 2020 12:19:25 +0200 Subject: [PATCH 11/37] Improve code and fix a bug --- paleofetch.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 1c947b9..6934e96 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -343,7 +343,7 @@ static char *get_cpu() { size_t len; /* unused */ int num_cores = 0, cpu_freq, prec = 3; double freq; - char freq_unit[4]; + char freq_unit[] = "GHz"; /* read the model name into cpu_model, and increment num_cores every time model name is found */ while(getline(&line, &len, cpuinfo) != -1) { @@ -371,29 +371,30 @@ static char *get_cpu() { status = -1; halt_and_catch_fire("Unable to open cpuinfo"); } - + while (getline(&line, &len, cpufreq) != -1) { if (sscanf(line, "cpu MHz : %lf", &freq) > 0) break; } - + cpu_freq = (int) freq; } - + free(line); fclose(cpufreq); - - freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double - while (cpu_freq % 10 == 0) { - --prec; - cpu_freq /= 10; - } - if (prec == 0) prec = 1; // we don't want zero decimal places - if (freq < 1.0) { - strcpy(freq_unit, "MHz"); - prec = 0; + if (cpu_freq < 1000) { + freq = (double) cpu_freq; + freq_unit[0] = 'M'; // make MHz from GHz + prec = 0; // show frequency as integer value } else { - strcpy(freq_unit, "GHz"); + freq = cpu_freq / 1000.0; // convert MHz to GHz and cast to double + + while (cpu_freq % 10 == 0) { + --prec; + cpu_freq /= 10; + } + + if (prec == 0) prec = 1; // we don't want zero decimal places } /* remove unneeded information */ From 61d9bffef0728817243ac4983bd24d8a40e94d87 Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 1 May 2020 17:56:01 +0200 Subject: [PATCH 12/37] Add string length check in replace_substring() --- paleofetch.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index e3b4b3b..b9ab03d 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -94,14 +94,18 @@ void remove_substring(char *str, const char* substring, size_t len) { /* * Replaces the first sub_len characters of sub_str from str * with the first repl_len characters of repl_str - * This can be dangerous if repl_str is bigger than sub_str - * as no checking is done if str is big enough */ void replace_substring(char *str, const char *sub_str, const char *repl_str, size_t sub_len, size_t repl_len) { - char buffer[BUF_SIZE]; + char buffer[BUF_SIZE / 2]; char *start = strstr(str, sub_str); if (start == NULL) return; // substring not found + /* check if we have enough space for new substring */ + if (strlen(str) - sub_len + repl_len >= BUF_SIZE / 2) { + status = -1; + halt_and_catch_fire("new substring too long to replace"); + } + strcpy(buffer, start + sub_len); strncpy(start, repl_str, repl_len); strcpy(start + repl_len, buffer); From a1c5ec76e0197538ff76d2eba9f6f22c2d7db566 Mon Sep 17 00:00:00 2001 From: dwzg Date: Fri, 24 Apr 2020 10:30:48 +0200 Subject: [PATCH 13/37] Make get_terminal get the actual tty name In tty, the variable $TERM simply contains the value "linux". Make it that if "linux" is read from $TERM, it will get the actual name of the tty. Tested in Arch and Debian tty --- paleofetch.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index 6934e96..63f5495 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -326,6 +326,10 @@ static char *get_terminal() { } else { terminal_fallback: strncpy(terminal, getenv("TERM"), BUF_SIZE); /* fallback to old method */ + /* in tty, $TERM is simply returned as "linux"; in this case get actual tty name */ + if (strcmp(terminal, "linux") == 0) { + strncpy(terminal, ttyname(STDIN_FILENO), BUF_SIZE); + } } return terminal; From fdab2c5ff30947c640c70023e5b8b9dac6758b3d Mon Sep 17 00:00:00 2001 From: sam-barr Date: Fri, 1 May 2020 16:06:31 -0500 Subject: [PATCH 14/37] added fallback for get_host --- paleofetch.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 63f5495..59307d1 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -145,36 +145,36 @@ static char *get_kernel() { } static char *get_host() { - FILE *product_name = fopen("/sys/devices/virtual/dmi/id/product_name", "r"); - - if(product_name == NULL) { - status = -1; - halt_and_catch_fire("unable to open product name file"); + char *host = malloc(BUF_SIZE), buffer[BUF_SIZE/2]; + FILE *product_name, *product_version, *model; + + if((product_name = fopen("/sys/devices/virtual/dmi/id/product_name", "r")) != NULL) { + if((product_version = fopen("/sys/devices/virtual/dmi/id/product_version", "r")) != NULL) { + fread(host, 1, BUF_SIZE/2, product_name); + remove_newline(host); + strcat(host, " "); + fread(buffer, 1, BUF_SIZE/2, product_version); + remove_newline(buffer); + strcat(host, buffer); + fclose(product_version); + } else { + fclose(product_name); + goto model_fallback; + } + fclose(product_name); + return host; } - char *host = malloc(BUF_SIZE); - fread(host, 1, BUF_SIZE, product_name); - fclose(product_name); - - FILE *product_version = fopen("/sys/devices/virtual/dmi/id/product_version", "r"); - - if(product_version == NULL) { - status = -1; - halt_and_catch_fire("unable to open product version file"); +model_fallback: + if((model = fopen("/sys/firmware/devicetree/base/model", "r")) != NULL) { + fread(host, 1, BUF_SIZE, model); + remove_newline(host); + return host; } - char version[BUF_SIZE]; - - fread(version, 1, BUF_SIZE, product_version); - fclose(product_version); - - remove_newline(host); - remove_newline(version); - - strcat(host, " "); - strcat(host, version); - - return host; + status = -1; + halt_and_catch_fire("unable to get host"); + return NULL; } static char *get_uptime() { From af6f3a7430867df0127be6e1d059a5477495a20e Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 1 May 2020 18:56:03 -0400 Subject: [PATCH 15/37] Update README.md with performance information Title's self explanatory. I timed paleofetch and neofetch, and though it is a good idea to put that in the readme. --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index eb81bb9..6f13e64 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,14 @@ paleofetch A rewrite of [neofetch](https://github.com/dylanaraps/neofetch) in C. Currently only supports Linux and Xorg. + +Why use paleofetch over neofetch? +----------------------------------------- +One major reason is the performance improvement. For example: neofetch finishes running after about 222 milliseconds where as paleofetch can finish running in a blazing fast 3 milliseconds. + +Note: this testing occured on only 1 computer, it's not a good representation on the performance benefit you may gain. + + Example output: ![example output](example.png) From ea7030311aaa6a58530ea595177c5af3bde55fec Mon Sep 17 00:00:00 2001 From: dwzg Date: Sun, 3 May 2020 18:54:20 +0200 Subject: [PATCH 16/37] Update README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index eb81bb9..bc886af 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,15 @@ The booleans in `CONFIG` tell paleofetch whether you want to cache an entry. When cached, paleofetch will save the value and not recompute it whenever you run paleofetch (unless you specify the `--recache` option). +The CPU and GPU name can be configured as well. This is done under the CPU_CONFIG and GPU_CONFIG section +in the config.h file. Two macros are provided to customize and tidy up the model names: + +* `REMOVE(string)`: removes the first occurence of `string` +* `REPLACE(string1, string2)`: replaces the first occurence of `string1` with `string2` + +Don't forget to run paleofetch with the --recache flag after compiling it with your new +configuration, otherwise it will still show the old name for already cached entries. + FAQ --- From 03e7bd719e32f22e7a0c41f1987dc30308703fac Mon Sep 17 00:00:00 2001 From: sam-barr Date: Sun, 3 May 2020 19:40:52 -0500 Subject: [PATCH 17/37] get_cpu returns empty string when model name isn't present --- paleofetch.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index 87c4aa6..a52ed32 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -437,6 +437,9 @@ static char *get_cpu() { free(cpu_model); truncate_spaces(cpu); + + if(num_cores == 0) + *cpu = '\0'; return cpu; } From 92b105a2d51d0916b831fe19c8a63f6c5eecb2db Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 10:47:41 -0400 Subject: [PATCH 18/37] Add get_battery_percentage() to header --- paleofetch.h | 1 + 1 file changed, 1 insertion(+) diff --git a/paleofetch.h b/paleofetch.h index 3dc43be..0e4578f 100644 --- a/paleofetch.h +++ b/paleofetch.h @@ -7,6 +7,7 @@ static char *get_title(), *get_kernel(), *get_host(), *get_uptime(), + *get_battery_percentage(), *get_packages_pacman(), *get_shell(), *get_resolution(), From 4dbac177f91d6602340195bd3b82520c88bc2bb0 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 10:48:22 -0400 Subject: [PATCH 19/37] Add battery to config, fix spacing --- config.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/config.h b/config.h index 3fb3eae..39c621c 100644 --- a/config.h +++ b/config.h @@ -3,25 +3,26 @@ #define CONFIG \ { \ - /* name function cached */\ - { "", get_title, false }, \ - { "", get_bar, false }, \ - { "OS: ", get_os, true }, \ - { "Host: ", get_host, true }, \ - { "Kernel: ", get_kernel, true }, \ - { "Uptime: ", get_uptime, false }, \ + /* name function cached */\ + { "", get_title, false }, \ + { "", get_bar, false }, \ + { "OS: ", get_os, true }, \ + { "Host: ", get_host, true }, \ + { "Kernel: ", get_kernel, true }, \ + { "Uptime: ", get_uptime, false }, \ + { "Battery: ", get_battery_percentage, false }, \ SPACER \ { "Packages: ", get_packages_pacman, false }, \ { "Shell: ", get_shell, false }, \ { "Resolution: ", get_resolution, false }, \ { "Terminal: ", get_terminal, false }, \ SPACER \ - { "CPU: ", get_cpu, true }, \ - { "GPU: ", get_gpu1, true }, \ - { "Memory: ", get_memory, false }, \ + { "CPU: ", get_cpu, true }, \ + { "GPU: ", get_gpu1, true }, \ + { "Memory: ", get_memory, false }, \ SPACER \ - { "", get_colors1, false }, \ - { "", get_colors2, false }, \ + { "", get_colors1, false }, \ + { "", get_colors2, false }, \ } #define CPU_CONFIG \ From b361dbc0406cb3820c387de100920a1b16ce36d0 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 10:48:53 -0400 Subject: [PATCH 20/37] Implement get_battery_percentage() function --- paleofetch.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index a52ed32..1fe4a0f 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -221,6 +221,23 @@ static char *get_uptime() { return uptime; } +static char *get_battery_percentage() { + char *battery_percentage = malloc(BUF_SIZE); /* , buffer[BUF_SIZE/2]; */ + FILE *battery_percentage_file; + + if((battery_percentage_file = fopen("/sys/class/power_supply/BAT0/capacity", "r")) != NULL) { + fread(battery_percentage, 1, BUF_SIZE/2, battery_percentage_file); + remove_newline(battery_percentage); + /* strcat(host, " "); */ + /* fread(buffer, 1, BUF_SIZE/2, product_version); */ + /* remove_newline(buffer); */ + /* strcat(host, buffer); */ + } + + fclose(battery_percentage_file); + return battery_percentage; +} + static char *get_packages(const char* dirname, const char* pacname, int num_extraneous) { int num_packages = 0; DIR * dirp; From 02e0d687ee89ca32cd33a9adaa7a5a856908aa36 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 11:00:57 -0400 Subject: [PATCH 21/37] Add battery status after percentage --- paleofetch.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 1fe4a0f..b955549 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -222,16 +222,24 @@ static char *get_uptime() { } static char *get_battery_percentage() { - char *battery_percentage = malloc(BUF_SIZE); /* , buffer[BUF_SIZE/2]; */ - FILE *battery_percentage_file; + char *battery_percentage = malloc(BUF_SIZE), battery_status[BUF_SIZE/2]; + FILE *battery_percentage_file, *battery_status_file; if((battery_percentage_file = fopen("/sys/class/power_supply/BAT0/capacity", "r")) != NULL) { fread(battery_percentage, 1, BUF_SIZE/2, battery_percentage_file); remove_newline(battery_percentage); - /* strcat(host, " "); */ - /* fread(buffer, 1, BUF_SIZE/2, product_version); */ - /* remove_newline(buffer); */ - /* strcat(host, buffer); */ + if((battery_status_file = fopen("/sys/class/power_supply/BAT0/status", "r")) != NULL) { + fread(battery_status, 1, BUF_SIZE/2, battery_status_file); + remove_newline(battery_status); + strcat(battery_percentage, " ["); + strcat(battery_percentage, battery_status); + strcat(battery_percentage, "]"); + } + else { + strcat(battery_percentage, " [Unknown]"); + } + + fclose(battery_status_file); } fclose(battery_percentage_file); From 948f29c95a4e9883148b062319e013c879dea285 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 11:01:22 -0400 Subject: [PATCH 22/37] Add panic if battery cannot be opened --- paleofetch.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index b955549..e457feb 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -241,6 +241,9 @@ static char *get_battery_percentage() { fclose(battery_status_file); } + else { + halt_and_catch_fire("unable to get battery information"); + } fclose(battery_percentage_file); return battery_percentage; From aadf25faf4705250ebfa37265a223a1e01b68a44 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 11:02:50 -0400 Subject: [PATCH 23/37] Add % symbol and cut down # of strcat's --- paleofetch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index e457feb..91a230b 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -228,15 +228,15 @@ static char *get_battery_percentage() { if((battery_percentage_file = fopen("/sys/class/power_supply/BAT0/capacity", "r")) != NULL) { fread(battery_percentage, 1, BUF_SIZE/2, battery_percentage_file); remove_newline(battery_percentage); + strcat(battery_percentage, "% ["); if((battery_status_file = fopen("/sys/class/power_supply/BAT0/status", "r")) != NULL) { fread(battery_status, 1, BUF_SIZE/2, battery_status_file); remove_newline(battery_status); - strcat(battery_percentage, " ["); strcat(battery_percentage, battery_status); strcat(battery_percentage, "]"); } else { - strcat(battery_percentage, " [Unknown]"); + strcat(battery_percentage, "Unknown]"); } fclose(battery_status_file); From 10a23a96b1453f1c16528eed120602f51a7e95b2 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 11:04:49 -0400 Subject: [PATCH 24/37] Add comment showing output of function --- paleofetch.c | 1 + 1 file changed, 1 insertion(+) diff --git a/paleofetch.c b/paleofetch.c index 91a230b..eeaa2b6 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -221,6 +221,7 @@ static char *get_uptime() { return uptime; } +// returns "% []" static char *get_battery_percentage() { char *battery_percentage = malloc(BUF_SIZE), battery_status[BUF_SIZE/2]; FILE *battery_percentage_file, *battery_status_file; From 274410c2d09cf8c1c2ea2f414904bcf49c723532 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 12:31:15 -0400 Subject: [PATCH 25/37] Write script for finding battery path --- battery_config.sh | 5 +++++ 1 file changed, 5 insertions(+) create mode 100755 battery_config.sh diff --git a/battery_config.sh b/battery_config.sh new file mode 100755 index 0000000..3649ec6 --- /dev/null +++ b/battery_config.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +BATTERY_DIRECTORY=`ls -1 /sys/class/power_supply | grep -i "^bat" | head -n 1` + +echo "#define BATTERY_DIRECTORY \"/sys/class/power_supply/$BATTERY_DIRECTORY\"" > battery_config.h From ab2d04382e092a49ad47fb689d2a9b6acfecdf24 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 12:31:34 -0400 Subject: [PATCH 26/37] Call battery config script in Makefile --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 3744ef6..5d4261e 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ clean: rm -f paleofetch $(CACHE)/paleofetch paleofetch: paleofetch.c paleofetch.h config.h + ./battery_config.sh $(CC) paleofetch.c -o paleofetch $(CFLAGS) strip paleofetch From 33285a9160079664c7a6eb110934e500e799d7fc Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 12:32:29 -0400 Subject: [PATCH 27/37] Include battery config and replace hardcoded refs --- paleofetch.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index eeaa2b6..11cefc4 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -18,6 +18,7 @@ #include "paleofetch.h" #include "config.h" +#include "battery_config.h" #define BUF_SIZE 150 #define COUNT(x) (int)(sizeof x / sizeof *x) @@ -226,11 +227,11 @@ static char *get_battery_percentage() { char *battery_percentage = malloc(BUF_SIZE), battery_status[BUF_SIZE/2]; FILE *battery_percentage_file, *battery_status_file; - if((battery_percentage_file = fopen("/sys/class/power_supply/BAT0/capacity", "r")) != NULL) { + if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { fread(battery_percentage, 1, BUF_SIZE/2, battery_percentage_file); remove_newline(battery_percentage); strcat(battery_percentage, "% ["); - if((battery_status_file = fopen("/sys/class/power_supply/BAT0/status", "r")) != NULL) { + if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { fread(battery_status, 1, BUF_SIZE/2, battery_status_file); remove_newline(battery_status); strcat(battery_percentage, battery_status); From 4007c41d4ecd28dcef36af1a54f2c63389c19b2c Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 14:28:19 -0400 Subject: [PATCH 28/37] Reduce file read sizes in get_battery_percentage --- paleofetch.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 11cefc4..4710f02 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -224,15 +224,17 @@ static char *get_uptime() { // returns "% []" static char *get_battery_percentage() { - char *battery_percentage = malloc(BUF_SIZE), battery_status[BUF_SIZE/2]; + // battery status is at most 11 characters: "discharging" + char *battery_percentage = malloc(BUF_SIZE / 2), battery_status[12]; FILE *battery_percentage_file, *battery_status_file; if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { - fread(battery_percentage, 1, BUF_SIZE/2, battery_percentage_file); + // at most 100, which is 3 characters + fread(battery_percentage, 1, 3, battery_percentage_file); remove_newline(battery_percentage); strcat(battery_percentage, "% ["); if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { - fread(battery_status, 1, BUF_SIZE/2, battery_status_file); + fread(battery_status, 1, 12, battery_status_file); remove_newline(battery_status); strcat(battery_percentage, battery_status); strcat(battery_percentage, "]"); From 5084173d502fe680c2962a642511173144cc8fde Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 14:47:28 -0400 Subject: [PATCH 29/37] Add remove_newline function that returns length of string --- paleofetch.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index 4710f02..c69d67d 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -58,6 +58,17 @@ void remove_newline(char *s) { *s = '\0'; } +/* + * Replaces the first newline character with null terminator + * and returns the length of the string + */ +int remove_newline_get_length(char *s) { + int i; + for (i = 0; *s != '\0' && *s != '\n'; s++, i++); + *s = '\0'; + return i; +} + /* * Cleans up repeated spaces in a string * Trim spaces at the front of a string From 488294010da7d58a415225cf56eac31fb44f38d0 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 14:55:39 -0400 Subject: [PATCH 30/37] Optimize battery function by using length of strings for strcat --- paleofetch.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index c69d67d..8c5174b 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -242,13 +242,15 @@ static char *get_battery_percentage() { if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { // at most 100, which is 3 characters fread(battery_percentage, 1, 3, battery_percentage_file); - remove_newline(battery_percentage); - strcat(battery_percentage, "% ["); + int battery_percentage_length = remove_newline_get_length(battery_percentage); + char* battery_percentage_end = battery_percentage + battery_percentage_length; + strcat(battery_percentage_end, "% ["); + battery_percentage_end += 3; if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { fread(battery_status, 1, 12, battery_status_file); - remove_newline(battery_status); - strcat(battery_percentage, battery_status); - strcat(battery_percentage, "]"); + int battery_status_length = remove_newline_get_length(battery_status); + strcat(battery_percentage_end, battery_status); + strcat(battery_percentage_end + battery_status_length, "]"); } else { strcat(battery_percentage, "Unknown]"); From fd9f29ee659a30b4d8de913e7b7077cfad36ff22 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 15:04:39 -0400 Subject: [PATCH 31/37] Get rid of the second buffer in battery function --- paleofetch.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 8c5174b..cc302ee 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -236,24 +236,24 @@ static char *get_uptime() { // returns "% []" static char *get_battery_percentage() { // battery status is at most 11 characters: "discharging" - char *battery_percentage = malloc(BUF_SIZE / 2), battery_status[12]; + char *battery_percentage = malloc(BUF_SIZE / 2); FILE *battery_percentage_file, *battery_status_file; if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { // at most 100, which is 3 characters - fread(battery_percentage, 1, 3, battery_percentage_file); + // read 4 so that the string is null or newline terminated even at 100 + fread(battery_percentage, 1, 4, battery_percentage_file); int battery_percentage_length = remove_newline_get_length(battery_percentage); char* battery_percentage_end = battery_percentage + battery_percentage_length; strcat(battery_percentage_end, "% ["); battery_percentage_end += 3; if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { - fread(battery_status, 1, 12, battery_status_file); - int battery_status_length = remove_newline_get_length(battery_status); - strcat(battery_percentage_end, battery_status); + fread(battery_percentage_end, 1, 12, battery_status_file); + int battery_status_length = remove_newline_get_length(battery_percentage_end); strcat(battery_percentage_end + battery_status_length, "]"); } else { - strcat(battery_percentage, "Unknown]"); + strcat(battery_percentage_end, "Unknown]"); } fclose(battery_status_file); From 883c58aa3beca8a1d54cdcdd6b66c5c5aac9500e Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 20:50:54 -0400 Subject: [PATCH 32/37] Pass battery path directly to compiler --- Makefile | 4 ++-- battery_config.sh | 2 +- paleofetch.c | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 5d4261e..78f31cf 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ clean: rm -f paleofetch $(CACHE)/paleofetch paleofetch: paleofetch.c paleofetch.h config.h - ./battery_config.sh - $(CC) paleofetch.c -o paleofetch $(CFLAGS) + $(eval battery_path := $(shell ./battery_config.sh)) + $(CC) paleofetch.c -o paleofetch $(CFLAGS) -D $(battery_path) strip paleofetch install: paleofetch diff --git a/battery_config.sh b/battery_config.sh index 3649ec6..fcdf277 100755 --- a/battery_config.sh +++ b/battery_config.sh @@ -2,4 +2,4 @@ BATTERY_DIRECTORY=`ls -1 /sys/class/power_supply | grep -i "^bat" | head -n 1` -echo "#define BATTERY_DIRECTORY \"/sys/class/power_supply/$BATTERY_DIRECTORY\"" > battery_config.h +echo "BATTERY_DIRECTORY='\"/sys/class/power_supply/$BATTERY_DIRECTORY\"'" diff --git a/paleofetch.c b/paleofetch.c index cc302ee..8036385 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -18,7 +18,6 @@ #include "paleofetch.h" #include "config.h" -#include "battery_config.h" #define BUF_SIZE 150 #define COUNT(x) (int)(sizeof x / sizeof *x) From 721972f8b552df0bd528b8de2d85666354ccc440 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 20:54:17 -0400 Subject: [PATCH 33/37] Remove unnecessary "-1" argument --- battery_config.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/battery_config.sh b/battery_config.sh index fcdf277..0a49740 100755 --- a/battery_config.sh +++ b/battery_config.sh @@ -1,5 +1,5 @@ #!/bin/sh -BATTERY_DIRECTORY=`ls -1 /sys/class/power_supply | grep -i "^bat" | head -n 1` +BATTERY_DIRECTORY=`ls /sys/class/power_supply | grep -i "^bat" | head -n 1` echo "BATTERY_DIRECTORY='\"/sys/class/power_supply/$BATTERY_DIRECTORY\"'" From 31e00671f085a5d410442c7db9119035de562163 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 21:35:29 -0400 Subject: [PATCH 34/37] Move battery_config.sh to config_scripts folder --- Makefile | 2 +- battery_config.sh => config_scripts/battery_config.sh | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename battery_config.sh => config_scripts/battery_config.sh (100%) diff --git a/Makefile b/Makefile index 78f31cf..0f79ef1 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ clean: rm -f paleofetch $(CACHE)/paleofetch paleofetch: paleofetch.c paleofetch.h config.h - $(eval battery_path := $(shell ./battery_config.sh)) + $(eval battery_path := $(shell ./config_scripts/battery_config.sh)) $(CC) paleofetch.c -o paleofetch $(CFLAGS) -D $(battery_path) strip paleofetch diff --git a/battery_config.sh b/config_scripts/battery_config.sh similarity index 100% rename from battery_config.sh rename to config_scripts/battery_config.sh From c7022eb2701c322295b41a208e123ddc813b255d Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 15:04:39 -0400 Subject: [PATCH 35/37] Revert "Get rid of the second buffer in battery function" This reverts commit fd9f29ee659a30b4d8de913e7b7077cfad36ff22. --- paleofetch.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 8036385..de3568b 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -235,24 +235,24 @@ static char *get_uptime() { // returns "% []" static char *get_battery_percentage() { // battery status is at most 11 characters: "discharging" - char *battery_percentage = malloc(BUF_SIZE / 2); + char *battery_percentage = malloc(BUF_SIZE / 2), battery_status[12]; FILE *battery_percentage_file, *battery_status_file; if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { // at most 100, which is 3 characters - // read 4 so that the string is null or newline terminated even at 100 - fread(battery_percentage, 1, 4, battery_percentage_file); + fread(battery_percentage, 1, 3, battery_percentage_file); int battery_percentage_length = remove_newline_get_length(battery_percentage); char* battery_percentage_end = battery_percentage + battery_percentage_length; strcat(battery_percentage_end, "% ["); battery_percentage_end += 3; if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { - fread(battery_percentage_end, 1, 12, battery_status_file); - int battery_status_length = remove_newline_get_length(battery_percentage_end); + fread(battery_status, 1, 12, battery_status_file); + int battery_status_length = remove_newline_get_length(battery_status); + strcat(battery_percentage_end, battery_status); strcat(battery_percentage_end + battery_status_length, "]"); } else { - strcat(battery_percentage_end, "Unknown]"); + strcat(battery_percentage, "Unknown]"); } fclose(battery_status_file); From 13e205800acdea664c7ed33c9ecee4367dab7abb Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Fri, 8 May 2020 14:55:39 -0400 Subject: [PATCH 36/37] Revert "Optimize battery function by using length of strings for strcat" This reverts commit 488294010da7d58a415225cf56eac31fb44f38d0. --- paleofetch.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index de3568b..956e8b9 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -241,15 +241,13 @@ static char *get_battery_percentage() { if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { // at most 100, which is 3 characters fread(battery_percentage, 1, 3, battery_percentage_file); - int battery_percentage_length = remove_newline_get_length(battery_percentage); - char* battery_percentage_end = battery_percentage + battery_percentage_length; - strcat(battery_percentage_end, "% ["); - battery_percentage_end += 3; + remove_newline(battery_percentage); + strcat(battery_percentage, "% ["); if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { fread(battery_status, 1, 12, battery_status_file); - int battery_status_length = remove_newline_get_length(battery_status); - strcat(battery_percentage_end, battery_status); - strcat(battery_percentage_end + battery_status_length, "]"); + remove_newline(battery_status); + strcat(battery_percentage, battery_status); + strcat(battery_percentage, "]"); } else { strcat(battery_percentage, "Unknown]"); From 964ca21454b7fe7b267074a1df088027964d2ac0 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Sat, 9 May 2020 09:36:16 -0400 Subject: [PATCH 37/37] Use scanf/sprintf approach rather than micromanaging buffers --- paleofetch.c | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 956e8b9..72ce826 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -233,34 +233,34 @@ static char *get_uptime() { } // returns "% []" +// Credit: allisio - https://gist.github.com/allisio/1e850b93c81150124c2634716fbc4815 static char *get_battery_percentage() { - // battery status is at most 11 characters: "discharging" - char *battery_percentage = malloc(BUF_SIZE / 2), battery_status[12]; - FILE *battery_percentage_file, *battery_status_file; - - if((battery_percentage_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) != NULL) { - // at most 100, which is 3 characters - fread(battery_percentage, 1, 3, battery_percentage_file); - remove_newline(battery_percentage); - strcat(battery_percentage, "% ["); - if((battery_status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { - fread(battery_status, 1, 12, battery_status_file); - remove_newline(battery_status); - strcat(battery_percentage, battery_status); - strcat(battery_percentage, "]"); - } - else { - strcat(battery_percentage, "Unknown]"); - } + int battery_capacity; + FILE *capacity_file, *status_file; + char battery_status[12] = "Unknown"; - fclose(battery_status_file); - } - else { - halt_and_catch_fire("unable to get battery information"); - } + if ((capacity_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) == NULL) { + status = ENOENT; + halt_and_catch_fire("Unable to get battery information"); + } + + fscanf(capacity_file, "%d", &battery_capacity); + fclose(capacity_file); + + if ((status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { + fscanf(status_file, "%s", battery_status); + fclose(status_file); + } + + // max length of resulting string is 19 + // one byte for padding incase there is a newline + // 100% [Discharging] + // 1234567890123456789 + char *battery = malloc(20); + + snprintf(battery, 20, "%d%% [%s]", battery_capacity, battery_status); - fclose(battery_percentage_file); - return battery_percentage; + return battery; } static char *get_packages(const char* dirname, const char* pacname, int num_extraneous) {