diff options
Diffstat (limited to 'common/common.c')
| -rw-r--r-- | common/common.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/common/common.c b/common/common.c new file mode 100644 index 0000000..be35e69 --- /dev/null +++ b/common/common.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/02/09 01:36:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +t_bool parse_args(t_philo_args *philo_args, int argc, char **argv) +{ + if (philo_args == NULL) + return (FALSE); + if (argc != 5 && argc != 6) + return (FALSE); + if ((philo_args->philo_num = h_strtoposint(argv[1])) == -1 || + (philo_args->timeout_death = h_strtoposint(argv[2])) == -1 || + (philo_args->timeout_eat = h_strtoposint(argv[3])) == -1 || + (philo_args->timeout_sleep = h_strtoposint(argv[4])) == -1) + return (FALSE); + if (argc == 6) + { + if ((philo_args->meal_num = h_strtoposint(argv[5])) == -1) + return (FALSE); + } + else + philo_args->meal_num = -1; + return (TRUE); +} + +void philo_put_state_change(t_philo *philo, t_philo_event event) // not correct for philo3 +{ + struct timeval tv; + + if (gettimeofday(&tv, NULL) == -1) + return ; + h_putnbr(tv.tv_sec); + h_putnbr(tv.tv_usec / 1000); + h_putchar(' '); + h_putnbr(philo->id); + if (event == EVENT_FORK) + h_putstr(" has taken fork\n"); + else if (event == EVENT_EATING) + h_putstr(" is eating\n"); + else if (event == EVENT_SLEEPING) + h_putstr(" is sleeping\n"); + else if (event == EVENT_THINKING) + h_putstr(" is thinking\n"); + else if (event == EVENT_DIED) + h_putstr(" died\n"); +} |
