-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·87 lines (79 loc) · 2.2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aollivie <aollivie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/03/09 17:37:41 by aollivie #+# #+# */
/* Updated: 2015/03/14 16:49:25 by aollivie ### ########.fr */
/* */
/* ************************************************************************** */
#include "sh1.h"
void sig(int sig)
{
sig = sig;
ft_putchar('\n');
ft_putstr("$>");
}
char **mainbis(char *line, char **env)
{
char **tmp;
if (!(line = ft_strtrim(line)))
return (env);
tmp = ft_strsplit(line, ' ');
if ((ft_strcmp(tmp[0], "unsetenv")) == 0 && env)
env = ft_unsetenv(env, tmp);
else if ((ft_strcmp(tmp[0], "setenv")) == 0)
env = ft_setenv(env, tmp);
else if ((ft_strcmp(tmp[0], "env")) == 0)
ft_env(env, tmp);
else if ((ft_strcmp(tmp[0], "exit")) == 0)
ft_exit(tmp);
else if ((ft_strcmp(tmp[0], "cd")) == 0)
env = ft_cd(tmp, env);
else if ((ft_strcmp(tmp[0], "cd") != 0))
{
if (env == NULL)
ft_putendl("env: No PATH");
else
ft_execve(tmp, env);
}
return (env);
}
static char **mainter(int ac, char **env, char *line)
{
char **tmp;
line = ft_strtrim(line);
tmp = ft_strsplit(line, ';');
ac = 0;
while (tmp[ac])
{
env = mainbis(tmp[ac], env);
ac++;
}
return (env);
}
int main(int ac, char **av, char **envs)
{
char *line;
int red;
char **env;
av = av;
env = ((envs[0]) ? ft_copy_tab(envs) : NULL);
signal(SIGINT, sig);
while (42)
{
ft_putstr("$>");
if ((red = get_next_line(0, &line)))
{
if ((ft_strlen(line)) > 0)
{
env = mainter(ac, env, line);
}
((red < 0) ? exit (0) : 0);
}
((red == 0) ? (ft_putchar('\n'), exit (0)) : 0);
}
return (0);
}