diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -245,7 +245,7 @@ /* mpid == -1 implies this system doesn't have an _mpid */ if (mpid != -1) { - printf("last pid: %5d; ", mpid); + printf("last pid: %*d; ", maxpidlen, mpid); } printf("load averages"); @@ -270,7 +270,7 @@ if (mpid != lmpid) { Move_to(x_lastpid, y_lastpid); - printf("%5d", mpid); + printf("%*d", maxpidlen, mpid); lmpid = mpid; } diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -398,9 +398,11 @@ switch (displaymode) { case DISP_CPU: { - sbuf_printf(header, " %s", ps.thread_id ? " THR" : "PID"); + sbuf_printf(header, "%*s", + ps.thread_id ? maxpidlen + 1 : maxpidlen, + ps.thread_id ? "THR" : "PID"); sbuf_printf(header, "%*s", ps.jail ? TOP_JID_LEN : 0, - ps.jail ? " JID" : ""); + ps.jail ? "JID" : ""); sbuf_printf(header, " %-*.*s ", namelength, namelength, uname_field); if (!ps.thread) { sbuf_cat(header, "THR "); @@ -420,9 +422,10 @@ break; } case DISP_IO: { - sbuf_printf(header, " %s%*s %-*.*s", - ps.thread_id ? " THR" : "PID", - ps.jail ? TOP_JID_LEN : 0, ps.jail ? " JID" : "", + sbuf_printf(header, "%*s%*s %-*.*s", + ps.thread_id ? maxpidlen + 1 : maxpidlen, + ps.thread_id ? "THR" : "PID", + ps.jail ? TOP_JID_LEN : 0, ps.jail ? "JID" : "", namelength, namelength, uname_field); sbuf_cat(header, " VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND"); sbuf_finish(header); @@ -1080,6 +1083,13 @@ } } + sbuf_printf(procbuf, "%*d ", (ps.thread_id) ? maxpidlen + 1 : maxpidlen, + (ps.thread_id) ? pp->ki_tid : pp->ki_pid); + if (ps.jail) { + sbuf_printf(procbuf, "%*d ", TOP_JID_LEN - 1, pp->ki_jid); + } + sbuf_printf(procbuf, "%-*.*s", namelength, namelength, (*get_userid)(pp->ki_ruid)); + if (displaymode == DISP_IO) { oldp = get_old_proc(pp); if (oldp != NULL) { @@ -1097,12 +1107,6 @@ p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt; s_tot = total_inblock + total_oublock + total_majflt; - sbuf_printf(procbuf, "%5d ", (ps.thread_id) ? pp->ki_tid : pp->ki_pid); - - if (ps.jail) { - sbuf_printf(procbuf, "%*d ", TOP_JID_LEN - 1, pp->ki_jid); - } - sbuf_printf(procbuf, "%-*.*s", namelength, namelength, (*get_userid)(pp->ki_ruid)); sbuf_printf(procbuf, "%6ld ", rup->ru_nvcsw); sbuf_printf(procbuf, "%6ld ", rup->ru_nivcsw); sbuf_printf(procbuf, "%6ld ", rup->ru_inblock); @@ -1112,16 +1116,10 @@ sbuf_printf(procbuf, "%6.2f%% ", s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot)); } else { - sbuf_printf(procbuf, "%5d ", (ps.thread_id) ? pp->ki_tid : pp->ki_pid); - if (ps.jail) { - sbuf_printf(procbuf, "%*d ", TOP_JID_LEN - 1, pp->ki_jid); - } - sbuf_printf(procbuf, "%-*.*s ", namelength, namelength, (*get_userid)(pp->ki_ruid)); - if (!ps.thread) { - sbuf_printf(procbuf, "%4d ", pp->ki_numthreads); + sbuf_printf(procbuf, " %4d ", pp->ki_numthreads); } else { - sbuf_printf(procbuf, " "); + sbuf_printf(procbuf, " "); } sbuf_printf(procbuf, "%3d ", pp->ki_pri.pri_level - PZERO); diff --git a/usr.bin/top/top.h b/usr.bin/top/top.h --- a/usr.bin/top/top.h +++ b/usr.bin/top/top.h @@ -32,6 +32,7 @@ extern int pcpu_stats; extern int overstrike; extern pid_t mypid; +extern int maxpidlen; extern int (*compares[])(const void*, const void*); diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -14,9 +14,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -69,6 +71,7 @@ /* miscellaneous things */ struct process_select ps; pid_t mypid; +int maxpidlen; /* pointers to display routines */ static void (*d_loadave)(int mpid, double *avenrun) = i_loadave; @@ -218,6 +221,26 @@ return (rc); } +static int +maxpidlen_get(void) +{ + int len, pidmax; + size_t size; + + size = sizeof(pidmax); + if (sysctlbyname("kern.pid_max", &pidmax, &size, NULL, 0) < 0) { + return (strlen(__XSTRING(PID_MAX))); + } + + len = 0; + do { + pidmax /= 10; + len++; + } while (pidmax > 0); + + return (len); +} + int main(int argc, const char *argv[]) { @@ -271,6 +294,7 @@ } mypid = getpid(); + maxpidlen = maxpidlen_get(); /* get our name */ /* initialize some selection options */