-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcd.c
More file actions
120 lines (109 loc) · 2.43 KB
/
cd.c
File metadata and controls
120 lines (109 loc) · 2.43 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bguzel <bguzel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/09 15:26:38 by bguzel #+# #+# */
/* Updated: 2023/09/10 12:38:58 by bguzel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void cd_back(char *str)
{
int i;
int l;
char *ktm;
l = 0;
cd_helper(str);
i = ft_strlen(str) - 1;
if (str[i] == '/')
i--;
while (str[i] != '/' && i >= 0)
i--;
ktm = malloc(sizeof(char) * (i + 2));
while (l <= i)
{
ktm[l] = str[l];
l++;
}
ktm[l] = '\0';
cd_helper_v2(ktm);
}
void cd_tilde(char *str)
{
int i;
int counter;
char *ktm;
counter = 0;
i = -1;
while (str[++i])
{
if (str[i] == '/')
counter += 1;
if (counter == 3)
break ;
}
ktm = malloc(sizeof(char) * i);
counter = -1;
if (malloc_path(i, str) == 0)
{
chdir(g_var.path);
free(ktm);
return ;
}
while (++counter < i)
ktm[counter] = str[counter];
ktm[counter] = '\0';
cd_helper_v2(ktm);
}
int malloc_path(int i, char *str)
{
static int flag;
int k;
if (flag == 1)
return (0);
flag = 0;
k = 0;
g_var.path = (char *)malloc(sizeof(char) * i);
while (k < i)
{
g_var.path[k] = str[k];
k++;
}
g_var.path[k] = '\0';
flag = 1;
return (1);
}
void cd_next(char *str, char *next)
{
int i;
char *ktm;
int t;
t = -1;
i = -1;
ktm = malloc(sizeof(char) * (ft_strlen(str) + ft_strlen(next) + 1));
while (str[++i])
ktm[i] = str[i];
ktm[i++] = '/';
while (next[++t])
ktm[i++] = next[t];
ktm[i] = '\0';
if (chdir(ktm) != 0)
no_such(next);
else
cd_helper_v2(ktm);
}
void cd_helper(char *str)
{
int result;
result = find_path("OLDPWD");
if (result != -1)
{
free(g_var.env[result]);
free(g_var.export[result]);
g_var.env[result] = ft_strjoin("OLDPWD=", str);
g_var.export[result] = ft_strjoin("OLDPWD=", str);
}
}