-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinite_loop.c
53 lines (51 loc) · 1.26 KB
/
infinite_loop.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
#include "main.h"
/**
* catch_signal - signal handler
* @number: the number of signal
*/
void catch_signal(int number)
{
if (number == SIGINT)
{
write(STDOUT_FILENO, "\n ", 1);
exit(EXIT_SUCCESS);
}
}
/**
* infinite_loop - function to make the shell always open
* inside the loop we get string, split it and execute it
* @program_path: the program path
* Return: status of execution
*/
int infinite_loop(char *program_path)
{
int status = 0;
char **args = NULL;
int proccess_counter = 1;
char *string = NULL;
size_t string_size = 0;
char **paths = get_all_paths();
signal(SIGINT, catch_signal);
/*print prompt if user run the program*/
if (isatty(STDIN_FILENO) != 0)
write(STDOUT_FILENO, "m$ ", 3);
/* if not or get the command from pipe*/
/*it will start execute the command*/
while ((_getline(&string, &string_size)))
{
/* split arguments && execution the commands*/
sh(string, args, paths, proccess_counter, program_path, &status);
proccess_counter++;
fflush(stdout);
/*check non interactive mood*/
if (isatty(STDIN_FILENO) != 0)
write(STDOUT_FILENO, "m$ ", 3);
}
/*check EOF*/
/*the only way you can arrive here with EOF = -1*/
if (isatty(STDIN_FILENO) != 0)
write(STDOUT_FILENO, "\n", 1);
free_all(args, string, paths);
free_array_of_pointers(environ);
return (status);
}