-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_history.c
81 lines (75 loc) · 2.08 KB
/
builtin_history.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_history.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 11:45:37 by mleclair #+# #+# */
/* Updated: 2017/05/12 17:30:40 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
void history3(char **split, t_env *env)
{
int i;
int j;
i = ft_atoi(split[1]);
j = 0;
while (env->history[j])
++j;
while (i-- > 0)
ft_printf("%s\n", env->history[j - i - 1]);
retvalue_into_loc(env, 0);
}
void history2(char **split, t_env *env)
{
char *tmp;
tmp = palloc(INPUT_SIZE);
*tmp = 0;
if (split[1][1] == 'c')
{
if (find_param(env->ev, "HOME") == -1)
{
free_double_array(env->history);
set_history();
return (free(tmp));
}
free_double_array(env->history);
ft_strcat(ft_strcat(tmp, env->ev[find_param(env->ev, "HOME")]
+ 5), "/.42shistory");
unlink(tmp);
set_history();
retvalue_into_loc(env, 0);
}
else
{
ft_putstr("Wrong argument, only -c accepted.\n");
retvalue_into_loc(env, 1);
}
free(tmp);
}
void history(t_env *env, char **split)
{
if (split[0] && split[1] && split[2])
error(-7, NULL, NULL);
else if (!(env->history))
error(-11, 0, 0);
else if (split[1])
{
if (split[1][0] == '-')
history2(split, env);
else if (split[1] && ft_isdigit(split[1][0]))
history3(split, env);
else
{
ft_putstr("Is not a number.\n");
retvalue_into_loc(env, 1);
}
}
else
{
print_split(env->history);
retvalue_into_loc(env, 0);
}
}