-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
52 lines (47 loc) · 1011 Bytes
/
main.c
File metadata and controls
52 lines (47 loc) · 1011 Bytes
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"
/**
* main - main shell handler.
*
* @argc: number of arguments.
* @argv: arguments' value.
*
* Return: (0) always success, (EXIT_CODE) otherwise.
*/
int main(int argc, char **argv)
{
char *buffer = NULL;
size_t bufferSize = 0;
ssize_t bufferLen = 0;
int lastSignal = 0, commandNumber = 0;
int interactiveMode = isatty(STDIN_FILENO);
Commands *commands = NULL;
if (argc > 1)
{
execFile(argv, &lastSignal);
}
else
{
signal(SIGINT, SIGINT_handler);
do {
if (interactiveMode)
{
write(STDOUT_FILENO, "($) ", 4);
fflush(NULL);
}
bufferLen = getline(&buffer, &bufferSize, stdin);
if (bufferLen == -1)
{
free(buffer);
if (interactiveMode)
write(STDOUT_FILENO, "\n", 1);
exit(lastSignal);
}
commands = parser(buffer);
commandNumber++;
execMe(commands, &lastSignal, commandNumber, argv[0], buffer);
free_commands(commands);
} while (interactiveMode || bufferLen != -1);
free(buffer);
}
return (lastSignal);
}