-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_hell.c
More file actions
50 lines (47 loc) · 1.1 KB
/
s_hell.c
File metadata and controls
50 lines (47 loc) · 1.1 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
#include "shellhead.h"
/**
* main - Program that functions as command line interpreter
* @ac: Args count
* @av: Args variable array
* Return: 0 on success or -1 on failure
*/
int main(int ac, char **av)
{
pid_t child;
int builtin = 0;
shellstruct sh = {NULL, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL};
int ext;
initialize(&sh, ac, av);
while (sh.get >= 0)
{
sh.buf[_strlen(sh.buf) - 1] = '\0';/** \n */
commandparser(&sh);/** tokenize input strings */
if (!(_strcmp(sh.cmd[0], "")))
{ /** if no tokens, input was spaces or \n */
freecmd(&sh), prompt(&sh);
continue;
}
builtin = builtins(&sh);
if (builtin == 1)
{ /** a builtin matched and executed, free and reprompt */
freecmd(&sh);
builtin = 0, prompt(&sh);
continue;
}
child = fork();
if (child == -1)
perror("Fork failed.\n"), exit(1);
if (child == 0) /** child process */
_execve(&sh);
else/** parent*/
{
wait(&sh.stat);
freecmd(&sh), prompt(&sh);
}
}
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "\n", 1);
freehelper(&sh);
ext = WEXITSTATUS(sh.stat); /** exit status from child */
return (ext);
}