From 8f1abd7940d9281e518a19457ee50e2c83871c74 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 11 Aug 2025 20:26:21 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20added=20the=20print=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/help.h | 23 +++++++++++++++++++++++ includes/opt_parse.h | 6 ++++-- src/help.c | 19 ++++++++++++++----- src/main.c | 4 +++- src/opt_parse.c | 20 ++++++++++++++------ 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/includes/help.h b/includes/help.h index 28c0eee..5d84dc2 100644 --- a/includes/help.h +++ b/includes/help.h @@ -1,5 +1,28 @@ #pragma once +typedef struct { + char *name; + char opt; + char *doc; +} options_t; + +static options_t options[] = { + { + "help", + '?', + "give this help list" + }, + { + "verbose", + 'v', + "verbose output" + } +}; + +#define OPT_NB 2 + +#define FT_PING_V 0.1 + /** * @brief Can be used to print the help message if no host (or addr) are * provided diff --git a/includes/opt_parse.h b/includes/opt_parse.h index 43de3b0..58201ab 100644 --- a/includes/opt_parse.h +++ b/includes/opt_parse.h @@ -1,11 +1,13 @@ #pragma once -typedef struct { +#include +typedef struct { + bool verbose; } args_t; /** * @brief Can be used to parse the command line options * @param av The argv of the program */ -int opt_parse(char **av, args_t *args); +int opt_parse(char **av, args_t *args); diff --git a/src/help.c b/src/help.c index d027343..9f4c829 100644 --- a/src/help.c +++ b/src/help.c @@ -1,13 +1,22 @@ #include +#include #include -struct options { - char opt; - char *desc; -}; +void print_help(void) { + uint16_t i = 0; -void print_help(void) {} + printf("Usage: ping [OPTION...] HOST ...\n"); + printf("Send ICMP ECHO_REQUEST packets to network hosts.\n\n"); + + while (i < OPT_NB) { + printf(" -%c, --%s:\t %s\n", options[i].opt, options[i].name, options[i].doc); + i++; + } + printf("\nMandatory or optional arguments to long options are also mandatory " + "or optional\nfor any corresponding short options.\n\n"); + printf("Report bugs to (pls don't).\n"); +} void print_no_host(char *av) { printf("%s: missing host operand\n", av); diff --git a/src/main.c b/src/main.c index 76494a0..d22a8b1 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,9 @@ int main(int ac, char **av) { args_t args; if (ac > 1) { int ret = opt_parse(av, &args); - if (ret != EXIT_SUCCESS) + if (ret == -1) + return EXIT_SUCCESS; + else if (ret != EXIT_SUCCESS) return ret; else return EXIT_SUCCESS; diff --git a/src/opt_parse.c b/src/opt_parse.c index ea88888..0cd1974 100644 --- a/src/opt_parse.c +++ b/src/opt_parse.c @@ -1,20 +1,29 @@ #include -#include #include +#include -#include #include +#include #include -int opt_parse(char **av, args_t *args) { +int opt_parse(char **av, args_t *args) { char *exec_name = *av; (void)args; while (*av != NULL) { char *a = *av; - + if (*a == '-') { - printf("omg an opt\n"); + switch ((char)*(a + 1)) { + case '-': + break; + case '?': + print_help(); + return -1; + case 'v': + printf("verboseeee"); + break; + }; } else { } @@ -28,6 +37,5 @@ int opt_parse(char **av, args_t *args) { return EXIT_SUCCESS; } - // 127.0.0.1 localhost : no error wtf EZ 42 parsing + ratio // -v 127.0.0.1