-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_command.c
123 lines (113 loc) · 2.89 KB
/
check_command.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: auspensk <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 10:38:44 by auspensk #+# #+# */
/* Updated: 2024/10/15 11:57:56 by auspensk ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int path_not_found(t_cmd *cmd, t_data *data)
{
if (cmd->cmd_check == CMDNF)
{
write(2, "Command \'", ft_strlen("Command \'"));
write(2, cmd->cmd, ft_strlen(cmd->cmd));
write(2, "\' not found\n", ft_strlen("\' not found\n"));
data->st_code = 127;
}
if (cmd->cmd_check == ISDIR)
{
write(2, cmd->cmd, ft_strlen(cmd->cmd));
write(2, ": Is a directory\n", ft_strlen(": Is a directory\n"));
data->st_code = 126;
}
if (cmd->cmd_check == NSCHFL)
{
write(2, cmd->cmd, ft_strlen(cmd->cmd));
write(2, ": No such file or directory\n",
ft_strlen(": No such file or directory\n"));
data->st_code = 127;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(data->std_in);
exit (clean_exit(NULL, data->st_code, data));
}
int parce_paths(t_data *data, t_cmd *cmd)
{
int i;
i = 0;
while (data->envp[i] && ft_strncmp(data->envp[i], "PATH=", 5))
i++;
if (!data->envp[i])
{
cmd->cmd_check = NSCHFL;
data->paths = NULL;
return (1);
}
data->paths = ft_split(data->envp[i] + 5, ':');
return (0);
}
int check_path(t_cmd *cmd)
{
DIR *dir;
if (access(cmd->cmd, F_OK))
cmd->cmd_check = NSCHFL;
else
{
dir = opendir(cmd->cmd);
if (dir)
{
closedir(dir);
cmd->cmd_check = ISDIR;
}
else
cmd->args[0] = ft_strdup(ft_strrchr(cmd->cmd, '/') + 1);
}
return (0);
}
int find_binary(t_data *data, t_cmd *cmd)
{
char *path;
char *buf;
int i;
if (parce_paths(data, cmd))
return (1);
i = 0;
while (data->paths[i])
{
buf = ft_strjoin(data->paths[i], "/");
path = ft_strjoin(buf, cmd->cmd);
free(buf);
if (access(path, F_OK) == 0)
{
cmd->args[0] = ft_strdup(cmd->cmd);
free(cmd->cmd);
cmd->cmd = NULL;
cmd->cmd = path;
return (0);
}
free(path);
i++;
}
cmd->cmd_check = CMDNF;
return (1);
}
int check_command(t_cmd *cmd, t_data *data)
{
int i;
if (ft_strchr(cmd->cmd, '/'))
return (check_path(cmd));
if (*cmd->cmd == '\0')
{
cmd->cmd_check = CMDNF;
return (1);
}
i = find_binary(data, cmd);
data->paths = free_paths(data->paths);
return (i);
}