-
Notifications
You must be signed in to change notification settings - Fork 1
/
execution.c
96 lines (88 loc) · 1.66 KB
/
execution.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "shell.h"
/**
* interactive - Handles interactive mode of the shell.
* @line: Pointer to the input line from the user.
* @last_exit: exit status
* Return: 0 to continue looping
*/
int interactive(char *line, int *last_exit)
{
ssize_t nread;
size_t line_size = 0;
char *command = NULL;
int check = errno, flag = 0;
char *argv[MAX];
char *nnread = NULL;
if (isatty(STDIN_FILENO))
write(1, "$ ", 2);
nread = getline(&line, &line_size, stdin);
if (checkbuiltins(check, line, nread, *last_exit) == 0)
return (0);
if (tokenize(nnread, argv, line) == 0)
return (0);
if (access(argv[0], X_OK) != 0)
{
flag = 1;
if (nopath(command, argv, last_exit) == 0)
{
free(line);
return (0);
}
}
if (access(argv[0], X_OK) == 0)
*last_exit = forking(argv, 1, line);
else
{
perror(argv[0]);
return (0);
}
if (line)
free(line);
if (argv[0] && flag == 1)
free(argv[0]);
return (1);
}
/**
* non_interactive - Handles non-interactive mode of the shell.
* @line: Pointer to the input line from the user.
*
*/
void non_interactive(char *line)
{
ssize_t nread;
size_t line_size = 0;
char *command = NULL;
int check = errno;
char *argv[MAX];
char *nnread = NULL;
int i = 0;
nread = getline(&line, &line_size, stdin);
if (tokenize(nnread, argv, line) == 0)
return;
while (argv[i] != NULL)
{
if (checkbuiltins(check, argv[i], nread, 0) == 0)
{
i++;
continue;
}
if (access(argv[i], X_OK) != 0)
if (nopath(command, &argv[i], NULL) == 0)
{
i++;
continue;
}
if (access(argv[i], X_OK) == 0)
{
forking(&argv[i], 2, line);
}
else
{
free(line);
perror(argv[i]);
i++;
continue;
}
i++;
}
}