-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.c
71 lines (58 loc) · 1.39 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "shell.h"
/**
* interactive_mode - Interactive commandline interpeter
*/
void interactive_mode(void)
{
char **command;
int loop = 0, built_in_check = 1;
signal(SIGINT, signal_handler);
while (++loop)
{
show_prompt();
command = _getline();
if (command == NULL)
continue;
built_in_check = built_ins(command, loop);
if (built_in_check == 0)
{
free_dp(command);
continue;
}
interpeter(command, loop);
free_dp(command);
}
}
/**
* show_prompt - Shows a prompt on the stdout
*/
void show_prompt(void)
{
char *identifier = "[\033[31m#FAKE SHELL\033[37m] /.../";
char *current;
int i, j, k = 0, last_index;
for (i = 0; *(environ + i); i++)
{
if (_strncmp(*(environ + i), "PWD=", 4))
{
for (j = 0; *(*(environ + i) + j); j++)
if (*(*(environ + i) + j) == 47)
last_index = j;
k = (_strlen(identifier)) + (_strlen(*(environ + i) + last_index + 1)) + 4;
current = malloc(k * sizeof(char));
if (current == NULL)
return;
for (k = 0; k < _strlen(identifier); k++)
*(current + k) = *(identifier + k);
for (j = 0; j < _strlen((*(environ + i) + last_index + 1)); j++)
*(current + k + j) = (*(*(environ + i) + last_index + 1 + j));
*(current + k + j) = 32;
*(current + k + j + 1) = 36;
*(current + k + j + 2) = 32;
*(current + k + j + 3) = 0;
break;
}
}
write(STDOUT_FILENO, current, _strlen(current));
free(current);
}