From 4466ac1c60d317ede05bff9865ffc425c8b4746c Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 16 Dec 2025 08:35:26 -0600 Subject: [PATCH] add: help message --- src/main.c | 7 ++++++- src/print.c | 15 +++++++++++++++ src/print.h | 4 +++- src/setting.c | 7 +++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index a580056..64ee345 100644 --- a/src/main.c +++ b/src/main.c @@ -93,7 +93,11 @@ int main(int ac, char **av) struct statistics stats; struct setting settings; - if (get_setting(av + 1, &settings)) + int ret = get_setting(av + 1, &settings); + + if (ret == -1) + goto finish; + if (ret) goto error0; size_t packet_size = sizeof(struct icmphdr) + settings.payload_size; @@ -168,6 +172,7 @@ int main(int ac, char **av) print_statistics(&stats, &settings); +finish: return 0; error3: diff --git a/src/print.c b/src/print.c index a811917..ddaa1e6 100644 --- a/src/print.c +++ b/src/print.c @@ -2,6 +2,7 @@ #include "icmp_error.h" #include "interval.h" +#include "parsing.h" #include "setting.h" #include "statistics.h" #include @@ -83,3 +84,17 @@ void print_recv(const struct setting *settings, const char *packet, const struct printf("\n"); } +void print_help(struct param *parameters) +{ + printf("PING [OPTION...] HOST...\n"); + for (size_t i = 0; parameters[i].name || parameters[i].alias; i++) + { + if (parameters[i].alias) + printf("-%s", parameters[i].alias); + printf("\t"); + if (parameters[i].name) + printf("--%s", parameters[i].name); + printf("\t"); + printf("%s\n", parameters[i].desc); + } +} \ No newline at end of file diff --git a/src/print.h b/src/print.h index fd431b6..7f41c72 100644 --- a/src/print.h +++ b/src/print.h @@ -1,5 +1,6 @@ #pragma once +#include "parsing.h" #include "setting.h" #include "statistics.h" #include @@ -9,4 +10,5 @@ void print_err(const char *format, ...); void print_header(struct setting const *settings); void print_statistics(struct statistics const *stats, struct setting const *settings); -void print_recv(const struct setting *settings, const char *packet, const struct timeval *start, const struct timeval *stop, struct sockaddr_in const *sender); \ No newline at end of file +void print_recv(const struct setting *settings, const char *packet, const struct timeval *start, const struct timeval *stop, struct sockaddr_in const *sender); +void print_help(struct param *parameters); \ No newline at end of file diff --git a/src/setting.c b/src/setting.c index ba6a013..ff6c950 100644 --- a/src/setting.c +++ b/src/setting.c @@ -47,6 +47,13 @@ int get_setting(char * const *av, struct setting *settings) {NULL, NULL, 0, NULL, NULL}, }; char *hostname = parsing(av, parameters); + + if (parameters[0].value) + { + print_help(parameters); + return -1; + } + if (hostname == NULL) return 1; settings->dest.hostname = hostname;