-
Notifications
You must be signed in to change notification settings - Fork 0
/
1check_args.c
executable file
·80 lines (73 loc) · 1.8 KB
/
1check_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 1check_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: skarunat <skarunat@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/30 11:27:46 by skarunat #+# #+# */
/* Updated: 2023/08/05 19:13:45 by skarunat ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int ft_present(int num, char **argv, int i)
{
i++;
while (argv[i])
{
if (ft_atoi(argv[i]) == num)
return (1);
i++;
}
return (0);
}
static int ft_is_num(char *num)
{
int i;
i = 0;
if (num[0] == '-')
i++;
while (num[i])
{
if (!ft_isdigit(num[i]))
return (0);
i++;
}
return (1);
}
void ft_error_1(char *msg, int argc, char **args)
{
ft_putendl_fd(msg, 1);
if (argc == 2)
{
ft_free(args);
}
exit(0);
}
void ft_args_check(int argc, char **argv)
{
int i;
long new;
char **args;
i = 0;
if (argc == 2)
args = ft_split(argv[1], ' ');
else
{
i = 1;
args = argv;
}
while (args[i])
{
new = ft_atoi(args[i]);
if (!ft_is_num(args[i]))
ft_error_1("Error\n", argc, args);
if (ft_present(new, args, i))
ft_error_1("Error\n", argc, args);
if (new < -2147483648 || new > 2147483647)
ft_error_1("Error\n", argc, args);
i++;
}
if (argc == 2)
ft_free(args);
}