From: Christopher Uhlig Date: Wed, 22 Jan 2025 15:50:05 +0000 (+0100) Subject: uiuiuiui X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=ad5eae00584c6c4a88e873b85645c10c47cd7bf8;p=42%2Fminishell.git uiuiuiui --- diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 67ab8f8..d40cee4 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/21 20:19:48 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 16:08:25 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/env_tools.c b/src/env_tools.c index f1c3748..b2e7873 100644 --- a/src/env_tools.c +++ b/src/env_tools.c @@ -6,16 +6,16 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/15 16:14:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 00:01:03 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 14:13:02 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -// char **get_split_path(t_env *env) -// { -// char *path; +char **get_split_path(t_env *env) +{ + char *path; -// path = env_get(env, "PATH"); -// return (ft_split(path, ':')); -// } + path = env_get(env, "PATH"); + return (ft_split(path, ':')); +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 012391f..2179515 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/21 23:52:38 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 16:29:34 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,22 @@ static void establish_pipeline(int original_stdin, int original_stdout); static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result); +int invalid_input(char *cmd) +{ + ft_putstr_fd("minishell: ", STDERR_FILENO); + ft_putstr_fd(cmd, STDERR_FILENO); + ft_putstr_fd(": command not found\n", STDERR_FILENO); + return (127); +} + int is_builtin(char *cmd) { - return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0) + if ((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)); + || (ft_strcmp(cmd, "env") == 0)) + return (1); + return(invalid_input(cmd)); } int execute_builtin(char **args, t_env **env) @@ -96,7 +106,7 @@ static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result) cmd_path = get_cmd_path(cmd->args[i], *env, &result); if (cmd_path != NULL) execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env)); - free(cmd_path); + // free(cmd_path); exit(result); } waitpid(pid, &status, 0); diff --git a/src/format_string.c b/src/format_string.c index 775a3da..ccc7084 100644 --- a/src/format_string.c +++ b/src/format_string.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */ -/* Updated: 2025/01/21 23:25:20 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 14:31:29 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,8 @@ static void append_slice(char **dst, char *src, int start, int end) if (*dst != NULL) free(*dst); *dst = result; + // free(src); + // src = *dst; } static void append_var(char **dst, char *src, int *pos, t_env *env) diff --git a/src/free_node.c b/src/free_node.c index d866727..733fe1c 100644 --- a/src/free_node.c +++ b/src/free_node.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:41:46 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 01:53:34 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 16:00:36 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,84 +28,84 @@ static void free_file(void *arg); // panic(UNREACHABLE); // free(node); // } -void free_redirections(t_redirection redirs[2]) -{ - for (int i = 0; i < 2; i++) - { - if (redirs[i].specifier) - free(redirs[i].specifier); - } -} -void free_node(t_node *node) -{ - if (!node) - return; +// static void free_pipe_node(t_node *node) +// { +// free_node(node->content.pipe.left); +// free_node(node->content.pipe.right); +// } + +// static void free_cmd_node(t_node *node) +// { +// int i; + +// i = 0; +// while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL) +// { +// free(node->content.cmd.args[i]); +// i++; +// } +// free(node->content.cmd.args); +// if (node->content.cmd.redirs[0].type != 0 +// && node->content.cmd.redirs[0].specifier != NULL) +// free(node->content.cmd.redirs[0].specifier); +// if (node->content.cmd.redirs[1].type != 0 +// && node->content.cmd.redirs[0].specifier != NULL) +// free(node->content.cmd.redirs[1].specifier); +// if (node->content.cmd.create_files != NULL) +// ft_lstclear(&node->content.cmd.create_files, free_file); +// } + +// static void free_file(void *arg) +// { +// t_redirection *file; +// file = (t_redirection *)arg; +// free(file->specifier); +// free(file); +// } + +void free_node(t_node *node) +{ if (node->type == PIPE_NODE) - { - free_node(node->content.pipe.left); - free_node(node->content.pipe.right); - } + free_pipe_node(node); else if (node->type == CMD_NODE) - { - if (node->content.cmd.args) - { - for (int i = 0; node->content.cmd.args[i]; i++) - free(node->content.cmd.args[i]); - free(node->content.cmd.args); - } - free_redirections(node->content.cmd.redirs); - // Assuming create_files is a list of dynamically allocated strings - t_list *current = node->content.cmd.create_files; - t_list *next; - while (current) - { - next = current->next; - free(current->content); - free(current); - current = next; - } - } + free_cmd_node(node); else if (node->type == STRING_NODE) - { free(node->content.string); - } free(node); } -static void free_pipe_node(t_node *node) +static void free_pipe_node(t_node *node) { - free_node(node->content.pipe.left); - free_node(node->content.pipe.right); + free_node(node->content.pipe.left); + free_node(node->content.pipe.right); } -static void free_cmd_node(t_node *node) +static void free_cmd_node(t_node *node) { - int i; + int i; - i = 0; - while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL) - { - free(node->content.cmd.args[i]); - i++; - } - free(node->content.cmd.args); - if (node->content.cmd.redirs[0].type != 0 - && node->content.cmd.redirs[0].specifier != NULL) - free(node->content.cmd.redirs[0].specifier); - if (node->content.cmd.redirs[1].type != 0 - && node->content.cmd.redirs[0].specifier != NULL) - free(node->content.cmd.redirs[1].specifier); - if (node->content.cmd.create_files != NULL) - ft_lstclear(&node->content.cmd.create_files, free_file); + i = 0; + while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL) + { + free(node->content.cmd.args[i]); + i++; + } + free(node->content.cmd.args); + if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL) + free(node->content.cmd.redirs[0].specifier); + if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[1].specifier != NULL) + free(node->content.cmd.redirs[1].specifier); + if (node->content.cmd.create_files != NULL) + ft_lstclear(&node->content.cmd.create_files, free_file); } -static void free_file(void *arg) +static void free_file(void *arg) { - t_redirection *file; + t_redirection *file; - file = (t_redirection *)arg; - free(file->specifier); - free(file); -} + file = (t_redirection *)arg; + free(file->specifier); + free(file); +} \ No newline at end of file diff --git a/src/free_token.c b/src/free_token.c index 64278a8..66ab1dd 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 00:07:58 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 15:56:38 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,11 @@ void free_token(t_token *token) token->previous->next = NULL; if (token->next != NULL) token->next->previous = NULL; - // if (token->type == STRING_TOKEN && token->content.string != NULL) - // free(token->content.string); // Ensure content is freed + // if (token->previous == NULL && token->next == NULL) + // { + // if (token->type == STRING_TOKEN && token->content.string != NULL) + // free(token->content.string); // Ensure content is freed + // } free(token);//maybe free token token = NULL; } @@ -35,23 +38,23 @@ void free_token_and_connect(t_token *token) token = NULL; } -// void free_tokens(t_token *tokens) -// { -// while (tokens->next != NULL) -// { -// tokens = tokens->next; -// free_token(tokens->previous); -// } -// free_token(tokens); -// } -void free_tokens(t_token *tokens) +void free_tokens(t_token *tokens) { - t_token *tmp; - - while (tokens) - { - tmp = tokens; - tokens = tokens->next; - free_token(tmp); // Ensure each token is freed - } + while (tokens->next != NULL) + { + tokens = tokens->next; + free_token(tokens->previous); + } + free_token(tokens); } +// void free_tokens(t_token *tokens) +// { +// t_token *tmp; + +// while (tokens) +// { +// tmp = tokens; +// tokens = tokens->next; +// free_token(tmp); // Ensure each token is freed +// } +// } diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 713c397..01f24b1 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/22 00:01:48 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 16:17:27 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,92 +61,77 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code) return (result); } -void free_split_path(char **path) +static char *find_in_path(char *cmd, t_env *env, int *return_code) { - char **tmp = path; - while (*tmp) - { - free(*tmp); - tmp++; - } - free(path); -} + char *cur_path; + char *cmd_path; + char **path; -char **get_split_path(t_env *env) -{ - char *path_env; - char **split_path; - path_env = env_get(env, "PATH"); - if (!path_env) - return (NULL); - split_path = ft_split(path_env, ':'); - free(path_env); - return (split_path); + path = get_split_path(env); + cmd_path = NULL; + while (*path) + { + if (cmd_path) + free(cmd_path); + cur_path = ft_strjoin(*path, "/"); + if (!cur_path) + return (NULL); + cmd_path = ft_strjoin(cur_path, cmd); + free(cur_path); + if (!cmd_path) + return (NULL); + if (access(cmd_path, X_OK) != -1) + return (cmd_path); + path++; + } + *return_code = 127; + free(cmd_path); + command_not_found_error(cmd); + return (NULL); } -static char *find_in_path(char *cmd, t_env *env, int *return_code) -{ - char *cur_path; - char *cmd_path; - char **path; - char **path_start; // To keep track of the start of the path array - - path = get_split_path(env); - path_start = path; // Save the start of the path array - cmd_path = NULL; - while (*path) - { - if (cmd_path) - free(cmd_path); - cur_path = ft_strjoin(*path, "/"); - if (!cur_path) - { - free_split_path(path_start); // Free the entire path array - return (NULL); - } - cmd_path = ft_strjoin(cur_path, cmd); - free(cur_path); - if (!cmd_path) - { - free_split_path(path_start); // Free the entire path array - return (NULL); - } - if (access(cmd_path, X_OK) != -1) - { - free_split_path(path_start); // Free the entire path array - return (cmd_path); - } - path++; - } - free_split_path(path_start); // Free the entire path array - *return_code = 127; - command_not_found_error(cmd); - return (NULL); -} +// static char *get_simple_cmd_path(char *cmd, int *return_code) +// { +// char *result; +// result = ft_strdup(cmd); +// if (!result) +// return (NULL); +// if (access(result, F_OK) == -1) +// { +// free(result); +// return (error(ENOENT, cmd, 127, return_code)); +// } +// if (access(result, X_OK) == -1) +// { +// free(result); +// return (error(EACCES, cmd, 126, return_code)); +// } +// if (is_directory(cmd)) +// { +// free(result); +// return (error(EISDIR, cmd, 126, return_code)); +// } +// return (result); +// } static char *get_simple_cmd_path(char *cmd, int *return_code) { char *result; result = ft_strdup(cmd); - if (!result) - return (NULL); if (access(result, F_OK) == -1) { free(result); return (error(ENOENT, cmd, 127, return_code)); } - if (access(result, X_OK) == -1) + else if (access(result, X_OK) == -1) { free(result); return (error(EACCES, cmd, 126, return_code)); } if (is_directory(cmd)) - { - free(result); return (error(EISDIR, cmd, 126, return_code)); - } return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index 11f0620..979a3cf 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/21 16:35:38 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 15:20:18 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/new_node.c b/src/new_node.c index b2ab7ea..21f8d9f 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:59:01 by dkaiser ### ########.fr */ +/* Updated: 2025/01/22 15:15:31 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,7 +51,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], node->content.cmd.redirs[0] = redirs[0]; node->content.cmd.redirs[1] = redirs[1]; node->content.cmd.create_files = create_files; - free(redirs); + // free(redirs); redirs = NULL; return (node); } diff --git a/src/repl.c b/src/repl.c index 2dcd16d..859dde3 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/21 21:29:16 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 15:22:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,8 @@ void free_repl(char *input, t_node *ast) { free(input); - free_node(ast); + if(ast) + free_node(ast); } void repl(const char *prompt, t_env **env, int *promptflag)