-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexit_command.c
64 lines (56 loc) · 959 Bytes
/
exit_command.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"
#include <unistd.h>
/**
* conv_int - converets str to int
* @s: string
* Return: -1 or 0
*/
int conv_int(const char *s)
{
int sign = 1, digit;
long int result = 0;
if (*s == '-')
{
sign = -1;
s++;
}
else if (*s == '+')
{
s++;
}
while (*s != '\0')
{
if (*s < '0' || *s > '9')
return (-1);
digit = *s - '0';
result = result * 10 + digit;
if ((sign == 1 && result > INT_MAX) || (sign == -1 && -result < INT_MIN))
{
return (-1);
}
s++;
}
return (sign * result);
}
/**
* exit_command - implement exit command
* @args: arguments
* Return: Nothing.
*/
void exit_command(char **args)
{
int exit_status = 0;
char *message = "Invalid exit status: ";
if (args[1])
{
exit_status = conv_int(args[1]);
if (exit_status < 0)
{
write(STDERR_FILENO, message, _strlen(message));
write(STDERR_FILENO, args[1], _strlen(args[1]));
write(1, "\n", 1);
return;
}
}
_exit(exit_status);
}