From 7fc1c801aa8861140e071bc1974eb6bbe44987cb Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Tue, 13 Oct 2009 22:44:56 +0200 Subject: [PATCH] gw_info: move code to where it belongs --- src/conky.c | 6 +++--- src/conky.h | 7 ------- src/core.c | 9 +-------- src/linux.c | 48 ++++++++++++++++++++++++++++++++++++++---------- src/linux.h | 5 +++++ 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/conky.c b/src/conky.c index 6958995..711e481 100644 --- a/src/conky.c +++ b/src/conky.c @@ -1360,15 +1360,15 @@ static void generate_text_internal(char *p, int p_max_size, get_sony_fanspeed(p, p_max_size); } OBJ(if_gw) { - if (!cur->gw_info.count) { + if (!gateway_exists()) { DO_JUMP; } } OBJ(gw_iface) { - snprintf(p, p_max_size, "%s", cur->gw_info.iface); + print_gateway_iface(p, p_max_size); } OBJ(gw_ip) { - snprintf(p, p_max_size, "%s", cur->gw_info.ip); + print_gateway_ip(p, p_max_size); } OBJ(laptop_mode) { snprintf(p, p_max_size, "%d", get_laptop_mode()); diff --git a/src/conky.h b/src/conky.h index aa89b9a..f0a850f 100644 --- a/src/conky.h +++ b/src/conky.h @@ -148,12 +148,6 @@ struct usr_info { int number; }; -struct gateway_info { - char *iface; - char *ip; - int count; -}; - #ifdef X11 struct monitor_info { int number; @@ -262,7 +256,6 @@ struct information { struct bmpx_s bmpx; #endif struct usr_info users; - struct gateway_info gw_info; struct dns_data nameserver_info; struct process *cpu[10]; struct process *memu[10]; diff --git a/src/core.c b/src/core.c index 13e2536..6decbd3 100644 --- a/src/core.c +++ b/src/core.c @@ -1977,14 +1977,7 @@ void free_text_objects(struct text_object *root, int internal) free(data.ifblock.str); case OBJ_gw_iface: case OBJ_gw_ip: - if (info.gw_info.iface) { - free(info.gw_info.iface); - info.gw_info.iface = 0; - } - if (info.gw_info.ip) { - free(info.gw_info.ip); - info.gw_info.ip = 0; - } + free_gateway_info(); break; case OBJ_ioscheduler: if(data.s) diff --git a/src/linux.c b/src/linux.c index 2eaf000..0c85a12 100644 --- a/src/linux.c +++ b/src/linux.c @@ -226,6 +226,12 @@ char *get_ioscheduler(char *disk) return strndup("n/a", text_buffer_size); } +static struct { + char *iface; + char *ip; + int count; +} gw_info; + #define COND_FREE(x) if(x) free(x); x = 0 #define SAVE_SET_STRING(x, y) \ if (x && strcmp((char *)x, (char *)y)) { \ @@ -241,8 +247,8 @@ void update_gateway_info_failure(const char *reason) perror(reason); } //2 pointers to 1 location causes a crash when we try to free them both - info.gw_info.iface = strndup("failed", text_buffer_size); - info.gw_info.ip = strndup("failed", text_buffer_size); + gw_info.iface = strndup("failed", text_buffer_size); + gw_info.ip = strndup("failed", text_buffer_size); } @@ -257,11 +263,9 @@ void update_gateway_info(void) unsigned long dest, gate, mask; unsigned int flags; - struct gateway_info *gw_info = &info.gw_info; - - COND_FREE(gw_info->iface); - COND_FREE(gw_info->ip); - gw_info->count = 0; + COND_FREE(gw_info.iface); + COND_FREE(gw_info.ip); + gw_info.count = 0; if ((fp = fopen("/proc/net/route", "r")) == NULL) { update_gateway_info_failure("fopen()"); @@ -278,16 +282,40 @@ void update_gateway_info(void) break; } if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate) ) { - gw_info->count++; - SAVE_SET_STRING(gw_info->iface, iface) + gw_info.count++; + SAVE_SET_STRING(gw_info.iface, iface) ina.s_addr = gate; - SAVE_SET_STRING(gw_info->ip, inet_ntoa(ina)) + SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina)) } } fclose(fp); return; } +void free_gateway_info(void) +{ + if (gw_info.iface) + free(gw_info.iface); + if (gw_info.ip) + free(gw_info.ip); + memset(&gw_info, 0, sizeof(gw_info)); +} + +int gateway_exists(void) +{ + return !!gw_info.count; +} + +void print_gateway_iface(char *p, int p_max_size) +{ + snprintf(p, p_max_size, "%s", gw_info.iface); +} + +void print_gateway_ip(char *p, int p_max_size) +{ + snprintf(p, p_max_size, "%s", gw_info.ip); +} + void update_net_stats(void) { FILE *net_dev_fp; diff --git a/src/linux.h b/src/linux.h index 85596a0..93e2a4b 100644 --- a/src/linux.h +++ b/src/linux.h @@ -24,7 +24,12 @@ struct i8k_struct i8k; char *get_ioscheduler(char *); int get_laptop_mode(void); + void update_gateway_info(void); +void free_gateway_info(void); +int gateway_exists(void); +void print_gateway_iface(char *, int); +void print_gateway_ip(char *, int); enum { PB_BATT_STATUS, PB_BATT_PERCENT, PB_BATT_TIME }; void get_powerbook_batt_info(char *, size_t, int); -- 1.7.9.5