aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO.md1
-rw-r--r--bat_tray.c75
-rw-r--r--defaults.c2
-rw-r--r--defaults.h2
-rw-r--r--getcore.c5
-rw-r--r--getfreq.c29
-rw-r--r--getfreq.h2
-rw-r--r--getgov.c17
-rw-r--r--paramano.c2
-rw-r--r--tray.c24
10 files changed, 56 insertions, 103 deletions
diff --git a/TODO.md b/TODO.md
index a20649d..b0926ac 100644
--- a/TODO.md
+++ b/TODO.md
@@ -3,7 +3,6 @@ To do
Normal Priority
---------------
-* Remove all calls to asprintf, replace with snprintf
* Get rid of various magic numbers
* Sniff for memory leaks
diff --git a/bat_tray.c b/bat_tray.c
index 8e9997e..3d01b85 100644
--- a/bat_tray.c
+++ b/bat_tray.c
@@ -22,7 +22,8 @@
static GtkStatusIcon* tray;
static char tooltip_text[1024];
static int bat_num; // Shortcoming: we only detect one battery
-static char *CHARGE_VALUE_PATH, *CHARGE_STATE_PATH;
+static char CHARGE_VALUE_PATH[1024];
+static char CHARGE_STATE_PATH[1024];
/***********************************************************************
* Return the battery level percentage
@@ -115,39 +116,34 @@ static gboolean show_tooltip(GtkStatusIcon* status_icon, gint x, gint y, gboolea
**********************************************************************/
static void update_tooltip_cache()
{
- char* msg;
+ char msg[512];
+ char time_left[64];
int seconds_left = get_bat_seconds_left();
- char* time_left;
if (seconds_left == -1)
- {
- asprintf(&time_left, _("Unknown time left"));
- } else {
- asprintf(&time_left, _("%02d:%02d left"), (int)(seconds_left/3600), (int)((seconds_left%3600)/60));
- }
+ snprintf(time_left, sizeof(time_left), _("Unknown time left"));
+ else
+ snprintf(time_left, sizeof(time_left), _("%02d:%02d left"), (int)(seconds_left/3600), (int)((seconds_left%3600)/60));
switch(get_battery_state())
{
case STATE_DISCHARGING:
- asprintf(&msg, _("Discharging (%d%%)\n%s"), get_bat_percent(), time_left);
+ snprintf(msg, sizeof(msg), _("Discharging (%d%%)\n%s"), get_bat_percent(), time_left);
break;
case STATE_CHARGING:
- asprintf(&msg, _("Charging (%d%%)\n%s"), get_bat_percent(), time_left);
+ snprintf(msg, sizeof(msg), _("Charging (%d%%)\n%s"), get_bat_percent(), time_left);
break;
case STATE_CHARGED:
- asprintf(&msg, _("Fully charged") );
+ snprintf(msg, sizeof(msg), _("Fully charged") );
break;
default:
- asprintf(&msg, _("Unknown status") );
+ snprintf(msg, sizeof(msg), _("Unknown status") );
break;
}
strncpy(tooltip_text, msg, sizeof(tooltip_text));
-
- free(time_left);
- free(msg);
}
@@ -157,32 +153,29 @@ static void update_tooltip_cache()
**********************************************************************/
static gboolean update()
{
- char *icon_file;
- unsigned int rounded;
+ char icon_file[1024];
+ unsigned int rounded = 0;
- rounded = 20* (int)((get_bat_percent()+10)/20); // Round percentage to 0, 20, 40, 60, 80 or 100
+ /* Round percentage to 0, 20, 40, 60, 80 or 100 */
+ rounded = 20 * (int)((get_bat_percent() + 10) / 20);
- switch ( get_battery_state() )
+ switch (get_battery_state())
{
case STATE_DISCHARGING:
- asprintf(&icon_file,"%s/bat-%d.png",DEFAULT_THEME,rounded);
+ snprintf(icon_file, sizeof(icon_file), "%s/bat-%d.png", DEFAULT_THEME, rounded);
break;
case STATE_CHARGING:
- asprintf(&icon_file,"%s/bat-%d-charging.png",DEFAULT_THEME,rounded);
+ snprintf(icon_file, sizeof(icon_file), "%s/bat-%d-charging.png", DEFAULT_THEME, rounded);
break;
default:
- asprintf(&icon_file,"%s/bat-charged.png",DEFAULT_THEME);
+ snprintf(icon_file, sizeof(icon_file), "%s/bat-charged.png", DEFAULT_THEME);
break;
}
-
update_tooltip_cache();
gtk_status_icon_set_from_file(tray, icon_file);
-
- free(icon_file);
-
return true;
}
@@ -192,40 +185,28 @@ static gboolean update()
**********************************************************************/
void bat_tray_init()
{
- char *icon_file;
+ char icon_file[1024];
// Get the battery number, store it for later
bat_num = get_bat_num();
// Set up battery info filenames/paths
- 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);
+ snprintf(CHARGE_VALUE_PATH, sizeof(CHARGE_STATE_PATH), "/sys/class/power_supply/BAT%d/capacity", bat_num);
+ snprintf(CHARGE_STATE_PATH, sizeof(CHARGE_STATE_PATH), "/sys/class/power_supply/BAT%d/status", bat_num);
tray = gtk_status_icon_new();
- asprintf(&icon_file,"%s/bat-charged.png",DEFAULT_THEME);
+ snprintf(icon_file, sizeof(icon_file), "%s/bat-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(show_tooltip), NULL);
g_timeout_add(120000, update, NULL);
- /* Force something useful to be in the cached tooltip text,
- * force meaningful icon */
+ /* Force useful cached tooltip text, force meaningful icon */
update();
}
/***********************************************************************
- * 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()
@@ -279,22 +260,20 @@ int get_battery_state()
int get_bat_num()
{
FILE* fd;
- char* file;
+ char filename[1024];
unsigned int i;
for(i = 0; i < 10; i++)
{
- asprintf(&file, "/sys/class/power_supply/BAT%d/present", i);
- if( (fd = fopen(file, "r")) )
+ snprintf(filename, sizeof(filename), "/sys/class/power_supply/BAT%d/present", i);
+ if( (fd = fopen(filename, "r")) )
{
if (fgetc(fd) == '1')
{
fclose(fd);
- free(file);
return i;
}
fclose(fd);
}
}
- free(file);
return -1;
}
diff --git a/defaults.c b/defaults.c
index 23a662c..d9ff21d 100644
--- a/defaults.c
+++ b/defaults.c
@@ -26,5 +26,5 @@ void defaults_init()
DEFAULT_BAT_GOV = NULL;
DEFAULT_AC_GOV = NULL;
DEFAULT_SHOW_BATTERY = true;
- asprintf(&DEFAULT_THEME, SHAREDIR"/paramano/themes/default");
+ snprintf(DEFAULT_THEME, sizeof(DEFAULT_THEME), SHAREDIR"/paramano/themes/default");
}
diff --git a/defaults.h b/defaults.h
index c4bd176..519c082 100644
--- a/defaults.h
+++ b/defaults.h
@@ -25,7 +25,7 @@ char* DEFAULT_PROG;
char* DEFAULT_BAT_GOV;
char* DEFAULT_AC_GOV;
bool DEFAULT_SHOW_BATTERY;
-char* DEFAULT_THEME;
+char DEFAULT_THEME[1024];
void defaults_init();
diff --git a/getcore.c b/getcore.c
index 4c7bd32..2c25c75 100644
--- a/getcore.c
+++ b/getcore.c
@@ -25,11 +25,10 @@ static unsigned int cores;
**********************************************************************/
bool core_exists(unsigned int core)
{
- char* path;
+ char path[1024];
int result;
- asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq", core);
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq", core);
result = access(path, F_OK);
- free (path);
return (result != -1);
}
diff --git a/getfreq.c b/getfreq.c
index c655ffd..fbf46e9 100644
--- a/getfreq.c
+++ b/getfreq.c
@@ -71,23 +71,19 @@ int gf_current(int core)
{
FILE* fd;
char buff[4096];
- char* path;
+ char path[1024];
int freq;
- asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", core);
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", core);
if(!(fd = fopen(path, "r")))
- {
- free(path);
return -1;
- }
fgets(buff, 13, fd);
freq = atoi(buff);
fclose(fd);
- free(path);
return freq;
}
@@ -98,39 +94,32 @@ int gf_current(int core)
int gf_available(int core, char* out, int size)
{
FILE* fd;
- char* path;
+ char path[1024];
- asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_frequencies", core);
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_frequencies", core);
if(!(fd = fopen(path, "r")))
- {
- 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`
**********************************************************************/
-char* gf_get_frequency_label(int freq)
+void gf_get_frequency_label(char *buffer, size_t max_size, int freq)
{
- char *string;
if(freq >= 1000000000) // >= 1 billion KHz (1 THz) This, ladies and gentlement, is future-proofing ;)
- asprintf(&string, "%.2f THz", (double)freq/1000000000 );
+ snprintf(buffer, max_size, "%.2f THz", (double)freq/1000000000 );
else if(freq >= 1000000) // >= 1 million KHz (1 GHz)
- asprintf(&string, "%.2f GHz", (double)freq/1000000 );
+ snprintf(buffer, max_size, "%.2f GHz", (double)freq/1000000 );
else if (freq >= 1000) // >= 1 thousand KHz (1 MHz)
- asprintf(&string, "%.2f MHz", (double)freq/1000 );
+ snprintf(buffer, max_size, "%.2f MHz", (double)freq/1000 );
else // < 1000 KHz (1 MHz)
- asprintf(&string, "%.2f KHz", (double)freq);
-
- return string;
+ snprintf(buffer, max_size, "%.2f KHz", (double)freq);
}
/***********************************************************************
diff --git a/getfreq.h b/getfreq.h
index 17b5242..f3c490c 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);
-char* gf_get_frequency_label(int freq);
+void gf_get_frequency_label(char* buffer, size_t max_size, int freq);
char* gf_freqa(int core, int index);
int gf_freqi(int core, int index);
unsigned int gf_number();
diff --git a/getgov.c b/getgov.c
index e86f7fd..7ff8562 100644
--- a/getgov.c
+++ b/getgov.c
@@ -57,14 +57,11 @@ void gg_init()
bool gg_current(int core, char* out, int size)
{
FILE* fd;
- char *path;
- asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", core);
+ char path[1024];
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", core);
if (!(fd = fopen(path, "r")))
- {
- free(path);
return false;
- }
fgets(out, size, fd);
@@ -73,7 +70,6 @@ bool gg_current(int core, char* out, int size)
*newline = '\0';
fclose(fd);
- free(path);
return true;
}
@@ -83,18 +79,15 @@ bool gg_current(int core, char* out, int size)
**********************************************************************/
bool gg_available(int core, char* out, int size)
{
- char *path;
+ char path[1024];
FILE *fd;
- asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_governors", core);
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_governors", core);
if (!(fd = fopen(path, "r")))
- {
- free(path);
return false;
- }
+
fgets(out, size, fd);
fclose(fd);
- free(path);
return true;
}
diff --git a/paramano.c b/paramano.c
index b73462e..b0e2671 100644
--- a/paramano.c
+++ b/paramano.c
@@ -103,7 +103,7 @@ void config_init()
info("UID: %d GID: %d\n", getuid(), getgid());
if ((temp = config_get_key(&config, "extra", "theme")))
- asprintf(&DEFAULT_THEME, "%s", temp);
+ snprintf(DEFAULT_THEME, sizeof(DEFAULT_THEME), "%s", temp);
g_free(config.file_name);
config_close(&config);
diff --git a/tray.c b/tray.c
index 899069f..696d6b8 100644
--- a/tray.c
+++ b/tray.c
@@ -93,7 +93,7 @@ static void tray_generate_menu()
tray_clear_menu();
gg_init();
- char *label;
+ char label[1024];
unsigned int i = 0;
char current_governor[20];
@@ -105,12 +105,10 @@ static void tray_generate_menu()
// Add available frequencies
for(i = 0; i < gf_number(); i++)
{
- label = gf_get_frequency_label(gf_freqi(0, i));
+ gf_get_frequency_label(label, sizeof(label), gf_freqi(0, 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)
@@ -159,27 +157,25 @@ static gboolean show_tooltip(GtkStatusIcon* status_icon, gint x, gint y, gboolea
**********************************************************************/
static void update_tooltip_cache()
{
- char *msg, *label;
+ char msg[1024], label[1024];
char current_governor[20]; // TO DO
unsigned int i = 0;
+ int offset = 0;
memset(current_governor, '\0', sizeof(current_governor) );
gg_current(0, current_governor, sizeof(current_governor) );
- asprintf(&msg, _("Governor: %s\n"), current_governor);
+ offset = snprintf(msg, sizeof(msg), _("Governor: %s\n"), current_governor);
for(i = 0; i < gc_number(); 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);
+ gf_get_frequency_label(label, sizeof(label), gf_current(i));
+ offset += snprintf(msg+offset, sizeof(msg), _("CPU%d: %s%s"), i, label, i == gc_number()-1 ? "" : "\n");
}
strncpy(tooltip_text, msg, sizeof(tooltip_text));
tooltip_text[sizeof(tooltip_text)] = '\0';
-
- free(msg);
}
@@ -198,7 +194,7 @@ static void popup_menu(GtkStatusIcon* statuc_icon,guint button,guint activate_ti
**********************************************************************/
static void update_icon()
{
- char* file;
+ char file[1024];
int max_frequency = gf_freqi(0, 0);
int adjusted_percent, percent;
@@ -224,10 +220,8 @@ static void update_icon()
}
}
- asprintf(&file, "%s/cpu-%d.png", DEFAULT_THEME,adjusted_percent);
+ snprintf(file, sizeof(file), "%s/cpu-%d.png", DEFAULT_THEME,adjusted_percent);
gtk_status_icon_set_from_file(tray, file);
-
- free(file);
}