nvram: improve argument check when program start
authorBangLang Huang <banglang.huang@foxmail.com>
Fri, 24 Feb 2017 02:52:52 +0000 (10:52 +0800)
committerAlexander Couzens <lynxis@fe80.eu>
Tue, 22 Aug 2017 12:31:32 +0000 (14:31 +0200)
print help message when argument count is less
than 2.

Signed-off-by: BangLang Huang <banglang.huang@foxmail.com>
package/utils/nvram/src/cli.c

index 149d307..538bdce 100644 (file)
@@ -155,15 +155,16 @@ int main( int argc, const char *argv[] )
        int done = 0;
        int i;
 
+       if( argc < 2 ) {
+               usage();
+               return 1;
+       }
+
        /* Ugly... iterate over arguments to see whether we can expect a write */
-       for( i = 1; i < argc; i++ )
-               if( ( !strcmp(argv[i], "set")   && ++i < argc ) ||
-                       ( !strcmp(argv[i], "unset") && ++i < argc ) ||
-                       !strcmp(argv[i], "commit") )
-               {
-                       write = 1;
-                       break;
-               }
+       if( ( !strcmp(argv[1], "set")  && 2 < argc ) ||
+               ( !strcmp(argv[1], "unset") && 2 < argc ) ||
+               !strcmp(argv[1], "commit") )
+               write = 1;
 
 
        nvram = write ? nvram_open_staging() : nvram_open_rdonly();