svn commit: trunk/busybox: libbb procps

vodz at busybox.net vodz at busybox.net
Tue Oct 4 16:48:27 UTC 2005


Author: vodz
Date: 2005-10-04 09:48:26 -0700 (Tue, 04 Oct 2005)
New Revision: 11755

Log:
use updated bb_getopt_ulflags() for ps applet

Modified:
   trunk/busybox/libbb/getopt_ulflags.c
   trunk/busybox/procps/ps.c


Changeset:
Modified: trunk/busybox/libbb/getopt_ulflags.c
===================================================================
--- trunk/busybox/libbb/getopt_ulflags.c	2005-10-04 15:28:15 UTC (rev 11754)
+++ trunk/busybox/libbb/getopt_ulflags.c	2005-10-04 16:48:26 UTC (rev 11755)
@@ -110,6 +110,22 @@
 	bb_getopt_ulflags's return value will be as if "-a -b -c" were
 	found.
 
+ "ww"   Option have int counter usaging. For example ps applet:
+	if w is given once, GNU ps sets the width to 132,
+	if w is given more than once, it is "unlimited"
+
+	int w_counter = 0;
+	bb_opt_complementally = "ww";
+	flags = bb_getopt_ulflags(argc, argv, "w", &w_counter);
+
+	if((flags & 1))
+		width = (w_counter == 1) ? 132 : INT_MAX;
+	else
+		get_terminal_width(...&width...);
+
+	w_counter - have counter -w usaging, must set int pointer
+	to bb_getopt_ulflags() after all other requires
+
 Special characters:
 
  "-"    A dash between two options causes the second of the two
@@ -188,6 +204,7 @@
 	unsigned long switch_off;
 	unsigned long incongruously;
 	void **optarg;               /* char **optarg or llist_t **optarg */
+	int *counter;
 } t_complementally;
 
 /* You can set bb_applet_long_options for parse called long options */
@@ -221,7 +238,7 @@
 	c = 0;
 	on_off = complementally;
 	for (; *s; s++) {
-		if(c >= (sizeof(flags)*8))
+		if(c >= (int)(sizeof(flags)*8))
 			break;
 		on_off->opt = *s;
 		on_off->switch_on = (1 << c);
@@ -229,6 +246,7 @@
 		on_off->switch_off = 0;
 		on_off->incongruously = 0;
 		on_off->optarg = NULL;
+		on_off->counter = NULL;
 		if (s[1] == ':') {
 			on_off->optarg = va_arg (p, void **);
 			do
@@ -245,13 +263,14 @@
 			if(on_off->opt == l_o->val)
 				break;
 		if(on_off->opt == 0) {
-			if(c >= (sizeof(flags)*8))
+			if(c >= (int)(sizeof(flags)*8))
 				break;
 			on_off->opt = l_o->val;
 			on_off->switch_on = (1 << c);
 			on_off->list_flg = 0;
 			on_off->switch_off = 0;
 			on_off->incongruously = 0;
+			on_off->counter = NULL;
 			if(l_o->has_arg != no_argument)
 				on_off->optarg = va_arg (p, void **);
 			else
@@ -290,7 +309,10 @@
 					pair_switch = c == '-' ? &(pair->switch_off) : &(pair->incongruously);
 				for (on_off = complementally; on_off->opt; on_off++)
 					if (on_off->opt == *s) {
-						*pair_switch |= on_off->switch_on;
+						if(pair_switch == &(on_off->switch_on))
+						  on_off->counter = va_arg (p, int *);
+						else
+						  *pair_switch |= on_off->switch_on;
 						break;
 					}
 			}
@@ -315,6 +337,8 @@
 		flags &= ~(on_off->switch_off ^ trigger);
 		flags |= on_off->switch_on ^ trigger;
 		flags ^= trigger;
+		if(on_off->counter)
+			(*(on_off->counter))++;
 		if(on_off->list_flg) {
 			*(llist_t **)(on_off->optarg) =
 				llist_add_to(*(llist_t **)(on_off->optarg), optarg);

Modified: trunk/busybox/procps/ps.c
===================================================================
--- trunk/busybox/procps/ps.c	2005-10-04 15:28:15 UTC (rev 11754)
+++ trunk/busybox/procps/ps.c	2005-10-04 16:48:26 UTC (rev 11755)
@@ -22,42 +22,50 @@
 #include <selinux/selinux.h>  /* for is_selinux_enabled()  */
 #endif
 
-#define TERMINAL_WIDTH 80
-
 extern int ps_main(int argc, char **argv)
 {
 	procps_status_t * p;
-	int i, len, terminal_width;
+	int i, len;
 #if ENABLE_SELINUX
 	int use_selinux = 0;
 	security_context_t sid=NULL;
 #endif
+#if ENABLE_FEATURE_PS_WIDE
+	int terminal_width;
+	int w_count = 0;
+#else
+# define terminal_width 80
+#endif
 
-	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;
+#if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX
+	bb_opt_complementally="ww";
+	i = bb_getopt_ulflags(argc, argv, "wc", &w_count);
+#elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX
+	bb_opt_complementally="ww";
+	i = bb_getopt_ulflags(argc, argv, "w", &w_count);
+#else /* !ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX */
+	i = bb_getopt_ulflags(argc, argv, "c");
 #endif
-			c++;
-		}
+#if ENABLE_FEATURE_PS_WIDE
+	/* if w is given once, GNU ps sets the width to 132,
+	 * if w is given more than once, it is "unlimited"
+	 */
+	if((i & 1)) {
+		terminal_width = (w_count==1) ? 132 : INT_MAX;
+	} else {
+		get_terminal_width_height(0, &terminal_width, NULL);
+		/* Go one less... */
+		terminal_width--;
 	}
 #endif
+#if ENABLE_SELINUX
+	if ((i & 2) && is_selinux_enabled())
+		use_selinux = 1;
+#endif
+#endif  /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
 
-	/* Go one less... */
-	terminal_width--;
 #if ENABLE_SELINUX
 	if (use_selinux)
 	  printf("  PID Context                          Stat Command\n");
@@ -68,7 +76,7 @@
 	while ((p = procps_scan(1)) != 0)  {
 		char *namecmd = p->cmd;
 #if ENABLE_SELINUX
-		if (use_selinux )
+		if (use_selinux)
 		  {
 			char sbuf[128];
 			len = sizeof(sbuf);
@@ -118,4 +126,3 @@
 	}
 	return EXIT_SUCCESS;
 }
-




More information about the busybox-cvs mailing list