-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_cd_utils1.c
More file actions
executable file
·114 lines (105 loc) · 2.7 KB
/
builtin_cd_utils1.c
File metadata and controls
executable file
·114 lines (105 loc) · 2.7 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
109
110
111
112
113
114
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_cd_utils1.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 update_envps(t_minish *sh, char *newpwd, char *oldpwd)
{
char *npwd;
char *opwd;
npwd = ft_strjoin("PWD=", newpwd);
if (!npwd)
return (-1);
opwd = ft_strjoin("OLDPWD=", oldpwd);
if (!opwd)
return (-1);
update_or_add_envps(sh, npwd);
free(npwd);
update_or_add_envps(sh, opwd);
free(opwd);
return (0);
}
int f_update_pwds(t_minish *sh, char *current_pwd)
{
char *oldpwd;
char *newpwd;
*f_exitcode() = 1;
oldpwd = ft_strdup(current_pwd);
if (!oldpwd)
return (1);
newpwd = getcwd(NULL, 0);
if (!newpwd)
{
free(oldpwd);
return (1);
}
if (update_envps(sh, newpwd, oldpwd))
{
free(oldpwd);
free(newpwd);
return (1);
}
free(oldpwd);
free(newpwd);
*f_exitcode() = 0;
return (0);
}
void f_putstr_fd_error_msg(char *builtin, char *msg, char *location, int fd)
{
ft_putstr_fd("bash: ", fd);
ft_putstr_fd(builtin, fd);
ft_putstr_fd(": ", fd);
if (location != NULL)
{
ft_putstr_fd(location, fd);
ft_putstr_fd(": ", fd);
}
if (msg != NULL)
ft_putstr_fd(msg, fd);
ft_putstr_fd("\n", fd);
*f_exitcode() = 1;
}
int f_check_params(char *param1, int length)
{
if (ft_strncmp(param1, "-", length) == 0)
return (1);
else if (ft_strncmp(param1, "~", length) == 0)
return (2);
else if (ft_strncmp(param1, "/", length) == 0)
return (3);
else
return (4);
}
int f_cd_process_params(t_minish *sh, char **cmds)
{
int size;
int res;
size = f_count_char(cmds);
if (size == 1)
return (f_cd_home(sh));
else if (size == 2)
{
res = f_check_params(cmds[1], ft_strlen(cmds[1]));
if (res == 1)
return (f_cd_go_back(sh));
else if (res == 2)
return (f_cd_home(sh));
else if (res == 3)
return (f_cd_root());
else if (res == 4)
return (f_cd_goto(cmds[1]));
}
else
{
f_putstr_fd_error_msg("cd", "too many arguments", NULL, STDERR_FILENO);
return (1);
}
return (0);
}