1 /* vi: set sw=4 ts=4: */
3 * Mini ps implementation(s) for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
12 int ps_main(int argc, char **argv)
19 security_context_t sid = NULL;
22 #if ENABLE_FEATURE_PS_WIDE
26 bb_opt_complementally = "-:ww";
28 # define terminal_width 79
31 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
32 /* handle arguments */
33 #if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX
34 i = bb_getopt_ulflags(argc, argv, "wc", &w_count);
35 #elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX
36 bb_getopt_ulflags(argc, argv, "w", &w_count);
37 #else /* !ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX */
38 i = bb_getopt_ulflags(argc, argv, "c");
40 #if ENABLE_FEATURE_PS_WIDE
41 /* if w is given once, GNU ps sets the width to 132,
42 * if w is given more than once, it is "unlimited"
45 terminal_width = (w_count==1) ? 132 : INT_MAX;
47 get_terminal_width_height(1, &terminal_width, NULL);
53 if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled())
56 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
60 puts(" PID Context Stat Command");
63 puts(" PID Uid VmSize Stat Command");
65 while ((p = procps_scan(1)) != 0) {
66 char *namecmd = p->cmd;
72 if (is_selinux_enabled()) {
73 if (getpidcon(p->pid,&sid) < 0)
78 /* I assume sid initilized with NULL */
80 safe_strncpy(sbuf, sid, len);
84 safe_strncpy(sbuf, "unknown", 7);
86 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
91 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
93 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
95 i = terminal_width-len;
97 if(namecmd && namecmd[0]) {
100 if(strlen(namecmd) > (size_t)i)
102 printf("%s\n", namecmd);
104 namecmd = p->short_cmd;
107 if(strlen(namecmd) > ((size_t)i-2))
109 printf("[%s]\n", namecmd);
111 /* no check needed, but to make valgrind happy.. */
112 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)