-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
86 lines (79 loc) · 1.72 KB
/
main.c
File metadata and controls
86 lines (79 loc) · 1.72 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "header.h"
int get_args(char ***args, int ac, char **av)
{
int i;
i = 0;
while (av[i] && !issep(av[i]) && !ispipe(av[i]))
i++;
if (!(*args = malloc(sizeof(char *) * (i + 1))))
return (1);
i = 0;
while (av[i] && !issep(av[i]) && !ispipe(av[i]))
{
(*args)[i] = av[i];
i++;
}
(*args)[i] = NULL;
return (0);
}
int get_lst(t_lst **lst, int ac, char **av)
{
char **args;
if (ac > 1)
{
av++;
while (*av)
{
if (ispipe(*av))
{
add_cmd(lst, PIPE, NULL);
av++;
}
else if (issep(*av))
av++;
else
{
if (get_args(&args, ac, av))
return (-1);
if (ft_strlen(args[0]) == 2 && !strncmp(args[0], "cd", 2))
add_cmd(lst, CD, args);
else
add_cmd(lst, BIN, args);
while (*av && !issep(*av) && !ispipe(*av))
av++;
}
}
}
return (0);
}
int launch_cmd(t_lst *lst)
{
if (lst->type == CD)
return (run_cd(lst->args));
else if (lst->type == BIN)
return (run_bin(lst));
return (0);
}
int main(int ac, char **av)
{
t_lst *lst;
if (get_lst(&lst, ac, av) == -1)
{
ft_putstr("error: fatal\n");
return (1);
}
while (lst)
{
if (launch_cmd(lst) == -1)
{
ft_putstr("error: fatal\n");
return (1);
}
if (lst->args)
free(lst->args);
free(lst);
lst = lst->next;
}
system("leaks a.out");
return (0);
}