paleofetch

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit f1f8a7eab6b4a5dcddb330de2c6e1601b66f9080
parent d23cf6a32033b00128f0aab14166cf46e168194e
Author: sam-barr <samfbarr@outlook.com>
Date:   Fri,  1 May 2020 08:55:53 -0500

Merge pull request #53 from dwzg/freq_mhz

Show CPU frequency in MHz if it is below 1 GHz
Diffstat:
Mpaleofetch.c | 29+++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)

diff --git 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[] = "GHz"; /* read the model name into cpu_model, and increment num_cores every time model name is found */ while(getline(&line, &len, cpuinfo) != -1) { @@ -370,23 +371,31 @@ cpufreq_fallback: 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 (cpu_freq < 1000) { + freq = (double) cpu_freq; + freq_unit[0] = 'M'; // make MHz from GHz + prec = 0; // show frequency as integer value + } else { + 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 (prec == 0) prec = 1; // we don't want zero decimal places /* remove unneeded information */ for (int i = 0; i < COUNT(cpu_remove); ++i) { @@ -394,7 +403,7 @@ cpufreq_fallback: } 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);