-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
79 lines (74 loc) · 1.26 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
#include <signal.h>
#include <unistd.h>
#include "main.h"
/**
* main - Simple Shell (Hsh)
* @argc: Argument Count
* @argv:Argument Value
* Return: Exit Value By Status
*/
int main(__attribute__((unused)) int argc, char **argv)
{
char *input, **cmd;
int counter = 0, status = 1, st = 0;
if (argv[1] != NULL)
read_file(argv[1], argv);
signal(SIGINT, signal_to_handle);
while (status)
{
if (isatty(STDIN_FILENO))
prompt();
input = _getline();
++counter;
if (input[0] == '\0')
{
continue;
}
history(input);
cmd = parse_cmd(input);
if (_strcmp(cmd[0], "exit") == 0)
{
ex_it(cmd, input, argv, counter);
}
else if (check_builtin(cmd) == 0)
{
st = handle_builtin(cmd, st);
free_all(cmd, input);
continue;
}
else
{
st = check_cmd(cmd, input, counter, argv);
}
free_all(cmd, input);
}
return (status);
}
/**
* check_builtin - check builtin
*
* @cmd:command to check
* Return: 0 success -1 Fail
*/
int check_builtin(char **cmd)
{
bul_t fun[] = {
{"cd", NULL},
{"help", NULL},
{"echo", NULL},
{"history", NULL},
{NULL, NULL}
};
int i = 0;
if (*cmd == NULL)
{
return (-1);
}
while ((fun + i)->command)
{
if (_strcmp(cmd[0], (fun + i)->command) == 0)
return (0);
i++;
}
return (-1);
}