-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_exit.c
More file actions
executable file
·106 lines (97 loc) · 2.54 KB
/
builtin_exit.c
File metadata and controls
executable file
·106 lines (97 loc) · 2.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyeyeom <hyeyeom@42student.gyeongsan.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/09 15:05:44 by hyeyeom #+# #+# */
/* Updated: 2025/04/14 00:38:35 by hyeyeom ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void exit_err_msg(char *copy_str, int code, t_minish *sh)
{
ft_putstr_fd("exit\n", STDOUT_FILENO);
ft_putstr_fd("minishell: exit: ", STDOUT_FILENO);
if (copy_str)
{
ft_putstr_fd(copy_str, STDOUT_FILENO);
ft_putstr_fd(": ", STDOUT_FILENO);
}
if (code == -1)
{
*f_exitcode() = 1;
ft_putstr_fd("too many arguments\n", STDOUT_FILENO);
}
else if (code == -2)
{
*f_exitcode() = 255;
ft_putstr_fd("numeric argument required\n", STDOUT_FILENO);
}
else
*f_exitcode() = code;
free_all_envps(sh);
free_minish(sh);
exit(*f_exitcode());
}
int check_number_or_not(char *second_cmd)
{
int i;
int res;
i = 0;
res = 0;
if (second_cmd[i] == '-')
i++;
while (second_cmd[i] != '\0')
{
if (ft_isalnum(second_cmd[i]) != 4)
res = -1;
i++;
}
return (res);
}
void f_exit_args(int size, char *second_cmd, t_minish *sh)
{
unsigned char check_valid_arg;
int length_check;
int res;
if (size > 2)
exit_err_msg(NULL, -1, sh);
res = check_number_or_not(second_cmd);
if (res == -1)
{
exit_err_msg(second_cmd, -2, sh);
return ;
}
length_check = check_input_length(second_cmd);
if (length_check == -1)
{
exit_err_msg(second_cmd, 255, sh);
return ;
}
check_valid_arg = ft_atol(second_cmd);
f_exit_only(check_valid_arg, sh);
}
void f_exit_only(int exitcode, t_minish *sh)
{
ft_putstr_fd("exit\n", STDERR_FILENO);
free_all_envps(sh);
free_minish(sh);
exit(exitcode);
}
int f_exit(t_minish *sh, t_ready *rdy)
{
char **cmds;
int size;
cmds = rdy->cmd;
size = f_count_char(cmds);
if (size == 1)
{
*f_exitcode() = 0;
f_exit_only(*f_exitcode(), sh);
}
else if (size > 1)
f_exit_args(size, cmds[1], sh);
return (0);
}