-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_utils1.c
More file actions
96 lines (88 loc) · 2.79 KB
/
Copy pathexecutor_utils1.c
File metadata and controls
96 lines (88 loc) · 2.79 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executor_utils1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yucchen <yucchen@student.42singapore.sg +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 16:47:15 by yucchen #+# #+# */
/* Updated: 2026/01/09 14:52:05 by yucchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
void ft_free_array(char **array)
{
int i;
i = 0;
if (!array)
return ;
while (array[i])
{
free(array[i]);
i++;
}
free(array);
}
void child_cleanup(t_cmd *cmd_head, t_env **env, char **envp)
{
if (envp)
ft_free_array(envp);
if (cmd_head)
free_cmd_list(cmd_head);
if (env && *env)
free_env_list(*env);
}
static void child_error(t_child *child, char *cmdname, char *msg, int code)
{
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd(cmdname, 2);
ft_putendl_fd(msg, 2);
child_cleanup(child->cmd_head, child->env, child->envp);
exit(code);
}
static void map_error(char *full_path, t_cmd *cmd, t_child *child)
{
execve(full_path, cmd->argv, child->envp);
free(full_path);
if (errno == ENOENT)
child_error(child, cmd->argv[0], ": No such file or directory", 127);
else if (errno == EISDIR)
child_error(child, cmd->argv[0], ": Is a directory", 126);
else if (errno == EACCES)
child_error(child, cmd->argv[0], ": Permission denied", 126);
else if (errno == ENOEXEC)
child_error(child, cmd->argv[0], ": Exec format error", 126);
else if (errno == ENOTDIR)
child_error(child, cmd->argv[0], ": Not a directory", 126);
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd(cmd->argv[0], 2);
ft_putstr_fd(": ", 2);
perror(NULL);
child_cleanup(child->cmd_head, child->env, child->envp);
exit(126);
}
void try_exec(t_cmd *cmd, t_child *child)
{
char *full_path;
struct stat st;
if (ft_strchr(cmd->argv[0], '/'))
{
full_path = ft_strdup(cmd->argv[0]);
if (!full_path)
{
perror("malloc");
child_cleanup(child->cmd_head, child->env, child->envp);
exit(EXIT_FAILURE);
}
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode))
{
free(full_path);
child_error(child, cmd->argv[0], ": Is a directory", 126);
}
}
else
full_path = search_path(cmd->argv[0], child->envp);
if (!full_path)
child_error(child, cmd->argv[0], ": command not found", 127);
map_error(full_path, cmd, child);
}