From 68a09f7a02d9db3eb9fd8b299888179fdcf042a1 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sun, 29 Nov 2009 16:40:23 +0100 Subject: [PATCH] gid_name, uid_name: outsource sub object parsing and convert to callbacks.print --- src/conky.c | 17 +++-------------- src/core.c | 3 +++ src/user.c | 14 ++++++++++---- src/user.h | 5 +++++ 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/conky.c b/src/conky.c index 7ca446bd..81ecda60 100644 --- a/src/conky.c +++ b/src/conky.c @@ -722,6 +722,9 @@ void generate_text_internal(char *p, int p_max_size, buff_in[0] = 0; #endif /* HAVE_ICONV */ + /* \o/ */ + (void)cur; + p[0] = 0; obj = root.next; while (obj && p_max_size > 0) { @@ -798,20 +801,6 @@ void generate_text_internal(char *p, int p_max_size, OBJ(text) { snprintf(p, p_max_size, "%s", obj->data.s); } - OBJ(gid_name) { - char buf[max_user_text]; - - generate_text_internal(buf, max_user_text, *obj->sub, cur); - obj->data.s = buf; - print_gid_name(obj, p, p_max_size); - } - OBJ(uid_name) { - char buf[max_user_text]; - - generate_text_internal(buf, max_user_text, *obj->sub, cur); - obj->data.s = buf; - print_uid_name(obj, p, p_max_size); - } OBJ(updates) { snprintf(p, p_max_size, "%d", total_updates); } diff --git a/src/core.c b/src/core.c index 3b531bac..dd47706e 100644 --- a/src/core.c +++ b/src/core.c @@ -65,6 +65,7 @@ #include "tailhead.h" #include "timeinfo.h" #include "top.h" +#include "user.h" #include "users.h" #ifdef NCURSES @@ -944,8 +945,10 @@ struct text_object *construct_text_object(const char *s, const char *arg, long obj->callbacks.print = &print_pid_fsgid; END OBJ_ARG(gid_name, 0, "gid_name needs a gid as argument") extract_object_args_to_sub(obj, arg); + obj->callbacks.print = &print_gid_name; END OBJ_ARG(uid_name, 0, "uid_name needs a uid as argument") extract_object_args_to_sub(obj, arg); + obj->callbacks.print = &print_uid_name; END OBJ_ARG(pid_read, 0, "pid_read needs a pid as argument") extract_object_args_to_sub(obj, arg); obj->callbacks.print = &print_pid_read; diff --git a/src/user.c b/src/user.c index c17bdc3e..b314ed31 100644 --- a/src/user.c +++ b/src/user.c @@ -38,10 +38,13 @@ void print_uid_name(struct text_object *obj, char *p, int p_max_size) { struct passwd *pw; uid_t uid; char* firstinvalid; + char objbuf[max_user_text]; + + generate_text_internal(objbuf, max_user_text, *obj->sub, &info); errno = 0; - uid = strtol(obj->data.s, &firstinvalid, 10); - if (errno == 0 && obj->data.s != firstinvalid) { + uid = strtol(objbuf, &firstinvalid, 10); + if (errno == 0 && objbuf != firstinvalid) { pw = getpwuid(uid); if(pw != NULL) { snprintf(p, p_max_size, "%s", pw->pw_name); @@ -57,10 +60,13 @@ void print_gid_name(struct text_object *obj, char *p, int p_max_size) { struct group *grp; gid_t gid; char* firstinvalid; + char objbuf[max_user_text]; + + generate_text_internal(objbuf, max_user_text, *obj->sub, &info); errno = 0; - gid = strtol(obj->data.s, &firstinvalid, 10); - if (errno == 0 && obj->data.s != firstinvalid) { + gid = strtol(objbuf, &firstinvalid, 10); + if (errno == 0 && objbuf != firstinvalid) { grp = getgrgid(gid); if(grp != NULL) { snprintf(p, p_max_size, "%s", grp->gr_name); diff --git a/src/user.h b/src/user.h index 606f43a5..58c7083b 100644 --- a/src/user.h +++ b/src/user.h @@ -28,5 +28,10 @@ * */ +#ifndef _USER_H +#define _USER_H + void print_gid_name(struct text_object *obj, char *p, int p_max_size); void print_uid_name(struct text_object *obj, char *p, int p_max_size); + +#endif /* _USER_H */