forked from gutudanii/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_shell.c
71 lines (65 loc) · 1.11 KB
/
simple_shell.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"
/**
* test - function used in refactor
* @p: struct with all vars
* Return: int
*/
int test(params *p)
{
int tkn;
char *aux = NULL;
aux = _strchr(p->buff, '\n');
if (aux != NULL)
*aux = '\0';
tkn = 0;
p->argv = _calloc(128, 8);
if (!p->argv)
return (-1);
aux = strtok(p->buff, " ");
if (!aux)
return (free(p->argv), 1);
while (aux)
{
p->argv[tkn++] = aux;
aux = strtok(NULL, " ");
}
simple_exec(p);
free(p->argv);
return (1);
}
/**
* main - our own UNIX cli
* @ac: arguments cound
* @av: argument array
* Return: 0
*/
int main(int __attribute__((unused)) ac, char **av)
{
int error = 0;
int loop = 1;
size_t len = 0;
params p;
p.argv = NULL;
p.buff = NULL;
p.cmd = NULL;
p.name = av[0];
p.exit_value = 0;
p.loop = &loop;
signal(SIGINT, signal_exit);
signal(SIGTSTP, SIG_IGN);
while (error != EOF)
{
if (isatty(STDIN_FILENO))
write(1, "hsh$ ", 5);
error = getline(&p.buff, &len, stdin);
if (error == -1)
break;
if (error != 1)
error = test(&p);
loop++;
}
free(p.buff);
if (isatty(STDIN_FILENO))
write(1, "\n", 1);
return (p.exit_value);
}