/
main.c
65 lines (59 loc) · 1.76 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsanzey <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/06 14:45:56 by tsanzey #+# #+# */
/* Updated: 2016/08/13 11:44:43 by tsanzey ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdio.h>
void free_array(char **argv)
{
int i;
char **tmp;
i = 0;
tmp = argv;
while (argv[i])
{
free(argv[i]);
i++;
}
free(tmp);
}
void init_head(t_shell *head)
{
head->sa = NULL;
head->sb = NULL;
head->max = INT_MIN;
}
int main(int argc, char **argv)
{
t_shell *head;
int i;
i = argc == 2 ? 0 : 1;
if (argc == 2 && string_is_valid(argv[1]))
argv = ft_strsplit(argv[1], ' ');
if (argc == 1 || !(head = (t_shell*)malloc(sizeof(t_shell))))
return (0);
init_head(head);
while (argv[i])
{
if (check_digit(argv[i]) == -1)
return (flush(head->sa, 0));
head->sa = push_back_stack2(head->sa, ft_atoi(argv[i]));
head->max = ft_atoi(argv[i]) > head->max ? ft_atoi(argv[i]) : head->max;
i++;
}
head->len = l_len(head->sa);
which_swap(head);
free_lst(head);
if (argc == 2)
free_array(argv);
if (head)
free(head);
return (0);
}