feat(parsing/sources): adding the moving flag part

This commit is contained in:
Raphael 2026-03-30 14:03:45 +02:00
parent bf2e836447
commit c50154379c
No known key found for this signature in database

View file

@ -6,140 +6,40 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/23 14:49:36 by rparodi #+# #+# */ /* Created: 2026/03/23 14:49:36 by rparodi #+# #+# */
/* Updated: 2026/03/28 15:43:45 by rparodi ### ########.fr */ /* Updated: 2026/03/30 12:22:07 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parsing.h" #include "parsing.h"
#include "macro.h" #include "macro.h"
#include <bits/getopt_core.h> #include "struct.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <errno.h>
const t_args _flags[] = { static const char *_find_target(int argc, char **argv) {
{'?', "help", NULL, "print help and exit", true}, const char *target = NULL;
{'W', "linger","<timeout>,", "time to wait for response", false},
{'c', "count","<count>", "stop after <count> replies", false},
{'f', "flood",NULL, "flood ping", false},
{'l', "preload","<preload>,", "send <preload> number of packages while waiting replies", false},
{'n', "numeric",NULL, "no reverse DNS name resolution", false},
{'p', "pattern","<pattern>,", "contents of padding byte", false},
{'q', "quiet", NULL, "reply wait <deadline> in seconds", false},
{'v', "verbose",NULL, "verbose output", true},
{'w', "timeout","<deadline>,", "reply wait <deadline> in seconds", false},
{0, "usage", NULL, "give a short usage message", false},
{0, NULL, NULL,NULL, true},
};
static struct option *_t_args_to_getopt_long(const size_t size) { for (int i = optind; i < argc; i++) {
struct option *long_options = (struct option *)calloc((size + 1), sizeof(struct option)); if (argv[i][0] != '-') {
if (!long_options) { target = argv[i];
return NULL; }
} }
for (size_t i = 0; i < size; i++) { if (target == NULL || target[0] == '\0') {
long_options[i].name = _flags[i].long_option; dprintf(2, "%s: missing host operand \n Try 'ping --help' or 'ping --usage' for more information.", argv[0]);
long_options[i].val = _flags[i].short_option; exit(64);
if (_flags[i].usage == NULL) {
long_options[i].has_arg = no_argument;
} else {
long_options[i].has_arg = required_argument;
}
long_options[i].flag = NULL;
} }
long_options[size] = (struct option){0}; return (target);
return long_options;
} }
void parsing_args(int argc, char **argv, t_flags *flags) void parsing_args(int argc, char **argv, t_flags *flags)
{ {
if (argc == 1) { if (argc == 1) {
dprintf(2, "%s: missing host operand \n Try 'ping --help' or 'ping --usage' for more information.", argv[0]); dprintf(2, "%s: missing host operand \n Try 'ping --help' or 'ping --usage' for more information.", argv[0]);
exit(64); exit(64);
} }
const size_t size = args_size();
size_t j = 0;
char args[2 * size + 1];
bzero(args, sizeof(args));
for (size_t i = 0; i < size; i++) { check_flags(argc, argv, flags);
if (_flags[i].is_mandatory != false || BONUS == 1) { check_target(_find_target(argc, argv));
args[j++] = _flags[i].short_option; SUCCESS_LOG("PARSING finished with success");
if (_flags[i].usage != NULL)
args[j++] = ':';
}
}
DEBUG_LOG(args);
struct option *options = _t_args_to_getopt_long(size);
if (!options) {
ERROR_LOG(strerror(ENOMEM));
exit(ENOMEM);
}
opterr = 1;
int opt;
while ((opt = getopt_long(argc, argv, args, options, NULL)) != -1) {
switch (opt) {
case 'v':
INFO_LOG("argument verbose");
flags->verbose = true;
break;
case 'f':
INFO_LOG("argument flood");
flags->flood = true;
break;
case 'n':
INFO_LOG("argument no-reverse");
flags->reverse_dns = true;
break;
case 'c':
INFO_LOG("argument count");
flags->count = check_num_arguments(optarg);
break;
case 'q':
INFO_LOG("argument quiet");
flags->quiet = true;
break;
case 'w':
INFO_LOG("argument wait");
flags->wait = check_num_arguments(optarg);
break;
case 'W':
INFO_LOG("argument timeout");
flags->timeout = check_num_arguments(optarg);
break;
case '?':
free(options);
if (optopt != 0) {
if (isprint(optopt)) {
char invalid_option[2] = {optopt, 0};
WARN_LOG(invalid_option);
}
dprintf(2, "Try 'ping --help' or 'ping --usage' for more information.\n");
exit(64);
}
print_help(size);
exit(EXIT_SUCCESS);
default:
if (strcmp(options[optind - 1].name, "usage"))
print_usage(size);
else {
if (isprint(optopt)) {
char invalid_option[2] = {optopt, 0};
WARN_LOG(invalid_option);
}
dprintf(2, "Try 'ping --help' or 'ping --usage' for more information.\n");
exit(64);
}
}
}
free(options);
} }