-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_error.c
92 lines (85 loc) · 1.98 KB
/
check_error.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alvutina <alvutina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 10:14:31 by alvutina #+# #+# */
/* Updated: 2024/06/12 10:26:35 by alvutina ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_number(int argc, char **argv)
{
int i;
int j;
i = 0;
while (i < argc - 1)
{
j = 0;
if (argv[i][j] == '-')
j++;
if (argv[i][j] == '\0' || !ft_isdigit(argv[i][j]))
return (-1);
while (argv[i][j])
{
if (ft_isdigit(argv[i][j]) == 0)
{
if (!(j == 0 && argv[i][j] == '-'))
return (-1);
}
j++;
}
i++;
}
return (1);
}
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}
int is_duplicate(int argc, char **argv)
{
int i;
int j;
if (argc == 2)
{
if (ft_atoi(argv[0]) == 2147483650)
return (-1);
}
i = 0;
while (i < argc)
{
j = i + 1;
while (j < argc - 1)
{
if (ft_atoi(argv[i]) == 2147483650 \
|| ft_atoi(argv[j]) == 2147483650)
return (-1);
if (ft_atoi(argv[i]) == ft_atoi(argv[j]))
return (-1);
j++;
}
i++;
}
return (0);
}
int check_error(int argc, char **argv)
{
if (argc < 2)
{
return (0);
}
if (is_number(argc, argv) == -1)
{
ft_putstr_fd(ERROR_MSG, 2);
return (ERR_INVALID_INPUT);
}
if (is_duplicate(argc, argv) == -1)
{
ft_putstr_fd(ERROR_MSG, 2);
return (ERR_DUPLICATE);
}
return (1);
}