-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
126 lines (115 loc) · 2.82 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ikgonzal <ikgonzal@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 07:57:14 by ikgonzal #+# #+# */
/* Updated: 2022/04/15 16:17:26 by ikgonzal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <limits.h>
char *ft_get_env_path(t_set *set, char *var, int v_l)
{
int i;
char *path;
int k;
int j;
i = -1;
while (set->env[++i])
{
if (ft_strncmp(set->env[i], var, v_l) == 0
&& set->env[i][v_l] == '=')
{
path = malloc(sizeof(char) * (ft_strlen(set->env[i])) - v_l + 1);
j = 0;
k = 0;
while (set->env[i][j++])
{
if (j > v_l)
path[k++] = set->env[i][j];
}
path[k] = '\0';
return (path);
}
}
return (NULL);
}
int ft_go_to_home(t_set *set)
{
char *home_path;
int cd_ret;
home_path = ft_get_env_path(set, "HOME", 4);
if (!home_path)
{
free(home_path);
ft_putendl_fd("papa$hell: cd: HOME not set", 2);
return (1);
}
else
{
ft_update_oldpwd(set);
cd_ret = chdir(home_path);
}
free(home_path);
return (cd_ret);
}
int ft_go_to_oldpwd(t_set *set)
{
char *old_pwd_path;
int cd_ret;
old_pwd_path = ft_get_env_path(set, "OLDPWD", 6);
if (!old_pwd_path)
{
free(old_pwd_path);
ft_putendl_fd("papa$shell: cd: OLDPWD not set", 2);
return (1);
}
else
{
ft_update_oldpwd(set);
cd_ret = chdir(old_pwd_path);
}
free(old_pwd_path);
return (cd_ret);
}
int ft_update_oldpwd(t_set *set)
{
char oldpwd_path[PATH_MAX];
char *var;
static int old_pwd;
if (getcwd(oldpwd_path, PATH_MAX) == NULL)
return (1);
var = ft_strjoin("OLDPWD=", oldpwd_path);
if (!old_pwd)
{
set->env = add_var(set, var);
old_pwd = 1;
}
else
edit_var(set, var);
free(var);
return (0);
}
int ft_cd(char **argv, t_set *set, int child)
{
int cd_ret;
if (argv[1] && (ft_strncmp(argv[1], "-", 1)) == 0)
cd_ret = ft_go_to_oldpwd(set);
else if (!argv[1])
cd_ret = ft_go_to_home(set);
else
{
ft_update_oldpwd(set);
cd_ret = chdir(argv[1]);
}
if (cd_ret == -1 && child)
print_error(": No such file or directory", 1, argv[1], 1);
else if (cd_ret == -1 && !child)
print_error(": No such file or directory", 1, argv[1], 0);
else if (cd_ret == 0 && child)
exit (0);
return (0);
}