-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit.c
58 lines (50 loc) · 907 Bytes
/
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
#include "shell.h"
/**
* exit_builtin - handles the exit built-in
* @cmd: the entered command
* @status: the status passed wirh the exit
*/
void exit_builtin(char *cmd, char *status)
{
int i = 0, exit_status;
if (status == NULL)
{
exit(0);
}
while (status[i] != '\0')
{
if (!(status[i] >= '0' && status[i] <= '9'))
{
print_error(_getenv("_"));
print_error(": 1: exit: Illegal number: ");
write(2, status, strlen(status));
write(2, "\n", 1);
return;
}
i++;
}
exit_status = _atoi(status);
free(cmd);
cmd = NULL;
exit(exit_status);
}
/**
* _atoi - coverts string to integer
* @str: the string to be converted
* Return: the integer
*/
int _atoi(char *str)
{
int result = 0, sign = 1, i = 0;
if (str[0] == '-')
{
sign = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + (str[i] - '0');
++i;
}
return (sign * result);
}