]> git.dkaiser.de - 42/minishell.git/commitdiff
Add some fixes to execute_cmd
authorDominik Kaiser <dkaiser@3-H-6.42heilbronn.de>
Wed, 15 Jan 2025 13:43:35 +0000 (14:43 +0100)
committerDominik Kaiser <dkaiser@3-H-6.42heilbronn.de>
Wed, 15 Jan 2025 13:43:35 +0000 (14:43 +0100)
include/minishell.h
src/execute_cmd.c
src/get_cmd_path.c

index c905ca56b83f966c2ecea442fd8648e8e6519bba..505b21b99b30c7dffa463689c63b173557209f91 100644 (file)
@@ -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);
index 525f333b3e2aa122da2f2355ba8d4465559d680a..bad6cf5aa661457de60c3866d88be15b50db48a1 100644 (file)
@@ -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);
index 8a27584c0e7ae90775c2512a09bcab4c2de4079a..56d987674102f29c56e6b052cf00d4fdf1ae62d2 100644 (file)
@@ -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);
 }