-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
74 lines (68 loc) · 1.48 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
#include "shell.h"
/**
* INThandler - handles signals and write the prompt
*@sig: signal to handle
*Return: Nothing (void)
*/
void INThandler(int sig)
{
(void)sig;
write(STDOUT_FILENO, "\n$ ", 3);
}
/**
* print_dollar - Function to print the dollar sign
*Return: Nothing(void)
*/
void print_dollar(void)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "$ ", 2);
}
/**
* main - principal function to run the shell
*@argc: argument count
*@argv: argument vector
*@env: enviroment variables
*Return: 0 on exit, 1 if any failures happen
*/
int main(int argc, char **argv, char **env)
{
char *buffer, **commands;
size_t length;
ssize_t characters;
pid_t pid;
int status, count;
(void)argc;
buffer = NULL, length = 0, count = 0;
/*write promt only if it's from standard input*/
print_dollar();
/*infinite loop*/
while ((characters = getline(&buffer, &length, stdin)))
{
/*signal kill for contro+c */
signal(SIGINT, INThandler);
/*check the end of file*/
if (characters == EOF)
end_file(buffer);
count++;
/*collect commands from the prompt and store in double pointer*/
commands = array_strtok(buffer);
/*create new process*/
pid = fork();
if (pid == -1)
fork_fail();
if (pid == 0)
execute(commands, buffer, env, argv, count);
/*free everithing*/
else
{
wait(&status);
send_to_free(buffer, commands);
}
length = 0, buffer = NULL; /*reset for getline*/
print_dollar();
}
if (characters == -1)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}