🏗️」 wip: work in progress, not done yet.

This commit is contained in:
2025-08-12 17:57:15 +02:00
parent bc57fe2cc2
commit f71753ffa8
6 changed files with 55 additions and 15 deletions

View File

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

View File

@ -2,17 +2,18 @@
#include <help.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct {
bool opts[OPT_NB];
char *arg[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);

View File

@ -1,3 +1,20 @@
#pragma once
#include <stdbool.h>
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

View File

@ -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");
}

View File

@ -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) {

5
src/send_ping.c Normal file
View File

@ -0,0 +1,5 @@
#include <opt_parse.h>
int send_ping(args_t *args) {
}