mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-06-25 02:13:35 +02:00
「✨」 feat: Added some security to the parsing
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user