-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuiltin_exit.c
More file actions
61 lines (55 loc) · 1.25 KB
/
Copy pathbuiltin_exit.c
File metadata and controls
61 lines (55 loc) · 1.25 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
#include <limits.h>
#include "minishell.h"
#include "builtin.h"
#include "env.h"
#include "libft/libft.h"
static void put_exit_errmsg_and_exit(char *exit_status)
{
char *tmp;
tmp = ft_strjoin(exit_status, ": numeric argument required");
check_malloc_has_succeeded("exit", tmp);
put_minish_err_msg_and_exit(2, "exit", tmp);
}
static void exit_atol(char *str)
{
char *nptr;
long num;
int sign;
nptr = str;
sign = 1;
num = 0;
while (*nptr == ' ' || *nptr == '\t' || *nptr == '\f'
|| *nptr == '\r' || *nptr == '\n' || *nptr == '\v')
nptr++;
if (*nptr == '-')
{
sign = -1;
nptr++;
}
if (!ft_isdigit(*nptr) || is_long_overflow(nptr, sign))
put_exit_errmsg_and_exit(str);
while (ft_isdigit(*nptr))
num = num * 10 + (*nptr++ - '0');
while (*nptr == ' ' || *nptr == '\t' || *nptr == '\f'
|| *nptr == '\r' || *nptr == '\n' || *nptr == '\v')
nptr++;
if (*nptr)
put_exit_errmsg_and_exit(str);
exit((sign * num) & 255);
}
/*
* builtin exit command
* argv: ["exit", "0"]
*/
int builtin_exit(char **argv)
{
int argv_len;
argv_len = ptrarr_len((void **)argv);
if (argv_len > 2)
return (put_minish_err_msg_and_ret(1, "exit", "too many arguments"));
else if (argv_len == 2)
exit_atol(argv[1]);
else
exit(g_shell.status);
return (0);
}