logutil: Drop unneeded getopt() sanity checking 58/142858/7
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 9 Aug 2017 12:08:27 +0000 (14:08 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 9 Aug 2017 14:34:15 +0000 (16:34 +0200)
getopt() internally checks if argument was specified.
This commit removes duplicate checking.

Change-Id: I53ce56756bfffa96fc82761679d29c1d9639a553

src/logutil/logutil.c

index 952d9ba..16b97af 100755 (executable)
@@ -771,7 +771,6 @@ int main(int argc, char ** argv)
        __attribute__ ((cleanup(fdi_array_free))) struct fd_info * fdi_ptrs[SIMULTANEOUS_BUFFERS + 1] = {0}; // null terminated
        int fdi_cnt = 0;
        int i;
-       int option;
        int tag_cnt = 0;
        struct stat stat_buf;
        __attribute__ ((cleanup(logfile_free))) struct log_file l_file = {
@@ -798,43 +797,26 @@ int main(int argc, char ** argv)
        logs.timeout = log_config_get_int(&conf, "util_sorting_time_window", DEFAULT_SORT_TIMEOUT);
 
        while (1) {
-               static struct option long_options[] = {
+               static const struct option long_options[] = {
                        {"dumpfile", required_argument, 0, 0},
                        {"help", no_argument, 0, 'h'},
                        {0, 0, 0, 0}
                };
-               int long_option_id = -1;
-               optarg = NULL;
-               option = getopt_long(argc, argv, "cdt:gsf:r:n:v:b:u:h", long_options, &long_option_id);
+               int err_arg_nondigit = 0;
+               int option = getopt_long(argc, argv, "cdt:gsf:r:n:v:b:u:h", long_options, NULL);
 
                if (option < 0)
                        break;
 
                switch (option) {
                case 0:
-                       switch (long_option_id) {
-                       case 0:
-                               if (!optarg)
-                                       goto missing_optarg;
-                               file_input_names[files_cnt++] = optarg;
-                               break;
-                       case 1:
-                               show_help(argv[0]);
-                               return 1;
-                       }
+                       file_input_names[files_cnt++] = optarg;
                        break;
-               case 'h':
-                       show_help(argv[0]);
-                       return 1;
                case 'd':
                        dump = -1;
                        break;
                case 't':
-                       if (!optarg)
-                               goto missing_optarg;
-                       if (!isdigit(optarg[0]))
-                               goto numeric_optarg;
-                       dump = atoi(optarg);
+                       dump = atoi_check_numeric(optarg, &err_arg_nondigit);
                        break;
                case 'c':
                        should_clear = 1;
@@ -843,25 +825,15 @@ int main(int argc, char ** argv)
                        should_getsize = 1;
                        break;
                case 'b':
-                       if (!optarg)
-                               goto missing_optarg;
                        buffer_names[buffer_cnt++] = optarg;
                        break;
                case 'u':
-                       if (!optarg)
-                               goto missing_optarg;
-                       if (!isdigit(optarg[0]))
-                               goto numeric_optarg;
-                       logs.size = atoi(optarg);
+                       logs.size = atoi_check_numeric(optarg, &err_arg_nondigit);
                        break;
                case 'f':
-                       if (!optarg)
-                               goto missing_optarg;
                        l_file.path = optarg;
                        break;
                case 'v':
-                       if (!optarg)
-                               goto missing_optarg;
                        log_set_print_format(l_file.format, log_format_from_string(optarg));
                        break;
                case 's':
@@ -869,21 +841,21 @@ int main(int argc, char ** argv)
                        log_add_filter_string(l_file.format, "*:s");
                        break;
                case 'r':
-                       if (!optarg)
-                               goto missing_optarg;
-                       if (!isdigit(optarg[0]))
-                               goto numeric_optarg;
-                       l_file.rotate_size_kbytes = atoi(optarg);
+                       l_file.rotate_size_kbytes = atoi_check_numeric(optarg, &err_arg_nondigit);
                        break;
                case 'n':
-                       if (!optarg)
-                               goto missing_optarg;
-                       if (!isdigit(optarg[0]))
-                               goto numeric_optarg;
-                       l_file.max_rotated = atoi(optarg);
+                       l_file.max_rotated = atoi_check_numeric(optarg, &err_arg_nondigit);
                        break;
+
+               case 'h':
+                       /* pass thru */
                default:
-                       printf("Error: invalid argument\n");
+                       show_help(argv[0]);
+                       return 1;
+               }
+
+               if (err_arg_nondigit) {
+                       printf("Error: -%c requires a numerical parameter\n", option);
                        return 1;
                }
        }
@@ -980,14 +952,6 @@ int main(int argc, char ** argv)
        handle_pipe(fdi_ptrs, fdi_cnt, dump, &logs, &l_file);
 
        return 0;
-
-missing_optarg:
-       printf("Error: -%c requires a parameter\n", option);
-       return 1;
-
-numeric_optarg:
-       printf("Error: -%c requires a numerical parameter\n", option);
-       return 1;
 }
 
 /**