-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.c
More file actions
51 lines (45 loc) · 1.2 KB
/
shell.c
File metadata and controls
51 lines (45 loc) · 1.2 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
51
#include "shell.h"
/**
* main - main function.
* @ac: number args passed to main
* @av: array of args passed to main
* @env: array of var env
* Return: Always 0 (Succes).
*/
int main(int ac, char **av, char **env)
{
int i = 1, interactive = 0;
char *path = _findpath_(env), **tok_path;
char *line = NULL, *EXIT = "exit", *ENV = "env", **array_token;
size_t len = 1024;
(void) ac, (void) av;
line = (char *)malloc(len * sizeof(char));
if (line == NULL)
perror("Unable allocate buffer"), exit(EXIT_FAILURE);
while (1)
{
if (isatty(STDIN_FILENO) == 1)
interactive = 1, write(1, "(devsjp@holberton $) ", 22);
if ((getline(&line, &len, stdin)) == -1)
{
if (interactive)
write(1, "\n", 1);
free(path), _exit_(line);
}
if (line[0] != 10)
{
array_token = _strtok_(line, ' '), tok_path = _strtok_(path, ':');
if (!_strcmp_(array_token[0], EXIT))
free(path), _freestrs_(array_token), _freestrs_(tok_path), _exit_(line);
if (!_strcmp_(array_token[0], ENV))
_env_(env);
else if (!_builtin_(tok_path, array_token[0]))
_execute_(array_token);
else
_notfound_(av[0], i, array_token[0]);
_freestrs_(array_token), _freestrs_(tok_path);
}
i++;
}
return (0);
}