Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/execution/my_mess/my_exit.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* my_exit.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/08/29 10:52:05 by ssabbaji #+# #+# */
9
/* Updated: 2022/09/30 16:01:04 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../../minishell.h"
14
15
int display_error(char *cmd)
16
{
17
printf("bash: exit: %s :numeric argument required\n", cmd);
18
return (255);
19
}
20
21
int ft_isdigit(char *num)
22
{
23
int i;
24
25
i = 0;
26
while (num[i])
27
{
28
if (num[i] >= '0' && num[i] <= '9')
29
return (1);
30
i++;
31
}
32
return (0);
33
}
34
35
long long ft_atoi_long(char *str)
36
{
37
int i;
38
int n;
39
long long res;
40
41
i = 0;
42
res = 0;
43
n = 1;
44
while (str[i] <= 32)
45
i++;
46
if (str[i] == '-')
47
n = -1;
48
if (str[i] == '-' || str[i] == '+')
49
i++;
50
while (str[i] != '\0' && str[i] >= '0' && str[i] <= '9')
51
{
52
res *= 10;
53
res += str[i] - '0';
54
if (res < 0)
55
return (display_error(str));
56
i++;
57
}
58
return (res * n);
59
}
60
61
int my_exit(t_cmd *lst_cmd, int old_error)
62
{
63
char **cmd;
64
int exit_stat;
65
66
exit_stat = g_vars.g_exit_stat;
67
cmd = lst_cmd->cmd;
68
printf("exit\n");
69
if (cmd[1])
70
{
71
if (cmd[2])
72
return (printf("bash: exit: too many arguments\n"), 1);
73
else if (ft_isdigit(cmd[1]))
74
exit_stat = ft_atoi_long(cmd[1]);
75
else
76
display_error(cmd[1]);
77
}
78
else
79
exit(old_error);
80
exit((unsigned char)exit_stat);
81
}
82
83