|
|
@ -6,24 +6,24 @@ |
|
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
|
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ |
|
|
|
/* Updated: 2022/05/06 13:32:33 by narnaud ### ########.fr */ |
|
|
|
/* Updated: 2022/05/07 23:16:47 by narnaud@stude ### ########.fr */ |
|
|
|
/* */ |
|
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
|
#include "minishell.h" |
|
|
|
|
|
|
|
static void exe(t_datas *datas, t_command *cmd); |
|
|
|
static pid_t caller(t_datas *datas, t_command *cmd); |
|
|
|
static void exe(t_datas *datas, t_command *cmd); |
|
|
|
static pid_t caller(t_datas *datas, t_command *cmd); |
|
|
|
|
|
|
|
int is_builtin(char *cmd) |
|
|
|
int is_builtin(char *cmd) |
|
|
|
{ |
|
|
|
if (!ft_strncmp(cmd, "echo", 5) || |
|
|
|
!ft_strncmp(cmd, "pwd", 4) || |
|
|
|
!ft_strncmp(cmd, "cd", 3) || |
|
|
|
!ft_strncmp(cmd, "export", 7) || |
|
|
|
!ft_strncmp(cmd, "unset", 6) || |
|
|
|
!ft_strncmp(cmd, "env", 4) || |
|
|
|
!ft_strncmp(cmd, "exit", 5)) |
|
|
|
if (!ft_strncmp(cmd, "echo", 5) \ |
|
|
|
|| !ft_strncmp(cmd, "pwd", 4) \ |
|
|
|
|| !ft_strncmp(cmd, "cd", 3) \ |
|
|
|
|| !ft_strncmp(cmd, "export", 7) \ |
|
|
|
|| !ft_strncmp(cmd, "unset", 6) \ |
|
|
|
|| !ft_strncmp(cmd, "env", 4) \ |
|
|
|
|| !ft_strncmp(cmd, "exit", 5)) |
|
|
|
return (1); |
|
|
|
else |
|
|
|
return (0); |
|
|
@ -52,10 +52,10 @@ int builtin_call(t_datas *datas, t_command *cmd) |
|
|
|
int piper(t_datas *datas, t_command *cmd) |
|
|
|
{ |
|
|
|
int pip[2]; |
|
|
|
int status; |
|
|
|
int status; |
|
|
|
pid_t pid; |
|
|
|
|
|
|
|
if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) |
|
|
|
if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) |
|
|
|
{ |
|
|
|
pipe(pip); |
|
|
|
cmd->fd[1] = pip[1]; |
|
|
@ -90,8 +90,9 @@ static void exe(t_datas *datas, t_command *cmd) |
|
|
|
path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2; |
|
|
|
path = ft_calloc(path_length, sizeof(char)); |
|
|
|
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); |
|
|
|
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); |
|
|
|
ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); |
|
|
|
ft_memcpy(path + ft_strlen(*path_dirs), "/", 1); |
|
|
|
ft_memcpy(path + ft_strlen(*path_dirs) + 1, \ |
|
|
|
cmd->argv[0], ft_strlen(cmd->argv[0])); |
|
|
|
execve(path, cmd->argv, datas->envp); |
|
|
|
free(path); |
|
|
|
path_dirs++; |
|
|
@ -99,14 +100,14 @@ static void exe(t_datas *datas, t_command *cmd) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static pid_t caller(t_datas *datas, t_command *cmd) |
|
|
|
static pid_t caller(t_datas *datas, t_command *cmd) |
|
|
|
{ |
|
|
|
pid_t pid; |
|
|
|
|
|
|
|
pid = fork(); |
|
|
|
if (!pid) |
|
|
|
{ |
|
|
|
if (builtin_call(datas, cmd) == -1) |
|
|
|
if (builtin_call(datas, cmd) == -1) |
|
|
|
{ |
|
|
|
if (cmd->fd[0]) |
|
|
|
dup2(cmd->fd[0], STDIN_FILENO); |
|
|
@ -116,5 +117,5 @@ static pid_t caller(t_datas *datas, t_command *cmd) |
|
|
|
} |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |
|
|
|
return pid; |
|
|
|
return (pid); |
|
|
|
} |
|
|
|