-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_builtin_exit.c
91 lines (84 loc) · 2.58 KB
/
ft_builtin_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_builtin_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/07 22:38:20 by abenamar #+# #+# */
/* Updated: 2023/09/13 16:42:35 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static uint8_t ft_check_first_argument(char *str, uint8_t silent)
{
size_t i;
i = 0;
if (str[0] == '-' || str[0] == '+')
++i;
while (str[i])
{
if (!ft_isdigit(str[i]))
{
if (!(str[i]))
return (1);
if (!silent && isatty(STDIN_FILENO))
(printf("exit\n"), \
ft_pstderr3("exit: ", str, ": numeric argument required"));
return (0);
}
++i;
}
return (1);
}
static uint8_t ft_handle_first_argument(char *str, uint8_t silent)
{
size_t i;
long n;
char *tmp;
i = 0;
if (!str || !(str[i]))
{
if (!silent && isatty(STDIN_FILENO))
(printf("exit\n"), \
ft_pstderr3("exit: ", str, ": numeric argument required"));
return (free(str), 0);
}
if (!ft_check_first_argument(str + i, silent))
return (free(str), 0);
n = ft_atol(str);
tmp = ft_ltoa(n);
if (!tmp)
return (free(str), 0);
if (!ft_strncmp(str, tmp, ft_strlen(str) + 1))
return (free(str), free(tmp), 1);
if (!silent && isatty(STDIN_FILENO))
(printf("exit\n"), \
ft_pstderr3("exit: ", str, ": numeric argument required"));
return (free(str), free(tmp), 0);
}
int ft_builtin_exit(char *cmd, char **argv, t_list **env, uint8_t silent)
{
int code;
(void) cmd;
if (!(argv[1]))
{
if (!silent && isatty(STDIN_FILENO))
printf("exit\n");
return (g_signum = SIGTERM, ft_env_geti(env, "?"));
}
if (!ft_handle_first_argument(ft_strtrim(argv[1], " "), silent))
return (g_signum = SIGTERM, 2);
if (argv[1] && argv[2])
{
if (!silent && isatty(STDIN_FILENO))
(printf("exit\n"), ft_pstderr("exit: too many arguments"));
return (EXIT_FAILURE);
}
code = ft_atoi(argv[1]) % 256;
if (code < 0)
code += 256;
if (!silent && isatty(STDIN_FILENO))
printf("exit\n");
return (g_signum = SIGTERM, code);
}