diff --git a/bin/ps/extern.h b/bin/ps/extern.h index 7b5522dc1508..930b85b0bcaa 100644 --- a/bin/ps/extern.h +++ b/bin/ps/extern.h @@ -39,63 +39,52 @@ extern fixpt_t ccpu; extern int cflag, eval, fscale, nlistread, rawcpu; extern unsigned long mempages; extern time_t now; -extern int showthreads, sumrusage, termwidth, totwidth; +extern int showthreads, sumrusage, termwidth; extern STAILQ_HEAD(velisthead, varent) varlist; __BEGIN_DECLS -void arguments(KINFO *, VARENT *); -void command(KINFO *, VARENT *); -void cputime(KINFO *, VARENT *); +char *arguments(KINFO *, VARENT *); +char *command(KINFO *, VARENT *); +char *cputime(KINFO *, VARENT *); int donlist(void); -void elapsed(KINFO *, VARENT *); -void elapseds(KINFO *, VARENT *); -void emulname(KINFO *, VARENT *); +char *elapsed(KINFO *, VARENT *); +char *elapseds(KINFO *, VARENT *); +char *emulname(KINFO *, VARENT *); VARENT *find_varentry(VAR *); const char *fmt_argv(char **, char *, size_t); double getpcpu(const KINFO *); -void kvar(KINFO *, VARENT *); -void label(KINFO *, VARENT *); -void loginclass(KINFO *, VARENT *); -void logname(KINFO *, VARENT *); -void longtname(KINFO *, VARENT *); -void lstarted(KINFO *, VARENT *); -void maxrss(KINFO *, VARENT *); -void lockname(KINFO *, VARENT *); -void mwchan(KINFO *, VARENT *); -void nwchan(KINFO *, VARENT *); -void pagein(KINFO *, VARENT *); +char *kvar(KINFO *, VARENT *); +char *label(KINFO *, VARENT *); +char *loginclass(KINFO *, VARENT *); +char *logname(KINFO *, VARENT *); +char *longtname(KINFO *, VARENT *); +char *lstarted(KINFO *, VARENT *); +char *maxrss(KINFO *, VARENT *); +char *lockname(KINFO *, VARENT *); +char *mwchan(KINFO *, VARENT *); +char *nwchan(KINFO *, VARENT *); +char *pagein(KINFO *, VARENT *); void parsefmt(const char *, int); -void pcpu(KINFO *, VARENT *); -void pmem(KINFO *, VARENT *); -void pri(KINFO *, VARENT *); +char *pcpu(KINFO *, VARENT *); +char *pmem(KINFO *, VARENT *); +char *pri(KINFO *, VARENT *); void printheader(void); -void priorityr(KINFO *, VARENT *); -void egroupname(KINFO *, VARENT *); -void rgroupname(KINFO *, VARENT *); -void runame(KINFO *, VARENT *); -void rvar(KINFO *, VARENT *); -int s_comm(KINFO *); -int s_cputime(KINFO *); -int s_label(KINFO *); -int s_loginclass(KINFO *); -int s_logname(KINFO *); -int s_egroupname(KINFO *); -int s_rgroupname(KINFO *); -int s_runame(KINFO *); -int s_systime(KINFO *); -int s_uname(KINFO *); -int s_usertime(KINFO *); +char *priorityr(KINFO *, VARENT *); +char *egroupname(KINFO *, VARENT *); +char *rgroupname(KINFO *, VARENT *); +char *runame(KINFO *, VARENT *); +char *rvar(KINFO *, VARENT *); void showkey(void); -void started(KINFO *, VARENT *); -void state(KINFO *, VARENT *); -void systime(KINFO *, VARENT *); -void tdev(KINFO *, VARENT *); -void tdnam(KINFO *, VARENT *); -void tname(KINFO *, VARENT *); -void ucomm(KINFO *, VARENT *); -void uname(KINFO *, VARENT *); -void upr(KINFO *, VARENT *); -void usertime(KINFO *, VARENT *); -void vsize(KINFO *, VARENT *); -void wchan(KINFO *, VARENT *); +char *started(KINFO *, VARENT *); +char *state(KINFO *, VARENT *); +char *systime(KINFO *, VARENT *); +char *tdev(KINFO *, VARENT *); +char *tdnam(KINFO *, VARENT *); +char *tname(KINFO *, VARENT *); +char *ucomm(KINFO *, VARENT *); +char *uname(KINFO *, VARENT *); +char *upr(KINFO *, VARENT *); +char *usertime(KINFO *, VARENT *); +char *vsize(KINFO *, VARENT *); +char *wchan(KINFO *, VARENT *); __END_DECLS diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c index 7dbaa4fe74cf..17ca0d87c512 100644 --- a/bin/ps/keyword.c +++ b/bin/ps/keyword.c @@ -57,178 +57,116 @@ static int vcmp(const void *, const void *); #define KOFF(x) offsetof(struct kinfo_proc, x) #define ROFF(x) offsetof(struct rusage, x) -#define EMULLEN 13 /* enough for "FreeBSD ELF32" */ #define LWPFMT "d" -#define LWPLEN 6 #define NLWPFMT "d" -#define NLWPLEN 4 #define UIDFMT "u" -#define UIDLEN 5 #define PIDFMT "d" -#define PIDLEN 5 -#define USERLEN (MAXLOGNAME - 1) /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */ static VAR var[] = { - {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 5, 0, CHAR, NULL, 0}, - {"%mem", "%MEM", NULL, 0, pmem, NULL, 4, 0, CHAR, NULL, 0}, - {"acflag", "ACFLG", NULL, 0, kvar, NULL, 3, KOFF(ki_acflag), USHORT, - "x", 0}, - {"acflg", "", "acflag", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"args", "COMMAND", NULL, COMM|LJUST|USER, arguments, NULL, 16, 0, + {"%cpu", "%CPU", NULL, 0, pcpu, 0, CHAR, NULL, 0}, + {"%mem", "%MEM", NULL, 0, pmem, 0, CHAR, NULL, 0}, + {"acflag", "ACFLG", NULL, 0, kvar, KOFF(ki_acflag), USHORT, "x", 0}, + {"acflg", "", "acflag", 0, NULL, 0, CHAR, NULL, 0}, + {"args", "COMMAND", NULL, COMM|LJUST|USER, arguments, 0, CHAR, NULL, 0}, - {"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"class", "CLASS", NULL, LJUST, loginclass, s_loginclass, - MAXLOGNAME-1, 0, CHAR, NULL, 0}, - {"comm", "COMMAND", NULL, LJUST, ucomm, s_comm, - COMMLEN + TDNAMLEN + 1, 0, CHAR, NULL, 0}, - {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, + {"blocked", "", "sigmask", 0, NULL, 0, CHAR, NULL, 0}, + {"caught", "", "sigcatch", 0, NULL, 0, CHAR, NULL, 0}, + {"class", "CLASS", NULL, LJUST, loginclass, 0, CHAR, NULL, 0}, + {"comm", "COMMAND", NULL, LJUST, ucomm, 0, CHAR, NULL, 0}, + {"command", "COMMAND", NULL, COMM|LJUST|USER, command, 0, CHAR, NULL, 0}, - {"cpu", "CPU", NULL, 0, kvar, NULL, 3, KOFF(ki_estcpu), UINT, "d", - 0}, - {"cputime", "", "time", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"egid", "", "gid", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"egroup", "", "group", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"emul", "EMUL", NULL, LJUST, emulname, NULL, EMULLEN, 0, CHAR, - NULL, 0}, - {"etime", "ELAPSED", NULL, USER, elapsed, NULL, 12, 0, CHAR, NULL, 0}, - {"etimes", "ELAPSED", NULL, USER, elapseds, NULL, 12, 0, CHAR, NULL, 0}, - {"euid", "", "uid", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"f", "F", NULL, 0, kvar, NULL, 8, KOFF(ki_flag), INT, "x", 0}, - {"flags", "", "f", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"gid", "GID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_groups), - UINT, UIDFMT, 0}, - {"group", "GROUP", NULL, LJUST, egroupname, s_egroupname, - USERLEN, 0, CHAR, NULL, 0}, - {"ignored", "", "sigignore", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"inblk", "INBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, - "ld", 0}, - {"inblock", "", "inblk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"jid", "JID", NULL, 0, kvar, NULL, 6, KOFF(ki_jid), INT, "d", 0}, - {"jobc", "JOBC", NULL, 0, kvar, NULL, 4, KOFF(ki_jobc), SHORT, "d", - 0}, - {"ktrace", "KTRACE", NULL, 0, kvar, NULL, 8, KOFF(ki_traceflag), INT, - "x", 0}, - {"label", "LABEL", NULL, LJUST, label, s_label, SHRT_MAX, 0, CHAR, - NULL, 0}, - {"lim", "LIM", NULL, 0, maxrss, NULL, 5, 0, CHAR, NULL, 0}, - {"lockname", "LOCK", NULL, LJUST, lockname, NULL, 6, 0, CHAR, NULL, - 0}, - {"login", "LOGIN", NULL, LJUST, logname, s_logname, MAXLOGNAME-1, - 0, CHAR, NULL, 0}, - {"logname", "", "login", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28, 0, CHAR, - NULL, 0}, - {"lwp", "LWP", NULL, 0, kvar, NULL, LWPLEN, KOFF(ki_tid), UINT, - LWPFMT, 0}, - {"majflt", "MAJFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), - LONG, "ld", 0}, - {"minflt", "MINFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), - LONG, "ld", 0}, - {"msgrcv", "MSGRCV", NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), - LONG, "ld", 0}, - {"msgsnd", "MSGSND", NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), - LONG, "ld", 0}, - {"mwchan", "MWCHAN", NULL, LJUST, mwchan, NULL, 6, 0, CHAR, NULL, 0}, - {"ni", "", "nice", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"nice", "NI", NULL, 0, kvar, NULL, 2, KOFF(ki_nice), CHAR, "d", - 0}, - {"nivcsw", "NIVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), - LONG, "ld", 0}, - {"nlwp", "NLWP", NULL, 0, kvar, NULL, NLWPLEN, KOFF(ki_numthreads), - UINT, NLWPFMT, 0}, - {"nsignals", "", "nsigs", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"nsigs", "NSIGS", NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), - LONG, "ld", 0}, - {"nswap", "NSWAP", NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), - LONG, "ld", 0}, - {"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), - LONG, "ld", 0}, - {"nwchan", "NWCHAN", NULL, LJUST, nwchan, NULL, sizeof(void *) * 2, 0, - CHAR, NULL, 0}, - {"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), - LONG, "ld", 0}, - {"oublock", "", "oublk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"paddr", "PADDR", NULL, 0, kvar, NULL, sizeof(void *) * 2, - KOFF(ki_paddr), KPTR, "lx", 0}, - {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, CHAR, NULL, 0}, - {"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"pending", "", "sig", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"pgid", "PGID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pgid), UINT, - PIDFMT, 0}, - {"pid", "PID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pid), UINT, - PIDFMT, 0}, - {"pmem", "", "%mem", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"ppid", "PPID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_ppid), UINT, - PIDFMT, 0}, - {"pri", "PRI", NULL, 0, pri, NULL, 3, 0, CHAR, NULL, 0}, - {"re", "RE", NULL, INF127, kvar, NULL, 3, KOFF(ki_swtime), UINT, "d", - 0}, - {"rgid", "RGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_rgid), - UINT, UIDFMT, 0}, - {"rgroup", "RGROUP", NULL, LJUST, rgroupname, s_rgroupname, - USERLEN, 0, CHAR, NULL, 0}, - {"rss", "RSS", NULL, 0, kvar, NULL, 6, KOFF(ki_rssize), PGTOK, "ld", 0}, - {"rtprio", "RTPRIO", NULL, 0, priorityr, NULL, 7, KOFF(ki_pri), CHAR, - NULL, 0}, - {"ruid", "RUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_ruid), - UINT, UIDFMT, 0}, - {"ruser", "RUSER", NULL, LJUST, runame, s_runame, USERLEN, - 0, CHAR, NULL, 0}, - {"sid", "SID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_sid), UINT, - PIDFMT, 0}, - {"sig", "PENDING", NULL, 0, kvar, NULL, 8, KOFF(ki_siglist), INT, - "x", 0}, - {"sigcatch", "CAUGHT", NULL, 0, kvar, NULL, 8, KOFF(ki_sigcatch), + {"cpu", "CPU", NULL, 0, kvar, KOFF(ki_estcpu), UINT, "d", 0}, + {"cputime", "", "time", 0, NULL, 0, CHAR, NULL, 0}, + {"egid", "", "gid", 0, NULL, 0, CHAR, NULL, 0}, + {"egroup", "", "group", 0, NULL, 0, CHAR, NULL, 0}, + {"emul", "EMUL", NULL, LJUST, emulname, 0, CHAR, NULL, 0}, + {"etime", "ELAPSED", NULL, USER, elapsed, 0, CHAR, NULL, 0}, + {"etimes", "ELAPSED", NULL, USER, elapseds, 0, CHAR, NULL, 0}, + {"euid", "", "uid", 0, NULL, 0, CHAR, NULL, 0}, + {"f", "F", NULL, 0, kvar, KOFF(ki_flag), INT, "x", 0}, + {"flags", "", "f", 0, NULL, 0, CHAR, NULL, 0}, + {"gid", "GID", NULL, 0, kvar, KOFF(ki_groups), UINT, UIDFMT, 0}, + {"group", "GROUP", NULL, LJUST, egroupname, 0, CHAR, NULL, 0}, + {"ignored", "", "sigignore", 0, NULL, 0, CHAR, NULL, 0}, + {"inblk", "INBLK", NULL, USER, rvar, ROFF(ru_inblock), LONG, "ld", 0}, + {"inblock", "", "inblk", 0, NULL, 0, CHAR, NULL, 0}, + {"jid", "JID", NULL, 0, kvar, KOFF(ki_jid), INT, "d", 0}, + {"jobc", "JOBC", NULL, 0, kvar, KOFF(ki_jobc), SHORT, "d", 0}, + {"ktrace", "KTRACE", NULL, 0, kvar, KOFF(ki_traceflag), INT, "x", 0}, + {"label", "LABEL", NULL, LJUST, label, 0, CHAR, NULL, 0}, + {"lim", "LIM", NULL, 0, maxrss, 0, CHAR, NULL, 0}, + {"lockname", "LOCK", NULL, LJUST, lockname, 0, CHAR, NULL, 0}, + {"login", "LOGIN", NULL, LJUST, logname, 0, CHAR, NULL, 0}, + {"logname", "", "login", 0, NULL, 0, CHAR, NULL, 0}, + {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 0, CHAR, NULL, 0}, + {"lwp", "LWP", NULL, 0, kvar, KOFF(ki_tid), UINT, LWPFMT, 0}, + {"majflt", "MAJFLT", NULL, USER, rvar, ROFF(ru_majflt), LONG, "ld", 0}, + {"minflt", "MINFLT", NULL, USER, rvar, ROFF(ru_minflt), LONG, "ld", 0}, + {"msgrcv", "MSGRCV", NULL, USER, rvar, ROFF(ru_msgrcv), LONG, "ld", 0}, + {"msgsnd", "MSGSND", NULL, USER, rvar, ROFF(ru_msgsnd), LONG, "ld", 0}, + {"mwchan", "MWCHAN", NULL, LJUST, mwchan, 0, CHAR, NULL, 0}, + {"ni", "", "nice", 0, NULL, 0, CHAR, NULL, 0}, + {"nice", "NI", NULL, 0, kvar, KOFF(ki_nice), CHAR, "d", 0}, + {"nivcsw", "NIVCSW", NULL, USER, rvar, ROFF(ru_nivcsw), LONG, "ld", 0}, + {"nlwp", "NLWP", NULL, 0, kvar, KOFF(ki_numthreads), UINT, NLWPFMT, 0}, + {"nsignals", "", "nsigs", 0, NULL, 0, CHAR, NULL, 0}, + {"nsigs", "NSIGS", NULL, USER, rvar, ROFF(ru_nsignals), LONG, "ld", 0}, + {"nswap", "NSWAP", NULL, USER, rvar, ROFF(ru_nswap), LONG, "ld", 0}, + {"nvcsw", "NVCSW", NULL, USER, rvar, ROFF(ru_nvcsw), LONG, "ld", 0}, + {"nwchan", "NWCHAN", NULL, LJUST, nwchan, 0, CHAR, NULL, 0}, + {"oublk", "OUBLK", NULL, USER, rvar, ROFF(ru_oublock), LONG, "ld", 0}, + {"oublock", "", "oublk", 0, NULL, 0, CHAR, NULL, 0}, + {"paddr", "PADDR", NULL, 0, kvar, KOFF(ki_paddr), KPTR, "lx", 0}, + {"pagein", "PAGEIN", NULL, USER, pagein, 0, CHAR, NULL, 0}, + {"pcpu", "", "%cpu", 0, NULL, 0, CHAR, NULL, 0}, + {"pending", "", "sig", 0, NULL, 0, CHAR, NULL, 0}, + {"pgid", "PGID", NULL, 0, kvar, KOFF(ki_pgid), UINT, PIDFMT, 0}, + {"pid", "PID", NULL, 0, kvar, KOFF(ki_pid), UINT, PIDFMT, 0}, + {"pmem", "", "%mem", 0, NULL, 0, CHAR, NULL, 0}, + {"ppid", "PPID", NULL, 0, kvar, KOFF(ki_ppid), UINT, PIDFMT, 0}, + {"pri", "PRI", NULL, 0, pri, 0, CHAR, NULL, 0}, + {"re", "RE", NULL, INF127, kvar, KOFF(ki_swtime), UINT, "d", 0}, + {"rgid", "RGID", NULL, 0, kvar, KOFF(ki_rgid), UINT, UIDFMT, 0}, + {"rgroup", "RGROUP", NULL, LJUST, rgroupname, 0, CHAR, NULL, 0}, + {"rss", "RSS", NULL, 0, kvar, KOFF(ki_rssize), PGTOK, "ld", 0}, + {"rtprio", "RTPRIO", NULL, 0, priorityr, KOFF(ki_pri), CHAR, NULL, 0}, + {"ruid", "RUID", NULL, 0, kvar, KOFF(ki_ruid), UINT, UIDFMT, 0}, + {"ruser", "RUSER", NULL, LJUST, runame, 0, CHAR, NULL, 0}, + {"sid", "SID", NULL, 0, kvar, KOFF(ki_sid), UINT, PIDFMT, 0}, + {"sig", "PENDING", NULL, 0, kvar, KOFF(ki_siglist), INT, "x", 0}, + {"sigcatch", "CAUGHT", NULL, 0, kvar, KOFF(ki_sigcatch), UINT, "x", 0}, + {"sigignore", "IGNORED", NULL, 0, kvar, KOFF(ki_sigignore), UINT, "x", 0}, - {"sigignore", "IGNORED", NULL, 0, kvar, NULL, 8, KOFF(ki_sigignore), - UINT, "x", 0}, - {"sigmask", "BLOCKED", NULL, 0, kvar, NULL, 8, KOFF(ki_sigmask), - UINT, "x", 0}, - {"sl", "SL", NULL, INF127, kvar, NULL, 3, KOFF(ki_slptime), UINT, "d", - 0}, - {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7, 0, CHAR, NULL, - 0}, - {"stat", "", "state", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"state", "STAT", NULL, 0, state, NULL, 4, 0, CHAR, NULL, 0}, - {"svgid", "SVGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svgid), - UINT, UIDFMT, 0}, - {"svuid", "SVUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svuid), - UINT, UIDFMT, 0}, - {"systime", "SYSTIME", NULL, USER, systime, s_systime, 15, 0, CHAR, - NULL, 0}, - {"tdaddr", "TDADDR", NULL, 0, kvar, NULL, sizeof(void *) * 2, - KOFF(ki_tdaddr), KPTR, "lx", 0}, - {"tdev", "TDEV", NULL, 0, tdev, NULL, 5, 0, CHAR, NULL, 0}, - {"tdnam", "TDNAM", NULL, LJUST, tdnam, NULL, COMMLEN, 0, CHAR, NULL, 0}, - {"time", "TIME", NULL, USER, cputime, s_cputime, 15, 0, CHAR, - NULL, 0}, - {"tpgid", "TPGID", NULL, 0, kvar, NULL, 4, KOFF(ki_tpgid), UINT, - PIDFMT, 0}, - {"tsid", "TSID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_tsid), UINT, - PIDFMT, 0}, - {"tsiz", "TSIZ", NULL, 0, kvar, NULL, 4, KOFF(ki_tsize), PGTOK, "ld", 0}, - {"tt", "TT ", NULL, 0, tname, NULL, 4, 0, CHAR, NULL, 0}, - {"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, CHAR, NULL, 0}, - {"ucomm", "UCOMM", NULL, LJUST, ucomm, s_comm, - COMMLEN + TDNAMLEN + 1, 0, CHAR, NULL, 0}, - {"uid", "UID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_uid), UINT, - UIDFMT, 0}, - {"upr", "UPR", NULL, 0, upr, NULL, 3, 0, CHAR, NULL, 0}, - {"uprocp", "UPROCP", NULL, 0, kvar, NULL, sizeof(void *) * 2, - KOFF(ki_paddr), KPTR, "lx", 0}, - {"user", "USER", NULL, LJUST, uname, s_uname, USERLEN, 0, CHAR, - NULL, 0}, - {"usertime", "USERTIME", NULL, USER, usertime, s_usertime, 15, 0, - CHAR, NULL, 0}, - {"usrpri", "", "upr", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"vsize", "", "vsz", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, - {"vsz", "VSZ", NULL, 0, vsize, NULL, 6, 0, CHAR, NULL, 0}, - {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6, 0, CHAR, NULL, 0}, - {"xstat", "XSTAT", NULL, 0, kvar, NULL, 4, KOFF(ki_xstat), USHORT, - "x", 0}, - {"", NULL, NULL, 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, + {"sigmask", "BLOCKED", NULL, 0, kvar, KOFF(ki_sigmask), UINT, "x", 0}, + {"sl", "SL", NULL, INF127, kvar, KOFF(ki_slptime), UINT, "d", 0}, + {"start", "STARTED", NULL, LJUST|USER, started, 0, CHAR, NULL, 0}, + {"stat", "", "state", 0, NULL, 0, CHAR, NULL, 0}, + {"state", "STAT", NULL, LJUST, state, 0, CHAR, NULL, 0}, + {"svgid", "SVGID", NULL, 0, kvar, KOFF(ki_svgid), UINT, UIDFMT, 0}, + {"svuid", "SVUID", NULL, 0, kvar, KOFF(ki_svuid), UINT, UIDFMT, 0}, + {"systime", "SYSTIME", NULL, USER, systime, 0, CHAR, NULL, 0}, + {"tdaddr", "TDADDR", NULL, 0, kvar, KOFF(ki_tdaddr), KPTR, "lx", 0}, + {"tdev", "TDEV", NULL, 0, tdev, 0, CHAR, NULL, 0}, + {"tdnam", "TDNAM", NULL, LJUST, tdnam, 0, CHAR, NULL, 0}, + {"time", "TIME", NULL, USER, cputime, 0, CHAR, NULL, 0}, + {"tpgid", "TPGID", NULL, 0, kvar, KOFF(ki_tpgid), UINT, PIDFMT, 0}, + {"tsid", "TSID", NULL, 0, kvar, KOFF(ki_tsid), UINT, PIDFMT, 0}, + {"tsiz", "TSIZ", NULL, 0, kvar, KOFF(ki_tsize), PGTOK, "ld", 0}, + {"tt", "TT ", NULL, 0, tname, 0, CHAR, NULL, 0}, + {"tty", "TTY", NULL, LJUST, longtname, 0, CHAR, NULL, 0}, + {"ucomm", "UCOMM", NULL, LJUST, ucomm, 0, CHAR, NULL, 0}, + {"uid", "UID", NULL, 0, kvar, KOFF(ki_uid), UINT, UIDFMT, 0}, + {"upr", "UPR", NULL, 0, upr, 0, CHAR, NULL, 0}, + {"uprocp", "UPROCP", NULL, 0, kvar, KOFF(ki_paddr), KPTR, "lx", 0}, + {"user", "USER", NULL, LJUST, uname, 0, CHAR, NULL, 0}, + {"usertime", "USERTIME", NULL, USER, usertime, 0, CHAR, NULL, 0}, + {"usrpri", "", "upr", 0, NULL, 0, CHAR, NULL, 0}, + {"vsize", "", "vsz", 0, NULL, 0, CHAR, NULL, 0}, + {"vsz", "VSZ", NULL, 0, vsize, 0, CHAR, NULL, 0}, + {"wchan", "WCHAN", NULL, LJUST, wchan, 0, CHAR, NULL, 0}, + {"xstat", "XSTAT", NULL, 0, kvar, KOFF(ki_xstat), USHORT, "x", 0}, + {"", NULL, NULL, 0, NULL, 0, CHAR, NULL, 0}, }; void diff --git a/bin/ps/print.c b/bin/ps/print.c index beb6f5205735..45c7823056ac 100644 --- a/bin/ps/print.c +++ b/bin/ps/print.c @@ -64,6 +64,9 @@ __FBSDID("$FreeBSD$"); #include "ps.h" +#define COMMAND_WIDTH 16 +#define ARGUMENTS_WIDTH 16 + #define ps_pgtok(a) (((a) * getpagesize()) / 1024) void @@ -93,53 +96,42 @@ printheader(void) (void)putchar('\n'); } -void +char * arguments(KINFO *k, VARENT *ve) { VAR *v; - int left; - char *cp, *vis_args; + char *vis_args; v = ve->var; if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL) errx(1, "malloc failed"); strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH); - if (STAILQ_NEXT(ve, next_ve) == NULL) { - /* last field */ - if (termwidth == UNLIMITED) { - (void)printf("%s", vis_args); - } else { - left = termwidth - (totwidth - v->width); - if (left < 1) /* already wrapped, just use std width */ - left = v->width; - for (cp = vis_args; --left >= 0 && *cp != '\0';) - (void)putchar(*cp++); - } - } else { - (void)printf("%-*.*s", v->width, v->width, vis_args); - } - free(vis_args); + + if (STAILQ_NEXT(ve, next_ve) != NULL && strlen(vis_args) > ARGUMENTS_WIDTH) + vis_args[ARGUMENTS_WIDTH] = '\0'; + + return (vis_args); } -void +char * command(KINFO *k, VARENT *ve) { VAR *v; - int left; - char *cp, *vis_env, *vis_args; + char *vis_args, *vis_env, *str; v = ve->var; if (cflag) { /* If it is the last field, then don't pad */ if (STAILQ_NEXT(ve, next_ve) == NULL) { - if (k->ki_d.prefix) - (void)printf("%s", k->ki_d.prefix); - (void)printf("%s", k->ki_p->ki_comm); - if (showthreads && k->ki_p->ki_numthreads > 1) - (void)printf("/%s", k->ki_p->ki_tdname); + asprintf(&str, "%s%s%s%s", + k->ki_d.prefix ? k->ki_d.prefix : "", + k->ki_p->ki_comm, + (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "", + (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : ""); } else - (void)printf("%-*s", v->width, k->ki_p->ki_comm); - return; + str = strdup(k->ki_p->ki_comm); + + return (str); } if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL) errx(1, "malloc failed"); @@ -157,89 +149,84 @@ command(KINFO *k, VARENT *ve) } else vis_env = NULL; - if (termwidth == UNLIMITED) { - if (k->ki_d.prefix) - (void)printf("%s", k->ki_d.prefix); - if (vis_env) - (void)printf("%s ", vis_env); - (void)printf("%s", vis_args); - } else { - left = termwidth - (totwidth - v->width); - if (left < 1) /* already wrapped, just use std width */ - left = v->width; - if ((cp = k->ki_d.prefix) != NULL) - while (--left >= 0 && *cp) - (void)putchar(*cp++); - if ((cp = vis_env) != NULL) { - while (--left >= 0 && *cp) - (void)putchar(*cp++); - if (--left >= 0) - putchar(' '); - } - for (cp = vis_args; --left >= 0 && *cp != '\0';) - (void)putchar(*cp++); - } + asprintf(&str, "%s%s%s%s", + k->ki_d.prefix ? k->ki_d.prefix : "", + vis_env ? vis_env : "", + vis_env ? " " : "", + vis_args); + if (vis_env != NULL) free(vis_env); - } else + free(vis_args); + } else { /* ki_d.prefix & ki_env aren't shown for interim fields */ - (void)printf("%-*.*s", v->width, v->width, vis_args); - free(vis_args); + str = vis_args; + + if (strlen(str) > COMMAND_WIDTH) + str[COMMAND_WIDTH] = '\0'; + } + + return (str); } -void +char * ucomm(KINFO *k, VARENT *ve) { - char tmpbuff[COMMLEN + TDNAMLEN + 2]; VAR *v; + char *str; v = ve->var; if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */ - if (k->ki_d.prefix) - (void)printf("%s", k->ki_d.prefix); - (void)printf("%s", k->ki_p->ki_comm); - if (showthreads && k->ki_p->ki_numthreads > 1) - printf("/%s", k->ki_p->ki_tdname); + asprintf(&str, "%s%s%s%s", + k->ki_d.prefix ? k->ki_d.prefix : "", + k->ki_p->ki_comm, + (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "", + (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : ""); } else { - bzero(tmpbuff, sizeof(tmpbuff)); if (showthreads && k->ki_p->ki_numthreads > 1) - sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm, - k->ki_p->ki_tdname); + asprintf(&str, "%s/%s", k->ki_p->ki_comm, k->ki_p->ki_tdname); else - sprintf(tmpbuff, "%s", k->ki_p->ki_comm); - (void)printf("%-*s", v->width, tmpbuff); + str = strdup(k->ki_p->ki_comm); } + return (str); } -void +char * tdnam(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; if (showthreads && k->ki_p->ki_numthreads > 1) - (void)printf("%-*s", v->width, k->ki_p->ki_tdname); + str = strdup(k->ki_p->ki_tdname); else - (void)printf("%-*s", v->width, " "); + str = strdup(" "); + + return (str); } -void +char * logname(KINFO *k, VARENT *ve) { VAR *v; - char *s; v = ve->var; - (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-"); + if (*k->ki_p->ki_login == '\0') + return (NULL); + return (strdup(k->ki_p->ki_login)); } -void +char * state(KINFO *k, VARENT *ve) { int flag, tdflags; - char *cp; + char *cp, *buf; VAR *v; - char buf[16]; + + buf = malloc(16); + if (buf == NULL) + errx(1, "malloc failed"); v = ve->var; flag = k->ki_p->ki_flag; @@ -301,274 +288,270 @@ state(KINFO *k, VARENT *ve) if (flag & P_JAILED) *cp++ = 'J'; *cp = '\0'; - (void)printf("%-*s", v->width, buf); + return (buf); } #define scalepri(x) ((x) - PZERO) -void +char * pri(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level)); + asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_level)); + return (str); } -void +char * upr(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user)); + asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_user)); + return (str); } #undef scalepri -void +char * uname(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0)); + return (strdup(user_from_uid(k->ki_p->ki_uid, 0))); } -int -s_uname(KINFO *k) -{ - return (strlen(user_from_uid(k->ki_p->ki_uid, 0))); -} - -void +char * egroupname(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - (void)printf("%-*s", v->width, - group_from_gid(k->ki_p->ki_groups[0], 0)); + return (strdup(group_from_gid(k->ki_p->ki_groups[0], 0))); } -int -s_egroupname(KINFO *k) -{ - return (strlen(group_from_gid(k->ki_p->ki_groups[0], 0))); -} - -void +char * rgroupname(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0)); + return (strdup(group_from_gid(k->ki_p->ki_rgid, 0))); } -int -s_rgroupname(KINFO *k) -{ - return (strlen(group_from_gid(k->ki_p->ki_rgid, 0))); -} - -void +char * runame(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0)); + return (strdup(user_from_uid(k->ki_p->ki_ruid, 0))); } -int -s_runame(KINFO *k) -{ - return (strlen(user_from_uid(k->ki_p->ki_ruid, 0))); -} - - -void +char * tdev(KINFO *k, VARENT *ve) { VAR *v; dev_t dev; + char *str; v = ve->var; dev = k->ki_p->ki_tdev; if (dev == NODEV) - (void)printf("%*s", v->width, "??"); + str = strdup("??"); else - (void)printf("%#*jx", v->width, (uintmax_t)dev); + asprintf(&str, "%#jx", (uintmax_t)dev); + + return (str); } -void +char * tname(KINFO *k, VARENT *ve) { VAR *v; dev_t dev; - char *ttname; + char *ttname, *str; v = ve->var; dev = k->ki_p->ki_tdev; if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) - (void)printf("%*s ", v->width - 1, "??"); + str = strdup("?? "); else { if (strncmp(ttname, "tty", 3) == 0 || strncmp(ttname, "cua", 3) == 0) ttname += 3; if (strncmp(ttname, "pts/", 4) == 0) ttname += 4; - (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname, + asprintf(&str, "%s%c", ttname, k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-'); } + + return (str); } -void +char * longtname(KINFO *k, VARENT *ve) { VAR *v; dev_t dev; - char *ttname; + const char *ttname; v = ve->var; dev = k->ki_p->ki_tdev; if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) - (void)printf("%-*s", v->width, "??"); - else - (void)printf("%-*s", v->width, ttname); + ttname = "??"; + + return (strdup(ttname)); } -void +char * started(KINFO *k, VARENT *ve) { VAR *v; time_t then; struct tm *tp; static int use_ampm = -1; - char buf[100]; + size_t buflen = 100; + char *buf; + + buf = malloc(buflen); + if (buf == NULL) + errx(1, "malloc failed"); v = ve->var; - if (!k->ki_valid) { - (void)printf("%-*s", v->width, "-"); - return; - } + if (!k->ki_valid) + return (NULL); if (use_ampm < 0) use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); then = k->ki_p->ki_start.tv_sec; tp = localtime(&then); if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) { - (void)strftime(buf, sizeof(buf), + (void)strftime(buf, buflen, use_ampm ? "%l:%M%p" : "%k:%M ", tp); } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) { - (void)strftime(buf, sizeof(buf), + (void)strftime(buf, buflen, use_ampm ? "%a%I%p" : "%a%H ", tp); } else - (void)strftime(buf, sizeof(buf), "%e%b%y", tp); - (void)printf("%-*s", v->width, buf); + (void)strftime(buf, buflen, "%e%b%y", tp); + return (buf); } -void +char * lstarted(KINFO *k, VARENT *ve) { VAR *v; time_t then; - char buf[100]; + char *buf; + size_t buflen = 100; + + buf = malloc(buflen); + if (buf == NULL) + errx(1, "malloc failed"); v = ve->var; - if (!k->ki_valid) { - (void)printf("%-*s", v->width, "-"); - return; - } + if (!k->ki_valid) + return (NULL); then = k->ki_p->ki_start.tv_sec; - (void)strftime(buf, sizeof(buf), "%c", localtime(&then)); - (void)printf("%-*s", v->width, buf); + (void)strftime(buf, buflen, "%c", localtime(&then)); + return (buf); } -void +char * lockname(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) { if (k->ki_p->ki_lockname[0] != 0) - (void)printf("%-*.*s", v->width, v->width, - k->ki_p->ki_lockname); + str = strdup(k->ki_p->ki_lockname); else - (void)printf("%-*s", v->width, "???"); + str = strdup("???"); } else - (void)printf("%-*s", v->width, "-"); + str = NULL; + + return (str); } -void +char * wchan(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; if (k->ki_p->ki_wchan) { if (k->ki_p->ki_wmesg[0] != 0) - (void)printf("%-*.*s", v->width, v->width, - k->ki_p->ki_wmesg); + str = strdup(k->ki_p->ki_wmesg); else - (void)printf("%-*lx", v->width, - (long)k->ki_p->ki_wchan); + asprintf(&str, "%lx", (long)k->ki_p->ki_wchan); } else - (void)printf("%-*s", v->width, "-"); + str = NULL; + + return (str); } -void +char * nwchan(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - if (k->ki_p->ki_wchan) { - (void)printf("%0*lx", v->width, - (long)k->ki_p->ki_wchan); - } else - (void)printf("%-*s", v->width, "-"); + if (k->ki_p->ki_wchan) + asprintf(&str, "%0lx", (long)k->ki_p->ki_wchan); + else + str = NULL; + + return (str); } -void +char * mwchan(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; if (k->ki_p->ki_wchan) { if (k->ki_p->ki_wmesg[0] != 0) - (void)printf("%-*.*s", v->width, v->width, - k->ki_p->ki_wmesg); + str = strdup(k->ki_p->ki_wmesg); else - (void)printf("%-*lx", v->width, - (long)k->ki_p->ki_wchan); + asprintf(&str, "%lx", (long)k->ki_p->ki_wchan); } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) { if (k->ki_p->ki_lockname[0]) { - (void)printf("%-*.*s", v->width, v->width, - k->ki_p->ki_lockname); + str = strdup(k->ki_p->ki_lockname); } else - (void)printf("%-*s", v->width, "???"); + str = strdup("???"); } else - (void)printf("%-*s", v->width, "-"); + str = NULL; + + return (str); } -void +char * vsize(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024)); + asprintf(&str, "%lu", (u_long)(k->ki_p->ki_size / 1024)); + return (str); } -static void +static char * printtime(KINFO *k, VARENT *ve, long secs, long psecs) /* psecs is "parts" of a second. first micro, then centi */ { VAR *v; - char obuff[128]; static char decimal_point; + char *str; if (decimal_point == '\0') decimal_point = localeconv()->decimal_point[0]; @@ -582,21 +565,12 @@ printtime(KINFO *k, VARENT *ve, long secs, long psecs) secs += psecs / 100; psecs = psecs % 100; } - (void)snprintf(obuff, sizeof(obuff), "%ld:%02ld%c%02ld", + asprintf(&str, "%ld:%02ld%c%02ld", secs / 60, secs % 60, decimal_point, psecs); - (void)printf("%*s", v->width, obuff); + return (str); } -static int -sizetime(long secs) -{ - - if (secs < 60) - return (7); - return (log10(secs / 60) + 7); -} - -void +char * cputime(KINFO *k, VARENT *ve) { long secs, psecs; @@ -612,10 +586,10 @@ cputime(KINFO *k, VARENT *ve) secs += k->ki_p->ki_childtime.tv_sec; psecs += k->ki_p->ki_childtime.tv_usec; } - printtime(k, ve, secs, psecs); + return (printtime(k, ve, secs, psecs)); } -void +char * systime(KINFO *k, VARENT *ve) { long secs, psecs; @@ -626,10 +600,10 @@ systime(KINFO *k, VARENT *ve) secs += k->ki_p->ki_childstime.tv_sec; psecs += k->ki_p->ki_childstime.tv_usec; } - printtime(k, ve, secs, psecs); + return (printtime(k, ve, secs, psecs)); } -void +char * usertime(KINFO *k, VARENT *ve) { long secs, psecs; @@ -640,22 +614,20 @@ usertime(KINFO *k, VARENT *ve) secs += k->ki_p->ki_childutime.tv_sec; psecs += k->ki_p->ki_childutime.tv_usec; } - printtime(k, ve, secs, psecs); + return (printtime(k, ve, secs, psecs)); } -void +char * elapsed(KINFO *k, VARENT *ve) { VAR *v; time_t val; int days, hours, mins, secs; - char obuff[128]; + char *str; v = ve->var; - if (!k->ki_valid) { - (void)printf("%-*s", v->width, "-"); - return; - } + if (!k->ki_valid) + return (NULL); val = now - k->ki_p->ki_start.tv_sec; days = val / (24 * 60 * 60); val %= 24 * 60 * 60; @@ -664,29 +636,28 @@ elapsed(KINFO *k, VARENT *ve) mins = val / 60; secs = val % 60; if (days != 0) - (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d", - days, hours, mins, secs); + asprintf(&str, "%3d-%02d:%02d:%02d", days, hours, mins, secs); else if (hours != 0) - (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d", - hours, mins, secs); + asprintf(&str, "%02d:%02d:%02d", hours, mins, secs); else - (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs); - (void)printf("%*s", v->width, obuff); + asprintf(&str, "%02d:%02d", mins, secs); + + return (str); } -void +char * elapseds(KINFO *k, VARENT *ve) { VAR *v; time_t val; + char *str; v = ve->var; - if (!k->ki_valid) { - (void)printf("%-*s", v->width, "-"); - return; - } + if (!k->ki_valid) + return (NULL); val = now - k->ki_p->ki_start.tv_sec; - (void)printf("%*jd", v->width, (intmax_t)val); + asprintf(&str, "%jd", (intmax_t)val); + return (str); } double @@ -710,13 +681,15 @@ getpcpu(const KINFO *k) (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu))))); } -void +char * pcpu(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - (void)printf("%*.1f", v->width, getpcpu(k)); + asprintf(&str, "%.1f", getpcpu(k)); + return (str); } static double @@ -738,42 +711,45 @@ getpmem(KINFO *k) return (100.0 * fracmem); } -void +char * pmem(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - (void)printf("%*.1f", v->width, getpmem(k)); + asprintf(&str, "%.1f", getpmem(k)); + return (str); } -void +char * pagein(KINFO *k, VARENT *ve) { VAR *v; + char *str; v = ve->var; - (void)printf("%*ld", v->width, - k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0); + asprintf(&str, "%ld", k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0); + return (str); } /* ARGSUSED */ -void +char * maxrss(KINFO *k __unused, VARENT *ve) { VAR *v; v = ve->var; /* XXX not yet */ - (void)printf("%*s", v->width, "-"); + return (NULL); } -void +char * priorityr(KINFO *k, VARENT *ve) { VAR *v; struct priority *lpri; - char str[8]; + char *str; unsigned class, level; v = ve->var; @@ -782,112 +758,111 @@ priorityr(KINFO *k, VARENT *ve) level = lpri->pri_level; switch (class) { case PRI_ITHD: - snprintf(str, sizeof(str), "intr:%u", level); + asprintf(&str, "intr:%u", level); break; case PRI_REALTIME: - snprintf(str, sizeof(str), "real:%u", level); + asprintf(&str, "real:%u", level); break; case PRI_TIMESHARE: - strncpy(str, "normal", sizeof(str)); + asprintf(&str, "normal"); break; case PRI_IDLE: - snprintf(str, sizeof(str), "idle:%u", level); + asprintf(&str, "idle:%u", level); break; default: - snprintf(str, sizeof(str), "%u:%u", class, level); + asprintf(&str, "%u:%u", class, level); break; } - str[sizeof(str) - 1] = '\0'; - (void)printf("%*s", v->width, str); + return (str); } /* * Generic output routines. Print fields from various prototype * structures. */ -static void +static char * printval(void *bp, VAR *v) { static char ofmt[32] = "%"; const char *fcp; - char *cp; + char *cp, *str; cp = ofmt + 1; fcp = v->fmt; - if (v->flag & LJUST) - *cp++ = '-'; - *cp++ = '*'; while ((*cp++ = *fcp++)); #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n)) switch (v->type) { case CHAR: - (void)printf(ofmt, v->width, *(char *)bp); + (void)asprintf(&str, ofmt, *(char *)bp); break; case UCHAR: - (void)printf(ofmt, v->width, *(u_char *)bp); + (void)asprintf(&str, ofmt, *(u_char *)bp); break; case SHORT: - (void)printf(ofmt, v->width, *(short *)bp); + (void)asprintf(&str, ofmt, *(short *)bp); break; case USHORT: - (void)printf(ofmt, v->width, *(u_short *)bp); + (void)asprintf(&str, ofmt, *(u_short *)bp); break; case INT: - (void)printf(ofmt, v->width, *(int *)bp); + (void)asprintf(&str, ofmt, *(int *)bp); break; case UINT: - (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp)); + (void)asprintf(&str, ofmt, CHKINF127(*(u_int *)bp)); break; case LONG: - (void)printf(ofmt, v->width, *(long *)bp); + (void)asprintf(&str, ofmt, *(long *)bp); break; case ULONG: - (void)printf(ofmt, v->width, *(u_long *)bp); + (void)asprintf(&str, ofmt, *(u_long *)bp); break; case KPTR: - (void)printf(ofmt, v->width, *(u_long *)bp); + (void)asprintf(&str, ofmt, *(u_long *)bp); break; case PGTOK: - (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp)); + (void)asprintf(&str, ofmt, ps_pgtok(*(u_long *)bp)); break; default: errx(1, "unknown type %d", v->type); } + + return (str); } -void +char * kvar(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - printval((char *)((char *)k->ki_p + v->off), v); + return (printval((char *)((char *)k->ki_p + v->off), v)); } -void +char * rvar(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - if (k->ki_valid) - printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v); - else - (void)printf("%*s", v->width, "-"); + if (!k->ki_valid) + return (NULL); + return (printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v)); } -void +char * emulname(KINFO *k, VARENT *ve) { VAR *v; v = ve->var; - printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-"); + if (k->ki_p->ki_emul == NULL) + return (NULL); + return (strdup(k->ki_p->ki_emul)); } -void +char * label(KINFO *k, VARENT *ve) { char *string; @@ -908,15 +883,10 @@ label(KINFO *k, VARENT *ve) } mac_free(proclabel); out: - if (string != NULL) { - (void)printf("%-*s", v->width, string); - free(string); - } else - (void)printf("%-*s", v->width, " -"); - return; + return (string); } -void +char * loginclass(KINFO *k, VARENT *ve) { VAR *v; @@ -929,103 +899,10 @@ loginclass(KINFO *k, VARENT *ve) * and limits don't apply to system processes. */ if (k->ki_p->ki_flag & P_SYSTEM) { - (void)printf("%-*s", v->width, "-"); - return; + return (strdup("-")); } - s = k->ki_p->ki_loginclass; - (void)printf("%-*s", v->width, *s ? s : "-"); -} - -int -s_comm(KINFO *k) -{ - char tmpbuff[COMMLEN + TDNAMLEN + 2]; - - bzero(tmpbuff, sizeof(tmpbuff)); - if (showthreads && k->ki_p->ki_numthreads > 1) - sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm, - k->ki_p->ki_tdname); - else - sprintf(tmpbuff, "%s", k->ki_p->ki_comm); - return (strlen(tmpbuff)); -} - -int -s_cputime(KINFO *k) -{ - long secs; - - secs = k->ki_p->ki_runtime / 1000000; - if (sumrusage) - secs += k->ki_p->ki_childtime.tv_sec; - return (sizetime(secs)); -} - -int -s_label(KINFO *k) -{ - char *string = NULL; - mac_t proclabel; - int error, size = 0; - - if (mac_prepare_process_label(&proclabel) == -1) { - warn("mac_prepare_process_label"); - return (0); - } - error = mac_get_pid(k->ki_p->ki_pid, proclabel); - if (error == 0 && mac_to_text(proclabel, &string) == 0) { - size = strlen(string); - free(string); - } - mac_free(proclabel); - return (size); -} - -int -s_loginclass(KINFO *k) -{ - char *s; - - if (k->ki_p->ki_flag & P_SYSTEM) - return (1); - s = k->ki_p->ki_loginclass; if (s == NULL) - return (1); - - return (strlen(s)); -} - -int -s_logname(KINFO *k) -{ - char *s; - - s = k->ki_p->ki_login; - if (s == NULL) - return (1); - - return (strlen(s)); -} - -int -s_systime(KINFO *k) -{ - long secs; - - secs = k->ki_p->ki_rusage.ru_stime.tv_sec; - if (sumrusage) - secs += k->ki_p->ki_childstime.tv_sec; - return (sizetime(secs)); -} - -int -s_usertime(KINFO *k) -{ - long secs; - - secs = k->ki_p->ki_rusage.ru_utime.tv_sec; - if (sumrusage) - secs += k->ki_p->ki_childutime.tv_sec; - return (sizetime(secs)); + return (NULL); + return (strdup(s)); } diff --git a/bin/ps/ps.c b/bin/ps/ps.c index fb2b752b2b9b..eb2d6301dc7b 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -99,14 +99,12 @@ time_t now; /* Current time(3) value */ int rawcpu; /* -C */ int sumrusage; /* -S */ int termwidth; /* Width of the screen (0 == infinity). */ -int totwidth; /* Calculated-width of requested variables. */ int showthreads; /* will threads be shown? */ struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); static int forceuread = DEF_UREAD; /* Do extra work to get u-area. */ static kvm_t *kd; -static KINFO *kinfo; static int needcomm; /* -o "command" */ static int needenv; /* -e */ static int needuser; /* -o "user" */ @@ -139,7 +137,7 @@ static int addelem_tty(struct listinfo *, const char *); static int addelem_uid(struct listinfo *, const char *); static void add_list(struct listinfo *, const char *); static void descendant_sort(KINFO *, int); -static void dynsizevars(KINFO *); +static void format_output(KINFO *); static void *expand_list(struct listinfo *); static const char * fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), @@ -172,12 +170,13 @@ main(int argc, char *argv[]) struct listinfo gidlist, pgrplist, pidlist; struct listinfo ruidlist, sesslist, ttylist, uidlist; struct kinfo_proc *kp; - KINFO *next_KINFO; + KINFO *kinfo = NULL, *next_KINFO; + KINFO_STR *ks; struct varent *vent; struct winsize ws; - const char *nlistf, *memf; + const char *nlistf, *memf, *fmtstr, *str; char *cols; - int all, ch, elem, flag, _fmt, i, lineno; + int all, ch, elem, flag, _fmt, i, lineno, linelen, left; int descendancy, nentries, nkept, nselectors; int prtheader, wflag, what, xkeep, xkeep_implied; char errbuf[_POSIX2_LINE_MAX]; @@ -588,19 +587,16 @@ main(int argc, char *argv[]) kp->ki_dsize + kp->ki_ssize; if (needuser) saveuser(next_KINFO); - dynsizevars(next_KINFO); nkept++; } } sizevars(); - /* - * print header - */ - printheader(); - if (nkept == 0) + if (nkept == 0) { + printheader(); exit(1); + } /* * sort proc list @@ -613,14 +609,59 @@ main(int argc, char *argv[]) if (descendancy) descendant_sort(kinfo, nkept); + /* - * For each process, call each variable output function. + * Prepare formatted output. + */ + for (i = 0; i < nkept; i++) + format_output(&kinfo[i]); + + /* + * Print header. + */ + printheader(); + + /* + * Output formatted lines. */ for (i = lineno = 0; i < nkept; i++) { + linelen = 0; STAILQ_FOREACH(vent, &varlist, next_ve) { - (vent->var->oproc)(&kinfo[i], vent); - if (STAILQ_NEXT(vent, next_ve) != NULL) + if (vent->var->flag & LJUST) + fmtstr = "%-*s"; + else + fmtstr = "%*s"; + + ks = STAILQ_FIRST(&kinfo[i].ki_ks); + STAILQ_REMOVE_HEAD(&kinfo[i].ki_ks, ks_next); + /* Truncate rightmost column if neccessary. */ + if (STAILQ_NEXT(vent, next_ve) == NULL && + termwidth != UNLIMITED && ks->ks_str != NULL) { + left = termwidth - linelen; + if (left > 0 && left < (int)strlen(ks->ks_str)) + ks->ks_str[left] = '\0'; + } + str = ks->ks_str; + if (str == NULL) + str = "-"; + /* No padding for the last column, if it's LJUST. */ + if (STAILQ_NEXT(vent, next_ve) == NULL && + vent->var->flag & LJUST) + linelen += printf(fmtstr, 0, str); + else + linelen += printf(fmtstr, vent->var->width, str); + + if (ks->ks_str != NULL) { + free(ks->ks_str); + ks->ks_str = NULL; + } + free(ks); + ks = NULL; + + if (STAILQ_NEXT(vent, next_ve) != NULL) { (void)putchar(' '); + linelen++; + } } (void)putchar('\n'); if (prtheader && lineno++ == prtheader - 4) { @@ -1078,10 +1119,6 @@ scanvars(void) STAILQ_FOREACH(vent, &varlist, next_ve) { v = vent->var; - if (v->sproc != NULL) { - v->dwidth = v->width; - v->width = 0; - } if (v->flag & USER) needuser = 1; if (v->flag & COMM) @@ -1090,21 +1127,29 @@ scanvars(void) } static void -dynsizevars(KINFO *ki) +format_output(KINFO *ki) { struct varent *vent; VAR *v; - int i; + KINFO_STR *ks; + char *str; + int len; + STAILQ_INIT(&ki->ki_ks); STAILQ_FOREACH(vent, &varlist, next_ve) { v = vent->var; - if (v->sproc == NULL) - continue; - i = (v->sproc)(ki); - if (v->width < i) - v->width = i; - if (v->width > v->dwidth) - v->width = v->dwidth; + str = (v->oproc)(ki, vent); + ks = malloc(sizeof(*ks)); + if (ks == NULL) + errx(1, "malloc failed"); + ks->ks_str = str; + STAILQ_INSERT_TAIL(&ki->ki_ks, ks, ks_next); + if (str != NULL) { + len = strlen(str); + } else + len = 1; /* "-" */ + if (v->width < len) + v->width = len; } } @@ -1120,9 +1165,7 @@ sizevars(void) i = strlen(vent->header); if (v->width < i) v->width = i; - totwidth += v->width + 1; /* +1 for space */ } - totwidth--; } static const char * diff --git a/bin/ps/ps.h b/bin/ps/ps.h index b1e9d9b7c1ed..86aaeb0fe652 100644 --- a/bin/ps/ps.h +++ b/bin/ps/ps.h @@ -35,6 +35,11 @@ #define UNLIMITED 0 /* unlimited terminal width */ enum type { CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, ULONG, KPTR, PGTOK }; +typedef struct kinfo_str { + STAILQ_ENTRY(kinfo_str) ks_next; + char *ks_str; /* formatted string */ +} KINFO_STR; + typedef struct kinfo { struct kinfo_proc *ki_p; /* kinfo_proc structure */ char *ki_args; /* exec args */ @@ -46,6 +51,7 @@ typedef struct kinfo { int level; /* used in decendant_sort() */ char *prefix; /* calculated in decendant_sort() */ } ki_d; + STAILQ_HEAD(, kinfo_str) ki_ks; } KINFO; /* Variables. */ @@ -65,10 +71,7 @@ typedef struct var { #define INF127 0x10 /* values >127 displayed as 127 */ u_int flag; /* output routine */ - void (*oproc)(struct kinfo *, struct varent *); - /* sizing routine */ - int (*sproc)(struct kinfo *); - short width; /* printing width */ + char *(*oproc)(struct kinfo *, struct varent *); /* * The following (optional) elements are hooks for passing information * to the generic output routine pvar (which prints simple elements @@ -77,10 +80,8 @@ typedef struct var { size_t off; /* offset in structure */ enum type type; /* type of element */ const char *fmt; /* printf format */ - short dwidth; /* dynamic printing width */ - /* - * glue to link selected fields together - */ + + short width; /* calculated width */ } VAR; #include "extern.h"