- add option 'w' to ps (wide output, optional feature defaults to off).

- move to ENABLE_ and use shorter boilerplate.

sizes without selinux-support:
   text	   data	    bss	    dec	    hex	filename
    356	      0	      0	    356	    164	procps/ps.o.with-w
    300	      0	      0	    300	    12c	procps/ps.o.no-w
    302	      0	      0	    302	    12e	procps/ps.o.oorig
This commit is contained in:
Bernhard Reutner-Fischer 2005-10-04 14:31:18 +00:00
parent 5209e18ba7
commit 6d6a40cc4b
3 changed files with 48 additions and 39 deletions

View File

@ -2245,17 +2245,12 @@
"$ printf \"Val=%d\\n\" 5\n" \ "$ printf \"Val=%d\\n\" 5\n" \
"Val=5\n" "Val=5\n"
#if !defined(CONFIG_SELINUX) && !defined(CONFIG_PS_FEATURE_WIDE) #if !defined CONFIG_SELINUX && !ENABLE_FEATURE_PS_WIDE
#define USAGE_PS "\n\tThis version of ps accepts no options." #define USAGE_PS "\n\tThis version of ps accepts no options."
#else #else
#define USAGE_PS "\nOptions:" #define USAGE_PS "\nOptions:"
#endif #endif
#ifdef CONFIG_SELINUX #if ENABLE_FEATURE_PS_WIDE
#define USAGE_NONSELINUX(a)
#else
#define USAGE_NONSELINUX(a) a
#endif
#ifdef CONFIG_PS_FEATURE_WIDE
#define USAGE_PS_WIDE(a) a #define USAGE_PS_WIDE(a) a
#else #else
#define USAGE_PS_WIDE(a) #define USAGE_PS_WIDE(a)
@ -2269,7 +2264,6 @@
USAGE_SELINUX("\n\t-c\tshow SE Linux context") \ USAGE_SELINUX("\n\t-c\tshow SE Linux context") \
USAGE_PS_WIDE("\n\tw\twide output") USAGE_PS_WIDE("\n\tw\twide output")
#define ps_example_usage \ #define ps_example_usage \
"$ ps\n" \ "$ ps\n" \
" PID Uid Gid State Command\n" \ " PID Uid Gid State Command\n" \

View File

@ -43,6 +43,15 @@ config CONFIG_PS
help help
ps gives a snapshot of the current processes. ps gives a snapshot of the current processes.
config CONFIG_FEATURE_PS_WIDE
bool " Enable argument for wide output (-w)"
default n
depends on CONFIG_PS
help
Support argument 'w' for wide output.
If given once, 132 chars are printed and given more than
one, the length is unlimited.
config CONFIG_RENICE config CONFIG_RENICE
bool "renice" bool "renice"
default n default n

View File

@ -4,19 +4,7 @@
* *
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
* *
* This program is free software; you can redistribute it and/or modify it * Licensed under the GPL v2, see the file LICENSE in this tarball.
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include <stdio.h> #include <stdio.h>
@ -30,32 +18,47 @@
#include <termios.h> #include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "busybox.h" #include "busybox.h"
#ifdef CONFIG_SELINUX #if ENABLE_SELINUX
#include <selinux/selinux.h> /* for is_selinux_enabled() */ #include <selinux/selinux.h> /* for is_selinux_enabled() */
#endif #endif
static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */ #define TERMINAL_WIDTH 80
extern int ps_main(int argc, char **argv) extern int ps_main(int argc, char **argv)
{ {
procps_status_t * p; procps_status_t * p;
int i, len; int i, len, terminal_width;
int terminal_width = TERMINAL_WIDTH; #if ENABLE_SELINUX
#ifdef CONFIG_SELINUX
int use_selinux = 0; int use_selinux = 0;
security_context_t sid=NULL; security_context_t sid=NULL;
if(is_selinux_enabled() && argv[1] && !strcmp(argv[1], "-c") )
use_selinux = 1;
#endif #endif
get_terminal_width_height(0, &terminal_width, NULL); get_terminal_width_height(0, &terminal_width, NULL);
#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
/* handle arguments */
/* bb_getopt_ulflags(argc, argv,) would force a leading dash */
for (len = 1; len < argc; len++) {
char *c = argv[len];
while (*c) {
if (ENABLE_FEATURE_PS_WIDE && *c == 'w')
/* if w is given once, GNU ps sets the width to 132,
* if w is given more than once, it is "unlimited"
*/
terminal_width =
(terminal_width==TERMINAL_WIDTH) ? 132 : INT_MAX;
#if ENABLE_SELINUX
if (*c == 'c' && is_selinux_enabled())
use_selinux = 1;
#endif
c++;
}
}
#endif
/* Go one less... */ /* Go one less... */
terminal_width--; terminal_width--;
#if ENABLE_SELINUX
#ifdef CONFIG_SELINUX
if (use_selinux) if (use_selinux)
printf(" PID Context Stat Command\n"); printf(" PID Context Stat Command\n");
else else
@ -64,8 +67,8 @@ extern int ps_main(int argc, char **argv)
while ((p = procps_scan(1)) != 0) { while ((p = procps_scan(1)) != 0) {
char *namecmd = p->cmd; char *namecmd = p->cmd;
#ifdef CONFIG_SELINUX #if ENABLE_SELINUX
if ( use_selinux ) if (use_selinux )
{ {
char sbuf[128]; char sbuf[128];
len = sizeof(sbuf); len = sizeof(sbuf);
@ -85,18 +88,19 @@ extern int ps_main(int argc, char **argv)
safe_strncpy(sbuf, "unknown",7); safe_strncpy(sbuf, "unknown",7);
} }
len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
} }
else else
#endif #endif
if(p->rss == 0) if(p->rss == 0)
len = printf("%5d %-8s %s ", p->pid, p->user, p->state); len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
else else
len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
i = terminal_width-len; i = terminal_width-len;
if(namecmd != 0 && namecmd[0] != 0) { if(namecmd && namecmd[0]) {
if(i < 0) if(i < 0)
i = 0; i = 0;
if(strlen(namecmd) > i) if(strlen(namecmd) > i)
namecmd[i] = 0; namecmd[i] = 0;
printf("%s\n", namecmd); printf("%s\n", namecmd);
@ -108,7 +112,9 @@ extern int ps_main(int argc, char **argv)
namecmd[i-2] = 0; namecmd[i-2] = 0;
printf("[%s]\n", namecmd); printf("[%s]\n", namecmd);
} }
free(p->cmd); /* no check needed, but to make valgrind happy.. */
if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
free(p->cmd);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }