add: help message

This commit is contained in:
2025-12-16 08:35:26 -06:00
parent bed729e8e1
commit 4466ac1c60
4 changed files with 31 additions and 2 deletions

View File

@@ -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:

View File

@@ -2,6 +2,7 @@
#include "icmp_error.h"
#include "interval.h"
#include "parsing.h"
#include "setting.h"
#include "statistics.h"
#include <arpa/inet.h>
@@ -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);
}
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include "parsing.h"
#include "setting.h"
#include "statistics.h"
#include <bits/types/struct_timeval.h>
@@ -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);
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);

View File

@@ -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;