-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_exit.c
86 lines (78 loc) · 2.05 KB
/
ms_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <dlana@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/17 15:15:18 by obeedril #+# #+# */
/* Updated: 2022/03/13 17:53:57 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int ft_isnum(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] < 48 || str[i] > 57)
{
if (str[0] == 45)
return (0);
return (1);
}
i++;
}
return (0);
}
static void exit_with_args_2(t_data *data, int num, char *exit_arg, char **line)
{
long long int exit_code;
exit_code = 0;
if (num > 2)
{
data->num_error = 1;
ms_print_error_builtin(NULL, 5);
}
else
{
exit_code = ft_ll_atoi(exit_arg);
if (exit_code == -1)
{
ms_print_error_builtin(exit_arg, 4);
ms_free_all(data, line);
exit (255);
}
else
{
ft_putstr_fd("\bexit\n", 2);
ms_free_all(data, line);
exit (exit_code);
}
}
}
static void exit_with_args(t_data *data, int num, char *exit_arg, char **line)
{
long long int exit_code;
exit_code = 0;
if (ft_isnum(exit_arg) == 1)
{
ms_print_error_builtin(exit_arg, 4);
ms_free_all(data, line);
exit (255);
}
else
exit_with_args_2(data, num, exit_arg, line);
}
void ms_exit(t_data *data, int num, char *exit_arg, char **line)
{
if (!exit_arg)
{
ft_putstr_fd("\bexit\n", 2);
ms_free_all(data, line);
exit(data->num_prev_error);
}
else
exit_with_args(data, num, exit_arg, line);
}