]> granicus.if.org Git - procps-ng/commitdiff
top: make command line switch parsing even more robust
authorJim Warner <james.warner@comcast.net>
Tue, 21 Mar 2017 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@enc.com.au>
Wed, 29 Mar 2017 11:10:33 +0000 (22:10 +1100)
This program has always tried to maintain an extermely
robust command line parsing procedure, far more robust
that what's available with the getopt stuff. But, with
the introduction of our first numeric switch it should
have been made even more robust than, in fact, it was.

This commit will now accomplish such a desirable goal.

Reference(s):
. added '1' command line switch
commit 5e708c5db71c2c765f5926fbc771471fa69c0ea6

Signed-off-by: Jim Warner <james.warner@comcast.net>
top/top.c

index fb686cee55f940b5735d4010f696b903212b413f..566b9d80b6c204a7da5ab655065c0835eba7af6f 100644 (file)
--- a/top/top.c
+++ b/top/top.c
@@ -3182,13 +3182,13 @@ static void parse_args (char **args) {
                else TOGw(Curwin, View_CPUSUM);
                OFFw(Curwin, View_CPUNOD);
                SETw(Curwin, View_STATES);
-               break;
+               goto bump_cp;
             case 'b':
                Batch = 1;
-               break;
+               goto bump_cp;
             case 'c':
                TOGw(Curwin, Show_CMDLIN);
-               break;
+               goto bump_cp;
             case 'd':
                if (cp[1]) ++cp;
                else if (*args) cp = *args++;
@@ -3206,10 +3206,10 @@ static void parse_args (char **args) {
                if (!(got = strchr(get, tolower(*cp))))
                   error_exit(fmtmk(N_fmt(BAD_memscale_fmt), *cp));
                Rc.summ_mscale = (int)(got - get);
-            }  break;
+            }  goto bump_cp;
             case 'H':
                Thread_mode = 1;
-               break;
+               goto bump_cp;
             case 'h':
             case 'v':
                puts(fmtmk(N_fmt(HELP_cmdline_fmt)
@@ -3218,7 +3218,7 @@ static void parse_args (char **args) {
             case 'i':
                TOGw(Curwin, Show_IDLEPS);
                Curwin->rc.maxtasks = 0;
-               break;
+               goto bump_cp;
             case 'n':
                if (cp[1]) cp++;
                else if (*args) cp = *args++;
@@ -3246,7 +3246,7 @@ static void parse_args (char **args) {
                   puts(N_col(i));
                bye_bye(NULL);
             case 'p':
-            {  unsigned pid; char *p;
+            {  int pid; char *p;
                if (Curwin->usrseltyp) error_exit(N_txt(SELECT_clash_txt));
                do {
                   if (cp[1]) cp++;
@@ -3255,7 +3255,7 @@ static void parse_args (char **args) {
                   if (Monpidsidx >= MONPIDMAX)
                      error_exit(fmtmk(N_fmt(LIMIT_exceed_fmt), MONPIDMAX));
                   if (1 != sscanf(cp, "%d", &pid)
-                  || strpbrk(cp, "+-."))
+                  || strpbrk(cp, wrong_str))
                      error_exit(fmtmk(N_fmt(BAD_mon_pids_fmt), cp));
                   if (!pid) pid = getpid();
                   for (i = 0; i < Monpidsidx; i++)
@@ -3265,14 +3265,13 @@ static void parse_args (char **args) {
                   if (!(p = strchr(cp, ','))) break;
                   cp = p;
                } while (*cp);
-            }
-               break;
+            }  break;
             case 's':
                Secure_mode = 1;
-               break;
+               goto bump_cp;
             case 'S':
                TOGw(Curwin, Show_CTIMES);
-               break;
+               goto bump_cp;
             case 'u':
             case 'U':
             {  const char *errmsg;
@@ -3303,8 +3302,9 @@ static void parse_args (char **args) {
          } // end: switch (*cp)
 
          // advance cp and jump over any numerical args used above
-         if (*cp) cp += strspn(&cp[1], numbs_str) + 1;
-
+         if (*cp) cp += strspn(&cp[1], numbs_str);
+bump_cp:
+         ++cp;
       } // end: while (*cp)
    } // end: while (*args)