From 4297d84d20dee84620e600a29bfa24572677cf05 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 25 Jul 2014 22:11:01 +1200 Subject: Moved from sprintf to asprintf --- getfreq.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'getfreq.c') diff --git a/getfreq.c b/getfreq.c index 6661a6e..5f724a0 100644 --- a/getfreq.c +++ b/getfreq.c @@ -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; } /*********************************************************************** -- cgit v1.1