]> granicus.if.org Git - procps-ng/commitdiff
pmap: add range arguments parsing function
authorSami Kerola <kerolasa@iki.fi>
Sat, 3 Mar 2012 16:11:45 +0000 (17:11 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 4 Mar 2012 11:13:18 +0000 (12:13 +0100)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
pmap.c

diff --git a/pmap.c b/pmap.c
index 1f8b0a0c6d777fd96eeeee8a88405d19229d5d18..b04b521b360b78b42d1275797d9d42b5b6795bc3 100644 (file)
--- a/pmap.c
+++ b/pmap.c
@@ -382,6 +382,28 @@ static int one_proc(proc_t * p)
        return 0;
 }
 
+static void range_arguments(char *optarg)
+{
+       char *arg1;
+       char *arg2;
+
+       arg1 = xstrdup(optarg);
+       arg2 = strchr(arg1, ',');
+       if (arg2)
+               *arg2 = '\0';
+       if (arg2)
+               ++arg2;
+       else
+               arg2 = arg1;
+       if (arg1 && *arg1)
+               range_low = STRTOUKL(arg1, &arg1, 16);
+       if (*arg2)
+               range_high = STRTOUKL(arg2, &arg2, 16);
+       if (arg1 && (*arg1 || *arg2))
+               xerrx(EXIT_FAILURE, "%s: '%s'", _("failed to parse argument"),
+                     optarg);
+}
+
 int main(int argc, char **argv)
 {
        unsigned *pidlist;
@@ -422,32 +444,7 @@ int main(int argc, char **argv)
                        q_option = 1;
                        break;
                case 'A':
-                       {
-                               /* FIXME: this should be a function. */
-                               char *walk = optarg;
-                               char *arg1;
-                               char *arg2;
-                               if (walk[1]) {
-                                       arg1 = walk + 1;
-                                       walk += strlen(walk) - 1;
-                               } else {
-                                       arg1 = *++argv;
-                                       if (!arg1)
-                                               usage(stderr);
-                               }
-                               arg2 = strchr(arg1, ',');
-                               if (arg2)
-                                       *arg2 = '\0';
-                               if(arg2) ++arg2;
-                               else arg2 = arg1;
-
-                               if (arg1 && *arg1)
-                                       range_low = STRTOUKL(arg1, &arg1, 16);
-                               if (*arg2)
-                                       range_high = STRTOUKL(arg2, &arg2, 16);
-                               if (arg1 && (*arg1 || *arg2))
-                                       usage(stderr);
-                       }
+                       range_arguments(optarg);
                        break;
                case 'h':
                        usage(stdout);