-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_cd.c
97 lines (88 loc) · 2.49 KB
/
builtin_cd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marlean <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/05 10:53:59 by hcolumbu #+# #+# */
/* Updated: 2022/06/17 13:51:14 by marlean ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *cut_tail_string(char *str)
{
int i;
if (!str)
return (NULL);
i = ft_strlen(str);
while (str[i] != '/')
i--;
return (ft_substr(str, 0, i + 1));
}
static int home_directory(t_com *com, char *home)
{
char *tmp;
if (!com->arg || !ft_strcmp(com->arg[0], "~"))
chdir(home);
else
{
if (!ft_strncmp(com->arg[0], "~/", 2))
{
tmp = com->arg[0] + 1;
com->arg[0] = ft_strjoin(home, "/");
com->arg[0] = ft_strjoin(com->arg[0], tmp);
}
else
{
tmp = cut_tail_string(home);
com->arg[0] = ft_strjoin(tmp, com->arg[0] + 1);
free(tmp);
}
chdir(com->arg[0]);
}
return (0);
}
static int change_directory(t_com *com, char *home, char *pwd, char *old_pwd)
{
char *tmp;
if (!com->arg || !ft_strncmp(com->arg[0], "~", 1))
home_directory(com, home);
else if (!ft_strcmp(com->arg[0], "."))
chdir(pwd);
else if (!ft_strcmp(com->arg[0], "-"))
{
chdir(old_pwd);
ft_putendl_fd(old_pwd, 1);
}
else if (!ft_strcmp(com->arg[0], ".."))
{
tmp = cut_tail_string(pwd);
chdir(tmp);
free(tmp);
}
else
chdir(com->arg[0]);
return (0);
}
int builtin_cd(t_com *com, t_envp *envp_list)
{
char *home;
char *pwd;
char *old_pwd;
if (!envp_list || !com)
return (1);
home = get_value_from_envp(envp_list, "HOME");
pwd = get_value_from_envp(envp_list, "PWD");
old_pwd = get_value_from_envp(envp_list, "OLDPWD");
if (!home || !pwd || !old_pwd)
return (1);
change_directory(com, home, pwd, old_pwd);
if (put_value_to_envp(envp_list, "OLDPWD", pwd))
return (1);
if (put_value_to_envp(envp_list, "PWD", getcwd(NULL, 0)))
return (1);
free(home);
free(old_pwd);
return (0);
}