-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharguments.c
95 lines (88 loc) · 2.35 KB
/
arguments.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arguments.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rtavabil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 13:46:28 by rtavabil #+# #+# */
/* Updated: 2024/03/21 18:32:58 by rtavabil ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
static int check_numerics(char *s)
{
if (!(*s == '+' || *s == '-' || (*s >= '0' && *s <= '9')))
return (1);
if ((*s == '+' || *s == '-' ) && (!(*(s + 1) >= '0' && *(s + 1) <= '9')))
return (1);
if (*s == '+' || *s == '-')
s++;
while (*s)
{
if (*s >= '0' && *s <= '9')
s++;
else
return (1);
}
return (0);
}
long int ft_atol(char *str)
{
long int nb;
nb = 0;
while (*str == ' ' || (9 <= *str && *str <= 13))
{
str++;
}
if (*str == '+' || *str == '-')
{
if (*str == '-')
{
write(2, "Parameter can not be negative\n", 31);
exit(1);
}
str++;
}
while (47 < *str && *str < 58)
{
nb = nb * 10 + (*str - 48);
str++;
}
return (nb);
}
static int check_args(char *argv)
{
long int check;
if ((check_numerics(argv)) || (ft_strlen(argv) == 0))
{
write(2, "Enter numeric parameters\n", 26);
exit(1);
}
check = ft_atol(argv);
if ((check > INT_MAX) || (ft_strlen(argv) > 10))
{
write(2, "Parameter is not in INT limits\n", 32);
exit(1);
}
return (check);
}
void set_args(char **argv, t_args *args)
{
args->num_phil = check_args(argv[1]);
args->t_die = check_args(argv[2]) * 1000;
args->t_eat = check_args(argv[3]) * 1000;
args->t_sleep = check_args(argv[4]) * 1000;
if (argv[5])
args->must_eat = check_args(argv[5]);
else
args->must_eat = -1;
if ((args->t_die < 60000) || (args->t_eat < 60000) \
|| (args->t_sleep < 60000))
{
write(2, \
"Use numbers more than 60 or less than 2147483\n", \
47);
exit(1);
}
}