Convert atoi -> strtonum. Plus more detailed error messages.

OK ?

-- 
Alexandr Shadchin

Index: wsmoused.c
===================================================================
RCS file: /cvs/src/usr.sbin/wsmoused/wsmoused.c,v
retrieving revision 1.34
diff -u -p -r1.34 wsmoused.c
--- wsmoused.c  22 Dec 2014 11:21:49 -0000      1.34
+++ wsmoused.c  22 Dec 2014 11:29:36 -0000
@@ -175,14 +175,6 @@ static int p2l[MOUSE_MAXBUTTON] = {
        MOUSE_BUTTON5,  MOUSE_BUTTON6,  MOUSE_BUTTON7,  MOUSE_BUTTON8,
 };
 
-static char *
-skipspace(char *s)
-{
-       while (isspace((unsigned char)*s))
-               ++s;
-       return s;
-}
-
 /* mouse_installmap : install a map between physical and logical buttons */
 static int
 mouse_installmap(char *arg)
@@ -190,29 +182,38 @@ mouse_installmap(char *arg)
        int pbutton;
        int lbutton;
        char *s;
+       const char *errstr;
 
        while (*arg) {
-               arg = skipspace(arg);
                s = arg;
                while (isdigit((unsigned char)*arg))
                        ++arg;
-               arg = skipspace(arg);
-               if ((arg <= s) || (*arg != '='))
+               if ((arg == s) || (*arg != '='))
                        return FALSE;
-               lbutton = atoi(s);
+               *arg = '\0';
+               lbutton = strtonum(s, 1, MOUSE_MAXBUTTON, &errstr);
+               if (errstr != NULL) {
+                       warnx("logical button value `%s' is %s, "
+                           "acceptable range is 1-%d",
+                           s, errstr, MOUSE_MAXBUTTON);
+                       *arg = '=';
+                       return FALSE;
+               }
+               *arg = '=';
 
-               arg = skipspace(++arg);
-               s = arg;
+               s = ++arg;
                while (isdigit((unsigned char)*arg))
                        ++arg;
-               if (arg <= s || (!isspace((unsigned char)*arg) && *arg != '\0'))
-                       return FALSE;
-               pbutton = atoi(s);
-
-               if (lbutton <= 0 || lbutton > MOUSE_MAXBUTTON)
+               if ((arg == s) || (*arg != '\0'))
                        return FALSE;
-               if (pbutton <= 0 || pbutton > MOUSE_MAXBUTTON)
+               pbutton = strtonum(s, 1, MOUSE_MAXBUTTON, &errstr);
+               if (errstr != NULL) {
+                       warnx("physical button value `%s' is %s, "
+                           "acceptable range is 1-%d",
+                           s, errstr, MOUSE_MAXBUTTON);
                        return FALSE;
+               }
+
                p2l[pbutton - 1] = lbutton - 1;
        }
        return TRUE;
@@ -454,6 +455,7 @@ main(int argc, char **argv)
        unsigned int type;
        int opt;
        int i;
+       const char *errstr;
 
 #define GETOPT_STRING "2dfhip:t:C:D:I:M:"
        while ((opt = (getopt(argc, argv, GETOPT_STRING))) != -1) {
@@ -498,11 +500,12 @@ main(int argc, char **argv)
                        break;
                case 'C':
 #define MAX_CLICKTHRESHOLD 2000 /* max delay for double click */
-                       mouse.clickthreshold = atoi(optarg);
-                       if (mouse.clickthreshold < 0 ||
-                           mouse.clickthreshold > MAX_CLICKTHRESHOLD) {
-                               warnx("invalid threshold `%s': max value is %d",
-                                   optarg, MAX_CLICKTHRESHOLD);
+                       mouse.clickthreshold = strtonum(optarg, 0,
+                           MAX_CLICKTHRESHOLD, &errstr);
+                       if (errstr != NULL) {
+                               warnx("threshold value `%s' is %s, "
+                                   "acceptable range is 0-%d",
+                                   optarg, errstr, MAX_CLICKTHRESHOLD);
                                usage();
                        }
                        break;

Reply via email to