minishell/srcs/cmd_cd.c
2019-01-13 19:03:08 +01:00

46 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
/* Updated: 2019/01/13 17:00:19 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_cd(char **argv, t_list **env)
{
int i;
int ret;
char *path;
t_list *tmp;
ret = 0;
i = 0;
tmp = *env;
path = NULL;
if (!argv[1])
{
while (tmp )
{
if (ft_strcmp(((t_envelem*)(tmp->content))->key, "HOME") == 0)
{
path = ((t_envelem*)(tmp->content))->val;
break ;
}
tmp = tmp->next;
}
if (path == NULL)
return (1);
}
else
path = argv[1];
ft_putendl(path);
ret = chdir(path);
return (ret);
}