-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_builtin_func.c
More file actions
103 lines (95 loc) · 2.99 KB
/
ms_builtin_func.c
File metadata and controls
103 lines (95 loc) · 2.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_builtin_func.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jheo <jheo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 21:32:44 by jinsecho #+# #+# */
/* Updated: 2024/12/26 23:06:50 by jheo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void start_pwd(t_cmd *c)
{
char *currdir;
currdir = getcwd(NULL, 0);
if (currdir == NULL)
exit(EXIT_FAILURE);
printf("%s\n", currdir);
free(currdir);
c->sts.process_status = EXIT_SUCCESS;
}
void start_echo(t_plst *l, t_cmd *c, int ca_cnt)
{
int i;
i = 1;
if (sn(l->pipe_split[1], "-n", 2) \
&& echo_cmd_check(l) == 1)
{
while (sn(l->pipe_split[i], "-n", 2) && echo_cmd_check(l) == 1)
i++;
while (i + 1 < ca_cnt)
printf("%s ", l->pipe_split[i++]);
if (l->pipe_split[i] != 0)
printf("%s", l->pipe_split[i]);
}
else
{
if (l->pipe_split[1] == NULL)
printf("\n");
else
{
while (i + 1 < ca_cnt)
printf("%s ", l->pipe_split[i++]);
printf("%s\n", l->pipe_split[i]);
}
}
c->sts.process_status = EXIT_SUCCESS;
}
void start_export(t_plst *l, t_cmd *c)
{
if (ft_export(l->pipe_split[1], c) == 0)
{
ft_putstr_fd("minishell: export: `", STDERR_FILENO);
ft_putstr_fd(l->pipe_split[1], STDERR_FILENO);
ft_putendl_fd("': not a valid identifier", STDERR_FILENO);
c->sts.process_status = EXIT_FAILURE;
}
else
c->sts.process_status = EXIT_SUCCESS;
}
void start_unset(t_cmd *c, t_plst *l)
{
if (l->pipe_split[1] != NULL)
ft_unset(l->pipe_split[1], c);
c->sts.process_status = EXIT_SUCCESS;
}
int ms_builtin_func(t_cmd *c, t_plst *l)
{
if (l->pipe_split[0] == NULL)
return (0);
count_ca_cnt(l);
if (sn(l->pipe_split[0], "cd", 2) && sl(l->pipe_split[0]) == 2)
start_cd(c, l, l->ca_cnt);
else if (sn(l->pipe_split[0], "pwd", 3) && sl(l->pipe_split[0]) == 3)
start_pwd(c);
else if (sn(l->pipe_split[0], "echo", 4) && sl(l->pipe_split[0]) == 4)
start_echo(l, c, l->ca_cnt);
else if (sn(l->pipe_split[0], "exit", 4) && sl(l->pipe_split[0]) == 4)
start_exit(&l->ca_cnt, l, c);
else if (sn(l->pipe_split[0], "export", 6) && sl(l->pipe_split[0]) == 6)
{
if (l->ca_cnt == 1)
print_export(c->envp);
else
start_export(l, c);
}
else if (sn(l->pipe_split[0], "unset", 5) && sl(l->pipe_split[0]) == 5)
start_unset(c, l);
else if (sn(l->pipe_split[0], "env", 3) && sl(l->pipe_split[0]) == 3)
c->sts.process_status = print_env(c->envp);
else
return (1);
return (0);
}