-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
50 lines (49 loc) · 1.1 KB
/
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
#include "shell.h"
/**
* main - carries out the read, execute then print output loop
* @ac: argument count
* @av: argument vector
* @envp: environment vector
*
* Return: 0
*/
int main(int ac, char **av, char *envp[])
{
char *line = NULL, *pathcommand = NULL, *path = NULL;
size_t bufsize = 0;
ssize_t linesize = 0;
char **command = NULL, **paths = NULL;
(void)envp, (void)av;
if (ac < 1)
return (-1);
signal(SIGINT, handle_signal);
while (1)
{
free_buffers(command);
free_buffers(paths);
free(pathcommand);
prompt_user();
linesize = getline(&line, &bufsize, stdin);
if (linesize < 0)
break;
info.ln_count++;
if (line[linesize - 1] == '\n')
line[linesize - 1] = '\0';
command = tokenizer(line);
if (command == NULL || *command == NULL || **command == '\0')
continue;
if (checker(command, line))
continue;
path = find_path();
paths = tokenizer(path);
pathcommand = test_path(paths, command[0]);
if (!pathcommand)
perror(av[0]);
else
execution(pathcommand, command);
}
if (linesize < 0 && flags.interactive)
write(STDERR_FILENO, "\n", 1);
free(line);
return (0);
}