commit b90bca503046ff1c76d9ca210332726243df8b1b
parent 5d413f0b7aa45599011a878433a23b1b1db6fcd8
Author: sam-barr <samfbarr@outlook.com>
Date: Sat, 25 Apr 2020 09:37:58 -0500
Merge pull request #43 from dwzg/dynamic_decimal_places
Make decimal places of CPU frequency dynamic
Diffstat:
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/paleofetch.c b/paleofetch.c
@@ -338,7 +338,7 @@ char *get_cpu() {
char *cpu_model = malloc(BUF_SIZE / 2);
char *line = NULL;
size_t len; /* unused */
- int num_cores = 0;
+ int num_cores = 0, cpu_freq, prec = 3;
double freq;
/* read the model name into cpu_model, and increment num_cores every time model name is found */
@@ -358,8 +358,14 @@ char *get_cpu() {
line = NULL;
if (getline(&line, &len, cpufreq) != -1) {
- sscanf(line, "%lf", &freq);
- freq /= 1e6; // convert kHz to GHz
+ 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 (prec == 0) prec = 1; // we don't want zero decimal places
} else {
freq = 0.0; // cpuinfo_max_freq not available?
}
@@ -373,7 +379,7 @@ char *get_cpu() {
}
char *cpu = malloc(BUF_SIZE);
- snprintf(cpu, BUF_SIZE, "%s (%d) @ %.1fGHz", cpu_model, num_cores, freq);
+ snprintf(cpu, BUF_SIZE, "%s (%d) @ %.*fGHz", cpu_model, num_cores, prec, freq);
free(cpu_model);
truncate_spaces(cpu);