[BusyBox] Added to ps: options parsing, a & n flags
Christophe Boyanique
totof at raceme.org
Thu Apr 12 16:20:33 UTC 2001
Here is the patch that add to the ps aplet:
- option parsing (if usefull)
- a flag to restrict display of all/current user process
- n flag to display numerical value for UID/GID
All this is optional and disabled by default.
Christophe.
-------------- next part --------------
diff -ur busybox-0.51.orig/Changelog busybox-0.51/Changelog
--- busybox-0.51.orig/Changelog Tue Apr 10 20:17:05 2001
+++ busybox-0.51/Changelog Thu Apr 12 18:14:27 2001
@@ -1,3 +1,8 @@
+
+ Other changes:
+ * Christophe Boyanique -- Added to ps (optional): flags parsing,
+ a and n flags.
+
0.51
Critical Bugfixes:
* Erik Andersen -- Fixed a bug that could crash the shell in 0.50
diff -ur busybox-0.51.orig/Config.h busybox-0.51/Config.h
--- busybox-0.51.orig/Config.h Tue Apr 10 00:48:11 2001
+++ busybox-0.51/Config.h Thu Apr 12 18:04:59 2001
@@ -155,6 +155,15 @@
// I emailed Linus and this patch will not be going into the stock kernel.
//#define BB_FEATURE_USE_DEVPS_PATCH
//
+// Implement the a flag to ps.
+// If this feature is disabled (default) then ps will display all processes.
+// If this feature is enabled, then ps will display only current user's
+// processes except if the a option is specified.
+//#define BB_FEATURE_PS_A
+//
+// Implement the n flag to ps: display numerical uid instead of name
+//#define BB_FEATURE_PS_N
+//
// show verbose usage messages
//#define BB_FEATURE_VERBOSE_USAGE
//
@@ -453,3 +462,8 @@
#if defined BB_DOS2UNIX
#define BB_UNIX2DOS
#endif
+//
+#if defined BB_FEATURE_PS_A || defined BB_FEATURE_PS_N
+ #define BB_FEATURE_PS_OPTIONS
+#endif
+
diff -ur busybox-0.51.orig/ps.c busybox-0.51/ps.c
--- busybox-0.51.orig/ps.c Thu Apr 12 15:49:42 2001
+++ busybox-0.51/ps.c Thu Apr 12 18:15:55 2001
@@ -5,6 +5,8 @@
* Copyright (C) 1999,2000,2001 by Lineo, inc.
* Written by Erik Andersen <andersen at lineo.com>, <andersee at debian.org>
*
+ * Options parsing, a and n flags feature Copyright (C) 2001 by Alcove
+ * written by Christophe Boyanique <Christophe.Boyanique at fr.alcove.com>
*
* This contains _two_ implementations of ps for Linux. One uses the
* traditional /proc virtual filesystem, and the other use the devps kernel
@@ -44,6 +46,12 @@
static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
+#ifdef BB_FEATURE_PS_A
+static int aFlag = FALSE;
+#endif
+#ifdef BB_FEATURE_PS_N
+static int nFlag = FALSE;
+#endif
#if ! defined BB_FEATURE_USE_DEVPS_PATCH
@@ -122,6 +130,12 @@
char uidName[9];
char groupName[9];
int len, i, c;
+#if defined BB_FEATURE_PS_OPTIONS
+ int j;
+#endif
+#if defined BB_FEATURE_PS_A
+ int curUid = getuid();
+#endif
#ifdef BB_FEATURE_AUTOWIDTH
struct winsize win = { 0, 0, 0, 0 };
int terminal_width = TERMINAL_WIDTH;
@@ -129,8 +143,27 @@
#define terminal_width TERMINAL_WIDTH
#endif
+#if defined BB_FEATURE_PS_OPTIONS
+ for (i=1; i<argc; i++) {
+ for (j=0; j<strlen(argv[i]); j++) {
+ switch (argv[i][j]) {
+#ifdef BB_FEATURE_PS_A
+ case 'a':
+ aFlag = TRUE;
+ break;
+#endif
+#ifdef BB_FEATURE_PS_N
+ case 'n':
+ nFlag = TRUE;
+ break;
+#endif
+ }
+ }
+ }
+#else
if (argc > 1 && **(argv + 1) == '-')
show_usage();
+#endif
dir = opendir("/proc");
if (!dir)
@@ -142,7 +175,12 @@
terminal_width = win.ws_col - 1;
#endif
- printf("%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
+#ifdef BB_FEATURE_PS_A
+ if (aFlag == FALSE)
+ printf("%5s %5s %s\n", "PID", "STATE", "COMMAND");
+ else
+#endif
+ printf("%5s %-8s %-3s %5s %s\n", "PID", "UID", "GID", "STATE", "COMMAND");
while ((entry = readdir(dir)) != NULL) {
if (!isdigit(*entry->d_name))
continue;
@@ -151,20 +189,39 @@
parse_proc_status(sbuf, &p);
}
+#ifdef BB_FEATURE_PS_A
+ if ((aFlag == FALSE) && (p.ruid != curUid))
+ continue;
+#endif
+
/* Make some adjustments as needed */
- my_getpwuid(uidName, p.ruid);
- if (*uidName == '\0')
+#ifdef BB_FEATURE_PS_N
+ if (nFlag == TRUE) {
sprintf(uidName, "%d", p.ruid);
- my_getgrgid(groupName, p.rgid);
- if (*groupName == '\0')
sprintf(groupName, "%d", p.rgid);
+ } else {
+#endif
+ my_getpwuid(uidName, p.ruid);
+ if (*uidName == '\0')
+ sprintf(uidName, "%d", p.ruid);
+ my_getgrgid(groupName, p.rgid);
+ if (*groupName == '\0')
+ sprintf(groupName, "%d", p.rgid);
+#ifdef BB_FEATURE_PS_N
+ }
+#endif
sprintf(path, "/proc/%s/cmdline", entry->d_name);
file = fopen(path, "r");
if (file == NULL)
continue;
i = 0;
- len = printf("%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
+#ifdef BB_FEATURE_PS_A
+ if (aFlag == FALSE)
+ len = printf("%5d %c ", p.pid, p.state);
+ else
+#endif
+ len = printf("%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
while (((c = getc(file)) != EOF) && (i < (terminal_width-len))) {
i++;
if (c == '\0')
@@ -200,6 +257,12 @@
struct pid_info info;
char uidName[9];
char groupName[9];
+#if defined BB_FEATURE_PS_OPTIONS
+ int j;
+#endif
+#if defined BB_FEATURE_PS_A
+ int curUid = getuid();
+#endif
#ifdef BB_FEATURE_AUTOWIDTH
struct winsize win = { 0, 0, 0, 0 };
int terminal_width = TERMINAL_WIDTH;
@@ -207,8 +270,27 @@
#define terminal_width TERMINAL_WIDTH
#endif
+#if defined BB_FEATURE_PS_OPTIONS
+ for (i=1; i<argc; i++) {
+ for (j=0; j<strlen(argv[i]); j++) {
+ switch (argv[i][j]) {
+#ifdef BB_FEATURE_PS_A
+ case 'a':
+ aFlag = TRUE;
+ break;
+#endif
+#ifdef BB_FEATURE_PS_N
+ case 'n':
+ nFlag = TRUE;
+ break;
+#endif
+ }
+ }
+ }
+#else
if (argc > 1 && **(argv + 1) == '-')
show_usage();
+#endif
/* open device */
fd = open(device, O_RDONLY);
@@ -237,22 +319,46 @@
#endif
/* Print up a ps listing */
- printf("%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
+#ifdef BB_FEATURE_PS_A
+ if (aFlag == FALSE)
+ printf("%5s %5s %s\n", "PID", "STATE", "COMMAND");
+ else
+#endif
+ printf("%5s %-8s %-3s %5s %s\n", "PID", "UID", "GID", "STATE", "COMMAND");
for (i=1; i<pid_array[0] ; i++) {
info.pid = pid_array[i];
if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
perror_msg_and_die("\nDEVPS_GET_PID_INFO");
-
+
+#ifdef BB_FEATURE_PS_A
+ if ((aFlag == FALSE) && (info.euid != curUid))
+ continue;
+#endif
+
/* Make some adjustments as needed */
- my_getpwuid(uidName, info.euid);
- if (*uidName == '\0')
+#ifdef BB_FEATURE_PS_N
+ if (nFlag == TRUE) {
sprintf(uidName, "%ld", info.euid);
- my_getgrgid(groupName, info.egid);
- if (*groupName == '\0')
sprintf(groupName, "%ld", info.egid);
+ } else {
+#endif
+ my_getpwuid(uidName, info.euid);
+ if (*uidName == '\0')
+ sprintf(uidName, "%ld", info.euid);
+ my_getgrgid(groupName, info.egid);
+ if (*groupName == '\0')
+ sprintf(groupName, "%ld", info.egid);
+#ifdef BB_FEATURE_PS_N
+ }
+#endif
+#ifdef BB_FEATURE_PS_A
+ if (aFlag == FALSE)
+ len = printf("%5d %c ", info.pid, info.state);
+ else
+#endif
len = printf("%5d %-8s %-8s %c ", info.pid, uidName, groupName, info.state);
if (strlen(info.command_line) > 1) {
diff -ur busybox-0.51.orig/usage.h busybox-0.51/usage.h
--- busybox-0.51.orig/usage.h Tue Apr 10 01:26:31 2001
+++ busybox-0.51/usage.h Thu Apr 12 18:01:45 2001
@@ -1205,11 +1205,32 @@
"$ printf "Val=%d\n" 5\n" \
"Val=5\n"
+#ifdef BB_FEATURE_PS_OPTIONS
+#ifdef BB_FEATURE_PS_A
+ #define USAGE_PS_A(a) a
+#else
+ #define USAGE_PS_A(a)
+#endif
+#ifdef BB_FEATURE_PS_N
+ #define USAGE_PS_N(a) a
+#else
+ #define USAGE_PS_N(a)
+#endif
+#define ps_trivial_usage \
+ ""
+#define ps_full_usage \
+ "Report process status\n" \
+ "\nOptions:\n" \
+ USAGE_PS_A("\n\ta\tPrint every process running") \
+ USAGE_PS_N("\n\tn\tPrint numerical UID and GID") \
+ ""
+#else
#define ps_trivial_usage \
""
#define ps_full_usage \
"Report process status\n" \
"\nThis version of ps accepts no options."
+#endif
#define ps_example_usage \
"$ ps\n" \
" PID Uid Gid State Command\n" \
More information about the busybox
mailing list