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)
16 SKIP_SELINUX(const) int use_selinux = 0;
17 USE_SELINUX(security_context_t sid = NULL;)
18 #if !ENABLE_FEATURE_PS_WIDE
19 enum { terminal_width = 79 };
25 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
26 #if ENABLE_FEATURE_PS_WIDE
27 opt_complementary = "-:ww";
28 USE_SELINUX(i =) getopt32(argc, argv, "w" USE_SELINUX("c"), &w_count);
29 /* if w is given once, GNU ps sets the width to 132,
30 * if w is given more than once, it is "unlimited"
33 terminal_width = (w_count==1) ? 132 : INT_MAX;
35 get_terminal_width_height(1, &terminal_width, NULL);
39 #else /* only ENABLE_SELINUX */
40 i = getopt32(argc, argv, "c");
43 if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled())
46 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
49 puts(" PID Context Stat Command");
51 puts(" PID Uid VmSize Stat Command");
53 while ((p = procps_scan(1)) != 0) {
54 char *namecmd = p->cmd;
60 if (is_selinux_enabled()) {
61 if (getpidcon(p->pid,&sid) < 0)
66 /* I assume sid initialized with NULL */
68 safe_strncpy(sbuf, sid, len);
72 safe_strncpy(sbuf, "unknown", 7);
74 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
79 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
81 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
83 i = terminal_width-len;
85 if (namecmd && namecmd[0]) {
88 if (strlen(namecmd) > (size_t)i)
92 namecmd = p->short_cmd;
95 if (strlen(namecmd) > ((size_t)i-2))
97 printf("[%s]\n", namecmd);
99 /* no check needed, but to make valgrind happy.. */
100 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)