move functions
This commit is contained in:
@ -61,5 +61,9 @@ typedef struct s_icmp_echo
|
||||
} t_icmp_echo;
|
||||
|
||||
t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier);
|
||||
int add_host(t_settings *res, char *host);
|
||||
void free_hosts(t_host_list *list);
|
||||
t_settings parse_args(int argc, char **argv);
|
||||
void show_help(t_settings *set, char *name);
|
||||
|
||||
#endif
|
||||
|
42
srcs/main.c
Normal file
42
srcs/main.c
Normal file
@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ping.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
||||
/* Updated: 2025/04/26 17:08:50 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "includes/ft_ping.h"
|
||||
|
||||
void show_help(t_settings *set, char *name)
|
||||
{
|
||||
set->stop = 1;
|
||||
write(1, "Usage: ", 7);
|
||||
write(1, name, strlen(name));
|
||||
write(1, HELP_MESSAGE, sizeof(HELP_MESSAGE));
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_settings settings;
|
||||
|
||||
settings = parse_args(argc, argv);
|
||||
if(settings.stop || settings.err)
|
||||
{
|
||||
free_hosts(settings.hosts);
|
||||
return((settings.err != 0) * 64);
|
||||
}
|
||||
if(!settings.hosts)
|
||||
{
|
||||
fprintf(stderr, "%s: missing host operand\n", argv[0]);
|
||||
return(64);
|
||||
}
|
||||
send_pings(settings);
|
||||
free_hosts(settings.hosts);
|
||||
return(0);
|
||||
}
|
@ -1,25 +1,16 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ping.c :+: :+: :+: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
||||
/* Updated: 2025/04/25 23:25:43 by tomoron ### ########.fr */
|
||||
/* Created: 2025/04/26 17:01:53 by tomoron #+# #+# */
|
||||
/* Updated: 2025/04/26 17:08:01 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "includes/ft_ping.h"
|
||||
|
||||
void show_help(t_settings *set, char *name)
|
||||
{
|
||||
set->stop = 1;
|
||||
write(1, "Usage: ", 7);
|
||||
write(1, name, strlen(name));
|
||||
write(1, HELP_MESSAGE, sizeof(HELP_MESSAGE));
|
||||
}
|
||||
|
||||
char *get_value_in_arg(char *arg, int end)
|
||||
{
|
||||
char *res;
|
||||
@ -85,45 +76,6 @@ void parse_opt(int *i, t_settings *set, int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
int add_host(t_settings *res, char *host)
|
||||
{
|
||||
t_host_list *new;
|
||||
t_host_list *tmp;
|
||||
|
||||
new = malloc(sizeof(t_host_list));
|
||||
if(!new)
|
||||
return(0);
|
||||
new->host = host;
|
||||
new->next = 0;
|
||||
tmp = res->hosts;
|
||||
while(tmp && tmp->next)
|
||||
tmp = tmp->next;
|
||||
if(tmp)
|
||||
tmp->next = new;
|
||||
else
|
||||
res->hosts = new;
|
||||
return(1);
|
||||
}
|
||||
|
||||
void free_hosts(t_host_list *list)
|
||||
{
|
||||
t_host_list *tmp;
|
||||
|
||||
while(list)
|
||||
{
|
||||
printf("free : %s\n", list->host);
|
||||
tmp = list->next;
|
||||
free(list);
|
||||
list = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void init_defaults(t_settings *set)
|
||||
{
|
||||
set->count = -1;
|
||||
set->timeout = -1;
|
||||
}
|
||||
|
||||
int check_values(t_settings *set, char *name)
|
||||
{
|
||||
if(set->setTtl)
|
||||
@ -149,7 +101,8 @@ t_settings parse_args(int argc, char **argv)
|
||||
|
||||
i = 1;
|
||||
bzero(&res, sizeof(t_settings));
|
||||
init_defaults(&res);
|
||||
res.count = -1;
|
||||
res.timeout = -1;
|
||||
while(i < argc)
|
||||
{
|
||||
if(*argv[i] == '-')
|
||||
@ -168,21 +121,3 @@ t_settings parse_args(int argc, char **argv)
|
||||
return(res);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_settings settings;
|
||||
|
||||
settings = parse_args(argc, argv);
|
||||
if(settings.stop || settings.err)
|
||||
{
|
||||
free_hosts(settings.hosts);
|
||||
return((settings.err != 0) * 64);
|
||||
}
|
||||
if(!settings.hosts)
|
||||
{
|
||||
fprintf(stderr, "%s: missing host operand\n", argv[0]);
|
||||
return(64);
|
||||
}
|
||||
free_hosts(settings.hosts);
|
||||
return(0);
|
||||
}
|
45
srcs/utils.c
Normal file
45
srcs/utils.c
Normal file
@ -0,0 +1,45 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/26 17:03:11 by tomoron #+# #+# */
|
||||
/* Updated: 2025/04/26 17:08:08 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "includes/ft_ping.h"
|
||||
|
||||
int add_host(t_settings *res, char *host)
|
||||
{
|
||||
t_host_list *new;
|
||||
t_host_list *tmp;
|
||||
|
||||
new = malloc(sizeof(t_host_list));
|
||||
if(!new)
|
||||
return(0);
|
||||
new->host = host;
|
||||
new->next = 0;
|
||||
tmp = res->hosts;
|
||||
while(tmp && tmp->next)
|
||||
tmp = tmp->next;
|
||||
if(tmp)
|
||||
tmp->next = new;
|
||||
else
|
||||
res->hosts = new;
|
||||
return(1);
|
||||
}
|
||||
|
||||
void free_hosts(t_host_list *list)
|
||||
{
|
||||
t_host_list *tmp;
|
||||
|
||||
while(list)
|
||||
{
|
||||
printf("free : %s\n", list->host);
|
||||
tmp = list->next;
|
||||
free(list);
|
||||
list = tmp;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user