-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_exit.c
More file actions
67 lines (57 loc) · 1.34 KB
/
f_exit.c
File metadata and controls
67 lines (57 loc) · 1.34 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
62
63
64
65
66
67
#include "shell.h"
/**
* handle_exit - Checks if the user entered the exit command
* @buff: User's input
* @cmds_list: Array of parsed commands
* @commands: User's input parsed as array of commands
*
* Return: 0 if the commad is NOT exit, -1 if the exit status was Illegal
*/
int handle_exit(char *buff, char **cmds_list, char **commands)
{
int status;
/* Command is NOT exit */
if (commands[0] == NULL || _strcmp(commands[0], "exit") != 0)
return (0);
/* Command is exit */
if (commands[1] == NULL)
{
write_history();
free_allocs(buff, cmds_list, commands, F_BUFF | F_CMDS);
if (*process_exit_code() == 127)
exit(2);
exit(0);
}
status = get_exit_status(commands[1]);
/* Command is exit status */
if (status >= 0)
{
write_history();
free_allocs(buff, cmds_list, commands, F_BUFF | F_CMDS);
exit(status);
}
/* the exit status passed was illegal */
print_builtin_error("exit: Illegal number: ", commands[1]);
return (-1);
}
/**
* get_exit_status - Calculates the exit status as a number
* @buff: User's input
*
* Return: Exist status as number, -1 on error
*/
int get_exit_status(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);
}