-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit_builtin.c
52 lines (47 loc) · 1.13 KB
/
exit_builtin.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
#include "main.h"
#include "utilities.h"
#include <stdio.h>
#include <stdlib.h>
/**
* exit_builtin - Handle the 'exit' built-in command.
* @args: Array of arguments passed to the command.
* @argc: Number of arguments in 'args'.
* @command: The original command string.
*
* This function is responsible for processing the 'exit' command.
* If 'argc' is 1, it performs a clean exit. If 'argc' is greater than 1
* and the argument is a positive integer, it exits with that value.
*
* @args and @command are freed before exiting the program.
*/
void exit_builtin(char **args, int argc, char *command) {
int status = 0;
(void)argc;
if (args[1] != NULL) {
status = _atoi(args[1]);
if (status != 1) {
if (envfreesignal == 1) {
free(environ);
free(newvariable);
}
free(args);
free(command);
args = NULL;
command = NULL;
exit(status);
}
errno = 2;
perror("Illegal number");
return;
}
if (envfreesignal == 1) {
free(environ);
free(newvariable);
}
status = errno;
free(args);
free(command);
args = NULL;
command = NULL;
exit(status);
}