From 0f43c49c06cb0af7f79fdca3b9ecb756aa71ba66 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 20 Mar 2012 22:44:54 +0000 Subject: [PATCH] Username table entry fits to the longest possible --- TODO | 2 ++ gui.c | 8 +++++--- main.c | 15 +++++++++++++-- util.c | 23 +++++++++++++++++++++++ util.h | 2 ++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 2ddbf55..25e92f2 100644 --- a/TODO +++ b/TODO @@ -7,3 +7,5 @@ show threads (/proc/%d/task/*) bug - bottom most process (at LINES-1, 0) only has its basename displayed fix shell-detach bug (SIGHUP?) + +option for clipping usernames, instead of expanding to the longest diff --git a/gui.c b/gui.c index b8ac466..8e545fa 100644 --- a/gui.c +++ b/gui.c @@ -28,7 +28,6 @@ static int pos_top = 0, pos_y = 0; - static int search = 0; static int search_idx = 0, search_offset = 0, search_pid = 0; static char search_str[32] = { 0 }; @@ -147,6 +146,8 @@ void showproc(struct proc *proc, int *py, int indent) if(y > 0){ extern int global_uid; + extern int max_unam_len, max_gnam_len; + const int owned = proc->uid == global_uid; char buf[256]; int len = LINES; @@ -168,11 +169,12 @@ void showproc(struct proc *proc, int *py, int indent) len -= snprintf(buf, sizeof buf, "% 7d %c " - "%-8s %-8s " + "%-*s %-*s " "% 4d" , proc->pid, proc->state, - proc->unam, proc->gnam, + max_unam_len, proc->unam, + max_gnam_len, proc->gnam, proc->pc_cpu ); addstr(buf); diff --git a/main.c b/main.c index ffb3f7a..d42c4ec 100644 --- a/main.c +++ b/main.c @@ -6,12 +6,24 @@ #include "proc.h" #include "gui.h" +#include "util.h" #define MS_TO_US(n) ((n) * 1000) int global_uid = 0; int global_force = 0; +int max_unam_len, max_gnam_len; + +void extra_init() +{ + global_uid = getuid(); + + /* for layout - username length */ + max_unam_len = longest_passwd_line("/etc/passwd"); + max_gnam_len = longest_passwd_line("/etc/group"); +} + int main(int argc, char **argv) { static struct proc **proclist; @@ -28,8 +40,7 @@ int main(int argc, char **argv) return 1; } - global_uid = getuid(); - + extra_init(); gui_init(); proclist = proc_init(); diff --git a/util.c b/util.c index 3fe580b..beff2d4 100644 --- a/util.c +++ b/util.c @@ -114,3 +114,26 @@ int str_to_sig(const char *s) return sigs[i].sig; return -1; } + +int longest_passwd_line(const char *fname) +{ + FILE *f = fopen(fname, "r"); + int max = 8; /* default */ + char buf[128]; + + if(!f) + return max; + + while(fgets(buf, sizeof buf, f)){ + char *colon = strchr(buf, ':'); + if(colon){ + int len = colon - buf; + if(len > max) + max = len; + } + } + + fclose(f); + + return max; +} diff --git a/util.h b/util.h index 5a9519f..4a5b785 100644 --- a/util.h +++ b/util.h @@ -9,4 +9,6 @@ char *ustrdup(const char *s); int str_to_sig(const char *); +int longest_passwd_line(const char *fname); + #endif