42_minishell/builtins/cd.c

63 lines
1.8 KiB
C
Raw Normal View History

2023-02-21 08:45:43 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */
2023-04-18 07:30:17 -04:00
/* Updated: 2023/04/18 13:22:22 by erey-bet ### ########.fr */
2023-02-21 08:45:43 -05:00
/* */
/* ************************************************************************** */
#include "./builtins_private.h"
2023-02-21 08:45:43 -05:00
2023-04-18 07:30:17 -04:00
int make_move(char *path, t_list **env)
2023-02-21 08:45:43 -05:00
{
char *join;
2023-04-18 07:30:17 -04:00
char *old;
join = ft_strjoin("/", path);
2023-04-18 07:30:17 -04:00
join = ft_strfjoin(get_pwd(), join);
old = get_pwd();
if (chdir(join) == 0)
{
2023-04-18 07:30:17 -04:00
set_value_by_key("OLDPWD", old, env);
set_value_by_key("PWD", get_pwd(), env);
free(join);
return (0);
}
2023-04-18 07:30:17 -04:00
free(old);
free(join);
write(2, "cd: No such file or directory\n", 30);
return (1);
}
2023-04-18 07:30:17 -04:00
int move_folder(char **args, t_list **env)
{
char *path;
2023-04-18 07:30:17 -04:00
char *old;
2023-02-21 08:45:43 -05:00
if (args[0] == NULL || args[1] != NULL)
{
2023-03-30 07:12:18 -04:00
write(2, "cd: Wrong number's argument\n", 28);
return (1);
}
path = args[0];
2023-02-21 08:45:43 -05:00
if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0)
{
2023-04-18 07:30:17 -04:00
old = get_pwd();
2023-02-21 08:45:43 -05:00
if (chdir(path) == 0)
2023-04-18 07:30:17 -04:00
{
set_value_by_key("OLDPWD", old, env);
set_value_by_key("PWD", get_pwd(), env);
2023-02-21 08:45:43 -05:00
return (0);
2023-04-18 07:30:17 -04:00
}
free(old);
write(2, "cd: No such file or directory\n", 30);
2023-02-21 08:45:43 -05:00
return (1);
}
else
2023-04-18 07:30:17 -04:00
return (make_move(path, env));
2023-02-21 08:45:43 -05:00
}