-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_cd.c
More file actions
84 lines (75 loc) · 2.13 KB
/
ft_cd.c
File metadata and controls
84 lines (75 loc) · 2.13 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mamoussa <mamoussa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/10 12:43:12 by mamoussa #+# #+# */
/* Updated: 2020/12/11 13:57:07 by mamoussa ### ########.fr */
/* */
/* ************************************************************************** */
#include "shell.h"
void path_checker(void)
{
t_env *current;
current = g_env_head;
while (current)
{
if (!ft_strncmp(current->key, "HOME", ft_strlen("HOME")))
{
g_tmp = g_cmd_head->string;
g_cmd_head->string = ft_strjoin(current->value,
g_cmd_head->string);
simple_pointer_free(g_tmp);
break ;
}
current = current->next;
}
}
void check_for_tilde(void)
{
char *str;
char *strtmp;
strtmp = ft_strdup(g_cmd_head->string);
str = (char*)malloc(ft_strlen(strtmp));
str_cpy(&str, &strtmp);
g_tmp = g_cmd_head->string;
g_cmd_head->string = ft_strdup(str);
simple_pointer_free(g_tmp);
path_checker();
simple_pointer_free(str);
simple_pointer_free(strtmp);
}
char *check_for_home(void)
{
t_env *current;
current = g_env_head;
while (current)
{
if (!ft_strncmp(current->key, "HOME", ft_strlen("HOME")))
{
return (current->value);
break ;
}
current = current->next;
}
return (NULL);
}
void ft_cd(void)
{
int ret;
if (ft_cd_helper())
return ;
if (!ft_strncmp(g_cmd_head->string, "~", ft_strlen("~")))
check_for_tilde();
ret = chdir(g_cmd_head->string);
if (ret < 0)
{
ft_error(strerror(errno));
write(1, ":", 1);
write(1, g_cmd_head->string, ft_strlen(g_cmd_head->string));
write(1, "\n", 1);
g_status = 1;
}
}