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 v2, see the file LICENSE in this tarball.
19 #include <sys/ioctl.h>
22 #include <selinux/selinux.h> /* for is_selinux_enabled() */
25 #define TERMINAL_WIDTH 80
27 extern int ps_main(int argc, char **argv)
30 int i, len, terminal_width;
33 security_context_t sid=NULL;
36 get_terminal_width_height(0, &terminal_width, NULL);
38 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
39 /* handle arguments */
40 /* bb_getopt_ulflags(argc, argv,) would force a leading dash */
41 for (len = 1; len < argc; len++) {
44 if (ENABLE_FEATURE_PS_WIDE && *c == 'w')
45 /* if w is given once, GNU ps sets the width to 132,
46 * if w is given more than once, it is "unlimited"
49 (terminal_width==TERMINAL_WIDTH) ? 132 : INT_MAX;
51 if (*c == 'c' && is_selinux_enabled())
63 printf(" PID Context Stat Command\n");
66 printf(" PID Uid VmSize Stat Command\n");
68 while ((p = procps_scan(1)) != 0) {
69 char *namecmd = p->cmd;
76 if (is_selinux_enabled()) {
77 if (getpidcon(p->pid,&sid)<0)
82 /* I assume sid initilized with NULL */
84 safe_strncpy(sbuf, sid, len);
88 safe_strncpy(sbuf, "unknown",7);
90 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
95 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
97 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
99 i = terminal_width-len;
101 if(namecmd && namecmd[0]) {
104 if(strlen(namecmd) > i)
106 printf("%s\n", namecmd);
108 namecmd = p->short_cmd;
111 if(strlen(namecmd) > (i-2))
113 printf("[%s]\n", namecmd);
115 /* no check needed, but to make valgrind happy.. */
116 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)