This commit is contained in:
2024-04-23 18:51:39 +02:00
parent e5b705ff64
commit 471a417bf9
7 changed files with 105 additions and 62 deletions

View File

@ -6,13 +6,34 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
/* Updated: 2024/04/23 16:41:42 by tomoron ### ########.fr */
/* Updated: 2024/04/23 18:34:12 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cd(t_token *args)
void cd_update_pwd(char *new_path, t_msh *msh)
{
char *pwd;
char *new;
new = ft_strdup(new_path);
if (!new)
return ;
pwd = ft_get_env(msh->env, "PWD");
pwd = ft_strdup(pwd);
if (!pwd)
ft_strdup("");
if (!pwd)
free(new);
if (!pwd)
return ;
msh->env = export_set_env(msh->env, ft_strdup("OLDPWD"), pwd, 0);
if (ft_get_env(msh->env, "PWD"))
msh->env = export_set_env(msh->env, ft_strdup("PWD"), new, 0);
}
int cd(t_token *args, t_env *env, t_msh *msh)
{
char *new_wd;
@ -22,9 +43,16 @@ int cd(t_token *args)
return (1);
}
if (!args->next)
new_wd = getenv("HOME");
{
new_wd = ft_get_env(env, "HOME");
if (!new_wd)
ft_putstr_fd("minishell: cd: HOME not set\n", 2);
if (!new_wd)
return (1);
}
else
new_wd = args->next->value;
cd_update_pwd(new_wd, msh);
if (chdir(new_wd) == -1)
{
perror("minishell: cd");