diff --git a/includes/help.h b/includes/help.h index 3984fd5..67d0190 100644 --- a/includes/help.h +++ b/includes/help.h @@ -23,9 +23,7 @@ static options_t options[] = { {"size", 's', "send NUMBER data octets", 1, GRP}, {"timeout", 'w', "stop after N seconds", 1, GRP}, {"ttl", 't', "specify N as time-to-live", 1, GRP}, - {"ip-timestamp", 'i', - "IP timestamp of type FLAG, which is one of \"tsonly\" and \"tsaddr\"", 1, - GRP} + {"interval", 'i', "wait NUMBER seconds between sending each packet", 1, GRP} #undef GRP }; @@ -40,23 +38,39 @@ static options_t options[] = { #define OPT_HANDLE(i) \ if (options[i].arg_nb == 1) { \ if (!(av + 1) || !*(av + 1)) { \ + print_parse_err(NEED_ARG, (*av) + 1); \ return EX_USAGE; \ } \ - args->arg[i] = *(av + 1); \ + int n = atoi(*(av + 1)); \ + if (n == 0) { \ + print_parse_err(ERR_ARG, (*av) + 1); \ + return EX_USAGE; \ + } \ + args->arg[i] = n; \ } \ args->opts[i] = true; \ return EXIT_SUCCESS; -typedef enum { MISSING_HOST, INVALID_OPT, NEED_ARG } error_t; +typedef enum { MISSING_HOST, INVALID_OPT, NEED_ARG, ERR_ARG } error_t; /** - * @brief Can be used to print the help message if no host (or addr) are - * provided + * @brief Can be used to print error message when needed + * @param err The type of error to print + * @param opt The option that cause an error (if applicable) */ void print_parse_err(error_t err, char *opt); +/** + * @brief Can be used to print the help message + */ void print_help(void); +/** + * @brief Can be used to print the usage help message + */ void print_usage(void); +/** + * @brief Can be used to print the version message + */ void print_version(void); diff --git a/includes/opt_parse.h b/includes/opt_parse.h index 2b23dfd..d36267e 100644 --- a/includes/opt_parse.h +++ b/includes/opt_parse.h @@ -2,17 +2,18 @@ #include -#include #include +#include typedef struct { - bool opts[OPT_NB]; - char *arg[OPT_NB]; + bool opts[OPT_NB]; + int arg[OPT_NB]; char **hosts; } args_t; /** * @brief Can be used to parse the command line options * @param av The argv of the program + * @param args The struct of argument */ int opt_parse(char **av, args_t *args); diff --git a/includes/ping.h b/includes/ping.h index cd6f93e..5b3f83a 100644 --- a/includes/ping.h +++ b/includes/ping.h @@ -1,3 +1,20 @@ #pragma once +#include + extern char *exec_name; + +typedef struct { + bool verbose; + int count; + int size; + int timeout; + int ttl; + int interval; +} ping_t; + +#define DEFAULT_COUNT -1 +#define DEFAULT_VERBOSE false +#define DEFAULT_SIZE 0 +#define DEFAULT_INTERVAL 0 +#define DEFAULT_TIMEOUT 0 diff --git a/src/help.c b/src/help.c index 4793da4..a37623a 100644 --- a/src/help.c +++ b/src/help.c @@ -63,8 +63,13 @@ void print_parse_err(error_t err, char *opt) { printf("%s: invalid option -- %s\n", exec_name, opt); break; case NEED_ARG: - printf("%s: option '--%s' requires an argument\n", exec_name, opt); + printf("%s: option '%s' requires an argument\n", exec_name, opt); + break; + case ERR_ARG: + printf("%s: option '%s' invalid argument \n", exec_name, opt); + break; } + printf("Try 'ping --help' or 'ping --usage' for more information.\n"); } diff --git a/src/main.c b/src/main.c index b3e7aa9..24a8c08 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ char *exec_name; void init_args_t(args_t *args) { OPT_WHILE { args->opts[i] = false; - args->arg[i] = NULL; + args->arg[i] = 0; i++; } args->hosts = NULL; @@ -31,10 +31,8 @@ int handle_options(args_t *args, char **av) { print_help(); return EXIT_SUCCESS; } else { - for (int i = 0; args->hosts[i] != NULL; ++i) - printf("%s\n", args->hosts[i]); + return send_ping(args); } - return EXIT_SUCCESS; } int main(int ac, char **av) { diff --git a/src/send_ping.c b/src/send_ping.c new file mode 100644 index 0000000..f5a68c2 --- /dev/null +++ b/src/send_ping.c @@ -0,0 +1,5 @@ +#include + +int send_ping(args_t *args) { + +}