-
Notifications
You must be signed in to change notification settings - Fork 0
/
argument_sanitization.c
80 lines (71 loc) · 1.97 KB
/
argument_sanitization.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* argument_sanitization.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadewumi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 12:00:37 by oadewumi #+# #+# */
/* Updated: 2024/05/22 20:10:19 by oadewumi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
// This function is ensuring the arguments received are thoroughly checked
// for duplicates, for int overflow and if only integers are parsed. If not,
// we print an error message.
// else, it returns a zero indicating success and no errors.
int sanitize_args(char **arg_arr)
{
int i;
i = 0;
ft_dupli_check(arg_arr);
while (arg_arr[i] != NULL)
{
ft_numeric_check(arg_arr[i]);
ft_overflow_check(arg_arr[i]);
i++;
}
return (0);
}
int ft_dupli_check(char **arg_arr)
{
int i;
int j;
i = 0;
while (arg_arr[i] != NULL)
{
j = i + 1;
while (arg_arr[j] != NULL)
{
if (ft_atol(arg_arr[i]) == ft_atol(arg_arr[j]))
ft_error();
j++;
}
i++;
}
return (0);
}
int ft_numeric_check(const char *arr_str)
{
int i;
i = 0;
if (arr_str[i] == '\0')
ft_error();
if (ft_strchr("+-", arr_str[i]) != NULL)
i++;
while (arr_str[i] != '\0')
{
if (arr_str[i] < '0' || arr_str[i] > '9')
ft_error();
i++;
}
return (0);
}
int ft_overflow_check(char *arr_str)
{
long long num;
num = ft_atol(arr_str);
if (num < INT_MIN || num > INT_MAX)
ft_error();
return (0);
}