diff options
author | David <dbphillipsnz@gmail.com> | 2014-07-25 22:11:01 +1200 |
---|---|---|
committer | David <dbphillipsnz@gmail.com> | 2014-07-25 22:11:01 +1200 |
commit | 4297d84d20dee84620e600a29bfa24572677cf05 (patch) | |
tree | c138d20b170beebc2b65619992e0b57282c40d5a /getfreq.c | |
parent | 42d79b75c81f6d25293c02eab71e3437d9d892b5 (diff) | |
download | paramano-4297d84d20dee84620e600a29bfa24572677cf05.tar.xz |
Moved from sprintf to asprintf
Diffstat (limited to 'getfreq.c')
-rw-r--r-- | getfreq.c | 38 |
1 files changed, 23 insertions, 15 deletions
@@ -35,9 +35,9 @@ int total_freqs; **********************************************************************/ void gf_init() { - gchar freq_string[500]; + char freq_string[500]; + unsigned int i; - int i = 0; for(i = 0; i < gc_number(); i++) { memset(freq_string, '\0', sizeof(freq_string) ); @@ -51,8 +51,8 @@ void gf_init() // freq_string is a space separated list of freqs so // iterate over each frequency in freq_string - gchar* curr = &freq_string[0]; - gchar* end_of_curr = g_strstr_len(curr, strlen(curr), " "); + char* curr = &freq_string[0]; + char* end_of_curr = g_strstr_len(curr, strlen(curr), " "); while(end_of_curr) { // TO DO : get rid of magic constants @@ -73,15 +73,16 @@ void gf_init() int gf_current(int core) { FILE* fd; - char buff[13]; - char path[80]; + char buff[13]; // TO DO : magic constant + char* path; int freq; - sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", core); + asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", core); if(!(fd = fopen(path, "r"))) { debug("Couldn't open '%s'\n",path); + free(path); return -1; } @@ -90,43 +91,50 @@ int gf_current(int core) freq = atoi(buff); fclose(fd); debug("Found freq %d on core %d\n",freq,core); + + free(path); return freq; } /*********************************************************************** - * Populate out with available frequencies for core + * Populate `out` with available frequencies for core **********************************************************************/ int gf_available(int core, char* out, int size) { FILE* fd; - char path[80]; + char* path; - sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_frequencies", core); + asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_frequencies", core); if(!(fd = fopen(path, "r"))) { debug("Couldn't open '%s'\n",path); + free(path); return -1; } fgets(out, size, fd); fclose(fd); + free(path); return 0; } /*********************************************************************** - * Populate out with a formatted, units-added freq label for freq + * Populate `out` with a formatted, units-added freq label for freq **********************************************************************/ -void gf_get_frequency_label(int freq, char* out) +char* gf_get_frequency_label(int freq) { + char *string; if(freq >= 1000000) // >= 1 million KHz (1GHz) - sprintf(out, "%.2f GHz", ((float)freq/1000000) ); + asprintf(&string, "%.2f GHz", ((float)freq/1000000) ); else - sprintf(out, "%.2d MHz", freq/1000); + asprintf(&string, "%.2d MHz", freq/1000); + + debug("Prepared freq label '%s' for freq %d\n",string,freq); - debug("Prepared freq label '%s' for freq %d\n",out,freq); + return string; } /*********************************************************************** |