-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_cd_controller.c
More file actions
75 lines (68 loc) · 2.04 KB
/
ms_cd_controller.c
File metadata and controls
75 lines (68 loc) · 2.04 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_cd_controller.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jheo <jheo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 20:27:19 by jheo #+# #+# */
/* Updated: 2024/12/20 15:34:58 by jheo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void cd_dir_error(char *tmp, t_cmd *cmd)
{
ft_putstr_fd("minishell: cd: ", STDERR_FILENO);
ft_putstr_fd(tmp, STDERR_FILENO);
ft_putendl_fd(": No such file or directory", STDERR_FILENO);
free(tmp);
cmd->sts.process_status = EXIT_FAILURE;
}
void cd_hmoe_error(t_plst *lst_tmp, t_cmd *cmd)
{
char *tmp;
int dir;
tmp = lst_tmp->pipe_split[1];
dir = chdir(tmp);
if (dir)
{
ft_putstr_fd("minishell: cd: ", STDERR_FILENO);
ft_putstr_fd(tmp, STDERR_FILENO);
ft_putendl_fd(": No such file or directory", STDERR_FILENO);
cmd->sts.process_status = EXIT_FAILURE;
}
}
int cd_home_error_controller(t_plst *lst_tmp, t_cmd *cmd)
{
int dir;
char *tmp;
if (lst_tmp->pipe_split[1][1] != '/')
{
cd_hmoe_error(lst_tmp, cmd);
return (0);
}
else
{
tmp = ft_strjoin(ft_strdup(getenv("HOME")), \
&(lst_tmp->pipe_split[1][1]));
dir = chdir(tmp);
if (dir)
{
cd_dir_error(tmp, cmd);
return (0);
}
free(tmp);
}
return (1);
}
int cd_tild_cntroller(t_plst *lst_tmp, t_cmd *cmd)
{
if (ft_strlen(lst_tmp->pipe_split[1]) == 1)
chdir(getenv("HOME"));
else
{
if (cd_home_error_controller(lst_tmp, cmd) == 0)
return (0);
}
return (1);
}