fix make file parsing can get arguments values

This commit is contained in:
2025-04-25 23:08:19 +02:00
parent 77668368ab
commit 149ab9e725
3 changed files with 126 additions and 24 deletions

View File

@ -12,13 +12,13 @@ OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o))
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(FLAGS) $^ -o $@
$(NAME): $(OBJS_DIR) $(OBJS)
$(CC) $(FLAGS) $(OBJS) -o $@
$(OBJS_DIR):
mkdir $(OBJS_DIR)
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c $(OBJS_DIR)
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c
$(CC) $(FLAGS) -c $< -o $@
clean:

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
/* Updated: 2025/04/25 19:29:48 by tomoron ### ########.fr */
/* Updated: 2025/04/25 23:05:43 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,23 +20,94 @@ void show_help(t_settings *set, char *name)
write(1, HELP_MESSAGE, sizeof(HELP_MESSAGE));
}
char *get_value_in_arg(char *arg, int end)
{
char *res;
res = strstr(arg, "=");
if(res - arg != end)
return (0);
if (!res)
return (0);
res++;
return(res);
}
int get_setting(int *i, int argc, char **argv, int end)
{
char *value;
char *tmp;
value = 0;
if(argv[*i][0] == '-' && argv[*i][1] == '-')
value = get_value_in_arg(argv[*i], end);
else if((*i) + 1 < argc)
value = argv[++(*i)];
if(!value)
{
fprintf(stderr, "%s: option '%s' requires an option\n", argv[0], argv[*i]);
return(0);
}
tmp = value;
while(*tmp)
{
if(!isdigit(*tmp))
{
fprintf(stderr, "%s: invalid value (`%s' near `%s')\n", argv[0], value, tmp);
return(0);
}
tmp++;
}
return(atoi(value));
}
void parse_opt(int *i, t_settings *set, int argc, char **argv)
{
(void)argc;
if(!strcmp(argv[*i], "-?") || !strcmp(argv[*i], "--help"))
show_help(set, *argv);
if(!strcmp(argv[*i], "-c") || !strcmp(argv[*i], "--count"))
get_setting(i, &set->count, argc, argv);
if(!strcmp(argv[*i], "-w") || !strcmp(argv[*i], "--timeout"))
get_setting(i, &set->timeout, argc, argv);
if(!strcmp(argv[*i], "-f") || !strcmp(argv[*i], "--flood"))
else if(!strcmp(argv[*i], "-c") || !strncmp(argv[*i], "--count", 7))
set->count = get_setting(i, argc, argv, 7);
else if(!strcmp(argv[*i], "-w") || !strncmp(argv[*i], "--timeout", 9))
set->timeout = get_setting(i, argc, argv, 9);
else if(!strncmp(argv[*i], "--ttl", 5))
set->ttl = get_setting(i, argc, argv, 5);
else if(!strcmp(argv[*i], "-f") || !strcmp(argv[*i], "--flood"))
set->flood = 1;
if(!strcmp(argv[*i], "-n") || !strcmp(argv[*i], "--numeric"))
else if(!strcmp(argv[*i], "-n") || !strcmp(argv[*i], "--numeric"))
set->no_resolve = 1;
else
{
fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[*i]);
set->err = 1;
}
(*i)++;
}
int add_host(t_settings *res, char *host)
{
t_host_list *new;
new = malloc(sizeof(t_host_list));
if(!new)
return(0);
new->host = host;
new->next = res->hosts;
res->hosts = new;
return(1);
}
void free_hosts(t_host_list *list)
{
t_host_list *tmp;
while(list)
{
tmp = list->next;
free(list);
list = tmp;
}
}
t_settings parse_args(int argc, char **argv)
{
int i;
@ -48,7 +119,14 @@ t_settings parse_args(int argc, char **argv)
{
if(*argv[i] == '-')
parse_opt(&i, &res, argc, argv);
else
{
if(!add_host(&res, argv[i]))
{
res.err = 1;
return(res);
}
}
}
return(res);
}
@ -59,7 +137,15 @@ int main(int argc, char **argv)
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);
}
return(0);
}

View File

@ -8,28 +8,44 @@
#include <unistd.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#define PACKET_DATA_LENGTH 40
#define HELP_MESSAGE " [OPTION...] HOST ...\n\
Send ICMP ECHO_REQUEST packets to network hosts.\n"
Send ICMP ECHO_REQUEST packets to network hosts.\n\
\n\
-c, --count=NUMBER stop after sending NUMBER packets\n\
-n, --numeric do not resolve host addresses\n\
--ttl=N specify N as time-to-live\n\
-w, --timeout=N stop after N seconds\n\
\n\
-f, --flood flood ping (root only)\n\
-?, --help give this help list\n"
typedef struct s_host_list
{
char *host;
struct s_host_list *next;
} t_host_list;
typedef struct s_settings
{
char *host;
int count; // -c (--count)
int timeout; // -w (--timeout)
short flood; // -f (--flood)
short no_resolve; //-n (--numeric)
int ttl; // --ttl
t_host_list *hosts;
int count; // -c (--count)
int timeout; // -w (--timeout)
short flood; // -f (--flood)
short no_resolve; //-n (--numeric)
int ttl; // --ttl
short stop;
short err;
short stop;
short err;
} t_settings;
} t_settings;
typedef struct s_icmp_echo
@ -41,7 +57,7 @@ typedef struct s_icmp_echo
uint16_t sequence;
struct timeval time;
uint8_t data[40];
} t_icmp_echo;
} t_icmp_echo;
t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier);