builtins cd, pwd (todo, refactoring cd)
This commit is contained in:
@@ -1,12 +1,46 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
(void)argc;
|
||||
chdir(argv[1]);
|
||||
c->prompt = init_prompt(c->prompt_base);
|
||||
if (!c->prompt)
|
||||
return (ft_reti_perror(0, "init_prompt() error"));
|
||||
return (0);
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user