-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_args.c
64 lines (60 loc) · 2.42 KB
/
handle_args.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 16:37:23 by ana-lda- #+# #+# */
/* Updated: 2024/10/28 14:28:17 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
/**
* @brief Checks if the input string contains only numeric characters.
*
* This function verifies that all characters in the input string
* are digits (0-9).
*
* @param argv The input string to check.
* @return 1 if the string contains non-numeric characters, 0 otherwise.
*/
int check_arg_content(char *arg)
{
int i;
i = 0;
while (arg[i] != '\0')
{
if (arg[i] < '0' || arg[i] > '9')
return (1);
i++;
}
return (0);
}
/**
* @brief Validates the command-line arguments provided to the program.
*
* This function checks each argument for validity, ensuring that
* they are positive integers within acceptable ranges. If any
* argument is invalid, an appropriate error message is written to
* standard error.
*
* @param argv Array of command-line arguments.
* @return 1 if any argument is invalid, 0 otherwise.
*/
int check_valid_args(char **argv)
{
if (ft_atoi(argv[1]) > PHILO_MAX || ft_atoi(argv[1]) <= 0
|| check_arg_content(argv[1]) == 1)
return (write(2, "Invalid philosophers number\n", 29), 1);
if (ft_atoi(argv[2]) <= 0 || check_arg_content(argv[2]) == 1)
return (write(2, "Invalid time to die\n", 21), 1);
if (ft_atoi(argv[3]) <= 0 || check_arg_content(argv[3]) == 1)
return (write(2, "Invalid time to eat\n", 21), 1);
if (ft_atoi(argv[4]) <= 0 || check_arg_content(argv[4]) == 1)
return (write(2, "Invalid time to sleep\n", 23), 1);
if (argv[5] && (ft_atoi(argv[5]) < 0 || check_arg_content(argv[5]) == 1))
return (write(2, "Invalid number of times each philosopher must eat\n",
51), 1);
return (0);
}