-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_check_first_arg.c
129 lines (119 loc) · 3.18 KB
/
ms_check_first_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
126
127
128
129
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_check_first_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: obeedril <obeedril@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 20:59:20 by obeedril #+# #+# */
/* Updated: 2022/03/13 15:06:54 by obeedril ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *add_slesh(char **arr_p, int i, int j)
{
char *str_slesh;
str_slesh = NULL;
ms_malloc_str(&str_slesh, ft_strlen(arr_p[i]) + 1);
while (arr_p[i][j] != '\0')
{
str_slesh[j] = arr_p[i][j];
j++;
}
str_slesh[j] = '/';
str_slesh[j + 1] = '\0';
return (str_slesh);
}
static int look_for_way(t_data *data, char **str, int i, int n)
{
char *str_slesh;
char *str_way;
int find_cmd;
find_cmd = 0;
str_way = NULL;
str_slesh = NULL;
while (str[i])
{
str_slesh = add_slesh(str, i, 0);
str_way = ft_strjoin(str_slesh, data->cmd[n].array_arg[0]);
ms_free_str(&str_slesh);
if (!access (str_way, 1))
{
find_cmd = -1;
data->cmd[n].way_cmd = ft_strdup(str_way);
ms_free_str(&str_way);
return (find_cmd);
}
else
find_cmd++;
ms_free_str(&str_way);
i++;
}
return (find_cmd);
}
static int check_str(t_data *data, char **arr_p, int i, int n)
{
int find_cmd;
char *str_slesh;
char *str_way;
find_cmd = 0;
str_way = NULL;
str_slesh = NULL;
if (arr_p == NULL)
return (find_cmd);
if (data->cmd[n].array_arg[0][0] == '.'
&& data->cmd[n].array_arg[0][1] == '/')
find_cmd = ms_check_way_itself(data, find_cmd, n);
else
{
data->cmd[n].way_cmd = NULL;
find_cmd = look_for_way(data, arr_p, i, n);
}
return (find_cmd);
}
static void check_find_cmd(t_data *data, int find_cmd, int n)
{
find_cmd = ms_way(data, find_cmd, n);
if (data->cmd[n].array_arg[0][0] == '\0')
find_cmd = 1;
if (find_cmd > 0)
{
data->num_error = ERR_CMD;
ms_print_errors_chfa(data->cmd[n].array_arg[0], 1);
}
if (find_cmd == -2)
{
data->num_error = ERR_FILE_OR_DIR;
ms_print_errors_chfa(data->cmd[n].array_arg[0], 2);
}
if (find_cmd == -3)
{
data->num_error = ERR_FILE_OR_DIR;
ms_print_errors_chfa(data->cmd[n].array_arg[0], 3);
}
if (find_cmd == 0)
{
data->num_error = ERR_FILE_OR_DIR;
ms_print_errors_chfa(data->cmd[n].array_arg[0], 4);
}
}
void ms_check_first_arg(t_data *data, int n)
{
char *path;
char **arr_p;
int i;
int find_cmd;
i = 0;
find_cmd = 0;
path = NULL;
arr_p = NULL;
if (data->num_error != 0 || data->empty_str == YES)
return ;
ms_search_var(data, &path, "PATH");
arr_p = ft_split(path, ':');
ms_free_str(&path);
find_cmd = check_str(data, arr_p, i, n);
check_find_cmd(data, find_cmd, n);
if (arr_p != NULL)
ms_free_arr(&arr_p);
}