-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
127 lines (116 loc) · 3.07 KB
/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <zhabri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 12:40:50 by zhabri #+# #+# */
/* Updated: 2023/01/13 14:12:43 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_list *get_env_node(char *var)
{
char *var_eq;
t_list *envp_entry;
var_eq = ft_strjoin(var, "=");
envp_entry = *g_glob->envp;
while (envp_entry \
&& ft_strncmp((char *)envp_entry->content, var_eq, ft_strlen(var_eq)) \
&& ft_strncmp((char *)envp_entry->content, var, ft_strlen(var) + 1))
envp_entry = envp_entry->next;
free(var_eq);
return (envp_entry);
}
static void ft_chdir(char *dir, int size)
{
char *tmp;
t_list *pwd;
t_list *old_pwd;
if (dir)
{
init_ft_chdir(&pwd, &old_pwd);
if (old_pwd)
free_null(old_pwd->content);
if (pwd)
tmp = ft_strdup(pwd->content + 4);
else
tmp = ft_strdup("");
if (old_pwd)
old_pwd->content = ft_strjoin("OLDPWD=", tmp);
else
ft_lstadd_back(g_glob->envp, ft_lstnew(ft_strjoin("OLDPWD=", tmp)));
free_and_null(&tmp);
chdir(dir);
ft_chdir_core(size, &tmp, pwd);
}
}
void cd(char *cmd, int fd_out)
{
char **cmd_split;
DIR *dir;
close(fd_out);
cmd_split = ft_split_sep(cmd, " \t");
if (cmd_split[1])
{
dir = opendir(cmd_split[1]);
if (cmd_split[2] || !dir)
{
if (cmd_split[2])
ft_putstr_fd("minishell: cd: too many arguments\n", 2);
if (!dir)
print_error_dir_cd(cmd_split[1]);
clean_and_free(cmd_split);
exit(1);
}
closedir(dir);
ft_chdir(cmd_split[1], 16);
}
else
ft_chdir(get_home_content(), 16);
clean_and_free(cmd_split);
exit(0);
}
bool cd_parent_arg(char **cmd_split)
{
DIR *dir;
dir = opendir(cmd_split[1]);
if (cmd_split[2] || !dir)
{
if (cmd_split[2])
ft_putstr_fd("minishell: cd: too many arguments\n", 2);
if (!dir)
print_error_dir_cd(cmd_split[1]);
free_tab(cmd_split);
g_glob->exit_ret = 1;
return (true);
}
closedir(dir);
ft_chdir(cmd_split[1], 16);
return (false);
}
bool cd_parent(void)
{
char **cmd_split;
if (ft_lstsize(*g_glob->cmds) == 1)
{
cmd_split = ft_split_quotes(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (cmd_split && cmd_split[0] && !ft_strncmp(cmd_split[0], "cd", 3))
{
if (cmd_split[1])
{
if (cd_parent_arg(cmd_split))
return (true);
}
else
ft_chdir(get_home_content(), 16);
free_tab(cmd_split);
g_glob->exit_ret = 0;
return (true);
}
free_tab(cmd_split);
}
return (false);
}