42 school project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
2.0 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:57:42 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
static int mini_atoi(char *nbr)
{
int ret;
ret = 0;
while (*nbr >= '0' && *nbr <= '9')
{
ret = (*nbr - '0') + (10 * ret);
nbr++;
}
return (ret);
}
t_table *parsing(char **argv)
{
t_table *table;
table = malloc(sizeof(t_table));
//ft_bzero(table, 0, sizeof(t_table));
table->param[PHILO_AMOUNT] = mini_atoi(argv[0]);
table->param[DIE_TIME] = mini_atoi(argv[1]);
table->param[EAT_DURATION] = mini_atoi(argv[2]);
table->param[SLEEP_DURATION] = mini_atoi(argv[3]);
if (argv[4])
table->param[MEALS_AMOUNT] = mini_atoi(argv[4]);
else
table->param[MEALS_AMOUNT] = 2147483647;
table->time = gettime();
table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]);
//memset(table->philos, 0, sizeof(t_philo) * table->nb_of_philos);
return (table);
}
int gettime(void)
{
static struct timeval starttime = {0,0};
struct timeval actualtime;
if(starttime.tv_sec == 0 && starttime.tv_usec == 0)
gettimeofday(&starttime, NULL);
gettimeofday(&actualtime, NULL);
return ((int)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\
- (starttime.tv_sec * 1000 + starttime.tv_usec / 1000)));
}