-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.c
51 lines (50 loc) · 1.37 KB
/
interactive.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
#include "main.h"
/**
* interactive - This function get command line a through getline in the mode
* interactive and non-interactive mode (echo / cat).
* @av: Pointer to strings with the Name of the function executed
* in the first position.
* @count_exe: Count how many times you receive a command to execute
* at that instance.
* @env: Enviroment variable.
* Return: status_exit value, if executed properly.
**/
int interactive(char *av[], int count_exe, char **env)
{
int interactive = 1, status_process = 0, i = 0, read = 0;
size_t len = 0;
char *line = NULL, *args[32], *token = NULL;
isatty(STDIN_FILENO) == 0 ? interactive = 0 : interactive;
while (1)
{
interactive == 1 ? write(STDIN_FILENO, "#cisfun$ ", 9) : interactive;
read = getline(&line, &len, stdin);
if (read == EOF)
{
free(line), write(STDIN_FILENO, "\n", 1);
return (status_process);
}
else if (_strncmp(line, "exit\n", 4) == 0)
{
free(line);
return (status_process);
}
else
{
if (_strncmp(line, "env\n", 3) == 0)
print_env(env);
else if (read > 1)
{
token = strtok(line, " \t\n\r"), args[0] = av[0];
for (i = 1; i < 32 && token != NULL; i++)
args[i] = token, token = strtok(NULL, " \t\n\r");
args[i] = NULL;
if (args[1])
{
status_process = create_process(args, count_exe, env);
}
} count_exe++;
}
}
return (status_process);
}