-
Notifications
You must be signed in to change notification settings - Fork 0
/
exit.c
63 lines (51 loc) · 1.25 KB
/
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
#include "builtins.h"
#include "general.h"
/**
* bin_exit - Implementation of the exit builtin
* Description: Free all the memory used and
* exit with the last status_code
*
* @info: Information about the shell
* @arguments: Arguments received
**/
void bin_exit(general_t *info, char **arguments)
{
int status_code, status;
status = _TRUE;
if (arguments[1] != NULL)
status = number_controller(info, arguments[1]);
if (status == _FALSE)
return;
status_code = info->status_code;
free_memory_pp((void **) arguments);
free_memory_p((void *) info->buffer);
free_memory_p((void *) info->environment);
free_memory_p((void *) info);
exit(status_code);
}
/**
* number_controller - Control the argument of exit
*
* @info: General information about the shell
* @number: Argument of the builtin
*
* Return: If the actual argument is valid, return _TRUE
* if not, return _FALSE
**/
int number_controller(general_t *info, char *number)
{
int _number;
_number = _atoi(number);
if (_number < 0 || contains_letter(number))
{
info->status_code = 2;
info->error_code = _CODE_ILLEGAL_NUMBER;
error_extra(info, number);
return (_FALSE);
}
if (_number > 255)
info->status_code = _number % 256;
else
info->status_code = _number;
return (_TRUE);
}