aboutsummaryrefslogtreecommitdiff
path: root/src/eval/eval.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval/eval.c')
-rw-r--r--src/eval/eval.c32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/eval/eval.c b/src/eval/eval.c
index d40c7f3..28cb386 100644
--- a/src/eval/eval.c
+++ b/src/eval/eval.c
@@ -18,19 +18,40 @@ static int eval_line(t_eval_state *state, t_line *line)
return (eval(state, line->right));
}
+static bool is_exec_path(char *path_str)
+{
+ return (ft_strncmp(path_str, "../", 3) == 0
+ || ft_strncmp(path_str, "./", 2) == 0
+ || ft_strncmp(path_str, "/", 1) == 0);
+}
+
+static bool is_valid_exec(char *exec_path)
+{
+ struct stat statbuf;
+
+ if (stat(exec_path, &statbuf) != 0)
+ return (false);
+ if (!S_ISREG(statbuf.st_mode)) // also need to manage link
+ return (false);
+ // could test permission but probably handled by execve
+ return (true);
+}
+
static char *search_exec_path(t_path path, char *path_var, char *exec_name)
{
- char *exec_path;
+ char *exec_path;
+ if (is_exec_path(exec_name))
+ return (exec_name);
// try current first
if ((exec_path = ft_htget(path, exec_name)) == NULL)
{
- if (path_update(path, path_var) == NULL)
+ if (path_update(path, path_var) == NULL) // optimise by not updating not changed path in ht
return (NULL);
if ((exec_path = ft_htget(path, exec_name)) == NULL)
return (NULL);
}
- return exec_path;
+ return (exec_path);
}
static int eval_cmd(t_eval_state *state, t_cmd *cmd)
@@ -38,7 +59,8 @@ static int eval_cmd(t_eval_state *state, t_cmd *cmd)
int child_pid;
char *exec_path;
- if ((exec_path = search_exec_path(state->path, ft_htget(state->env, "PATH"), cmd->argv[0])) == NULL)
+ if ((exec_path = search_exec_path(state->path,
+ ft_htget(state->env, "PATH"), cmd->argv[0])) == NULL)
return (-1);
if (cmd->in != NULL)
{
@@ -51,7 +73,6 @@ static int eval_cmd(t_eval_state *state, t_cmd *cmd)
(cmd->is_append ? O_WRONLY : O_APPEND) | O_CREAT)) < 0)
return (-1);
}
-
if ((child_pid = fork()) == -1)
return (-1);
if (child_pid == 0)
@@ -69,6 +90,7 @@ static int eval_cmd(t_eval_state *state, t_cmd *cmd)
int eval(t_eval_state *state, t_ast *ast)
{
+ errno = 0;
if (ast->tag == TAG_LINE)
return eval_line(state, &ast->data.line);
if (ast->tag == TAG_CMD)