-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_exiter.c
64 lines (53 loc) · 1.11 KB
/
shell_exiter.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
#include "shell.h"
/**
* exit_handler - Checks if user entered the exit command
* @buff: input
* @list_cmd: list of commands
* @cmd: input parsed as array of commands
*
* Return: 0 if the commad is NOT exit, -1 if the exit status was Illegal
*/
int exit_handler(char *buff, char **list_cmd, char **cmd)
{
int status;
if (cmd[0] == NULL || strcmp(cmd[0], "exit") != 0)
return (0);
if (cmd[1] == NULL)
{
write_hist();
allc_free(buff, list_cmd, cmd, FL_BUFF | FL_CMDS);
if (*code_exiter() == 127)
exit(2);
exit(0);
}
status = check_exit(cmd[1]);
if (status >= 0)
{
write_hist();
allc_free(buff, list_cmd, cmd, FL_BUFF | FL_CMDS);
exit(status);
}
print_builtin_error("exit: Illegal number: ", cmd[1]);
return (-1);
}
/**
* check_exit - Calculates the exit status
* @buff: input
*
* Return: Exist status as number, -1 on error
*/
int check_exit(char *buff)
{
int i;
int status = 0;
for (i = 0; buff[i] != '\0'; i++)
{
if (buff[i] == '\n')
return (status);
if (buff[i] < '0' || buff[i] > '9')
return (-1);
status *= 10;
status += buff[i] - '0';
}
return (status);
}