-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
66 lines (55 loc) · 930 Bytes
/
main.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
65
66
#include "main.h"
/**
* main - Entry point for the shell
*
* Return: 0 on success, or the exit status of the command on failure
*/
int main(void)
{
char *command = NULL, **argv_exec;
size_t len = 0;
set_sigint_handler();
while (1)
{
prompt();
if (read_command(&command, &len) == NULL)
{
if (feof(stdin))
{
free_command_cache();
exit(0);
}
continue;
}
if (strcmp(command, "exit") == 0)
{
free(command);
free_command_cache();
builtin_exit(NULL);
}
if (command[0] != '\0')
{
argv_exec = parse_command(command);
if (argv_exec == NULL || argv_exec[0] == NULL)
{
free(argv_exec);
free(command);
command = NULL;
continue;
}
if (strcmp(argv_exec[0], "env") == 0)
{
print_env();
}
else
{
execute_command(argv_exec);
}
free(argv_exec);
}
free(command);
command = NULL;
}
free_command_cache();
return (0);
}