This repository was archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_file.c
98 lines (89 loc) · 2.4 KB
/
exec_file.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
/*
** EPITECH PROJECT, 2019
** minishell.c
** File description:
** Minishell
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <unistd.h>
#include <errno.h>
#include "include/my.h"
#include "include/env.h"
#include "include/error.h"
char *my_cat(char *str1, char *str2);
int char_in_array(char c, char *str);
int minishell_execute4(int exit_code)
{
if (WIFSIGNALED(exit_code)) {
if (WTERMSIG(exit_code) == 11)
my_putstr_error("Segmentation fault\n");
if (WTERMSIG(exit_code) == 8)
my_putstr_error("Floating exception\n");
}
exit_code = 0;
return exit_code;
}
void minishell_exec_error(char **argv)
{
my_putstr_error(argv[0]);
my_putstr_error(": ");
my_putstr_error("Permission denied.\n");
}
int minishell_execute3(char **argv, char *final_path, char **env)
{
struct stat st;
int exit_code = 0;
if (stat(argv[0], &st) == 0 && char_in_array('/', argv[0])) {
if ((st.st_mode & S_IXUSR) && (st.st_mode & __S_IFREG)) {
pid_t childpid = fork();
if (childpid == 0)
exit_code = execve(final_path, argv, env);
else {
wait(&exit_code);
return minishell_execute4(exit_code);
}
} else {
minishell_exec_error(argv);
exit_code = 1;
}
} else
exit_code = 1;
return exit_code;
}
int minishell_execute2(char **argv, char *path, char **env)
{
int exit_code = 0;
char *newenviron[] = { NULL };
char *final_path;
errno = 0;
if (argv[0][0] == '/')
final_path = argv[0];
else {
final_path = my_cat(my_cat(path, "/"), argv[0]);
}
exit_code = minishell_execute3(argv, final_path, env);
return exit_code;
}
int minishell_execute(char **argv, char *path, char **env)
{
char **tmp = searsh_in_env(env, "PATH");
int continu;
char *str1 = my_strdup(argv[0]);
int exit_code = 0;
continu = minishell_execute2(argv, path, env);
if (errno == 2) {
for (int i = 0; tmp[i] != NULL && errno == 2; i++) {
errno = 0;
my_strcat(tmp[i], "/");
my_strcat(tmp[i], str1);
argv[0] = my_strdup(tmp[i]);
continu = minishell_execute2(argv, path, env);
}
}
if (errno != 0)
my_error_handle(NULL, argv[0], errno);
return continu;
}