forked from Kinnune/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
115 lines (104 loc) · 2.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: djames <djames@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 15:44:35 by djames #+# #+# */
/* Updated: 2023/07/18 12:28:12 by djames ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_split(char **tem)
{
int i;
i = 0;
while (tem[i] != NULL)
{
free(tem[i]);
i++;
}
free(tem);
}
int chance_oldpwd(int i, char *str)
{
char **tem;
char *str3;
char *str2;
tem = ft_split(g_data.envir[i], '=');
str3 = ((ft_strjoin("OLDPWD=", tem[1])));
add_string(str3);
str2 = ((ft_strjoin("PWD=", str)));
add_string(str2);
free(str3);
free(str2);
free_split(tem);
return (0);
}
int chance_pwd(char *str)
{
int i;
int flag;
i = 0;
flag = 0;
while (g_data.envir[i] != NULL)
{
if (ft_strncmp(g_data.envir[i], "PWD", 3) == 0 && flag == 0)
{
chance_oldpwd(i, str);
flag = 1;
}
i++;
}
return (0);
}
char *find_word2(char *str)
{
int i;
int j;
int equal;
int length;
j = 0;
equal = 0;
length = ft_strlen(str);
i = 0;
while (g_data.envir[i] != NULL)
{
if (ft_strncmp(g_data.envir[i], str, length) == 0)
{
while (g_data.envir[i][j] != '\0' && equal == 0)
{
if (g_data.envir[i][j] == '=')
equal = 1;
j++;
}
return (find_helper(equal, j, i));
}
i++;
}
return (NULL);
}
int change_directory(char *path)
{
char *result;
char buf[BUFFER];
if (path == NULL)
path = find_word2("HOME");
if (chdir(path) == 0)
{
result = getcwd(buf, sizeof(buf));
if (!(result != NULL))
{
perror("MINISHELL");
return (1);
}
}
else
{
perror("MINISHELL");
return (1);
}
chance_pwd(buf);
return (0);
}