diff --git a/headers/philo_proto.h b/headers/philo_proto.h index 20f068d..3e5bf23 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:27:13 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:29:49 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 17:50:11 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,10 @@ int diff_time(t_time *old, struct timeval *new); int print_message(t_philo *philo, char *clr, char *msg); // utils.c +int ft_isdigit_2d_arr(char **str); +size_t ft_strlen(char *str); +int ft_strncmp(char *s1, char *s2, size_t n); +int ft_int_overflow(char *str); int ft_atoi(const char *str); #endif diff --git a/srcs/init.c b/srcs/init.c index f1d3040..3b03b91 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ -/* Updated: 2022/01/26 16:29:38 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 17:55:34 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,22 +57,29 @@ static t_philo *init_chain_philo(t_params *params, t_global *global) static t_params *init_params(int ac, char **av) { t_params *params; + int i; - if (ac == 5 || ac == 6) - { - params = malloc(sizeof(t_params)); - if (!params) - return (NULL); - params->n_phi = ft_atoi(av[1]); - params->t_die = ft_atoi(av[2]); - params->t_eat = ft_atoi(av[3]); - params->t_slp = ft_atoi(av[4]); - params->n_eat = -1; - if (ac == 6) - params->n_eat = ft_atoi(av[5]); - } - else + if (ac < 5 || ac > 6) return (NULL); + if (!ft_isdigit_2d_arr(av + 1)) + return (NULL); + i = 0; + while (av[i]) + { + if (ft_int_overflow(av[i])) + return (NULL); + i++; + } + params = malloc(sizeof(t_params)); + if (!params) + return (NULL); + params->n_phi = ft_atoi(av[1]); + params->t_die = ft_atoi(av[2]); + params->t_eat = ft_atoi(av[3]); + params->t_slp = ft_atoi(av[4]); + params->n_eat = -1; + if (ac == 6) + params->n_eat = ft_atoi(av[5]); return (params); } diff --git a/srcs/utils.c b/srcs/utils.c index 48ef485..53546e5 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -1,17 +1,79 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* generic.c :+: :+: :+: */ +/* utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 17:50:31 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +int ft_isdigit_2d_arr(char **str) +{ + int i; + int j; + + i = 0; + while (str[i]) + { + j = 0; + while (str[i][j]) + { + if (str[i][j] < '0' || str[i][j] > '9') + return (0); + j++; + } + i++; + } + return (1); +} + +size_t ft_strlen(char *str) +{ + size_t i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int ft_strncmp(char *s1, char *s2, size_t n) +{ + size_t i; + int res; + + i = 0; + res = 0; + while (s1[i] && s1[i] == s2[i] && i < n - 1) + i++; + if (n != 0) + res = (unsigned char)s1[i] - (unsigned char)s2[i]; + return (res); +} + +int ft_int_overflow(char *str) +{ + size_t len; + char *int_xtrem; + + int_xtrem = "2147483647"; + if (str[0] == '-') + int_xtrem = "2147483648"; + if (str[0] == '+' || str[0] == '-') + str++; + len = ft_strlen(str); + if (len < 10) + return (0); + else if (len > 10 || ft_strncmp(str, int_xtrem, len) > 0) + return (1); + return (0); +} + int ft_atoi(const char *str) { int i;