-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
49 lines (46 loc) · 800 Bytes
/
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
#include "shell.h"
/**
* main - takes a command and execute it
* Return: integer
**/
int main(void)
{
int i;
pid_t pid;
char *command, **tokens;
size_t bufferSize = 1024;
while (1)
{
write(1, "$ ", 2);
command = malloc(1024 * sizeof(char));
_getline(command, bufferSize);
if (command)
{
if (strcmp(command, "exit") == 0)
{
_exitShell(command);
}
tokens = tokenizeTheCommand(command);
pid = fork();
if (pid == -1)
{
write(1, "hsh: Failed to fork the process\n", 31);
}
else if (pid == 0)
{
if (strcmp(tokens[0], "env") == 0)
printEnv();
else
executeCommand(tokens);
exit(0);
}
else
wait(NULL);
for (i = 0; tokens[i] != NULL; i++)
free(tokens[i]);
free(tokens);
}
free(command);
}
return (0);
}