file perm error
This commit is contained in:
@@ -25,10 +25,11 @@ typedef struct s_ast_n t_ast_n;
|
||||
int execute_command(t_ast_n *node);
|
||||
|
||||
//EXEC_REDIR
|
||||
void handle_file(t_ast_n *node, int check, int i);
|
||||
void handle_redir(t_ast_n *node);
|
||||
int handle_file(t_ast_n *node, int check, int i);
|
||||
int handle_redir(t_ast_n *node);
|
||||
void reset_redir(t_ast_n *node);
|
||||
int err_fork_pline(int *pipes);
|
||||
void dup_redir(t_ast_n *node, int i);
|
||||
|
||||
//EXEC_BUILTIN
|
||||
int is_builtin(char *str);
|
||||
|
||||
@@ -12,20 +12,25 @@
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
static int execute_and(t_ast_n *node)
|
||||
{
|
||||
node->msh->ex_code = execute_command(node->left);
|
||||
if (node->msh->ex_code == 0)
|
||||
node->msh->ex_code = execute_command(node->right);
|
||||
return (node->msh->ex_code);
|
||||
}
|
||||
|
||||
int execute_command(t_ast_n *node)
|
||||
{
|
||||
if (!node || (node->state == _CMD && node->cmd == NULL))
|
||||
return (0);
|
||||
if (node->state == _CMD)
|
||||
handle_redir(node);
|
||||
if (handle_redir(node))
|
||||
return (1);
|
||||
if (node->state == _CMD)
|
||||
node->msh->ex_code = exec_scmd(node);
|
||||
else if (node->state == _AND)
|
||||
{
|
||||
node->msh->ex_code = execute_command(node->left);
|
||||
if (node->msh->ex_code == 0)
|
||||
node->msh->ex_code = execute_command(node->right);
|
||||
}
|
||||
node->msh->ex_code = execute_and(node);
|
||||
else if (node->state == _OR)
|
||||
{
|
||||
node->msh->ex_code = execute_command(node->left);
|
||||
|
||||
@@ -71,7 +71,12 @@ int exec(t_ast_n *node)
|
||||
close(node->msh->hist);
|
||||
path = find_path(node->cmd, node->msh->env);
|
||||
if (!path)
|
||||
return_error(node->cmd, "command not found", 127, node);
|
||||
{
|
||||
if (!access(node->cmd, F_OK) && access(node->cmd, X_OK))
|
||||
return_error(node->cmd, "Permission denied", 126, node);
|
||||
else
|
||||
return_error(node->cmd, "command not found", 127, node);
|
||||
}
|
||||
if (access(path, X_OK) != 0)
|
||||
return_error(path, "Permission denied", 126, node);
|
||||
if (execve(path, node->args, node->msh->env) == -1)
|
||||
|
||||
@@ -44,7 +44,11 @@ void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds)
|
||||
dup2(pipes[1], STDOUT_FILENO);
|
||||
close(pipes[0]);
|
||||
close(pipes[1]);
|
||||
handle_redir(pcmd);
|
||||
if (handle_redir(pcmd))
|
||||
{
|
||||
free_child(pcmd->msh);
|
||||
exit(1);
|
||||
}
|
||||
if (pcmd->state == _CMD)
|
||||
exec_pcmd(pcmd);
|
||||
else if (pcmd->state == _SUBSH)
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/12 09:42:31 by lderidde #+# #+# */
|
||||
/* Updated: 2025/02/12 13:30:49 by lderidde ### ########.fr */
|
||||
/* Updated: 2025/02/13 15:16:21 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void handle_file(t_ast_n *node, int check, int i)
|
||||
int handle_file(t_ast_n *node, int check, int i)
|
||||
{
|
||||
int fd;
|
||||
|
||||
@@ -24,13 +24,14 @@ void handle_file(t_ast_n *node, int check, int i)
|
||||
fd = open(node->files[i], O_WRONLY | O_CREAT | O_APPEND, 0666);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
exit(1);
|
||||
ft_fprintf(2, "%s: %s\n",node->files[i], "Permission denied");
|
||||
return (1);
|
||||
}
|
||||
if (check == 1)
|
||||
node->_stdin = fd;
|
||||
else if (check == 2 || check == 3)
|
||||
node->_stdout = fd;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void save_stds(t_ast_n *node)
|
||||
@@ -42,33 +43,29 @@ static void save_stds(t_ast_n *node)
|
||||
}
|
||||
}
|
||||
|
||||
void handle_redir(t_ast_n *node)
|
||||
int handle_redir(t_ast_n *node)
|
||||
{
|
||||
int i;
|
||||
int check;
|
||||
|
||||
i = -1;
|
||||
check = 0;
|
||||
save_stds(node);
|
||||
while (node->state != _PLINE && node->redir[++i] && node->redir[i] != _NR)
|
||||
{
|
||||
if (node->redir[i] == _RED_L)
|
||||
handle_file(node, 1, i);
|
||||
check = handle_file(node, 1, i);
|
||||
else if (node->redir[i] == _RED_R)
|
||||
handle_file(node, 2, i);
|
||||
check = handle_file(node, 2, i);
|
||||
else if (node->redir[i] == _RED_DR)
|
||||
handle_file(node, 3, i);
|
||||
check = handle_file(node, 3, i);
|
||||
else if (node->redir[i] == _RED_DL)
|
||||
here_doc(node, i);
|
||||
if (node->redir[i] == _RED_L)
|
||||
{
|
||||
dup2(node->_stdin, STDIN_FILENO);
|
||||
close(node->_stdin);
|
||||
}
|
||||
else if (node->redir[i] == _RED_R || node->redir[i] == _RED_DR)
|
||||
{
|
||||
dup2(node->_stdout, STDOUT_FILENO);
|
||||
close(node->_stdout);
|
||||
}
|
||||
if (check == 1)
|
||||
return (1);
|
||||
dup_redir(node, i);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void reset_redir(t_ast_n *node)
|
||||
|
||||
27
srcs/execution/exec_redir_utils.c
Normal file
27
srcs/execution/exec_redir_utils.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec_redir_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/13 15:13:52 by lderidde #+# #+# */
|
||||
/* Updated: 2025/02/13 15:13:52 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void dup_redir(t_ast_n *node, int i)
|
||||
{
|
||||
if (node->redir[i] == _RED_L)
|
||||
{
|
||||
dup2(node->_stdin, STDIN_FILENO);
|
||||
close(node->_stdin);
|
||||
}
|
||||
else if (node->redir[i] == _RED_R || node->redir[i] == _RED_DR)
|
||||
{
|
||||
dup2(node->_stdout, STDOUT_FILENO);
|
||||
close(node->_stdout);
|
||||
}
|
||||
}
|
||||
@@ -38,18 +38,23 @@ int exec_shcmd(t_ast_n *node)
|
||||
}
|
||||
}
|
||||
|
||||
static int execute_shand(t_ast_n *node)
|
||||
{
|
||||
node->msh->ex_code = execute_shcommand(node->left);
|
||||
if (node->msh->ex_code == 0)
|
||||
node->msh->ex_code = execute_shcommand(node->right);
|
||||
return (node->msh->ex_code);
|
||||
}
|
||||
|
||||
int execute_shcommand(t_ast_n *node)
|
||||
{
|
||||
if (node->state == _CMD)
|
||||
handle_redir(node);
|
||||
if (handle_redir(node) == 1)
|
||||
return (1);
|
||||
if (node->state == _CMD)
|
||||
node->msh->ex_code = exec_shcmd(node);
|
||||
else if (node->state == _AND)
|
||||
{
|
||||
node->msh->ex_code = execute_shcommand(node->left);
|
||||
if (node->msh->ex_code == 0)
|
||||
node->msh->ex_code = execute_shcommand(node->right);
|
||||
}
|
||||
execute_shand(node);
|
||||
else if (node->state == _OR)
|
||||
{
|
||||
node->msh->ex_code = execute_shcommand(node->left);
|
||||
@@ -65,6 +70,20 @@ int execute_shcommand(t_ast_n *node)
|
||||
return (node->msh->ex_code);
|
||||
}
|
||||
|
||||
static int in_subsh(t_ast_n *node)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (handle_redir(node->parent) == 1)
|
||||
{
|
||||
free_child(node->msh);
|
||||
return (1);
|
||||
}
|
||||
ret = execute_shcommand(node);
|
||||
free_child(node->msh);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int exec_subsh(t_ast_n *node)
|
||||
{
|
||||
int status;
|
||||
@@ -74,10 +93,8 @@ int exec_subsh(t_ast_n *node)
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
handle_redir(node->parent);
|
||||
ret = execute_shcommand(node);
|
||||
free_child(node->msh);
|
||||
exit(ret);
|
||||
ret = in_subsh(node);
|
||||
exit (ret);
|
||||
}
|
||||
else if (pid > 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user