」 feat: Added some security to the parsing

This commit is contained in:
2024-07-03 19:13:43 +02:00
parent 9f25b17170
commit d24f097ae9
6 changed files with 21 additions and 79 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdbool.h>
# 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)"

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -1,33 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_redir.c :+: :+: :+: */
/* check_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdbool.h>
#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);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"));
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);