-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·46 lines (44 loc) · 807 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
#include "main.h"
/**
* main - main function for shell
* Return: int
*/
int main(void)
{
ssize_t num_char_read;
size_t n;
char *line_ptr = NULL;
char **tokens;
int state, loop_count = 0;
while (1)
{
loop_count++;
if (isatty(STDIN_FILENO))
printf("$ ");
num_char_read = _getline(&line_ptr, &n);
if (num_char_read == -1)
{
if (line_ptr)
free(line_ptr);
exit(0);
}
line_ptr = trim_spaces(line_ptr);
if (!line_ptr)
{
free(line_ptr);
continue; }
if (_strcmp(line_ptr, "exit") == 0)
{
free(line_ptr);
line_ptr = NULL;
exit(0); } /* line end */
tokens = create_tokens(line_ptr);
if (tokens && tokens[0])
{
execute_command(tokens);
if (tokens)
free_tokens(tokens);
wait(&state); } /* line end */
}
free(line_ptr);
return (state); }