-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
38 lines (35 loc) · 1.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ethanlim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/13 00:39:43 by ethanlim #+# #+# */
/* Updated: 2024/09/16 01:51:44 by ethanlim ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cd(char **cmd, t_data *data)
{
char *path;
change_env_oldpwd(data);
if (cmd[1] == NULL || cmd[1][0] == '\0')
{
path = get_expand_string(ft_strdup("HOME"), data->envp);
if (!path)
{
printf("Error: cd: HOME not set\n");
return (1);
}
}
else
path = cmd[1];
if (chdir(path) == -1)
{
perror("cd");
return (1);
}
change_env_pwd(data);
return (0);
}