-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
122 lines (118 loc) · 1.99 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "library.h"
/**
* main - Entry point
*
* Return: Always 0 (Success)
*/
int main(void)
{
launch();
return (0);
}
/**
* launch - this function starts all the procedure
*
* Return: Always 0 (Success)
*/
void launch(void)
{
char *buffer, **cmds, *cmd0;
size_t buffsize = 0;
cantLoops = 1;
signal(SIGINT, signals);
signal(SIGQUIT, SIG_IGN);
buffer = NULL;
while (1)
{
flag = 0;
if (isatty(STDIN_FILENO))
_puts("> ");
if (getline(&buffer, &buffsize, stdin) != -1)
{
if (buffer[0] != '\n' && buffer[0] != '#')
{
cmds = parse(buffer);
if (cmds[0] == NULL)
freeall(cmds);
else
{
cmd0 = cmds[0];
if (isbuiltin(cmds, buffer) == -1)
flag = 1, constructor(cmds);
if (issame(cmd0, cmds[0]) == 0)
free(cmds);
else
freeall(cmds);
}
cantLoops++;
}
}
else
{
free(buffer);
if (isatty(STDIN_FILENO))
_putchar('\n');
_exit(0);
}
}
}
/**
* isbuiltin - This function has the built-in commands
* @cmds: double pointer
* @buffer: buffer
* Return: Always 0 (Success)
*/
int isbuiltin(char **cmds, char *buffer)
{
builtins_t builtins[] = {
{"exit", exitF},
{"env", envF},
{"unsetenv", unsetF},
{"help", helpF},
{NULL, NULL}
};
int i = 0, len;
while (builtins[i].name != NULL)
{
len = _strlen(cmds[0]);
if (_strncmp(cmds[0], "exit", 4) == 0)
{
if (cmds[0][4] == 0)
{
return (builtins[0].func(cmds, buffer));
}
}