-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins3.c
119 lines (109 loc) · 2.9 KB
/
builtins3.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 15:47:09 by bfrochot #+# #+# */
/* Updated: 2017/03/22 19:00:07 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
void isbin2(int *i, char ***split_path, t_env *env)
{
if ((*i = find_param(env->ev, "PATH")) == -1)
{
if ((*i = find_param(env->loc->ev, "PATH")) != -1)
*split_path = ft_strsplitquote(env->loc->ev[*i], ':', 0);
}
else
*split_path = ft_strsplitquote(env->ev[*i], ':', 0);
}
char *isbin(char *str)
{
int i;
char **split_path;
DIR *dir;
t_dirent *dirent;
split_path = NULL;
isbin2(&i, &split_path, env());
i = -1;
while (split_path && split_path[++i])
if ((dir = opendir(split_path[i])))
{
while ((dirent = readdir(dir)))
if (ft_strcmp(dirent->d_name, str) == 0)
{
str = ft_strjoin(split_path[i], "/");
str = ft_strjoinfree(str, dirent->d_name, 1);
closedir(dir);
free_double_array(split_path);
return (str);
}
closedir(dir);
}
free_double_array(split_path);
return (NULL);
}
void gthash(char *str)
{
int i;
int j;
int k;
if (!env()->hash)
{
env()->hash = palloc(sizeof(char *) * 2);
env()->hash[1] = 0;
env()->hash[0] = ft_strdup("hits\tcommand");
}
i = 0;
while (env()->hash[++i])
if (ft_strcmp(str, env()->hash[i] + 5) == 0)
{
free(str);
str = ft_itoa(ft_atoi(env()->hash[i]) + 1);
j = 3 - ft_strlen(str);
k = -1;
while (env()->hash[i][++j] && ++k + 1 && j != 4)
env()->hash[i][j] = str[k];
break ;
}
if (!env()->hash[i] && str)
add_str_to_dstr(&env()->hash, (str = ft_strjoinfree(" 1\t", str, 2)));
free(str);
}
void go_away(char *str)
{
int i;
i = 0;
while (env()->hash[++i])
if (ft_strcmp(str, env()->hash[i] + 5) == 0)
{
env()->hash[i][0] = ' ';
env()->hash[i][1] = ' ';
env()->hash[i][2] = ' ';
env()->hash[i][3] = '0';
return ;
}
ft_printf("hash: no such command: %s\n", str);
}
void builtin_hash(t_env *env, char **split)
{
int i;
if (!env->hash)
{
ft_printf("hash: no command\n");
return ;
}
i = -1;
if (split[1] == 0)
while (env->hash && env->hash[++i])
ft_printf("%s\n", env->hash[i]);
else
while (split[++i])
if (i == 0)
continue ;
else
go_away(split[i]);
}