file perm error

This commit is contained in:
Loic Deridder
2025-02-13 15:41:12 +01:00
parent bee6feb644
commit fbcc5403f2
7 changed files with 94 additions and 38 deletions

View File

@@ -25,10 +25,11 @@ typedef struct s_ast_n t_ast_n;
int execute_command(t_ast_n *node); int execute_command(t_ast_n *node);
//EXEC_REDIR //EXEC_REDIR
void handle_file(t_ast_n *node, int check, int i); int handle_file(t_ast_n *node, int check, int i);
void handle_redir(t_ast_n *node); int handle_redir(t_ast_n *node);
void reset_redir(t_ast_n *node); void reset_redir(t_ast_n *node);
int err_fork_pline(int *pipes); int err_fork_pline(int *pipes);
void dup_redir(t_ast_n *node, int i);
//EXEC_BUILTIN //EXEC_BUILTIN
int is_builtin(char *str); int is_builtin(char *str);

View File

@@ -12,20 +12,25 @@
#include "../../includes/minishell.h" #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) int execute_command(t_ast_n *node)
{ {
if (!node || (node->state == _CMD && node->cmd == NULL)) if (!node || (node->state == _CMD && node->cmd == NULL))
return (0); return (0);
if (node->state == _CMD) if (node->state == _CMD)
handle_redir(node); if (handle_redir(node))
return (1);
if (node->state == _CMD) if (node->state == _CMD)
node->msh->ex_code = exec_scmd(node); node->msh->ex_code = exec_scmd(node);
else if (node->state == _AND) else if (node->state == _AND)
{ node->msh->ex_code = execute_and(node);
node->msh->ex_code = execute_command(node->left);
if (node->msh->ex_code == 0)
node->msh->ex_code = execute_command(node->right);
}
else if (node->state == _OR) else if (node->state == _OR)
{ {
node->msh->ex_code = execute_command(node->left); node->msh->ex_code = execute_command(node->left);

View File

@@ -71,7 +71,12 @@ int exec(t_ast_n *node)
close(node->msh->hist); close(node->msh->hist);
path = find_path(node->cmd, node->msh->env); path = find_path(node->cmd, node->msh->env);
if (!path) if (!path)
{
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); return_error(node->cmd, "command not found", 127, node);
}
if (access(path, X_OK) != 0) if (access(path, X_OK) != 0)
return_error(path, "Permission denied", 126, node); return_error(path, "Permission denied", 126, node);
if (execve(path, node->args, node->msh->env) == -1) if (execve(path, node->args, node->msh->env) == -1)

View File

@@ -44,7 +44,11 @@ void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds)
dup2(pipes[1], STDOUT_FILENO); dup2(pipes[1], STDOUT_FILENO);
close(pipes[0]); close(pipes[0]);
close(pipes[1]); close(pipes[1]);
handle_redir(pcmd); if (handle_redir(pcmd))
{
free_child(pcmd->msh);
exit(1);
}
if (pcmd->state == _CMD) if (pcmd->state == _CMD)
exec_pcmd(pcmd); exec_pcmd(pcmd);
else if (pcmd->state == _SUBSH) else if (pcmd->state == _SUBSH)

View File

@@ -6,13 +6,13 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 09:42:31 by lderidde #+# #+# */ /* 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" #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; 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); fd = open(node->files[i], O_WRONLY | O_CREAT | O_APPEND, 0666);
if (fd == -1) if (fd == -1)
{ {
perror("open"); ft_fprintf(2, "%s: %s\n",node->files[i], "Permission denied");
exit(1); return (1);
} }
if (check == 1) if (check == 1)
node->_stdin = fd; node->_stdin = fd;
else if (check == 2 || check == 3) else if (check == 2 || check == 3)
node->_stdout = fd; node->_stdout = fd;
return (0);
} }
static void save_stds(t_ast_n *node) 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 i;
int check;
i = -1; i = -1;
check = 0;
save_stds(node); save_stds(node);
while (node->state != _PLINE && node->redir[++i] && node->redir[i] != _NR) while (node->state != _PLINE && node->redir[++i] && node->redir[i] != _NR)
{ {
if (node->redir[i] == _RED_L) if (node->redir[i] == _RED_L)
handle_file(node, 1, i); check = handle_file(node, 1, i);
else if (node->redir[i] == _RED_R) 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) 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) else if (node->redir[i] == _RED_DL)
here_doc(node, i); here_doc(node, i);
if (node->redir[i] == _RED_L) if (check == 1)
{ return (1);
dup2(node->_stdin, STDIN_FILENO); dup_redir(node, i);
close(node->_stdin);
}
else if (node->redir[i] == _RED_R || node->redir[i] == _RED_DR)
{
dup2(node->_stdout, STDOUT_FILENO);
close(node->_stdout);
}
} }
return (0);
} }
void reset_redir(t_ast_n *node) void reset_redir(t_ast_n *node)

View 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);
}
}

View File

@@ -38,18 +38,23 @@ int exec_shcmd(t_ast_n *node)
} }
} }
int execute_shcommand(t_ast_n *node) static int execute_shand(t_ast_n *node)
{ {
if (node->state == _CMD)
handle_redir(node);
if (node->state == _CMD)
node->msh->ex_code = exec_shcmd(node);
else if (node->state == _AND)
{
node->msh->ex_code = execute_shcommand(node->left); node->msh->ex_code = execute_shcommand(node->left);
if (node->msh->ex_code == 0) if (node->msh->ex_code == 0)
node->msh->ex_code = execute_shcommand(node->right); node->msh->ex_code = execute_shcommand(node->right);
} return (node->msh->ex_code);
}
int execute_shcommand(t_ast_n *node)
{
if (node->state == _CMD)
if (handle_redir(node) == 1)
return (1);
if (node->state == _CMD)
node->msh->ex_code = exec_shcmd(node);
else if (node->state == _AND)
execute_shand(node);
else if (node->state == _OR) else if (node->state == _OR)
{ {
node->msh->ex_code = execute_shcommand(node->left); node->msh->ex_code = execute_shcommand(node->left);
@@ -65,6 +70,20 @@ int execute_shcommand(t_ast_n *node)
return (node->msh->ex_code); 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 exec_subsh(t_ast_n *node)
{ {
int status; int status;
@@ -74,10 +93,8 @@ int exec_subsh(t_ast_n *node)
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
handle_redir(node->parent); ret = in_subsh(node);
ret = execute_shcommand(node); exit (ret);
free_child(node->msh);
exit(ret);
} }
else if (pid > 0) else if (pid > 0)
{ {