-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_cd_utils2.c
More file actions
executable file
·108 lines (99 loc) · 2.67 KB
/
builtin_cd_utils2.c
File metadata and controls
executable file
·108 lines (99 loc) · 2.67 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_cd_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyeyeom <hyeyeom@42student.gyeongsan.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 02:12:09 by hyeyeom #+# #+# */
/* Updated: 2025/04/14 00:38:35 by hyeyeom ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int f_cd_root(void)
{
if (chdir("/") == -1)
{
ft_putstr_fd("bash: cd: Failed to change to root directory\n", \
STDERR_FILENO);
*f_exitcode() = 1;
return (1);
}
*f_exitcode() = 0;
return (0);
}
int f_cd_goto(char *location)
{
int res;
res = 0;
if (access(location, F_OK) != -1)
{
if (location[0] != '\\')
{
res = chdir(location);
if (res == -1)
res = cd_err_msg(location, 1, 1);
}
else
res = cd_err_msg(location, 1, 2);
}
else
res = cd_err_msg(location, 1, 3);
return (res);
}
int f_cd_home(t_minish *sh)
{
char *home;
home = f_getenv(sh->envp, "HOME");
if (!home)
{
ft_putstr_fd("bash: cd: HOME not set\n", STDERR_FILENO);
*f_exitcode() = 1;
return (1);
}
if (chdir(home) == -1)
{
ft_putstr_fd("bash: cd: Failed to change \
directory to HOME\n", STDERR_FILENO);
*f_exitcode() = 1;
return (1);
}
*f_exitcode() = 0;
return (0);
}
int f_cd_go_back(t_minish *sh)
{
char *back;
back = f_getenv(sh->envp, "OLDPWD");
if (!back)
{
ft_putstr_fd("bash: cd: OLDPWD not set\n", STDERR_FILENO);
*f_exitcode() = 1;
return (1);
}
if (chdir(back) == -1)
{
ft_putstr_fd("bash: cd: Failed to change directory to OLDPWD\n", \
STDERR_FILENO);
*f_exitcode() = 1;
return (1);
}
ft_putstr_fd(back, STDOUT_FILENO);
ft_putstr_fd("\n", STDOUT_FILENO);
*f_exitcode() = 0;
return (0);
}
int cd_err_msg(char *location, int exitstatus, int situation)
{
if (situation == 1)
f_putstr_fd_error_msg("cd", "No such file or directory", location, \
STDERR_FILENO);
else if (situation == 2)
f_putstr_fd_error_msg("cd", "Permission denied", location, \
STDERR_FILENO);
else if (situation == 3)
f_putstr_fd_error_msg("cd", "No such file or directory", location, \
STDERR_FILENO);
*f_exitcode() = exitstatus;
return (exitstatus);
}