-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildin_cd2_utils.c
107 lines (99 loc) · 2.93 KB
/
buildin_cd2_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* buildin_cd2_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ggiannit <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/15 17:56:09 by ggiannit #+# #+# */
/* Updated: 2023/04/17 10:12:55 by ggiannit ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_cd_meno(t_mish *meta)
{
char *tmp;
tmp = ft_strdup(meta->curdir);
ft_free((void **) &(meta->curdir));
meta->curdir = ft_strdup(meta->olddir);
ft_free((void **) &(meta->olddir));
meta->olddir = tmp;
printf("%s\n", meta->curdir);
chdir(meta->curdir);
return (1);
}
int ft_cd_slash(t_mish *meta, t_cmd *node)
{
if (node->pot[1][0] != '-')
{
ft_cd_slash2(meta, node);
return (1);
}
if (node->pot[1][0] == '-' && (!node->pot[1][1] || node->pot[1][1] == 32))
return (ft_cd_meno(meta));
return (0);
}
void ft_pre_slash_2(t_mish *meta, t_cmd *node, int i)
{
while (node->pot[1][i])
{
if (node->pot[1][i] && (!ft_strncmp(&node->pot[1][i], "..", 2)
|| (!ft_strncmp(&node->pot[1][i], "../", 3))))
{
ft_cd_pre(meta);
chdir(meta->curdir);
i += 3;
}
else if (node->pot[1][i] && ((ft_isalpha(node->pot[1][i])
|| ft_isdigit(node->pot[1][i])) ||
(!ft_strncmp(&node->pot[1][i], "./", 2)
&& (ft_isalpha(node->pot[1][i + 2])
|| ft_isdigit(node->pot[1][i + 2])))))
{
if (!ft_strncmp(&node->pot[1][i], "./", 2))
i += 2;
chdir(&node->pot[1][i]);
ft_next_slash(meta, &node->pot[1][i]);
i += len_slash(&node->pot[1][i]);
i++;
}
else
i++;
}
}
static void ft_cd_slash2_2_2(t_mish *meta, t_cmd *node, int *i)
{
int k;
k = 0;
if (!ft_strncmp(&node->pot[1][*i], "./", 2))
*i += 2;
ft_find_path(meta->home_path, &k);
chdir(&meta->home_path[k]);
ft_next_slash(meta, &node->pot[1][*i]);
(*i) += len_slash(&node->pot[1][*i]);
*i += 1;
}
void ft_cd_slash2_2(t_mish *meta, t_cmd *node, int i, int k)
{
while (node->pot[1][i])
{
k = 0;
if (!ft_strncmp(&node->pot[1][i], "..", 2)
|| (!ft_strncmp(&node->pot[1][i], "../", 3)))
{
ft_find_path(meta->home_path, &k);
chdir(&meta->home_path[k]);
ft_cd_pre(meta);
i += 3;
}
else if ((ft_isalpha(node->pot[1][i]) || ft_isdigit(node->pot[1][i])) ||
(!ft_strncmp(&node->pot[1][i], "./", 2)
&& (ft_isalpha(node->pot[1][i + 2])
|| ft_isdigit(node->pot[1][i + 2]))))
{
ft_cd_slash2_2_2(meta, node, &i);
}
else
i++;
}
}