Repository navigation
Expand file tree
/
Copy pathexec_command.c
More file actions
107 lines (96 loc) · 2.47 KB
/
Copy pathexec_command.c
File metadata and controls
107 lines (96 loc) · 2.47 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
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hsennane <hsennane@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/12 05:16:01 by hsennane #+# #+# */
/* Updated: 2025/08/12 05:16:02 by hsennane ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *find_in_paths(char **paths, char *cmd)
{
char *full_path;
int i;
struct stat st;
i = 0;
while (paths[i])
{
full_path = str_concat_three(paths[i], "/", cmd);
if (full_path && stat(full_path, &st) == 0)
{
return (full_path);
}
i++;
}
return (NULL);
}
static char *search_path_in_env(char *cmd, t_env *env_list)
{
char **paths;
char *path_env;
char *found;
path_env = get_env_value("PATH", env_list);
if (!path_env)
return (NULL);
paths = ft_split(path_env, ':');
if (!paths)
return (NULL);
found = find_in_paths(paths, cmd);
if (found)
{
free_strs(paths);
return (found);
}
free_strs(paths);
return (NULL);
}
char *get_cmd_path(char *cmd, t_env *env_list)
{
char *dup;
struct stat st;
if (!cmd)
return (NULL);
if (ft_strchr(cmd, '/'))
{
if (stat(cmd, &st) == 0)
{
dup = ft_strdup(cmd);
return (dup);
}
return (NULL);
}
return (search_path_in_env(cmd, env_list));
}
static int child_process_exec(char **args, t_env **env)
{
char **envp;
envp = envlist_to_array(*env);
if (!envp)
{
ft_putstr_fd("minishell: failed to get environment\n", STDERR_FILENO);
exit(EXIT_FAILURE);
}
execve(args[0], args, envp);
ft_putstr_fd("minishell: execve failed\n", STDERR_FILENO);
exit(EXIT_FAILURE);
}
int execute_command(char **args, t_env **env)
{
pid_t pid;
int status;
pid = fork();
if (pid < 0)
{
ft_putstr_fd("minishell: fork failed\n", STDERR_FILENO);
return (-1);
}
if (pid == 0)
child_process_exec(args, env);
waitpid(pid, &status, 0);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
return (0);
}