paleofetch

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

commit 6eba61457e9e674d661fb0acb7e9d5397ac63d19
parent 02120c749b4dd2725d7760279caedde0ebd26357
Author: dwzg <dennis@wtzg.de>
Date:   Fri,  1 May 2020 17:40:29 +0200

Merge remote-tracking branch 'upstream/master' into replace_substring

Diffstat:
Mpaleofetch.c | 49+++++++++++++++++++++++++++++++++++--------------
1 file changed, 35 insertions(+), 14 deletions(-)

diff --git a/paleofetch.c b/paleofetch.c @@ -361,6 +361,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,29 +371,49 @@ static char *get_cpu() { fclose(cpuinfo); FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); + line = NULL; - if (cpufreq == NULL) { - status = -1; - halt_and_catch_fire("Unable to open cpufreq"); + 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; + } + } else { +cpufreq_fallback: + cpufreq = fopen("/proc/cpuinfo", "r"); /* read from cpu info */ + if (cpufreq == 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; } - line = NULL; + free(line); + fclose(cpufreq); - if (getline(&line, &len, cpufreq) != -1) { - sscanf(line, "%d", &cpu_freq); - cpu_freq /= 1000; // convert kHz to MHz + 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 - } else { - freq = 0.0; // cpuinfo_max_freq not available? - } - free(line); - fclose(cpufreq); + if (prec == 0) prec = 1; // we don't want zero decimal places + } /* remove unneeded information */ for (int i = 0; i < COUNT(cpu_config); ++i) { @@ -404,7 +425,7 @@ static char *get_cpu() { } 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);