-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_check_arg.c
125 lines (114 loc) · 3.43 KB
/
ft_check_arg.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
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pbourdon <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/07/18 20:47:18 by pbourdon #+# #+# */
/* Updated: 2016/08/29 20:16:46 by pbourdon ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_boucle(char *arg)
{
int index;
int index2;
char *process;
process = malloc(sizeof(char) * ft_strlen(arg));
index2 = 0;
index = 0;
while (arg[index] == ' ' || arg[index] == '\t' || arg[index] == '\n'
|| arg[index] == '\r')
index++;
while (arg[index] != '\0' && arg[index] != '\t' && arg[index] != '\n'
&& arg[index] != ' ')
{
process[index2] = arg[index];
index2++;
index++;
}
process[index2] = '\0';
index2 = 0;
while (arg[index] == '\t' || arg[index] == ' ' || arg[index] == '\n'
|| arg[index] == '\n')
index++;
return (process);
}
char **ft_get_options(char *arg, t_dlist *list, int x, char **option)
{
size_t index;
option = ft_get_options1(arg, option);
index = ft_strlen(option[0]);
while (arg[index++] != '\0')
{
if (arg[index] == '-')
option = ft_get_options2(arg, option, &index, &x);
if (index < ft_strlen(arg) && arg[index] != '\0' && arg[index] != ' ')
{
if (home(list) != NULL)
option[x] = ft_memalloc(ft_strlen(arg) + ft_strlen(home(list)));
else
option[x] = ft_memalloc(ft_strlen(arg) + 1);
if (arg[index] == '~' && home(list) != NULL)
option[x] = ft_get_options3(home(list), arg, &index, option[x]);
else if (arg[index] == '~' && home(list) == NULL)
return (ft_error_home());
else
option[x] = ft_get_options4(&index, 0, option[x], arg);
x++;
}
}
option[x] = NULL;
return (option);
}
char *ft_generate_path(char *arg, t_dlist *list)
{
int compteur;
char *strjoin;
char *auto_path;
compteur = ft_get_total_path(list) + 1;
while (compteur > 0)
{
auto_path = ft_get_auto_path(compteur, list, 0, 0);
strjoin = ft_strjoin(auto_path, arg);
if (auto_path != NULL)
free(auto_path);
if (access(strjoin, X_OK) == 0)
return (strjoin);
compteur--;
free(strjoin);
}
ft_putstr("zsh: command not found (or minishell did not find the correct");
ft_putstr(" PATH. Check your PATH env variable)\n");
return (NULL);
}
int ft_check_arg2(char *arg, char **options, t_dlist *list, char *boucl)
{
char *tmp;
tmp = ft_give_path(arg);
ft_run_exe(tmp, options, list);
ft_free_tab(options);
free(boucl);
free(tmp);
return (1);
}
int ft_check_arg(char *arg, t_dlist *list, int index, char *boucle)
{
char **options;
options = NULL;
options = ft_get_options(arg, list, 1, options);
if (options == NULL)
return (1);
if (arg[index] == '/')
return (ft_check_arg2(arg, options, list, boucle));
else if (arg[index] != '\0')
return (ft_check_arg3(boucle, list, options));
else
{
ft_free_tab(options);
free(boucle);
return (0);
}
return (1);
}