Skip to content

Commit

Permalink
Username table entry fits to the longest possible
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrippling committed Mar 20, 2012
1 parent e940c29 commit 0f43c49
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,8 +40,7 @@ int main(int argc, char **argv)
return 1;
}

global_uid = getuid();

extra_init();
gui_init();

proclist = proc_init();
Expand Down
23 changes: 23 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ char *ustrdup(const char *s);

int str_to_sig(const char *);

int longest_passwd_line(const char *fname);

#endif

0 comments on commit 0f43c49

Please sign in to comment.