ps: reduce #ifdef forest
[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, "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"
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+ENABLE_FEATURE_PS_WIDE)) && 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("%5d %-32s %s ", p->pid, sbuf, p->state);
75                 }
76                 else
77 #endif
78                         if (p->rss == 0)
79                                 len = printf("%5d %-8s        %s ", p->pid, p->user, p->state);
80                         else
81                                 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
82
83                 i = terminal_width-len;
84
85                 if (namecmd && namecmd[0]) {
86                         if (i < 0)
87                                 i = 0;
88                         if (strlen(namecmd) > (size_t)i)
89                                 namecmd[i] = 0;
90                         puts(namecmd);
91                 } else {
92                         namecmd = p->short_cmd;
93                         if (i < 2)
94                                 i = 2;
95                         if (strlen(namecmd) > ((size_t)i-2))
96                                 namecmd[i-2] = 0;
97                         printf("[%s]\n", namecmd);
98                 }
99                 /* no check needed, but to make valgrind happy.. */
100                 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
101                         free(p->cmd);
102         }
103         return EXIT_SUCCESS;
104 }