move functions

This commit is contained in:
2025-04-26 19:53:55 +02:00
parent 621b9fc4ee
commit 4e344d945e
5 changed files with 100 additions and 72 deletions

View File

@ -5,8 +5,10 @@ FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result
SRCS_DIR = srcs SRCS_DIR = srcs
OBJS_DIR = .objs OBJS_DIR = .objs
SRCS = ft_ping.c\ SRCS = main.c\
icmp.c icmp.c\
parsing.c\
utils.c
OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o)) OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o))

View File

@ -61,5 +61,9 @@ typedef struct s_icmp_echo
} t_icmp_echo; } t_icmp_echo;
t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier); 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 #endif

42
srcs/main.c Normal file
View 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);
}

View File

@ -1,25 +1,16 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_ping.c :+: :+: :+: */ /* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */ /* Created: 2025/04/26 17:01:53 by tomoron #+# #+# */
/* Updated: 2025/04/25 23:25:43 by tomoron ### ########.fr */ /* Updated: 2025/04/26 17:08:01 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/ft_ping.h" #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 *get_value_in_arg(char *arg, int end)
{ {
char *res; 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) int check_values(t_settings *set, char *name)
{ {
if(set->setTtl) if(set->setTtl)
@ -149,7 +101,8 @@ t_settings parse_args(int argc, char **argv)
i = 1; i = 1;
bzero(&res, sizeof(t_settings)); bzero(&res, sizeof(t_settings));
init_defaults(&res); res.count = -1;
res.timeout = -1;
while(i < argc) while(i < argc)
{ {
if(*argv[i] == '-') if(*argv[i] == '-')
@ -168,21 +121,3 @@ t_settings parse_args(int argc, char **argv)
return(res); 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
View 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;
}
}