-
Notifications
You must be signed in to change notification settings - Fork 0
/
bi_exit.c
130 lines (119 loc) · 3.2 KB
/
bi_exit.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bi_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wlalaoui <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 16:07:55 by wlalaoui #+# #+# */
/* Updated: 2024/01/19 11:12:03 by aoizel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void clean_envlst(t_envlst **envlst)
{
t_envlst *temp;
t_envlst *curr;
if (!envlst || !*envlst)
return ;
curr = *envlst;
while (curr != NULL)
{
temp = curr;
curr = curr->next;
free(temp->value);
free(temp->key);
free(temp);
}
*envlst = NULL;
}
int free_shell(t_shell *shell_to_free)
{
int ret;
ret = clean_processlst(&shell_to_free->processlst, shell_to_free);
clean_wordlst(&shell_to_free->wordlst);
clean_envlst(&shell_to_free->envlst);
free_envarray(shell_to_free->envarray);
free(shell_to_free->line);
free(shell_to_free->prompt);
free(shell_to_free);
rl_clear_history();
return (ret);
}
void check_digits(char **args, t_shell *shell_to_free, int fd_out)
{
size_t i;
i = 1;
if (!ft_isdigit(args[1][0]) && !(args[1][0] == '+' || args[1][0] == '-'))
{
ft_putstr_fd("exit\n", fd_out);
ft_putstr_fd("exit: ", 2);
ft_putstr_fd(args[1], 2);
ft_putstr_fd(": numeric argument required\n", 2);
free_shell(shell_to_free);
exit(2);
}
while (args[1][i])
{
if (!ft_isdigit(args[1][i]))
{
ft_putstr_fd("exit\n", fd_out);
ft_putstr_fd("exit: ", 2);
ft_putstr_fd(args[1], 2);
ft_putstr_fd(": numeric argument required\n", 2);
free_shell(shell_to_free);
exit(2);
}
i++;
}
}
int piped_check_digits(char **args)
{
size_t i;
i = 1;
if (!args[1])
return (0);
if (args[2])
return (ft_putstr_fd("exit: too many arguments\n", 2), 2);
if (!ft_isdigit(args[1][0]) && !(args[1][0] == '+' || args[1][0] == '-'))
{
ft_putstr_fd("exit: ", 2);
ft_putstr_fd(args[1], 2);
return (ft_putstr_fd(": numeric argument required\n", 2), 2);
}
while (args[1][i])
{
if (!ft_isdigit(args[1][i++]))
{
ft_putstr_fd("exit: ", 2);
ft_putstr_fd(args[1], 2);
return (ft_putstr_fd(": numeric argument required\n", 2), 2);
}
}
return (ft_atoi(args[1]));
}
int bi_exit(t_shell *shell_to_free, char **args, int is_piped)
{
int number;
if (!is_piped)
{
if (!args || !args[1])
{
number = free_shell(shell_to_free);
ft_putstr_fd("exit\n", 1);
exit(number & 255);
}
check_digits(args, shell_to_free, 1);
if (args[2])
{
ft_putstr_fd("exit\n", 1);
ft_putstr_fd("exit: too many arguments\n", 2);
return (1);
}
number = ft_atoi(args[1]);
free_shell(shell_to_free);
ft_putstr_fd("exit\n", 1);
exit(number & 255);
}
return (piped_check_digits(args));
}