-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.1noninteractive.c
48 lines (47 loc) · 1.15 KB
/
2.1noninteractive.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
#include "shell.h"
/**
* noninteractive - shell in non interactive mode (not user interaction)
* @env: enviroment of the system by default
* Return: 0 on success or -1 if it fails
*/
int noninteractive(char **env)
{
char *line = NULL, **commands = NULL;
size_t n;
ssize_t read;
pid_t child_pid;
int status;
bool execute = false;
/* ========== S H E L L P R O M P T ==========*/
signal(SIGINT, signalhandle);
while ((read = getline(&line, &n, stdin)))
{
if (read == EOF) /*=== CHECK FOR END OF FILE ====*/
ctrl_c(line);
commands = parse_input(line);
child_pid = fork();
if (child_pid == -1)
fork_error(line, commands);
if (child_pid == 0)
{
execute = execute_cmd(commands, line, env);
if (execute == false)
excutefalse(line, commands);
}
else /* ====== if it is parent ====*/
{
wait(&status);
if (commands == NULL)/*free(line);*/
free_arrays(commands);
else if ((_strcmp("exit", commands[0])) == 0)
exit_shell(line, commands);
else/*free(line);*/
free_arrays(commands);
}
n = 0; /*line = NULL;*/
if (isatty(STDIN_FILENO) == 1)
write(STDOUT_FILENO, PROMPT, 2);
free(line);
}
return (0);
}