Unneeded code removed, usused field "unsigned pscpu" removed
[oweals/busybox.git] / procps / ps.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ps implementation(s) for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL version 2, see the file LICENSE in this tarball.
8  */
9
10 #include "busybox.h"
11
12 int ps_main(int argc, char **argv)
13 {
14         procps_status_t * p;
15         int i, len;
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 };
20 #else
21         int terminal_width;
22         int w_count = 0;
23 #endif
24
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, USE_SELINUX("c") "w", &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"
31          */
32         if (w_count) {
33                 terminal_width = (w_count==1) ? 132 : INT_MAX;
34         } else {
35                 get_terminal_width_height(1, &terminal_width, NULL);
36                 /* Go one less... */
37                 terminal_width--;
38         }
39 #else /* only ENABLE_SELINUX */
40         i = getopt32(argc, argv, "c");
41 #endif
42 #if ENABLE_SELINUX
43         if ((i & 1) && is_selinux_enabled())
44                 use_selinux = 1;
45 #endif
46 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
47
48         if (use_selinux)
49                 puts("  PID Context                          Stat Command");
50         else
51                 puts("  PID  Uid     VmSize Stat Command");
52
53         while ((p = procps_scan(1)) != 0)  {
54                 char *namecmd = p->cmd;
55 #if ENABLE_SELINUX
56                 if (use_selinux) {
57                         char sbuf[128];
58                         len = sizeof(sbuf);
59
60                         if (is_selinux_enabled()) {
61                                 if (getpidcon(p->pid, &sid) < 0)
62                                         sid = NULL;
63                         }
64
65                         if (sid) {
66                                 /* I assume sid initialized with NULL */
67                                 len = strlen(sid)+1;
68                                 safe_strncpy(sbuf, sid, len);
69                                 freecon(sid);
70                                 sid = NULL;
71                         } else {
72                                 safe_strncpy(sbuf, "unknown", 7);
73                         }
74                         len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state);
75                 } else
76 #endif
77                         if (p->rss == 0)
78                                 len = printf("%5u %-8s        %s ", (unsigned)p->pid, p->user, p->state);
79                         else
80                                 len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state);
81
82                 i = terminal_width-len;
83
84                 if (namecmd && namecmd[0]) {
85                         if (i < 0)
86                                 i = 0;
87                         if (strlen(namecmd) > (size_t)i)
88                                 namecmd[i] = 0;
89                         puts(namecmd);
90                 } else {
91                         namecmd = p->short_cmd;
92                         if (i < 2)
93                                 i = 2;
94                         if (strlen(namecmd) > ((size_t)i-2))
95                                 namecmd[i-2] = 0;
96                         printf("[%s]\n", namecmd);
97                 }
98                 /* no check needed, but to make valgrind happy.. */
99                 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
100                         free(p->cmd);
101         }
102         return EXIT_SUCCESS;
103 }