-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_execute.c
153 lines (136 loc) · 3.23 KB
/
mini_execute.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mini_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kblack <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/14 13:13:54 by kblack #+# #+# */
/* Updated: 2019/03/29 15:42:09 by kblack ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
** Dispatch table builtin commands followed by their corresponding functions
*/
struct s_dispatch builtins[BUILTIN_COUNT] = {
{"cd", handle_cd},
{"env", handle_env},
{"echo", handle_echo},
{"exit", handle_exit},
{"setenv", handle_setenv},
{"unsetenv", handle_unsetenv}
};
/*
** Updates environment variables array (/usr/bin/env)
*/
void free_and_execute(t_shell *sh, int i)
{
int j;
char **tmp;
char *join;
char *str;
t_env *list;
j = -1;
free_env(sh->arr);
list = sh->env_info;
tmp = (char **)malloc((i + 1) * sizeof(char *));
while (++j < i)
{
join = ft_strjoin(list->key, "=");
str = ft_strjoin(join, list->value);
tmp[j] = ft_strdup(str);
free(join);
free(str);
list = list->next;
}
tmp[j] = NULL;
sh->arr = tmp;
}
/*
** Starts a new process using the fork() & execve() system calls
*/
void launch(char *ex, char **arr, t_shell *sh)
{
int len;
int status;
pid_t childpid;
pid_t wait_ret;
childpid = fork();
if (childpid < 0)
{
ft_printf("ash: unable to fork process: %d\n", childpid);
exit(-1);
}
if (childpid == 0)
{
len = ft_this_list_size(sh->env_info);
free_and_execute(sh, len);
if (execve(ex, arr, sh->arr) == -1)
ft_printf("ash: permission denied: %s\n", ex);
exit(-1);
}
else
wait_ret = waitpid(childpid, &status, 0);
}
void join_path(char **p, char **arr, t_shell *sh)
{
int i;
char *tmp;
char *path;
i = -1;
while (p[++i])
{
tmp = ft_strjoin(p[i], "/");
path = ft_strjoin(tmp, arr[0]);
free(tmp);
if (access(path, F_OK) != -1)
{
launch(path, arr, sh);
free(path);
break ;
}
free(path);
}
check_error(p[i], arr[0]);
}
/*
** Finds the path of the program to run
*/
void find_path(char **arr, t_shell *sh)
{
t_env *list;
char **p;
p = NULL;
list = sh->env_info;
while (list)
{
if (ft_strcmp(list->key, "PATH") == 0)
p = ft_strsplit(list->value, ':');
if (list->next == NULL)
break ;
list = list->next;
}
if (!p)
return ;
join_path(p, arr, sh);
free_env(p);
}
/*
** Determines whether to launch a builtin or a process
*/
int execute(char **arr, t_shell *sh)
{
int i;
i = 0;
if (arr[0] == NULL)
return (1);
while (i < BUILTIN_COUNT)
{
if (ft_strcmp(arr[0], builtins[i].key) == 0)
return (builtins[i].fxnptr(arr, sh));
i++;
}
(access(arr[0], F_OK) != -1) ? launch(arr[0], arr, sh) : find_path(arr, sh);
return (1);
}