[BusyBox] [patch] ps w, take two

Bernhard Fischer rep.nop at aon.at
Tue Feb 8 14:49:14 UTC 2005


On Fri, Feb 04, 2005 at 01:17:41PM +0300, Vladimir N. Oleynik wrote:
>Rainer,
>
>>>The attached patch adds an option CONFIG_PS_FEATURE_WIDE which adds
>>>support for the argument w for printing up to 511 chars of command and
>>>args.
>>>While at it, i added another option -- CONFIG_PS_FEATURE_WIDEPIPE --
>>>which looks if stdout is a fifo and if so, also sets the maximum
>>>line-length to 511.
>
>I have analyzed a source code procps-3.0.3.
>If "-w" selected then use 132 cols, if it is specified more once
>or non tty output, infinite value of width is used.
>
>I think:
>
>1) must change libbb/get_terminal_width_height for can see result
>of ioctl(fd, TIOCGWINSZ, &win):

ok.
>>+config CONFIG_PS_FEATURE_WIDEPIPE
>>+	bool "  Default to wide on descriptors not refering to tty devices"
>>+	default n
>>+	depends on CONFIG_PS
>>+	help
>>+	  Automatically use wide format if output goes to a descriptor
>>+          that does not refer to a terminal device.
>
>2) It is better to not do it in general.
why? ps does exactly this.
>
>3) usind isatty() is not require
ok.
[snip]
>+#ifdef CONFIG_FEATURE_AUTOWIDTH
>+  	if(get_terminal_width_height(0, &terminal_width, NULL) == 0)
i changed that stdin to stdout (see 2. above). and dropped ....WIDEPIPE
>+		w_opt = 2;
[snip]


What do you think?

include/libbb.h: return the retval of tiocgwinsz when FEATURE_AUTOWIDTH
	is on.
procps/Config.in, include/usage.h: add helptext and config for the
	optional argument 'w' of ps. -w is off by default.
libbb/get_terminal_width_height.c: return the retval of tiocgwinsz
	when FEATURE_AUTOWIDTH is on.
procps/ps.c: implement option 'w' as proposed by Vladimir N. Oleynik.
	if FEATURE_AUTOWIDTH is set, do not truncate the output if
	stdout is not a tty. This mimics the big GNU ps.

-------------- next part --------------
diff -X ../excl -rdup upstream/busybox/include/libbb.h busybox/include/libbb.h
--- upstream/busybox/include/libbb.h	2005-01-24 08:00:00.000000000 +0100
+++ busybox/include/libbb.h	2005-02-07 09:32:39.000000000 +0100
@@ -477,7 +477,11 @@ extern void print_login_prompt(void);
 
 extern void vfork_daemon_rexec(int nochdir, int noclose,
 		int argc, char **argv, char *foreground_opt);
+#ifdef CONFIG_FEATURE_AUTOWIDTH
+extern int get_terminal_width_height(int fd, int *width, int *height);
+#else
 extern void get_terminal_width_height(int fd, int *width, int *height);
+#endif
 extern unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *));
 extern void xregcomp(regex_t *preg, const char *regex, int cflags);
 
diff -X ../excl -rdup upstream/busybox/include/usage.h busybox/include/usage.h
--- upstream/busybox/include/usage.h	2005-01-24 08:00:01.000000000 +0100
+++ busybox/include/usage.h	2005-02-07 18:30:53.000000000 +0100
@@ -1943,18 +1962,29 @@
 	"$ printf "Val=%d\\n" 5\n" \
 	"Val=5\n"
 
+#if !defined(CONFIG_SELINUX) && !defined(CONFIG_PS_FEATURE_WIDE)
+#define USAGE_PS "\n\tThis version of ps accepts no options."
+#else
+#define USAGE_PS "\nOptions:"
+#endif
 #ifdef CONFIG_SELINUX
 #define USAGE_NONSELINUX(a)
 #else
 #define USAGE_NONSELINUX(a) a
 #endif
+#ifdef CONFIG_PS_FEATURE_WIDE
+#define USAGE_PS_WIDE(a) a
+#else
+#define USAGE_PS_WIDE(a)
+#endif
 
 #define ps_trivial_usage \
 	""
 #define ps_full_usage \
 	"Report process status\n" \
-	USAGE_NONSELINUX("\n\tThis version of ps accepts no options.") \
-	USAGE_SELINUX("\nOptions:\n\t-c\tshow SE Linux context")
+	USAGE_PS \
+	USAGE_PS_WIDE("\n\tw\twide output") \
+	USAGE_SELINUX("\n\t-c\tshow SE Linux context")
 
 #define ps_example_usage \
 	"$ ps\n" \
diff -X ../excl -rdup upstream/busybox/libbb/get_terminal_width_height.c busybox/libbb/get_terminal_width_height.c
--- upstream/busybox/libbb/get_terminal_width_height.c	2004-03-24 00:15:35.000000000 +0100
+++ busybox/libbb/get_terminal_width_height.c	2005-02-07 18:21:52.000000000 +0100
@@ -32,11 +32,16 @@
  * height, in which case that value will not be set.  It is also
  * perfectly ok to have CONFIG_FEATURE_AUTOWIDTH disabled, in
  * which case you will always get 80x24 */
+#ifdef CONFIG_FEATURE_AUTOWIDTH
+int get_terminal_width_height(int fd, int *width, int *height)
+#else
 void get_terminal_width_height(int fd, int *width, int *height)
+#endif
 {
 	struct winsize win = { 0, 0, 0, 0 };
 #ifdef CONFIG_FEATURE_AUTOWIDTH
-	if (ioctl(fd, TIOCGWINSZ, &win) != 0) {
+	int rez = ioctl(fd, TIOCGWINSZ, &win);
+	if (rez) {
 		win.ws_row = 24;
 		win.ws_col = 80;
 	}
@@ -53,6 +58,9 @@ void get_terminal_width_height(int fd, i
 	if (width) {
 		*width = (int) win.ws_col;
 	}
+#ifdef CONFIG_FEATURE_AUTOWIDTH
+	return rez;
+#endif
 }
 
 /* END CODE */
diff -X ../excl -rdup upstream/busybox/procps/Config.in busybox/procps/Config.in
--- upstream/busybox/procps/Config.in	2003-12-24 07:02:11.000000000 +0100
+++ busybox/procps/Config.in	2005-02-08 09:45:42.000000000 +0100
@@ -43,6 +43,15 @@ config CONFIG_PS
 	help
 	  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
 	bool "renice"
 	default n
diff -X ../excl -rdup upstream/busybox/procps/ps.c busybox/procps/ps.c
--- upstream/busybox/procps/ps.c	2004-03-15 09:29:03.000000000 +0100
+++ busybox/procps/ps.c	2005-02-08 10:42:53.000000000 +0100
@@ -36,15 +36,20 @@
 #include <flask_util.h>          /* for is_flask_enabled() */
 #endif
 
-static const int TERMINAL_WIDTH = 79;      /* not 80 in case terminal has linefold bug */
-
-
 
+static const int TERMINAL_WIDTH = 79;      /* not 80 in case terminal has
+											* linefold bug
+											*/
 extern int ps_main(int argc, char **argv)
 {
 	procps_status_t * p;
 	int i, len;
 	int terminal_width = TERMINAL_WIDTH;
+#if defined(CONFIG_FEATURE_PS_WIDE) || defined(CONFIG_FEATURE_AUTOWIDTH)
+	int w_opt = 0;
+#else
+#define w_opt 0
+#endif
 
 #ifdef CONFIG_SELINUX
 	int use_selinux = 0;
@@ -52,8 +57,26 @@ extern int ps_main(int argc, char **argv
 	if(is_flask_enabled() && argv[1] && !strcmp(argv[1], "-c") )
 		use_selinux = 1;
 #endif
-
+#ifdef CONFIG_FEATURE_AUTOWIDTH
+	/* do not truncate the output if stdout is not a tty */
+	if (get_terminal_width_height(1, &terminal_width, NULL))
+		w_opt = 2;
+#else
 	get_terminal_width_height(0, &terminal_width, NULL);
+#endif
+#ifdef CONFIG_FEATURE_PS_WIDE
+	/* handle argument w */
+	/* bb_getopt_ulflags(argc, argv, "w") would force a leading dash */
+	if (argv[1])
+		while (*(argv[1])!='\0') {
+			if (*(argv[1]) == 'w')
+				w_opt++;
+			(argv[1])++;
+		}
+#endif
+
+	if (w_opt == 1)
+		terminal_width = 132;
 	/* Go one less... */
 	terminal_width--;
 
@@ -88,10 +111,10 @@ extern int ps_main(int argc, char **argv
 			len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
 		i = terminal_width-len;
 
-		if(namecmd != 0 && namecmd[0] != 0) {
+		if(namecmd && namecmd[0]) {
 			if(i < 0)
-		i = 0;
-			if(strlen(namecmd) > i)
+				i = 0;
+			if((w_opt < 2) && (strlen(namecmd) > i))
 				namecmd[i] = 0;
 			printf("%s\n", namecmd);
 		} else {
@@ -102,7 +125,10 @@ extern int ps_main(int argc, char **argv
 				namecmd[i-2] = 0;
 			printf("[%s]\n", namecmd);
 		}
-		free(p->cmd);
+#ifdef CONFIG_FEATURE_CLEAN_UP
+		if (p->cmd)
+			free(p->cmd);
+#endif
 	}
 	return EXIT_SUCCESS;
 }


More information about the busybox mailing list