aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-15 14:43:35 +0100
committerDominik Kaiser2025-01-15 14:43:35 +0100
commit2d2df595dcf01315d080aa1b4fcf810d28f2a3a4 (patch)
treead8114546873f8514f1ee453a966c71bf20064fc
parent7777515cfd76a1b39e8365749953350afc221b51 (diff)
downloadminishell-2d2df595dcf01315d080aa1b4fcf810d28f2a3a4.tar.gz
minishell-2d2df595dcf01315d080aa1b4fcf810d28f2a3a4.zip
Add some fixes to execute_cmd
-rw-r--r--include/minishell.h4
-rw-r--r--src/execute_cmd.c28
-rw-r--r--src/get_cmd_path.c62
3 files changed, 68 insertions, 26 deletions
diff --git a/include/minishell.h b/include/minishell.h
index c905ca5..505b21b 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2025/01/14 15:29:46 by chuhlig ### ########.fr */
+/* Updated: 2025/01/15 14:04:19 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,7 +39,7 @@ t_redirection *collect_redirs(t_token **tokens);
void print_ast(t_node *ast);
int eval(t_node *node, t_env **env);
-char *get_cmd_path(char *cmd, t_env *env);
+char *get_cmd_path(char *cmd, t_env *env, int *return_code);
int execute_cmd(t_cmd *cmd, t_env **env);
char *format_string(char *str, t_env *env);
int set_return_code(int return_code, t_env **env);
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 525f333..bad6cf5 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -6,20 +6,18 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 19:55:18 by chuhlig ### ########.fr */
+/* Updated: 2025/01/15 14:42:00 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
+#include <stdlib.h>
int is_builtin(char *cmd)
{
- return ((ft_strcmp(cmd, "export") == 0)
- || (ft_strcmp(cmd, "unset") == 0)
- || (ft_strcmp(cmd, "cd") == 0)
- || (ft_strcmp(cmd, "exit") == 0)
- || (ft_strcmp(cmd, "echo") == 0)
- || (ft_strcmp(cmd, "pwd") == 0)
+ return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0)
+ || (ft_strcmp(cmd, "cd") == 0) || (ft_strcmp(cmd, "exit") == 0)
+ || (ft_strcmp(cmd, "echo") == 0) || (ft_strcmp(cmd, "pwd") == 0)
|| (ft_strcmp(cmd, "env") == 0));
}
@@ -50,6 +48,7 @@ int execute_cmd(t_cmd *cmd, t_env **env)
int original_stdout;
int original_stdin;
int result;
+ int i;
original_stdout = dup(STDOUT_FILENO);
original_stdin = dup(STDIN_FILENO);
@@ -82,15 +81,14 @@ int execute_cmd(t_cmd *cmd, t_env **env)
}
if (pid == 0)
{
- cmd_path = get_cmd_path(cmd->args[0], *env);
+ i = 0;
+ while (cmd->args[i][0] == '\0')
+ i++;
+ cmd_path = get_cmd_path(cmd->args[i], *env, &result);
if (!cmd_path)
- {
- perror("command not found");
- exit(EXIT_FAILURE);
- }
- execve(cmd_path, cmd->args, env_to_strlst(*env));
- perror("execve");
- exit(EXIT_FAILURE);
+ exit(result);
+ execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env));
+ exit(EXIT_SUCCESS);
}
waitpid(pid, &status, 0);
dup2(original_stdout, STDOUT_FILENO);
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c
index 8a27584..56d9876 100644
--- a/src/get_cmd_path.c
+++ b/src/get_cmd_path.c
@@ -6,28 +6,53 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */
-/* Updated: 2024/12/17 19:20:08 by chuhlig ### ########.fr */
+/* Updated: 2025/01/15 14:33:58 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "minishell.h"
+#include <errno.h>
+#include <sys/errno.h>
+#include <sys/unistd.h>
+#include <unistd.h>
-static char *get_absolute_cmd_path(char *cmd, t_env *env);
-static char *find_in_path(char *cmd, t_env *env);
+static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code);
+static char *find_in_path(char *cmd, t_env *env, int *return_code);
char **get_split_path(t_env *env);
-char *get_cmd_path(char *cmd, t_env *env)
+char *get_cmd_path(char *cmd, t_env *env, int *return_code)
{
+ char *result;
+
if (cmd[0] == '/')
- return (ft_strdup(cmd));
+ {
+ result = ft_strdup(cmd);
+ if (access(result, F_OK) == -1)
+ {
+ free(result);
+ errno = ENOENT;
+ perror(cmd);
+ *return_code = 127;
+ return (NULL);
+ }
+ else if (access(result, X_OK) == -1)
+ {
+ free(result);
+ errno = EACCES;
+ perror(cmd);
+ *return_code = 126;
+ return (NULL);
+ }
+ return (result);
+ }
else if (ft_strchr(cmd, '/'))
- return (get_absolute_cmd_path(cmd, env));
+ return (get_absolute_cmd_path(cmd, env, return_code));
else
- return (find_in_path(cmd, env));
+ return (find_in_path(cmd, env, return_code));
}
-static char *get_absolute_cmd_path(char *cmd, t_env *env)
+static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code)
{
char *cur_dir;
char *result;
@@ -38,16 +63,32 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env)
result = ft_strjoin(cur_dir, cmd);
free(cur_dir);
if (!result)
+ {
+ *return_code = 127;
+ errno = ENOENT;
+ perror(cmd);
return (NULL);
+ }
+ if (access(result, F_OK) == -1)
+ {
+ free(result);
+ errno = ENOENT;
+ *return_code = 127;
+ perror(cmd);
+ return (NULL);
+ }
if (access(result, X_OK) == -1)
{
free(result);
+ errno = EACCES;
+ *return_code = 126;
+ perror(cmd);
return (NULL);
}
return (result);
}
-static char *find_in_path(char *cmd, t_env *env)
+static char *find_in_path(char *cmd, t_env *env, int *return_code)
{
char *cur_path;
char *cmd_path;
@@ -70,6 +111,9 @@ static char *find_in_path(char *cmd, t_env *env)
return (cmd_path);
path++;
}
+ *return_code = 127;
+ printf("%s:", cmd);
+ ft_putstr_fd(" command not found", 2);
return (NULL);
}