diff --git a/configure b/configure index 36e129e..5553136 100755 --- a/configure +++ b/configure @@ -512,7 +512,7 @@ generate_build_mk() { # Strict flags if [ "$ENABLE_STRICT" = "yes" ]; then - strict_flags="-Wpedantic -Wconversion -Wshadow -Wcast-align -Wstrict-prototypes" + strict_flags="-Wpedantic -Wconversion -Wshadow -Wcast-align -Wstrict-prototypes -Wvla" fi # libicmp-specific configuration diff --git a/includes/cli.h b/includes/cli.h index 7667cd4..76bdee2 100644 --- a/includes/cli.h +++ b/includes/cli.h @@ -32,8 +32,6 @@ typedef struct s_option_descriptor } t_option_descriptor; extern const t_option_descriptor g_options[]; -extern const size_t g_options_len; -extern const size_t g_opt_str_len; int cli_handle_count(const char *arg, t_ping_config *config); int cli_handle_flood(const char *arg, t_ping_config *config); diff --git a/includes/cli_opt.h b/includes/cli_opt.h index 5c8e856..a4bec58 100644 --- a/includes/cli_opt.h +++ b/includes/cli_opt.h @@ -32,7 +32,7 @@ no_argument, \ cli_handle_quiet, \ OPT_ARG_NONE, \ - "Quiet mode (only show summary)" \ + "Quiet mode (only show summary)" \ ) \ X( \ 'c', \ @@ -40,7 +40,7 @@ required_argument, \ cli_handle_count, \ OPT_ARG_UINT, \ - "Stop after sending N packets" \ + "Stop after sending N packets" \ ) \ X( \ 'i', \ @@ -48,7 +48,7 @@ required_argument, \ cli_handle_interval, \ OPT_ARG_SECONDS, \ - "Wait N seconds between packets" \ + "Wait N seconds between packets" \ ) \ X( \ 't', \ @@ -64,7 +64,7 @@ required_argument, \ cli_handle_size, \ OPT_ARG_BYTES, \ - "Packet size in bytes" \ + "Packet size in bytes" \ ) \ X( \ 'W', \ @@ -72,7 +72,7 @@ required_argument, \ cli_handle_timeout, \ OPT_ARG_SECONDS, \ - "Timeout for replies in seconds" \ + "Timeout for replies in seconds" \ ) \ X( \ 'f', \ @@ -83,12 +83,16 @@ "Flood mode" \ ) +#undef X +#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) + 1 +enum { CLI_OPT_LEN = (0 CLI_OPTIONS_LIST) }; + +#include #undef X #define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \ + (1 * (has_arg == no_argument) \ + (2 * (has_arg == required_argument) \ + (3 * (has_arg == optional_argument)))) - -#define CLI_OPT_STR_LEN (0 CLI_OPTIONS_LIST) +enum { CLI_OPT_STR_LEN = (0 CLI_OPTIONS_LIST) }; #endif diff --git a/src/cli/handlers/handle_help.c b/src/cli/handlers/handle_help.c index c5a3b1c..6409107 100644 --- a/src/cli/handlers/handle_help.c +++ b/src/cli/handlers/handle_help.c @@ -3,6 +3,7 @@ #include #include "cli.h" +#include "cli_opt.h" #include "ft_ping.h" /* Forward declarations */ @@ -19,7 +20,7 @@ cli_handle_help(const char *arg, t_ping_config *config) printf("Usage: ft_ping [options] \n\n"); printf("Options:\n"); - for (size_t i = 0; i < g_options_len; ++i) + for (size_t i = 0; i < CLI_OPT_LEN; ++i) { const t_option_descriptor *opt = &g_options[i]; const char *argstr = option_arg_type_to_str(opt->arg_type); diff --git a/src/cli/handlers/handler_map.c b/src/cli/handlers/handler_map.c index 1dff651..7faf992 100644 --- a/src/cli/handlers/handler_map.c +++ b/src/cli/handlers/handler_map.c @@ -13,14 +13,4 @@ const t_option_descriptor g_options[] = { {0, NULL, 0, NULL, OPT_ARG_NONE, NULL } }; - -const size_t g_options_len = ((sizeof(g_options) / sizeof(*g_options)) - 1); - -#undef X -#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \ - + (1 * (has_arg == no_argument) \ - + (2 * (has_arg == required_argument) \ - + (3 * (has_arg == optional_argument)))) - -const size_t g_opt_str_len = CLI_OPT_STR_LEN; #undef X diff --git a/src/cli/parse.c b/src/cli/parse.c index ad16060..f033c3f 100644 --- a/src/cli/parse.c +++ b/src/cli/parse.c @@ -3,6 +3,7 @@ #include #include "cli.h" +#include "cli_opt.h" #include "ft_ping.h" #include "ft_ping_const.h" @@ -17,13 +18,13 @@ static int handle_one_option(int opt, char *arg, t_ping_config *config); int cli_parse_arguments(int argc, char **argv, t_ping_config *config) { - struct option long_opts[g_options_len + 1]; - const char *opt_str = "hVvq:c:i:t:s:W:f"; + struct option long_opts[CLI_OPT_LEN + 1]; + const char *opt_str = "hVvqc:i:t:s:W:f"; int opt; int res; init_config(config); - build_long_options(long_opts, g_options_len, g_options); + build_long_options(long_opts, CLI_OPT_LEN, g_options); while (-1 != (opt = getopt_long(argc, argv, opt_str, long_opts, NULL))) { res = handle_one_option(opt, optarg, config); @@ -77,7 +78,7 @@ build_long_options(struct option *long_opts, size_t nb_opts, static const t_option_descriptor *find_option_handler(int opt) { - for (size_t i = 0; i < g_options_len; ++i) + for (size_t i = 0; i < CLI_OPT_LEN; ++i) { if (g_options[i].short_opt == opt) return &g_options[i];