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.

57 lines
1.9 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/20 10:36:06 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
3 years ago
#include "philo.h"
3 years ago
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));
3 years ago
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]);
3 years ago
if (argv[4])
3 years ago
table->param[MEALS_AMOUNT] = mini_atoi(argv[4]);
3 years ago
else
3 years ago
table->param[MEALS_AMOUNT] = 2147483647;
table->time = gettime();
table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]);
3 years ago
return (table);
}
3 years ago
int gettime(void)
3 years ago
{
3 years ago
static struct timeval starttime = {0, 0};
3 years ago
struct timeval actualtime;
3 years ago
if (starttime.tv_sec == 0 && starttime.tv_usec == 0)
3 years ago
gettimeofday(&starttime, NULL);
3 years ago
gettimeofday(&actualtime, NULL);
3 years ago
return ((int)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)
3 years ago
- (starttime.tv_sec * 1000 + starttime.tv_usec / 1000)));
3 years ago
}