1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-02-10 07:59:03 +00:00

gw_info: move code to where it belongs

This commit is contained in:
Phil Sutter 2009-10-13 22:44:56 +02:00
parent 27a2a253a8
commit 7fc1c801aa
5 changed files with 47 additions and 28 deletions

View File

@ -1360,15 +1360,15 @@ static void generate_text_internal(char *p, int p_max_size,
get_sony_fanspeed(p, p_max_size); get_sony_fanspeed(p, p_max_size);
} }
OBJ(if_gw) { OBJ(if_gw) {
if (!cur->gw_info.count) { if (!gateway_exists()) {
DO_JUMP; DO_JUMP;
} }
} }
OBJ(gw_iface) { OBJ(gw_iface) {
snprintf(p, p_max_size, "%s", cur->gw_info.iface); print_gateway_iface(p, p_max_size);
} }
OBJ(gw_ip) { OBJ(gw_ip) {
snprintf(p, p_max_size, "%s", cur->gw_info.ip); print_gateway_ip(p, p_max_size);
} }
OBJ(laptop_mode) { OBJ(laptop_mode) {
snprintf(p, p_max_size, "%d", get_laptop_mode()); snprintf(p, p_max_size, "%d", get_laptop_mode());

View File

@ -148,12 +148,6 @@ struct usr_info {
int number; int number;
}; };
struct gateway_info {
char *iface;
char *ip;
int count;
};
#ifdef X11 #ifdef X11
struct monitor_info { struct monitor_info {
int number; int number;
@ -262,7 +256,6 @@ struct information {
struct bmpx_s bmpx; struct bmpx_s bmpx;
#endif #endif
struct usr_info users; struct usr_info users;
struct gateway_info gw_info;
struct dns_data nameserver_info; struct dns_data nameserver_info;
struct process *cpu[10]; struct process *cpu[10];
struct process *memu[10]; struct process *memu[10];

View File

@ -1977,14 +1977,7 @@ void free_text_objects(struct text_object *root, int internal)
free(data.ifblock.str); free(data.ifblock.str);
case OBJ_gw_iface: case OBJ_gw_iface:
case OBJ_gw_ip: case OBJ_gw_ip:
if (info.gw_info.iface) { free_gateway_info();
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;
}
break; break;
case OBJ_ioscheduler: case OBJ_ioscheduler:
if(data.s) if(data.s)

View File

@ -226,6 +226,12 @@ char *get_ioscheduler(char *disk)
return strndup("n/a", text_buffer_size); 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 COND_FREE(x) if(x) free(x); x = 0
#define SAVE_SET_STRING(x, y) \ #define SAVE_SET_STRING(x, y) \
if (x && strcmp((char *)x, (char *)y)) { \ if (x && strcmp((char *)x, (char *)y)) { \
@ -241,8 +247,8 @@ void update_gateway_info_failure(const char *reason)
perror(reason); perror(reason);
} }
//2 pointers to 1 location causes a crash when we try to free them both //2 pointers to 1 location causes a crash when we try to free them both
info.gw_info.iface = strndup("failed", text_buffer_size); gw_info.iface = strndup("failed", text_buffer_size);
info.gw_info.ip = 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 long dest, gate, mask;
unsigned int flags; unsigned int flags;
struct gateway_info *gw_info = &info.gw_info; COND_FREE(gw_info.iface);
COND_FREE(gw_info.ip);
COND_FREE(gw_info->iface); gw_info.count = 0;
COND_FREE(gw_info->ip);
gw_info->count = 0;
if ((fp = fopen("/proc/net/route", "r")) == NULL) { if ((fp = fopen("/proc/net/route", "r")) == NULL) {
update_gateway_info_failure("fopen()"); update_gateway_info_failure("fopen()");
@ -278,16 +282,40 @@ void update_gateway_info(void)
break; break;
} }
if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate) ) { if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate) ) {
gw_info->count++; gw_info.count++;
SAVE_SET_STRING(gw_info->iface, iface) SAVE_SET_STRING(gw_info.iface, iface)
ina.s_addr = gate; ina.s_addr = gate;
SAVE_SET_STRING(gw_info->ip, inet_ntoa(ina)) SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina))
} }
} }
fclose(fp); fclose(fp);
return; 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) void update_net_stats(void)
{ {
FILE *net_dev_fp; FILE *net_dev_fp;

View File

@ -24,7 +24,12 @@ struct i8k_struct i8k;
char *get_ioscheduler(char *); char *get_ioscheduler(char *);
int get_laptop_mode(void); int get_laptop_mode(void);
void update_gateway_info(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 }; enum { PB_BATT_STATUS, PB_BATT_PERCENT, PB_BATT_TIME };
void get_powerbook_batt_info(char *, size_t, int); void get_powerbook_batt_info(char *, size_t, int);