-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_shell.c
50 lines (46 loc) · 906 Bytes
/
simple_shell.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "simple_shell.h"
/**
* main - main function of the simple_shell
*
* @argc: number of arguments
* @argv: arguments vector
* @envp: environment variable list
* Return: Always 0
*/
int main(int __attribute__((unused)) argc, char **argv, char **envp)
{
size_t n = 0;
char *line = NULL, **tokens, *cmd_exec, *prmpt = "$ ";
while (1)
{
if (isatty(STDIN_FILENO) != 0)
{
write(STDOUT_FILENO, prmpt, _strlen(prmpt));
}
line = NULL;
readline(&line, n);
tokens = split(line, " ");
exit_shell(tokens, line);
cmd_exec = get_cmd_exec(tokens[0], envp);
if (cmd_exec == NULL)
{
print_error(argv[0], tokens[0]);
free(tokens);
free(line);
continue;
}
else
{
handle_cmd(tokens, envp, cmd_exec, line);
}
if (isatty(STDIN_FILENO) == 0)
{
exit(0);
}
}
return (0);
}