-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilt_in_exit.c
More file actions
47 lines (43 loc) · 1.15 KB
/
built_in_exit.c
File metadata and controls
47 lines (43 loc) · 1.15 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
#include "main.h"
/**
* ___exit - exits the shell.
*
* @CMD: Command type single list.
* @lastSignal: last signal from executed functions.
* @cmdNum: the executed Command number.
* @programName: the running executable name.
* @buffer: the original buffer from getline function.
*
* Return: (0) always success, (EXIT_CODE) otherwise.
*/
int ___exit(Commands *CMD, int *lastSignal, int cmdNum,
char *programName, char *buffer)
{
int argsLen, status;
char **command = CMD->cmd;
char *msg = ": exit: Illegal number: ", *cmdNumStr;
for (argsLen = 0; command[argsLen];)
argsLen++;
fflush(NULL);
free(buffer);
if (argsLen > 1)
{
cmdNumStr = _btoi(cmdNum);
status = _atoi(command[1]);
if ((status == 0 && command[1][0] != '0') || status < 0)
{
write(STDERR_FILENO, programName, _strlen(programName));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, cmdNumStr, _strlen(cmdNumStr));
write(STDERR_FILENO, msg, _strlen(msg));
write(STDERR_FILENO, command[1], _strlen(command[1]));
write(STDERR_FILENO, "\n", 1);
status = 2;
}
free(cmdNumStr);
free_commands(CMD);
exit(status);
}
free_commands(CMD);
exit(*lastSignal);
}