minishell/src/builtin/builtin_cd.c
2025-02-26 16:17:07 +01:00

42 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_cd.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:45:55 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_cd(t_minishell *msh, t_command *cmd)
{
t_environment *env;
char *path;
if (cmd->args[1] == NULL)
{
env = environment_get(msh, "HOME");
if (env == NULL || env->value == NULL)
{
ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO);
return (FAILURE);
}
path = env->value;
}
else
path = cmd->args[1];
if (chdir(path) == -1)
{
ft_putstr_fd("minishell: cd: ", STDERR_FILENO);
ft_putstr_fd(path, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
return (FAILURE);
}
return (SUCCESS);
}