diff options
-rw-r--r-- | libbb/getopt_ulflags.c | 30 | ||||
-rw-r--r-- | procps/ps.c | 55 |
2 files changed, 58 insertions, 27 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c index 58077c5..8c03214 100644 --- a/libbb/getopt_ulflags.c +++ b/libbb/getopt_ulflags.c @@ -110,6 +110,22 @@ const char *bb_opt_complementally 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 @@ typedef struct { 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 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 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 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 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 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 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 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 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 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 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); diff --git a/procps/ps.c b/procps/ps.c index a610bb2..a0081c8 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -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 - - get_terminal_width_height(0, &terminal_width, NULL); +#if ENABLE_FEATURE_PS_WIDE + int terminal_width; + int w_count = 0; +#else +# define terminal_width 80 +#endif #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 @@ extern int ps_main(int argc, char **argv) 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 @@ extern int ps_main(int argc, char **argv) } return EXIT_SUCCESS; } - |