tgrekov-philosophers
HIVE philosophers July 2024
Loading...
Searching...
No Matches
main.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/07/22 04:21:41 by tgrekov #+# #+# */
9/* Updated: 2024/08/06 17:17:31 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "utils/utils.h"
14#include "philosopher/philo.h"
15
16int seat(t_opt opt);
17
18int main(int argc, char **argv)
19{
20 t_opt opt;
21
22 if (argc != 5 && argc != 6)
23 return (err("n_philos time_to_die time_to_eat"
24 " time_to_sleep [eat_count]\n", 1));
25 if (atoi_errable(argv[1], &opt.n) || opt.n < 1 || opt.n > 1000)
26 return (err("Number of philosophers must be between 1 and 1000\n", 1));
27 if (atoi_errable(argv[2], &opt.tt_die) || opt.tt_die < 1)
28 return (err("Time to die must be between 1 "
29 "and 2147483647\n", 1));
30 if (atoi_errable(argv[3], &opt.tt_eat) || opt.tt_eat < 1)
31 return (err("Time to eat must be between 1 "
32 "and 2147483647\n", 1));
33 if (atoi_errable(argv[4], &opt.tt_sleep) || opt.tt_sleep < 1)
34 return (err("Time to sleep must be between 1 "
35 "and 2147483647\n", 1));
36 opt.eat_n = 0;
37 if (argc == 6 && (atoi_errable(argv[5], &opt.eat_n) || opt.eat_n < 1))
38 return (err("Eat count must be between 1 and 2147483647\n", 1));
39 return (seat(opt));
40}
int atoi_errable(const char *str, int *n)
Converts ascii digits from str to a signed integer, output into n . Returns 0 on success,...
int err(const char *str, int retval)
Write str to stderr and return retval.
Definition err.c:29
int seat(t_opt opt)
Allocate for, initialize, seat, wait on (pun intended), and free all philos.
Definition seat.c:90
Definition philo.h:26