-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodes.c
More file actions
105 lines (96 loc) · 1.92 KB
/
modes.c
File metadata and controls
105 lines (96 loc) · 1.92 KB
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
97
98
99
100
101
102
103
104
105
#include "shell.h"
/**
* non_interactive - non interactive mode
* @agv: argument variable
* @cmd: command argument
* @cmd_count: Number of commands passed
* @stream: stream
* @status: status
* @argv: argument variable
* @colon: Command seperator
*
* Return: Nothing
*/
void non_interactive(char **agv, char *cmd, size_t cmd_count, FILE *stream,
int status, char **argv, char **colon)
{
ssize_t bytes_read;
size_t n;
char st;
n = 0;
st = !isatty(STDIN_FILENO);
(void)agv;
do {
bytes_read = getline(&cmd, &n, stream);
if (bytes_read != 1)
{
remov(cmd);
rm_white_space(cmd);
if (empty(cmd))
{
cmd_count++;
continue;
}
if (*cmd == '\n')
continue;
handle_comment(cmd);
cmd_count++;
if (*cmd != '\0')
execute(cmd, agv, argv, cmd_count, st, status, colon);
if (st)
break;
if (!st)
continue;
}
} while (bytes_read != -1);
exit(status);
}
/**
* interactive - interactive mode
* @argc: argument count
* @agv: argument variable
* @cmd: command argument
* @cmd_count: Number of commands passed
* @stream: stream
* @status: status
* @argv: argument variable
* @colon: Command seperator
*
* Return: Nothing
*/
void interactive(int argc, char **agv, char *cmd, size_t cmd_count,
FILE *stream, int status, char **argv, char **colon)
{
ssize_t bytes_read;
size_t n;
char st;
(void)agv;
st = !isatty(STDIN_FILENO);
while (1 && isatty(STDIN_FILENO) && (!st || bytes_read != 0))
{
cmd_count++;
st = !isatty(STDIN_FILENO);
if (argc == 1 && !st)
write(STDOUT_FILENO, "$ ", 2);
bytes_read = getline(&cmd, &n, stream);
if (bytes_read == -1)
{
free(cmd);
write(STDOUT_FILENO, "\n", 1);
break;
}
rm_white_space(cmd);
remov(cmd);
if (empty(cmd))
continue;
if (*cmd == '\n')
continue;
handle_comment(cmd);
if (*cmd != '\0')
execute(cmd, agv, argv, cmd_count, st, status, colon);
if (!st)
continue;
if (st)
break;
}
}