-
Notifications
You must be signed in to change notification settings - Fork 0
/
__exec.c
49 lines (43 loc) · 973 Bytes
/
__exec.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
#include "builtins.h"
/**
* __exec - replace the running shell with a new program
* @info: arguments passed
* Return: int
*/
int __exec(info_t *info)
{
char *exe, **args = info->tokens + 1, **env = NULL;
if (!*args)
return ((info->status = EXIT_SUCCESS));
info->tokens = args;
args = arrdup(args);
if (_strchr(*args, '/') == -1)
{
free_list(&info->path);
info->path = str_to_list(get_dict_val(info->env, "PATH"), ':');
exe = search_path(info, info->path);
}
else
{
exe = _strdup(*args);
}
info->tokens -= 1;
if (access(exe, X_OK) == 0)
{
env = dict_to_env(info->env);
free_info(info);
execve(exe, args, env);
perrorl_default(*info->argv, info->lineno, "Not found",
*info->tokens, *args, NULL);
free(exe);
free_tokens(&args);
free_tokens(&env);
exit(127);
}
perrorl_default(*info->argv, info->lineno, "Permission denied",
*info->tokens, *args, NULL);
free(exe);
free_tokens(&args);
free_info(info);
exit(126);
}