-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_init.c
More file actions
66 lines (61 loc) · 1.61 KB
/
Copy pathstack_init.c
File metadata and controls
66 lines (61 loc) · 1.61 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yelallam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/20 17:52:18 by yelallam #+# #+# */
/* Updated: 2026/02/24 11:01:51 by yelallam ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int parsing(char *splitted_arg, t_stack **head)
{
long num;
if (!ft_isnum(splitted_arg))
return (0);
num = ft_atol(splitted_arg);
if (num > INT_MAX || num < INT_MIN)
return (0);
if (ft_check_dup(*head, (int)num))
return (0);
ft_lstadd_back(head, ft_lstnew((int)num));
return (1);
}
static void cleanup(t_stack **head, char **splitted_args)
{
if (head && *head)
ft_lstfree(*head);
if (splitted_args)
ft_free_args(splitted_args);
write(2, "Error\n", 6);
exit(1);
}
t_stack *stack_init(int argc, char **argv, t_stack **head)
{
char **splitted_args;
int i;
int j;
if (argc < 2)
return (NULL);
i = 1;
while (i < argc)
{
if (!argv[i] || argv[i][0] == '\0')
cleanup(head, NULL);
splitted_args = ft_split(argv[i], ' ');
if (!splitted_args || !splitted_args[0])
cleanup(head, splitted_args);
j = 0;
while (splitted_args[j])
{
if (parsing(splitted_args[j], head) == 0)
cleanup(head, splitted_args);
j++;
}
ft_free_args(splitted_args);
i++;
}
return (*head);
}