-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_cd.c
More file actions
96 lines (88 loc) · 2.63 KB
/
ms_cd.c
File metadata and controls
96 lines (88 loc) · 2.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jheo <jheo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/20 16:44:04 by jheo #+# #+# */
/* Updated: 2024/12/23 21:27:26 by jheo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void cd_no_dir_error(t_plst *lst_tmp, t_cmd *cmd, char *tmp)
{
ft_putstr_fd("minishell: cd: ", STDERR_FILENO);
ft_putstr_fd(lst_tmp->pipe_split[1], STDERR_FILENO);
ft_putendl_fd(": No such file or directory", STDERR_FILENO);
free(tmp);
cmd->sts.process_status = EXIT_FAILURE;
}
int cd_slash_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;
return (0);
}
return (1);
}
int cd_to_ca_cnt_controller(int ca_cnt, t_cmd *cmd)
{
if (ca_cnt > 2)
{
ft_putendl_fd("minishell: cd: too many arguments", STDERR_FILENO);
cmd->sts.process_status = EXIT_FAILURE;
return (0);
}
else if (ca_cnt == 1)
chdir(getenv("HOME"));
return (1);
}
int cd_another_error(t_plst *lst_tmp, t_cmd *cmd)
{
char *tmp;
int dir;
tmp = ft_strjoin(getcwd(NULL, 0), "/");
tmp = ft_strjoin(tmp, lst_tmp->pipe_split[1]);
dir = chdir(tmp);
if (dir)
{
cd_no_dir_error(lst_tmp, cmd, tmp);
return (0);
}
free(tmp);
return (1);
}
int start_cd(t_cmd *cmd, t_plst *lst_tmp, int ca_cnt)
{
if ((ca_cnt > 0 || ca_cnt <= 2) && \
cd_to_ca_cnt_controller(ca_cnt, cmd) == 0)
return (0);
else if (lst_tmp->pipe_split[1] == NULL)
chdir(getenv("HOME"));
else if (strncmp("~", lst_tmp->pipe_split[1], 1) == 0)
{
if (cd_tild_cntroller(lst_tmp, cmd) == 0)
return (0);
}
else if (strncmp("/", lst_tmp->pipe_split[1], 1) == 0)
{
if (cd_slash_error(lst_tmp, cmd) == 0)
return (0);
}
else
{
if (cd_another_error(lst_tmp, cmd) == 0)
return (0);
}
cmd->sts.process_status = EXIT_SUCCESS;
return (0);
}