-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
84 lines (77 loc) · 2.09 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emohamed <emohamed@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/17 18:41:04 by haarab #+# #+# */
/* Updated: 2023/10/01 09:40:49 by emohamed ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void change_oldpwd(t_vars *vars, char *pwd)
{
int count;
count = 0;
while (count < vars->env_number)
{
if (ft_strncmp(vars->env[count].key, "OLDPWD",
ft_strlen(vars->env[count].key) + 1) == 0)
{
free(vars->env[count].value);
vars->env[count].value = dp_en(pwd);
}
count++;
}
}
void change_pwd(t_vars *vars, char *pwd)
{
int count;
count = 0;
while (count < vars->env_number)
{
if (ft_strncmp(vars->env[count].key, "PWD",
ft_strlen(vars->env[count].key) + 1) == 0)
{
free(vars->env[count].value);
vars->env[count].value = dp_en(pwd);
}
count++;
}
}
void ft_cd(t_vars *vars, char **args, char *pwd)
{
if (chdir(args[1]) == -1)
{
ft_putendl_fd("minishell: No such file or directory", 2);
g_exit_status = 1;
return ;
}
if (vars->cmds[0].is_nex_pip)
{
g_exit_status = 0;
return ;
}
change_oldpwd(vars, pwd);
g_exit_status = 0;
}
void run_cd(char **args, t_vars *vars, char *pwd)
{
if (args[1])
{
ft_cd(vars, args, pwd);
}
else if (!args[1])
{
if (!ft_getenv("HOME", vars))
{
ft_putendl_fd("minishell: cd: HOME not set", 2);
g_exit_status = 1;
return ;
}
chdir(ft_getenv("HOME", vars));
change_oldpwd(vars, pwd);
g_exit_status = 0;
}
}