/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* cd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */ /* Updated: 2021/12/04 20:01:37 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" int builtin_cd(int argc, char *argv[], t_all *c) { char *tmp; if (argc == 2) tmp = argv[1]; else if (argc < 2) { tmp = getenv("HOME"); if (!tmp) { shell_error("cd: ", "HOME not set"); return (EXIT_FAILURE); } } else if (argc > 2) { shell_error("cd: ", "too many arguments"); return (EXIT_FAILURE); } if (chdir(tmp) == -1) { shell_perror("cd: ", tmp, ": "); return (EXIT_FAILURE); } tmp = init_prompt(c->prompt_base); if (!tmp) return (ft_reti_perror(EXIT_FAILURE, "builtin_cd, init_prompt()")); free(c->prompt); c->prompt = tmp; return (EXIT_SUCCESS); }