From d24f097ae9329df0a1008fcc59b5e27b6f833e37 Mon Sep 17 00:00:00 2001 From: Adam Joly Date: Wed, 3 Jul 2024 19:13:43 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20Added=20s?= =?UTF-8?q?ome=20security=20to=20the=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/error_msg.h | 9 ++-- include/parsing.h | 3 +- src/builtins/ft_cd.c | 52 +------------------ .../{check_redir.c => check_argv.c} | 23 ++++---- src/parsing/check_error/check_pipe.c | 5 +- src/parsing/check_error/send_error.c | 8 +-- 6 files changed, 21 insertions(+), 79 deletions(-) rename src/parsing/check_error/{check_redir.c => check_argv.c} (58%) diff --git a/include/error_msg.h b/include/error_msg.h index b3f6e30..ed9d1fc 100644 --- a/include/error_msg.h +++ b/include/error_msg.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */ -/* Updated: 2024/06/30 16:08:28 by adjoly ### ########.fr */ +/* Updated: 2024/07/03 19:07:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +18,12 @@ */ # include -# define ERROR_SYNTAX ": syntax error" -# define ERROR_NO_REDIR ": need redirection file" -# define ERROR_NO_EOF ": need delimiter to heredoc" +# define ERROR_SYNTAX "syntax error" +# define ERROR_NO_REDIR "need redirection file" +# define ERROR_NO_EOF "need delimiter to heredoc" # define ERROR_NO_FILE "No such file or directory" # define ERROR_CMD_PIPE "No command after pipe" +# define ERROR_NO_CMD "No command in the pipe" # define ERROR_COREDUMP "(core dumped)" diff --git a/include/parsing.h b/include/parsing.h index f3b1514..f290cbf 100644 --- a/include/parsing.h +++ b/include/parsing.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */ -/* Updated: 2024/06/30 17:27:13 by adjoly ### ########.fr */ +/* Updated: 2024/07/03 19:03:35 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ typedef enum s_quote bool check_syntax(char *readline); void send_error(char *msg, char **argv); bool check_redir(t_list *redir); +bool check_argv(t_list *token); t_cmd *get_redir_fd(void *content); t_list *get_cmd_list(t_list *list); void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2]); diff --git a/src/builtins/ft_cd.c b/src/builtins/ft_cd.c index 4508a1d..25b9417 100644 --- a/src/builtins/ft_cd.c +++ b/src/builtins/ft_cd.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 15:07:24 by adjoly #+# #+# */ -/* Updated: 2024/07/03 11:45:04 by adjoly ### ########.fr */ +/* Updated: 2024/07/03 12:18:18 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,51 +16,6 @@ #include "libft.h" #include "builtins.h" -char *__get_parent_directory(char *pwd) -{ - char *tmp; - char *dir; - - tmp = pwd; - while (*tmp) - { - if (*tmp == '/') - dir = tmp; - tmp++; - } - ft_strlcpy(pwd, pwd, dir - pwd + 1); - return (pwd); -} - -char *__relative_path(char *args, char *pwd) -{ - char **path; - char **tmp; - char *ret; - char *new_path; - - path = ft_split(args, '/'); - tmp = path; - new_path = pwd; - if (!new_path) - return (NULL); - while (*tmp) - { - if (is_str(*tmp, "..")) - new_path = __get_parent_directory(new_path); - else if (*tmp && !is_str(*tmp, ".")) - { - ft_strlcat(new_path, "/", ft_strlen(new_path) + 2); - ft_strlcat(new_path, *tmp, ft_strlen(new_path) + \ - ft_strlen(*tmp) + 1); - } - tmp++; - } - ret = ft_strdup(new_path); - ft_free("a", &path); - return (ret); -} - void ft_cd(t_env *env, char *args) { char *pwd; @@ -71,17 +26,14 @@ void ft_cd(t_env *env, char *args) pwd = ft_strdup(ret_cwd()); if (!args) new_pwd = env_get_value("HOME", env); - //else if (args[0] == '/') - //new_pwd = ft_strdup(args); else if (args[0] == '-') new_pwd = env_get_value("OLDPWD", env); else new_pwd = ft_strdup(args); - //new_pwd = __relative_path(args, pwd); ret = chdir(new_pwd); if (ret == -1) { - printf("./minishell: cd: %s: %s\n", args, ERROR_NO_FILE); + send_error_parsing(ERROR_NO_FILE); return ; } env_edit("PWD", ft_strdup(ret_cwd()), env); diff --git a/src/parsing/check_error/check_redir.c b/src/parsing/check_error/check_argv.c similarity index 58% rename from src/parsing/check_error/check_redir.c rename to src/parsing/check_error/check_argv.c index d9dce81..6b9fac7 100644 --- a/src/parsing/check_error/check_redir.c +++ b/src/parsing/check_error/check_argv.c @@ -1,33 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* check_redir.c :+: :+: :+: */ +/* check_argv.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/28 18:17:26 by adjoly #+# #+# */ -/* Updated: 2024/06/30 13:49:21 by adjoly ### ########.fr */ +/* Created: 2024/07/03 16:34:19 by adjoly #+# #+# */ +/* Updated: 2024/07/03 19:07:23 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "tokenizer.h" +#include #include "parsing.h" #include "error_msg.h" -bool check_redir(t_list *redir) +bool check_argv(t_list *token) { - t_list *tmp; - t_redirection *tmp_redir; + t_list *tmp; - tmp = redir; + tmp = token; while (tmp) { - tmp_redir = tmp->content; - if (tmp_redir->sign == HEREDOC && \ - !((t_redirection *)(tmp->content))->file_name) - return (send_error_parsing(ERROR_NO_EOF)); - if (!((t_redirection *)(tmp->content))->file_name) - return (send_error_parsing(ERROR_NO_REDIR)); + if (!((t_token *)tmp->content)->argv) + return (send_error_parsing(ERROR_NO_CMD)); tmp = tmp->next; } return (false); diff --git a/src/parsing/check_error/check_pipe.c b/src/parsing/check_error/check_pipe.c index a101bdb..30e270c 100644 --- a/src/parsing/check_error/check_pipe.c +++ b/src/parsing/check_error/check_pipe.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 12:52:22 by adjoly #+# #+# */ -/* Updated: 2024/06/30 17:26:09 by adjoly ### ########.fr */ +/* Updated: 2024/07/03 16:17:02 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,8 +34,7 @@ bool check_pipe(char *readline) { if (*tmp == '|' && is_inquote(readline, tmp - readline) == FALSE) { - tmp++; - tmp += strlen_till_end_char(tmp, ' '); + tmp += strlen_till_end_char(tmp + 1, ' ') + 1; if (!*tmp) return (send_error_parsing("No command after pipe")); } diff --git a/src/parsing/check_error/send_error.c b/src/parsing/check_error/send_error.c index 23838da..2daf3cd 100644 --- a/src/parsing/check_error/send_error.c +++ b/src/parsing/check_error/send_error.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/28 18:09:49 by adjoly #+# #+# */ -/* Updated: 2024/06/30 16:11:03 by adjoly ### ########.fr */ +/* Updated: 2024/07/03 16:15:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,12 +21,6 @@ char *get_program_name(char *argv_one) return (prog_name); } -void send_error(char *msg, char **argv) -{ - ft_putstr_fd(argv[0], STDERR_FILENO); - ft_putendl_fd(msg, STDERR_FILENO); -} - bool send_error_parsing(char *msg) { ft_putstr_fd(get_program_name(NULL), STDERR_FILENO);