about summary refs log tree commit diff
path: root/generator/pgmmake.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2017-07-03 03:21:40 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2017-07-03 03:21:40 +0000
commit8c894859b2c95aec6da3798a46321b630b77ca33 (patch)
tree8fa9dc5f76627ea7034243f19ee0f64793f3e94f /generator/pgmmake.c
parent27bd2045e7b5a276d64745a4049f588976081f42 (diff)
downloadnetpbm-mirror-8c894859b2c95aec6da3798a46321b630b77ca33.tar.gz
netpbm-mirror-8c894859b2c95aec6da3798a46321b630b77ca33.tar.xz
netpbm-mirror-8c894859b2c95aec6da3798a46321b630b77ca33.zip
Fix lazy parsing of gray level argument
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3021 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'generator/pgmmake.c')
-rw-r--r--generator/pgmmake.c48
1 files changed, 37 insertions, 11 deletions
diff --git a/generator/pgmmake.c b/generator/pgmmake.c
index 9d5e82fd..3843e316 100644
--- a/generator/pgmmake.c
+++ b/generator/pgmmake.c
@@ -1,3 +1,6 @@
+#include <stdlib.h>
+#include <string.h>
+
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "shhopt.h"
@@ -9,7 +12,7 @@ struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    gray grayLevel;
+    double grayLevel;
     unsigned int cols;
     unsigned int rows;
     gray maxval;
@@ -17,6 +20,34 @@ struct CmdlineInfo {
 
 
 
+static double
+grayLevelFromArg(const char * const arg) {
+
+    double retval;
+
+    if (strlen(arg) < 1)
+        pm_error("Gray level argument is a null string");
+    else {
+        char * endPtr;
+
+        retval = strtod(arg, &endPtr);
+
+        if (*endPtr != '\0')
+            pm_error("Gray level argument '%s' is not a floating point number",
+                     arg);
+
+        if (retval < 0.0)
+            pm_error("You can't have a negative gray level (%f)", retval);
+        if (retval > 1.0)
+            pm_error("Gray level must be in the range [0.0, 1.0].  "
+                     "You specified %f", retval);
+
+    }
+    return retval;
+}
+
+
+
 static void
 parseCommandLine(int argc, const char ** argv,
                  struct CmdlineInfo * const cmdlineP) {
@@ -48,8 +79,6 @@ parseCommandLine(int argc, const char ** argv,
     pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
-    free (option_def);
-
     if (!maxvalSpec)
         cmdlineP->maxval = PGM_MAXMAXVAL;
     else {
@@ -67,13 +96,7 @@ parseCommandLine(int argc, const char ** argv,
         pm_error("Only 3 arguments allowed: gray level, width, height.  "
                  "You specified %d", argc-1);
     else {
-        double const grayLevel = atof(argv[1]);
-        if (grayLevel < 0.0)
-            pm_error("You can't have a negative gray level (%f)", grayLevel);
-        if (grayLevel > 1.0)
-            pm_error("Gray level must be in the range [0.0, 1.0].  "
-                     "You specified %f", grayLevel);
-        cmdlineP->grayLevel = ROUNDU(grayLevel * cmdlineP->maxval);
+        cmdlineP->grayLevel = grayLevelFromArg(argv[1]);
         cmdlineP->cols = pm_parse_width(argv[2]);
         cmdlineP->rows = pm_parse_height(argv[3]);
     }
@@ -88,18 +111,21 @@ main(int argc, const char ** const argv) {
     struct CmdlineInfo cmdline;
     gray * grayrow;
     unsigned int col, row;
+    gray grayLevel;
 
     pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
+    grayLevel = ROUNDU(cmdline.grayLevel * cmdline.maxval);
+
     pgm_writepgminit(stdout, cmdline.cols, cmdline.rows, cmdline.maxval, 0);
 
     grayrow = pgm_allocrow(cmdline.cols);
 
     /* All rows are identical.  Fill once. */
     for (col = 0; col < cmdline.cols; ++col)
-        grayrow[col] = cmdline.grayLevel;
+        grayrow[col] = grayLevel;
 
     for (row = 0; row < cmdline.rows; ++row)
         pgm_writepgmrow(stdout, grayrow, cmdline.cols, cmdline.maxval, 0);