From 9c6b3292fcd778a06ce54c38c459bc521e045eee Mon Sep 17 00:00:00 2001 From: David Date: Fri, 25 Jul 2014 22:11:01 +1200 Subject: Moved from sprintf to asprintf --- Makefile | 3 ++- bat_tray.c | 60 +++++++++++++++++++++++++++++++++++++----------------------- defaults.c | 4 ++-- defaults.h | 2 +- getcore.c | 11 ++++++----- getfreq.c | 38 +++++++++++++++++++++++--------------- getfreq.h | 2 +- tray.c | 50 +++++++++++++++++++++++++------------------------- 8 files changed, 97 insertions(+), 73 deletions(-) diff --git a/Makefile b/Makefile index 589df84..d721590 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,8 @@ EXTRA_CFLAGS+= -DPREFIX=\"$(PREFIX)\" \ -DTRAYFREQ_CONF=\"$(TRAYFREQ_CONF)\" \ -DLOCALEDIR=\"$(LOCALEDIR)\" \ -DSHAREDIR=\"$(SHAREDIR)\" \ - -DROOT_UID=$(ROOT_UID) + -DROOT_UID=$(ROOT_UID) \ + -D_GNU_SOURCE DEPS = bat_tray.h \ diff --git a/bat_tray.c b/bat_tray.c index ac8fc94..3b2b698 100644 --- a/bat_tray.c +++ b/bat_tray.c @@ -21,6 +21,7 @@ #include #include +#include #include "common.h" #include "defaults.h" @@ -28,8 +29,7 @@ static GtkStatusIcon* tray; int bat_num; // Shortcoming: we only detect one battery -char CHARGE_VALUE_PATH[FILE_PATH_SIZE]; -char CHARGE_STATE_PATH[FILE_PATH_SIZE]; +char *CHARGE_VALUE_PATH, *CHARGE_STATE_PATH; /*********************************************************************** * Return the battery level percentage @@ -40,43 +40,41 @@ int get_bat_percent() } -#define TOOLTIP_TEXT_SIZE 128 -char tooltip_text[TOOLTIP_TEXT_SIZE]; - - /*********************************************************************** * Updates the battery tray tooltip text **********************************************************************/ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data) { - char msg[TOOLTIP_TEXT_SIZE]; + char* msg; switch(get_battery_state()) { case STATE_DISCHARGING: debug("Discharging\n"); - sprintf(msg, _("Discharging (%d%%)"), get_bat_percent()); + asprintf(&msg, _("Discharging (%d%%)"), get_bat_percent()); break; case STATE_CHARGING: debug("Charging\n"); - sprintf(msg, _("Charging (%d%%)"), get_bat_percent()); + asprintf(&msg, _("Charging (%d%%)"), get_bat_percent()); break; case STATE_CHARGED: debug("Charged\n"); - sprintf(msg, _("Fully charged") ); + asprintf(&msg, _("Fully charged") ); break; default: debug("Unknown\n"); - sprintf(msg, _("Unknown status") ); + asprintf(&msg, _("Unknown status") ); break; } debug("Setting tooltip text to '%s'\n",msg); gtk_tooltip_set_text(tooltip, msg); + free(msg); + return true; } @@ -87,52 +85,55 @@ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean gboolean update_icon(gpointer user_data) { char *icon_file; - char percent_string[4]; // worst case scenario 100 + \0 unsigned int rounded; rounded = 20* (int)((get_bat_percent()+10)/20); // Round percentage to 0, 20, 40, 60, 80 or 100 debug("Rounded/adjusted percentage: %d\n",rounded); - sprintf(percent_string, "%d", rounded); switch ( get_battery_state() ) { case STATE_DISCHARGING: - icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", percent_string, ".png", NULL); + asprintf(&icon_file,"%s/traybat-%d.png",_DEFAULT_THEME,rounded); break; case STATE_CHARGING: - icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", percent_string, "-charging.png", NULL); + asprintf(&icon_file,"%s/traybat-%d-charging.png",_DEFAULT_THEME,rounded); break; default: - icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-charged.png", NULL); + asprintf(&icon_file,"%s/traybat-charged.png",_DEFAULT_THEME); break; } debug("Setting tray icon to '%s'\n",icon_file); gtk_status_icon_set_from_file(tray, icon_file); + + free(icon_file); + return true; } - /*********************************************************************** * Initialise the tray and related variables **********************************************************************/ void bat_tray_init() { + char *icon_file; + // Get the battery number, store it for later bat_num = get_bat_num(); // Set up battery info filenames/paths - sprintf(CHARGE_VALUE_PATH, "/sys/class/power_supply/BAT%d/capacity", bat_num); - sprintf(CHARGE_STATE_PATH, "/sys/class/power_supply/BAT%d/status", bat_num); + asprintf(&CHARGE_VALUE_PATH, "/sys/class/power_supply/BAT%d/capacity", bat_num); + asprintf(&CHARGE_STATE_PATH, "/sys/class/power_supply/BAT%d/status", bat_num); debug("Spawning new status icon\n"); tray = gtk_status_icon_new(); - char* icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-charged.png", NULL); + asprintf(&icon_file,"%s/traybat-charged.png",_DEFAULT_THEME); gtk_status_icon_set_from_file(tray, icon_file); + free(icon_file); gtk_status_icon_set_has_tooltip (tray, TRUE); g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(update_tooltip), NULL); g_timeout_add(5000, update_icon, NULL); @@ -140,6 +141,16 @@ void bat_tray_init() /*********************************************************************** + * Free memory allocated in bat_tray_init() + **********************************************************************/ +void bat_tray_end() +{ + free(CHARGE_VALUE_PATH); + free(CHARGE_STATE_PATH); +} + + +/*********************************************************************** * Show the battery tray **********************************************************************/ void bat_tray_show() @@ -185,6 +196,7 @@ int get_battery_state() return STATE_UNKNOWN; } + /*********************************************************************** * Get the number of the first (who has more than one?) battery * Returns -1 if no battery present @@ -192,11 +204,11 @@ int get_battery_state() int get_bat_num() { FILE* fd; - gchar file[40]; + char* file; unsigned int i; - for(i = 0; i < 3; i++) + for(i = 0; i < 10; i++) { - sprintf(file, "/sys/class/power_supply/BAT%d/present", i); + asprintf(&file, "/sys/class/power_supply/BAT%d/present", i); debug("Attempting to open '%s'\n",file); if( (fd = fopen(file, "r")) ) { @@ -205,10 +217,12 @@ int get_bat_num() { debug("Battery %d is present\n",i); fclose(fd); + free(file); return i; } } } debug("Fallthrough: couldn't find battery\n"); + free(file); return -1; } diff --git a/defaults.c b/defaults.c index 627b702..0efd12d 100644 --- a/defaults.c +++ b/defaults.c @@ -26,7 +26,7 @@ char* _DEFAULT_PROG; char* _DEFAULT_BAT_GOV; char* _DEFAULT_AC_GOV; bool _DEFAULT_SHOW_BATTERY = true; -char _DEFAULT_THEME[2048]; // to do : make dynamic with malloc +char* _DEFAULT_THEME; void defaults_init() { @@ -36,5 +36,5 @@ void defaults_init() _DEFAULT_BAT_GOV = NULL; _DEFAULT_AC_GOV = NULL; _DEFAULT_SHOW_BATTERY = true; - sprintf (_DEFAULT_THEME, SHAREDIR"/trayfreq/themes/default"); + asprintf(&_DEFAULT_THEME, SHAREDIR"/trayfreq/themes/default"); } diff --git a/defaults.h b/defaults.h index 0963b8d..95e2d44 100644 --- a/defaults.h +++ b/defaults.h @@ -28,7 +28,7 @@ char* _DEFAULT_PROG; char* _DEFAULT_BAT_GOV; char* _DEFAULT_AC_GOV; bool _DEFAULT_SHOW_BATTERY; -char _DEFAULT_THEME[2048]; +char* _DEFAULT_THEME; void defaults_init(); diff --git a/getcore.c b/getcore.c index 51ac0e4..3014db0 100644 --- a/getcore.c +++ b/getcore.c @@ -33,12 +33,13 @@ unsigned int cores; **********************************************************************/ bool core_exists(unsigned int core) { - FILE* fd; - char path[128]; - - sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq", core); + char* path; + int result; + asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq", core); debug("Checking if core %d exists by opening '%s'\n",core,path); - return (access(path, F_OK) != -1); + result = access(path, F_OK); + free (path); + return (result != -1); } 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; } /*********************************************************************** diff --git a/getfreq.h b/getfreq.h index 8df9eca..54527a6 100644 --- a/getfreq.h +++ b/getfreq.h @@ -22,7 +22,7 @@ void gf_init(); int gf_current(int core); int gf_available(int core, char* out, int size); -void gf_get_frequency_label(int freq, char* out); +char* gf_get_frequency_label(int freq); char* gf_freqa(int core, int index); int gf_freqi(int core, int index); int gf_number(); diff --git a/tray.c b/tray.c index e1c2940..f4c08ab 100644 --- a/tray.c +++ b/tray.c @@ -34,10 +34,10 @@ #include -#define TOOLTIP_TEXT_SIZE 500 +//#define TOOLTIP_TEXT_SIZE 500 GtkStatusIcon* tray; -char tooltip_text[TOOLTIP_TEXT_SIZE]; +//char tooltip_text[TOOLTIP_TEXT_SIZE]; GtkWidget* menu; GSList* menu_items; @@ -128,7 +128,7 @@ static void tray_generate_menu() tray_clear_menu(); gg_init(); - char label[20]; + char *label; int i = 0; char current_governor[20]; @@ -140,11 +140,13 @@ static void tray_generate_menu() // Add available frequencies for(i = 0; i < gf_number(); ++i) { - memset(label, '\0', 20); - gf_get_frequency_label(gf_freqi(0, i), label); + label = gf_get_frequency_label(gf_freqi(0, i)); debug("Got freq label '%s', i=%d\n",label,i); GtkWidget* item = gtk_radio_menu_item_new_with_label(menu_items, label); + + free(label); + menu_items = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM (item)); if(g_strcmp0(current_governor, "userspace") == 0 && gf_freqi(0, i) == current_frequency) @@ -198,28 +200,28 @@ static void tray_generate_menu() **********************************************************************/ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data) { - char msg[TOOLTIP_TEXT_SIZE]; - char current_governor[20]; - char label[20]; + char *msg, *label; + char current_governor[20]; // TO DO int i = 0; - memset(msg, '\0', sizeof(msg)); memset(current_governor, '\0', sizeof(current_governor) ); gg_current(0, current_governor, sizeof(current_governor) ); - sprintf(msg+strlen(msg), _("Governor: %s\n"), current_governor); + + asprintf(&msg, _("Governor: %s\n"), current_governor); for(i = 0; i < gc_number(); ++i) { - debug("Adding CPU%i's frequency\n",i); - memset(label, '\0', sizeof(label)); - gf_get_frequency_label(gf_current(i), label); - sprintf(msg+strlen(msg), _("CPU%i: %s%s"), i, label, i == gc_number()-1 ? "" : "\n"); + debug("Adding CPU%d's frequency\n",i); + label = gf_get_frequency_label(gf_current(i)); + asprintf(&msg, _("%sCPU%d: %s%s"), msg, i, label, i == gc_number()-1 ? "" : "\n"); + free(label); } debug("Setting tooltip text\n"); - tray_set_tooltip(msg); - gtk_tooltip_set_text(tooltip, tooltip_text); + gtk_tooltip_set_text(tooltip, msg); + + free(msg); return TRUE; } @@ -326,8 +328,8 @@ void tray_init() void tray_set_tooltip(const char* msg) { debug("Setting up toolip var with text '%s'\n",msg); - memset(tooltip_text, '\0', TOOLTIP_TEXT_SIZE); - memmove(tooltip_text, msg, strlen(msg)); + //memset(tooltip_text, '\0', TOOLTIP_TEXT_SIZE); + //memmove(tooltip_text, msg, strlen(msg)); } /********************************************************************** @@ -335,6 +337,7 @@ void tray_set_tooltip(const char* msg) **********************************************************************/ void tray_update_icon_percent() { + char* file; gulong max_frequency = gf_freqi(0, 0); gint adjusted_percent = 0; // If no governor, set percentage to 0. This if statement fixes an FPE a few lines down @@ -358,16 +361,13 @@ void tray_update_icon_percent() } } - debug("Rounded/adjusted bat percentage: %d\n",adjusted_percent); - /* convert the int to a string */ - char adjusted_percent_string[] = {'\0', '\0', '\0', '\0'}; - sprintf(adjusted_percent_string, "%i", adjusted_percent); - - char* file = g_strconcat(_DEFAULT_THEME, "/cpufreq-", adjusted_percent_string, ".png", NULL); + debug("Rounded/adjusted CPU percentage: %d\n",adjusted_percent); + asprintf(&file, "%s/cpufreq-%d.png", _DEFAULT_THEME,adjusted_percent); debug("Setting tray icon to '%s'\n",file); gtk_status_icon_set_from_file(tray, file); - g_free(file); + + free(file); } -- cgit v1.1