1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-09-29 21:49:07 +00:00

2008-03-19

* Fixed a number of small memory leaks.
	* Performed some minor profiling on Conky; introduced
	small_text_buffer_size and large_text_buffer_size config options instead
	of just text_buffer_size.
	* Fixed some minor bugs introduced by new patches.


git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@1014 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
Brenden Matthews 2008-03-19 22:28:23 +00:00
parent 4ad58c9d04
commit 2698046591
8 changed files with 365 additions and 289 deletions

View File

@ -1,5 +1,12 @@
# $Id$
2008-03-19
* Fixed a number of small memory leaks.
* Performed some minor profiling on Conky; introduced
small_text_buffer_size and large_text_buffer_size config options instead
of just text_buffer_size.
* Fixed some minor bugs introduced by new patches.
2008-03-17
* Applied 12 patches:
1) sysfs battery support (for Linux >=2.6.24) (thanks Kapil)

View File

@ -229,11 +229,20 @@
</varlistentry>
<varlistentry>
<term><command><option>text_buffer_size</option></command>
<term><command><option>large_text_buffer_size</option></command>
<option>bytes</option>
</term>
<listitem>
Size of the standard text buffer (default is 1280 bytes).
Size of the standard large text buffer (default is 1024 bytes). This buffer is used for the entirety of Conky's text. Increasing the buffer's size will decrease performance and increase the memory footprint of Conky, but it will allow for more text to be displayed in Conky.
<para></para></listitem>
</varlistentry>
<varlistentry>
<term><command><option>small_text_buffer_size</option></command>
<option>bytes</option>
</term>
<listitem>
Size of the standard small text buffer (default is 128 bytes). This buffer is used for intermediary text, such as individual lines, output from $exec vars, and various other variables. Increasing the size of this buffer can drastically reduce Conky's performance, but will allow for more text display per variable. The size of this buffer cannot be smaller than the default value of 128 bytes.
<para></para></listitem>
</varlistentry>

View File

@ -150,7 +150,7 @@ enum alignment {
/* for fonts */
struct font_list {
char name[TEXT_BUFFER_SIZE];
char name[DEFAULT_TEXT_BUFFER_SIZE];
int num;
XFontStruct *font;
@ -210,8 +210,8 @@ int addfont(const char *data_in)
CRIT_ERR("realloc in addfont");
}
// must account for null terminator
if (strlen(data_in) < TEXT_BUFFER_SIZE) {
strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
#ifdef XFT
fonts[font_count].font_alpha = 0xffff;
#endif
@ -228,10 +228,11 @@ void set_first_font(const char *data_in)
== NULL) {
CRIT_ERR("malloc");
}
memset(fonts, 0, sizeof(struct font_list));
font_count++;
}
if (strlen(data_in) > 1) {
strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE - 1);
strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE - 1);
#ifdef XFT
fonts[0].font_alpha = 0xffff;
#endif
@ -269,8 +270,9 @@ static void load_fonts()
/* if (fonts[i].xftfont != NULL && selected_font == 0) {
XftFontClose(display, fonts[i].xftfont);
} */
if ((fonts[i].xftfont = XftFontOpenName(display, screen,
fonts[i].name)) != NULL) {
fonts[i].xftfont = XftFontOpenName(display, screen,
fonts[i].name);
if (fonts[i].xftfont != NULL) {
continue;
}
@ -368,7 +370,9 @@ static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
/* maximum size of individual text buffers, ie $exec buffer size */
unsigned int text_buffer_size = TEXT_BUFFER_SIZE;
unsigned int small_text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
unsigned int large_text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE * 8;
unsigned int p_p_max_size = DEFAULT_TEXT_BUFFER_SIZE * 8; // sorry I couldn't come up with a better var name
#ifdef HAVE_ICONV
#define CODEPAGE_LENGTH 20
@ -485,7 +489,7 @@ static inline int calc_text_width(const char *s, int l)
/* formatted text to render on screen, generated in generate_text(),
* drawn in draw_stuff() */
static char text_buffer[TEXT_BUFFER_SIZE * 4];
static char *text_buffer;
/* special stuff in text_buffer */
@ -680,7 +684,7 @@ static void new_font(char *buf, char *args)
if (args) {
struct special_t *s = new_special(buf, FONT);
if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
if (s->font_added > font_count || !s->font_added || strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE)) {
int tmp = selected_font;
selected_font = s->font_added = addfont(args);
@ -1909,7 +1913,7 @@ void *threaded_exec(struct text_object *obj)
FILE *fp = popen(obj->data.texeci.cmd, "r");
timed_thread_lock(obj->data.texeci.p_timed_thread);
int n2 = fread(p2, 1, text_buffer_size, fp);
int n2 = fread(p2, 1, small_text_buffer_size, fp);
pclose(fp);
p2[n2] = '\0';
@ -2195,45 +2199,22 @@ static void free_text_objects(unsigned int count, struct text_object *objs)
}
free(objs);
#ifdef MPD
if (info.mpd.title) {
free(info.mpd.title);
info.mpd.title = NULL;
}
if (info.mpd.artist) {
free(info.mpd.artist);
info.mpd.artist = NULL;
}
if (info.mpd.album) {
free(info.mpd.album);
info.mpd.album = NULL;
}
if (info.mpd.random) {
free(info.mpd.random);
info.mpd.random = NULL;
}
if (info.mpd.repeat) {
free(info.mpd.repeat);
info.mpd.repeat = NULL;
}
if (info.mpd.track) {
free(info.mpd.track);
info.mpd.track = NULL;
}
if (info.mpd.name) {
free(info.mpd.name);
info.mpd.name = NULL;
}
if (info.mpd.file) {
free(info.mpd.file);
info.mpd.file = NULL;
}
if (info.mpd.status) {
free(info.mpd.status);
info.mpd.status = NULL;
}
free_mpd_vars(&info);
#endif
/* text_objects = NULL;
text_object_count = 0; */
/* if (tmpstring1) {
free(tmpstring1);
tmpstring1 = 0;
}
if (tmpstring2) {
free(tmpstring2);
tmpstring2 = 0;
}
if (text_buffer) {
free(text_buffer);
text_buffer = 0;
}*/
}
void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
@ -2708,7 +2689,7 @@ static struct text_object *construct_text_object(const char *s,
obj->data.s = strdup(buf);
} else {
obj->data.execi.cmd = strdup(arg + n);
obj->data.execi.buffer = (char *) calloc(1, text_buffer_size);
obj->data.execi.buffer = (char *) calloc(1, small_text_buffer_size);
}
END OBJ(texeci, 0)
unsigned int n;
@ -2722,7 +2703,7 @@ static struct text_object *construct_text_object(const char *s,
obj->data.s = strdup(buf);
} else {
obj->data.texeci.cmd = strdup(arg + n);
obj->data.texeci.buffer = (char *) calloc(1, text_buffer_size);
obj->data.texeci.buffer = (char *) calloc(1, small_text_buffer_size);
}
obj->data.texeci.p_timed_thread = NULL;
END OBJ(pre_exec, 0)
@ -3029,7 +3010,7 @@ static struct text_object *construct_text_object(const char *s,
}
if (fp || obj->data.tail.fd != -1) {
obj->data.tail.logfile = malloc(text_buffer_size);
obj->data.tail.logfile = malloc(small_text_buffer_size);
strcpy(obj->data.tail.logfile, buf);
obj->data.tail.wantedlines = n1;
obj->data.tail.interval = update_interval * 2;
@ -3074,7 +3055,7 @@ static struct text_object *construct_text_object(const char *s,
}
if (fp || obj->data.tail.fd != -1) {
obj->data.tail.logfile = malloc(text_buffer_size);
obj->data.tail.logfile = malloc(small_text_buffer_size);
strcpy(obj->data.tail.logfile, buf);
obj->data.tail.wantedlines = n1;
obj->data.tail.interval = n2;
@ -3093,7 +3074,7 @@ static struct text_object *construct_text_object(const char *s,
return NULL;
}
/* asumming all else worked */
obj->data.tail.buffer = malloc(text_buffer_size * 20);
obj->data.tail.buffer = malloc(small_text_buffer_size * 20);
END OBJ(head, 0)
char buf[64];
int n1, n2;
@ -3114,7 +3095,7 @@ static struct text_object *construct_text_object(const char *s,
fp = fopen(buf, "r");
if (fp != NULL) {
obj->data.tail.logfile = malloc(text_buffer_size);
obj->data.tail.logfile = malloc(small_text_buffer_size);
strcpy(obj->data.tail.logfile, buf);
obj->data.tail.wantedlines = n1;
obj->data.tail.interval = update_interval * 2;
@ -3139,7 +3120,7 @@ static struct text_object *construct_text_object(const char *s,
fp = fopen(buf, "r");
if (fp != NULL) {
obj->data.tail.logfile = malloc(text_buffer_size);
obj->data.tail.logfile = malloc(small_text_buffer_size);
strcpy(obj->data.tail.logfile, buf);
obj->data.tail.wantedlines = n1;
obj->data.tail.interval = n2;
@ -3155,7 +3136,7 @@ static struct text_object *construct_text_object(const char *s,
return NULL;
}
/* asumming all else worked */
obj->data.tail.buffer = malloc(text_buffer_size * 20);
obj->data.tail.buffer = malloc(small_text_buffer_size * 20);
END OBJ(loadavg, INFO_LOADAVG)
int a = 1, b = 2, c = 3, r = 3;
@ -3259,11 +3240,11 @@ static struct text_object *construct_text_object(const char *s,
obj->data.local_mail.box = strdup(dst);
obj->data.local_mail.interval = n1;
END OBJ(mboxscan, 0)
obj->data.mboxscan.args = (char *) malloc(TEXT_BUFFER_SIZE);
obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
obj->data.mboxscan.args = (char *) malloc(small_text_buffer_size);
obj->data.mboxscan.output = (char *) malloc(small_text_buffer_size);
/* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
obj->data.mboxscan.output[0] = 1;
strncpy(obj->data.mboxscan.args, arg, TEXT_BUFFER_SIZE);
strncpy(obj->data.mboxscan.args, arg, small_text_buffer_size);
END OBJ(mem, INFO_MEM)
END OBJ(memmax, INFO_MEM)
END OBJ(memperc, INFO_MEM)
@ -3932,7 +3913,7 @@ void parse_conky_vars(char *text, char *p, struct information *cur)
struct text_object_list *object_list =
extract_variable_text_internal(text);
generate_text_internal(p, P_MAX_SIZE, object_list->text_objects,
generate_text_internal(p, p_p_max_size, object_list->text_objects,
object_list->text_object_count, cur);
free(object_list);
}
@ -4018,6 +3999,46 @@ static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
}
char *format_time(unsigned long time, const int width)
{
char buf[10];
unsigned long nt; // narrow time, for speed on 32-bit
unsigned cc; // centiseconds
unsigned nn; // multi-purpose whatever
nt = time;
cc = nt % 100; // centiseconds past second
nt /= 100; // total seconds
nn = nt % 60; // seconds past the minute
nt /= 60; // total minutes
if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
nt, nn, cc)) {
return strdup(buf);
}
if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
return strdup(buf);
}
nn = nt % 60; // minutes past the hour
nt /= 60; // total hours
if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
return strdup(buf);
}
nn = nt; // now also hours
if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
return strdup(buf);
}
nn /= 24; // now days
if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
return strdup(buf);
}
nn /= 7; // now weeks
if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
return strdup(buf);
}
// well shoot, this outta' fit...
return strdup("<inf>");
}
static void generate_text_internal(char *p, int p_max_size,
struct text_object *objs, unsigned int object_count,
struct information *cur)
@ -4025,7 +4046,8 @@ static void generate_text_internal(char *p, int p_max_size,
unsigned int i;
#ifdef HAVE_ICONV
char buff_in[P_MAX_SIZE] = { 0 };
char buff_in[p_max_size];
buff_in[0] = 0;
iconv_converting = 0;
#endif
@ -4652,8 +4674,8 @@ static void generate_text_internal(char *p, int p_max_size,
char *output = obj->data.execi.buffer;
FILE *fp = popen(obj->data.execi.cmd, "r");
// int length = fread(output, 1, text_buffer_size, fp);
int length = fread(output, 1, text_buffer_size, fp);
// int length = fread(output, 1, small_text_buffer_size, fp);
int length = fread(output, 1, small_text_buffer_size, fp);
pclose(fp);
output[length] = '\0';
@ -5209,7 +5231,7 @@ static void generate_text_internal(char *p, int p_max_size,
}
OBJ(mboxscan) {
mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
TEXT_BUFFER_SIZE);
small_text_buffer_size);
snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
}
OBJ(new_mails) {
@ -5624,47 +5646,6 @@ static void generate_text_internal(char *p, int p_max_size,
snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
}
#endif
char *format_time(unsigned long time, const int width)
{
char buf[10];
unsigned long nt; // narrow time, for speed on 32-bit
unsigned cc; // centiseconds
unsigned nn; // multi-purpose whatever
nt = time;
cc = nt % 100; // centiseconds past second
nt /= 100; // total seconds
nn = nt % 60; // seconds past the minute
nt /= 60; // total minutes
if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
nt, nn, cc)) {
return strdup(buf);
}
if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
return strdup(buf);
}
nn = nt % 60; // minutes past the hour
nt /= 60; // total hours
if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
return strdup(buf);
}
nn = nt; // now also hours
if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
return strdup(buf);
}
nn /= 24; // now days
if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
return strdup(buf);
}
nn /= 7; // now weeks
if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
return strdup(buf);
}
// well shoot, this outta' fit...
return strdup("<inf>");
}
OBJ(top) {
if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
char *time;
@ -5785,10 +5766,10 @@ static void generate_text_internal(char *p, int p_max_size,
}
/* Make sure bsize is at least 1 byte smaller than the
* buffer max size. */
if (bsize > (long) ((text_buffer_size * 20) - 1)) {
fseek(fp, bsize - text_buffer_size * 20 - 1,
if (bsize > (long) ((small_text_buffer_size * 20) - 1)) {
fseek(fp, bsize - small_text_buffer_size * 20 - 1,
SEEK_CUR);
bsize = text_buffer_size * 20 - 1;
bsize = small_text_buffer_size * 20 - 1;
}
bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
fclose(fp);
@ -5844,8 +5825,8 @@ head:
obj->data.tail.readlines = iter;
/* Make sure nl is at least 1 byte smaller than the
* buffer max size. */
if (nl > (long) ((text_buffer_size * 20) - 1)) {
nl = text_buffer_size * 20 - 1;
if (nl > (long) ((small_text_buffer_size * 20) - 1)) {
nl = small_text_buffer_size * 20 - 1;
}
nl = fread(obj->data.tail.buffer, 1, nl, fp);
fclose(fp);
@ -5970,7 +5951,7 @@ head:
dummy1 = dummy2 = a;
strncpy(buff_in, p, P_MAX_SIZE);
strncpy(buff_in, p, p_max_size);
iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
while (dummy1 > 0) {
@ -5986,7 +5967,7 @@ head:
* singlebyte codepage */
a = outptr - p;
}
#endif
#endif /* HAVE_ICONV */
p += a;
p_max_size -= a;
}
@ -6018,7 +5999,7 @@ static void generate_text()
p = text_buffer;
generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
generate_text_internal(p, p_p_max_size, text_objects, text_object_count, cur);
if (stuff_in_upper_case) {
char *p;
@ -6299,9 +6280,9 @@ static void draw_string(const char *s)
printf("%s\n", s);
fflush(stdout); /* output immediately, don't buffer */
}
memset(tmpstring1, 0, TEXT_BUFFER_SIZE);
memset(tmpstring2, 0, TEXT_BUFFER_SIZE);
strncpy(tmpstring1, s, TEXT_BUFFER_SIZE - 1);
memset(tmpstring1, 0, small_text_buffer_size);
memset(tmpstring2, 0, small_text_buffer_size);
strncpy(tmpstring1, s, small_text_buffer_size - 1);
pos = 0;
added = 0;
char space[2];
@ -6313,25 +6294,19 @@ static void draw_string(const char *s)
/* This code looks for tabs in the text and coverts them to spaces.
* The trick is getting the correct number of spaces, and not going
* over the window's size without forcing the window larger. */
for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
for (i = 0; i < (int)small_text_buffer_size; i++) {
if (tmpstring1[i] == '\t') { // 9 is ascii tab
i2 = 0;
for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
/* if (pos + i2 > TEXT_BUFFER_SIZE - 1) {
fprintf(stderr, "buffer overrun detected\n");
} */
/* guard against overrun */
tmpstring2[MIN(pos + i2, TEXT_BUFFER_SIZE - 1)] = ' ';
tmpstring2[MIN(pos + i2, (int)small_text_buffer_size - 1)] = ' ';
added++;
}
pos += i2;
} else {
if (tmpstring1[i] != 9) {
/* if (pos > TEXT_BUFFER_SIZE - 1) {
fprintf(stderr, "buffer overrun detected\n");
} */
/* guard against overrun */
tmpstring2[MIN(pos, TEXT_BUFFER_SIZE - 1)] = tmpstring1[i];
tmpstring2[MIN(pos, (int)small_text_buffer_size - 1)] = tmpstring1[i];
pos++;
}
}
@ -6376,7 +6351,7 @@ static void draw_string(const char *s)
}
cur_x += width_of_s;
#endif /* X11 */
memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
memcpy(tmpstring1, s, small_text_buffer_size);
}
long redmask, greenmask, bluemask;
@ -7291,6 +7266,8 @@ void reload_config(void)
#ifdef X11
free_fonts();
load_fonts();
set_font();
#endif /* X11 */
#ifdef TCP_PORT_MONITOR
@ -7308,9 +7285,6 @@ void reload_config(void)
}
#ifdef X11
load_fonts();
set_font();
// clear the window first
XClearWindow(display, RootWindow(display, screen));
@ -7360,8 +7334,9 @@ void clean_up(void)
text_object_count = 0;
text_objects = NULL;
if (!text) {
if (text) {
free(text);
text = 0;
}
free(current_config);
@ -8124,15 +8099,30 @@ static void load_config_file(const char *f)
}
}
CONF("text_buffer_size") {
ERR("text_buffer_size is deprecated in favour of small_text_buffer size and large_text_buffer_size");
}
CONF("small_text_buffer_size") {
if (value) {
text_buffer_size = atoi(value);
small_text_buffer_size = atoi(value);
if (small_text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
small_text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
}
} else {
CONF_ERR;
}
}
CONF("large_text_buffer_size") {
if (value) {
large_text_buffer_size = atoi(value);
p_p_max_size = large_text_buffer_size;
} else {
CONF_ERR;
}
}
CONF("text") {
if (!text) {
if (text) {
free(text);
text = 0;
}
text = (char *) malloc(1);
@ -8190,6 +8180,24 @@ static void load_config_file(const char *f)
// default to update_interval
info.music_player_interval = update_interval;
}
if (tmpstring1) {
free(tmpstring1);
tmpstring1 = 0;
tmpstring1 = malloc(small_text_buffer_size);
memset(tmpstring1, 0, small_text_buffer_size);
}
if (tmpstring2) {
free(tmpstring2);
tmpstring2 = 0;
tmpstring2 = malloc(small_text_buffer_size);
memset(tmpstring2, 0, small_text_buffer_size);
}
if (text_buffer) {
free(text_buffer);
text_buffer = 0;
text_buffer = malloc(large_text_buffer_size);
memset(text_buffer, 0, large_text_buffer_size);
}
}
/* : means that character before that takes an argument */
@ -8418,8 +8426,9 @@ int main(int argc, char **argv)
#endif
#endif /* X11 */
case 't':
if (!text) {
if (text) {
free(text);
text = 0;
}
text = strdup(optarg);
convert_escapes(text);
@ -8458,8 +8467,9 @@ int main(int argc, char **argv)
/* generate text and get initial size */
extract_variable_text(text);
if (!text) {
if (text) {
free(text);
text = 0;
}
text = NULL;
/* fork */
@ -8488,6 +8498,13 @@ int main(int argc, char **argv)
}
}
text_buffer = malloc(large_text_buffer_size);
memset(text_buffer, 0, large_text_buffer_size);
tmpstring1 = malloc(small_text_buffer_size);
memset(tmpstring1, 0, small_text_buffer_size);
tmpstring2 = malloc(small_text_buffer_size);
memset(tmpstring2, 0, small_text_buffer_size);
#ifdef X11
selected_font = 0;
update_text_area(); /* to get initial size of the window */

View File

@ -85,9 +85,10 @@
#define TOP_MEM 4
#define TOP_TIME 5
#define TEXT_BUFFER_SIZE 1280
#define P_MAX_SIZE ((TEXT_BUFFER_SIZE * 4) - 2)
extern unsigned int text_buffer_size;
#define DEFAULT_TEXT_BUFFER_SIZE 128
extern unsigned int large_text_buffer_size;
extern unsigned int small_text_buffer_size;
extern unsigned int p_max_size;
/* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
#define MAX_SPECIALS_DEFAULT 512
@ -141,11 +142,12 @@ unsigned int diskio_read_value;
unsigned int diskio_write_value;
struct fs_stat {
char *path;
char *type;
char path[DEFAULT_TEXT_BUFFER_SIZE];
char type[DEFAULT_TEXT_BUFFER_SIZE];
long long size;
long long avail;
long long free;
char set;
};
#include "diskio.h"
@ -398,8 +400,8 @@ int use_spacer;
enum spacer_opts { NO_SPACER = 0, LEFT_SPACER, RIGHT_SPACER };
char tmpstring1[TEXT_BUFFER_SIZE];
char tmpstring2[TEXT_BUFFER_SIZE];
char *tmpstring1;
char *tmpstring2;
#ifdef X11
/* in x11.c */
@ -535,6 +537,7 @@ char get_freq(char *, size_t, char *, int, unsigned int);
void get_freq_dynamic(char *, size_t, char *, int);
char get_voltage(char *, size_t, char *, int, unsigned int); /* ptarjan */
void update_load_average();
int interface_up(const char *dev);
int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
int *div, char *devtype);
@ -663,6 +666,7 @@ char *get_apm_battery_time(void);
extern void init_mpd_stats(struct information *current_info);
void *update_mpd(void);
extern timed_thread *mpd_timed_thread;
void free_mpd_vars(struct information *current_info);
#endif /* MPD */
/* in xmms2.c */

View File

@ -57,14 +57,14 @@ struct fs_stat *fs_stats = fs_stats_;
static void update_fs_stat(struct fs_stat *fs);
static char* get_fs_type(const char* path);
void get_fs_type(const char *path, char *result);
void update_fs_stats()
{
unsigned i;
for (i = 0; i < MAX_FS_STATS; ++i) {
if (fs_stats[i].path) {
if (fs_stats[i].set) {
update_fs_stat(&fs_stats[i]);
}
}
@ -73,13 +73,8 @@ void update_fs_stats()
void clear_fs_stats()
{
unsigned i;
for (i = 0; i < MAX_FS_STATS; ++i) {
if (fs_stats[i].path) {
free(fs_stats[i].path);
free(fs_stats[i].type);
fs_stats[i].path = NULL;
}
memset(&fs_stats[i], 0, sizeof(struct fs_stat));
}
}
@ -90,8 +85,8 @@ struct fs_stat *prepare_fs_stat(const char *s)
/* lookup existing or get new */
for (i = 0; i < MAX_FS_STATS; ++i) {
if (fs_stats[i].path) {
if (strcmp(fs_stats[i].path, s) == 0) {
if (fs_stats[i].set) {
if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
return &fs_stats[i];
}
} else {
@ -103,7 +98,8 @@ struct fs_stat *prepare_fs_stat(const char *s)
ERR("too many fs stats");
return 0;
}
new->path = strdup(s);
strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
new->set = 1;
update_fs_stat(new);
return new;
}
@ -113,55 +109,58 @@ static void update_fs_stat(struct fs_stat *fs)
struct statfs s;
if (statfs(fs->path, &s) == 0) {
fs->size = (long long) s.f_blocks * s.f_bsize;
fs->size = (long long)s.f_blocks * s.f_bsize;
/* bfree (root) or bavail (non-roots) ? */
fs->avail = (long long) s.f_bavail * s.f_bsize;
fs->free = (long long) s.f_bfree * s.f_bsize;
fs->type = get_fs_type(fs->path);
fs->avail = (long long)s.f_bavail * s.f_bsize;
fs->free = (long long)s.f_bfree * s.f_bsize;
get_fs_type(fs->path, fs->type);
} else {
fs->size = 0;
fs->avail = 0;
fs->free = 0;
fs->type = "unknown";
strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
ERR("statfs '%s': %s", fs->path, strerror(errno));
}
}
static char* get_fs_type(const char* path)
void get_fs_type(const char *path, char *result)
{
#ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME
struct statfs s;
if(statfs(path, &s) == 0)
return s.f_fstypename;
else
if (statfs(path, &s) == 0) {
strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
} else {
ERR("statfs '%s': %s", path, strerror(errno));
}
return;
#else
#else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
/* TODO: walk up the directory tree so it works on
* on paths that are not actually mount points. */
/* TODO: walk up the directory tree so it works on on paths that are not actually mount points. */
FILE* mtab = setmntent( "/etc/mtab", "r" );
FILE *mtab = setmntent("/etc/mtab", "r");
if(mtab == NULL) {
if (mtab == NULL) {
ERR("setmntent /etc/mtab: %s", strerror(errno));
return "unknown";
strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
return;
}
struct mntent* me = getmntent(mtab);
struct mntent *me = getmntent(mtab);
// find our path in the mtab
while(getmntent(mtab) && strcmp(path,me->mnt_dir));
while (strcmp(path, me->mnt_dir) && getmntent(mtab));
endmntent(mtab);
if(me)
return strdup(me->mnt_type);
if (me && !strcmp(path, me->mnt_dir)) {
strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
return;
}
#endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
#endif // HAVE_STRUCT_STATFS_F_FSTYPENAME
return "unknown";
strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
}

View File

@ -56,28 +56,13 @@ static int subject_width;
static int print_mails;
static int time_delay;
/* I don't know what to use: TEXT_BUFFER_SIZE or text_buffer_size
* text_buffer_size is the maximum output in chars
* TEXT_BUFFER_SIZE actually something like a allowed size for things in the
* config, below 'TEXT'. Or what is more probably max_user_text.
* Anyway, I used TEXT_BUFFER_SIZE for not 'output' things here
* -- calmar
*
* To clarify, TEXT_BUFFER_SIZE is used for buffers of fixed size, and
* text_buffer_size is used for buffers which can change in size.
* text_buffer_size is just defined as TEXT_BUFFER_SIZE to start, so it's okay
* for most things, however if something is allocated with text_buffer_size and
* then text_buffer_size changes but the array doesn't, you might have some
* issues if you are using text_buffer_size to determine the size of the array.
* -- brenden */
static char mbox_mail_spool[TEXT_BUFFER_SIZE];
static char mbox_mail_spool[DEFAULT_TEXT_BUFFER_SIZE];
void mbox_scan(char *args, char *output, size_t max_len)
{
int i, u, flag;
int force_rescan = 0;
char buf[text_buffer_size];
char buf[small_text_buffer_size];
struct stat statbuf;
/* output was set to 1 after malloc'ing in conky.c */
@ -130,11 +115,11 @@ void mbox_scan(char *args, char *output, size_t max_len)
}
/* encapsulated with "'s find first occurrence of " */
if (args[strlen(args) - 1] == '"') {
strncpy(mbox_mail_spool, args, TEXT_BUFFER_SIZE);
strncpy(mbox_mail_spool, args, DEFAULT_TEXT_BUFFER_SIZE);
char *start = strchr(mbox_mail_spool, '"') + 1;
start[(long) (strrchr(mbox_mail_spool, '"') - start)] = '\0';
strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE);
strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE);
} else {
char *copy_args = strdup(args);
char *tmp = strtok(copy_args, " ");
@ -146,7 +131,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
start = tmp;
}
}
strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE);
strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE);
free(copy_args);
}
if (strlen(mbox_mail_spool) < 1) {
@ -225,7 +210,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
/* first find a "From " to set it to 0 for header-sarchings */
flag = 1;
while (!feof(fp)) {
if (fgets(buf, text_buffer_size, fp) == NULL) {
if (fgets(buf, small_text_buffer_size, fp) == NULL) {
break;
}
@ -234,7 +219,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
/* skip until \n */
while (strchr(buf, '\n') == NULL && !feof(fp)) {
fgets(buf, text_buffer_size, fp);
fgets(buf, small_text_buffer_size, fp);
}
flag = 0; /* in the headers now */
@ -250,7 +235,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
/* then search for new mail ("From ") */
while (strchr(buf, '\n') == NULL && !feof(fp)) {
fgets(buf, text_buffer_size, fp);
fgets(buf, small_text_buffer_size, fp);
}
flag = 1; /* in the body now */
continue;
@ -267,7 +252,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
curr = curr->previous;
/* Skip until \n */
while (strchr(buf, '\n') == NULL && !feof(fp)) {
fgets(buf, text_buffer_size, fp);
fgets(buf, small_text_buffer_size, fp);
}
continue;
}
@ -290,7 +275,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
curr->from[i] = '\0';
/* skip until \n */
while (strchr(buf, '\n') == NULL && !feof(fp)) {
fgets(buf, text_buffer_size, fp);
fgets(buf, small_text_buffer_size, fp);
}
break;
}
@ -309,7 +294,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
curr->from[i] = '\0';
/* skip until \n */
while (strchr(buf, '\n') == NULL && !feof(fp)) {
fgets(buf, text_buffer_size, fp);
fgets(buf, small_text_buffer_size, fp);
}
break;
}
@ -339,7 +324,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
/* skip until \n */
while (strchr(buf, '\n') == NULL && !feof(fp)) {
fgets(buf, text_buffer_size, fp);
fgets(buf, small_text_buffer_size, fp);
}
break;
}
@ -359,14 +344,14 @@ void mbox_scan(char *args, char *output, size_t max_len)
while (i) {
if (curr->from[0] != '\0') {
if (i != print_mails) {
snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width,
snprintf(buf, small_text_buffer_size, "\nF: %-*s S: %-*s", from_width,
curr->from, subject_width, curr->subject);
} else { /* first time - no \n in front */
snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width,
snprintf(buf, small_text_buffer_size, "F: %-*s S: %-*s", from_width,
curr->from, subject_width, curr->subject);
}
} else {
snprintf(buf, text_buffer_size, "\n");
snprintf(buf, small_text_buffer_size, "\n");
}
strncat(output, buf, max_len - strlen(output));

View File

@ -37,35 +37,79 @@ void clear_mpd_stats(struct information *current_info);
void init_mpd_stats(struct information *current_info)
{
if (current_info->mpd.artist == NULL) {
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.artist = malloc(small_text_buffer_size);
}
if (current_info->mpd.album == NULL) {
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.album = malloc(small_text_buffer_size);
}
if (current_info->mpd.title == NULL) {
current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.title = malloc(small_text_buffer_size);
}
if (current_info->mpd.random == NULL) {
current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.random = malloc(small_text_buffer_size);
}
if (current_info->mpd.repeat == NULL) {
current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.repeat = malloc(small_text_buffer_size);
}
if (current_info->mpd.track == NULL) {
current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.track = malloc(small_text_buffer_size);
}
if (current_info->mpd.status == NULL) {
current_info->mpd.status = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.status = malloc(small_text_buffer_size);
}
if (current_info->mpd.name == NULL) {
current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.name = malloc(small_text_buffer_size);
}
if (current_info->mpd.file == NULL) {
current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
current_info->mpd.file = malloc(small_text_buffer_size);
}
clear_mpd_stats(current_info);
}
void free_mpd_vars(struct information *current_info)
{
if (current_info->mpd.title) {
free(current_info->mpd.title);
current_info->mpd.title = NULL;
}
if (current_info->mpd.artist) {
free(current_info->mpd.artist);
current_info->mpd.artist = NULL;
}
if (current_info->mpd.album) {
free(current_info->mpd.album);
current_info->mpd.album = NULL;
}
if (current_info->mpd.random) {
free(current_info->mpd.random);
current_info->mpd.random = NULL;
}
if (current_info->mpd.repeat) {
free(current_info->mpd.repeat);
current_info->mpd.repeat = NULL;
}
if (current_info->mpd.track) {
free(current_info->mpd.track);
current_info->mpd.track = NULL;
}
if (current_info->mpd.name) {
free(current_info->mpd.name);
current_info->mpd.name = NULL;
}
if (current_info->mpd.file) {
free(current_info->mpd.file);
current_info->mpd.file = NULL;
}
if (current_info->mpd.status) {
free(current_info->mpd.status);
current_info->mpd.status = NULL;
}
if (current_info->conn) {
mpd_closeConnection(current_info->conn);
current_info->conn = 0;
}
}
void clear_mpd_stats(struct information *current_info)
{
*current_info->mpd.name = 0;
@ -107,7 +151,7 @@ void *update_mpd(void)
clear_mpd_stats(current_info);
strncpy(current_info->mpd.status, "MPD not responding",
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
timed_thread_unlock(mpd_timed_thread);
if (timed_thread_test(mpd_timed_thread)) {
timed_thread_exit(mpd_timed_thread);
@ -126,7 +170,7 @@ void *update_mpd(void)
clear_mpd_stats(current_info);
strncpy(current_info->mpd.status, "MPD not responding",
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
timed_thread_unlock(mpd_timed_thread);
if (timed_thread_test(mpd_timed_thread)) {
timed_thread_exit(mpd_timed_thread);
@ -151,14 +195,14 @@ void *update_mpd(void)
} */
if (status->state == MPD_STATUS_STATE_PLAY) {
strncpy(current_info->mpd.status, "Playing", TEXT_BUFFER_SIZE - 1);
strncpy(current_info->mpd.status, "Playing", small_text_buffer_size - 1);
}
if (status->state == MPD_STATUS_STATE_STOP) {
clear_mpd_stats(current_info);
strncpy(current_info->mpd.status, "Stopped", TEXT_BUFFER_SIZE - 1);
strncpy(current_info->mpd.status, "Stopped", small_text_buffer_size - 1);
}
if (status->state == MPD_STATUS_STATE_PAUSE) {
strncpy(current_info->mpd.status, "Paused", TEXT_BUFFER_SIZE - 1);
strncpy(current_info->mpd.status, "Paused", small_text_buffer_size - 1);
}
if (status->state == MPD_STATUS_STATE_UNKNOWN) {
clear_mpd_stats(current_info);
@ -209,37 +253,37 @@ void *update_mpd(void)
if (song->artist) {
strncpy(current_info->mpd.artist, song->artist,
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
} else {
*current_info->mpd.artist = 0;
}
if (song->album) {
strncpy(current_info->mpd.album, song->album,
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
} else {
*current_info->mpd.album = 0;
}
if (song->title) {
strncpy(current_info->mpd.title, song->title,
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
} else {
*current_info->mpd.title = 0;
}
if (song->track) {
strncpy(current_info->mpd.track, song->track,
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
} else {
*current_info->mpd.track = 0;
}
if (song->name) {
strncpy(current_info->mpd.name, song->name,
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
} else {
*current_info->mpd.name = 0;
}
if (song->file) {
strncpy(current_info->mpd.file, song->file,
TEXT_BUFFER_SIZE - 1);
small_text_buffer_size - 1);
} else {
*current_info->mpd.file = 0;
}

View File

@ -34,105 +34,112 @@
#include <stdlib.h>
#include <time.h>
static void user_name(char **ptr) {
const struct utmp *usr;
char buf[512];
static void user_name(char *ptr)
{
const struct utmp *usr = 0;
setutent();
while((usr=getutent())!=NULL) {
if (usr->ut_type==USER_PROCESS) {
strncat(buf, usr->ut_name, 9); strcat(buf, "\n");
while ((usr = getutent()) != NULL) {
if (usr->ut_type == USER_PROCESS) {
strncat(ptr, usr->ut_name, 9);
}
}
*ptr = buf;
}
static void user_num(int *ptr) {
static void user_num(int *ptr)
{
const struct utmp *usr;
int users_num = 0;
setutent();
while ((usr=getutent())!=NULL) {
if (usr->ut_type==USER_PROCESS) {
while ((usr = getutent()) != NULL) {
if (usr->ut_type == USER_PROCESS) {
++users_num;
}
}
*ptr = users_num;
}
static void user_term(char **ptr) {
static void user_term(char *ptr)
{
const struct utmp *usr;
char buf[512];
setutent();
while((usr=getutent())!=NULL) {
if (usr->ut_type==USER_PROCESS) {
strncat(buf, usr->ut_line, 13); strncat(buf, "\n", 3);
while ((usr = getutent()) != NULL) {
if (usr->ut_type == USER_PROCESS) {
strncat(ptr, usr->ut_line, 13);
}
}
*ptr = buf;
}
static void user_time(char **ptr) {
static void user_time(char *ptr)
{
const struct utmp *usr;
time_t login, real, diff;
struct tm *dtime;
char buf[512] = "";
char output[512] = "";
setutent();
while ((usr=getutent())!=NULL) {
if (usr->ut_type==USER_PROCESS) {
login=usr->ut_time;
while ((usr = getutent()) != NULL) {
if (usr->ut_type == USER_PROCESS) {
login = usr->ut_time;
time(&real);
diff = difftime(real, login);
dtime = localtime(&diff);
dtime->tm_year = dtime->tm_year-70;
dtime->tm_mon = dtime->tm_mon-1;
dtime->tm_mday = dtime->tm_mday-1;
if(dtime->tm_year>0){strftime(buf,512,"%yy %mm %dd %Hh %Mm\n", dtime); goto end;}
else if(dtime->tm_mon>0){strftime(buf,512,"%mm %dd %Hh %Mm\n", dtime); goto end;}
else if(dtime->tm_mday>0){strftime(buf,512,"%dd %Hh %Mm\n", dtime); goto end;}
else if(dtime->tm_hour>0){strftime(buf,512,"%Hh %Mm\n", dtime); goto end;}
else if(dtime->tm_min>0){strftime(buf,512,"%Mm\n", dtime); goto end;}
end:
strncat(output, buf, 512);
dtime->tm_year = dtime->tm_year - 70;
dtime->tm_mon = dtime->tm_mon - 1;
dtime->tm_mday = dtime->tm_mday - 1;
if (dtime->tm_year > 0) {
strftime(buf, 512, "%yy %mm %dd %Hh %Mm", dtime);
} else if (dtime->tm_mon > 0) {
strftime(buf, 512, "%mm %dd %Hh %Mm", dtime);
} else if (dtime->tm_mday > 0) {
strftime(buf, 512, "%dd %Hh %Mm", dtime);
} else if (dtime->tm_hour > 0) {
strftime(buf, 512, "%Hh %Mm", dtime);
} else if (dtime->tm_min > 0) {
strftime(buf, 512, "%Mm", dtime);
}
strncat(ptr, buf, 512);
}
}
*ptr = output;
}
static void users_alloc(struct information *ptr) {
static void users_alloc(struct information *ptr)
{
if (ptr->users.names == NULL) {
ptr->users.names = malloc(TEXT_BUFFER_SIZE);
ptr->users.names = malloc(small_text_buffer_size);
}
if (ptr->users.terms == NULL) {
ptr->users.terms = malloc(TEXT_BUFFER_SIZE);
ptr->users.terms = malloc(small_text_buffer_size);
}
if (ptr->users.times == NULL) {
ptr->users.times = malloc(TEXT_BUFFER_SIZE);
ptr->users.times = malloc(small_text_buffer_size);
}
}
void update_users() {
struct information * current_info = &info;
char *temp;
void update_users()
{
struct information *current_info = &info;
char temp[512] = "";
int t;
users_alloc(current_info);
user_name(&temp);
if (temp!=NULL) {
user_name(temp);
if (temp != NULL) {
if (current_info->users.names) {
free(current_info->users.names); current_info->users.names = 0;
free(current_info->users.names);
current_info->users.names = 0;
}
current_info->users.names = malloc(TEXT_BUFFER_SIZE);
strncpy(current_info->users.names, temp, TEXT_BUFFER_SIZE);
current_info->users.names = malloc(small_text_buffer_size);
strncpy(current_info->users.names, temp, small_text_buffer_size);
} else {
if (current_info->users.names) {
free(current_info->users.names); current_info->users.names = 0;
free(current_info->users.names);
current_info->users.names = 0;
}
current_info->users.names = malloc(TEXT_BUFFER_SIZE);
strncpy(current_info->users.names, "broken", TEXT_BUFFER_SIZE);
current_info->users.names = malloc(small_text_buffer_size);
strncpy(current_info->users.names, "broken", small_text_buffer_size);
}
user_num(&t);
if (t!=0) {
if (t != 0) {
if (current_info->users.number) {
current_info->users.number = 0;
}
@ -140,33 +147,37 @@ void update_users() {
} else {
current_info->users.number = 0;
}
temp = "\0";
user_term(&temp);
if (temp!=NULL) {
temp[0] = 0;
user_term(temp);
if (temp != NULL) {
if (current_info->users.terms) {
free(current_info->users.terms); current_info->users.terms = 0;
free(current_info->users.terms);
current_info->users.terms = 0;
}
current_info->users.terms = malloc(TEXT_BUFFER_SIZE);
strncpy(current_info->users.terms, temp, TEXT_BUFFER_SIZE);
current_info->users.terms = malloc(small_text_buffer_size);
strncpy(current_info->users.terms, temp, small_text_buffer_size);
} else {
if (current_info->users.terms) {
free(current_info->users.terms); current_info->users.terms = 0;
free(current_info->users.terms);
current_info->users.terms = 0;
}
current_info->users.terms = malloc(TEXT_BUFFER_SIZE);
strncpy(current_info->users.terms, "broken", TEXT_BUFFER_SIZE);
current_info->users.terms = malloc(small_text_buffer_size);
strncpy(current_info->users.terms, "broken", small_text_buffer_size);
}
user_time(&temp);
if (temp!=NULL) {
user_time(temp);
if (temp != NULL) {
if (current_info->users.times) {
free(current_info->users.times); current_info->users.times = 0;
free(current_info->users.times);
current_info->users.times = 0;
}
current_info->users.times = malloc(TEXT_BUFFER_SIZE);
strncpy(current_info->users.times, temp, TEXT_BUFFER_SIZE);
current_info->users.times = malloc(small_text_buffer_size);
strncpy(current_info->users.times, temp, small_text_buffer_size);
} else {
if (current_info->users.times) {
free(current_info->users.times); current_info->users.times = 0;
free(current_info->users.times);
current_info->users.times = 0;
}
current_info->users.times = malloc(TEXT_BUFFER_SIZE);
strncpy(current_info->users.times, "broken", TEXT_BUFFER_SIZE);
current_info->users.times = malloc(small_text_buffer_size);
strncpy(current_info->users.times, "broken", small_text_buffer_size);
}
}