-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhsh.c
More file actions
48 lines (47 loc) · 1 KB
/
Copy pathhsh.c
File metadata and controls
48 lines (47 loc) · 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
#include "holberton.h"
/**
* main - main loop of shell
* Return: 0 on success
*/
int main(void)
{
char *line, *path, *fullpath;
char **tokens;
int flag, builtin_status, child_status;
struct stat buf;
while (TRUE)
{
prompt(STDIN_FILENO, buf);
line = _getline(stdin);
if (_strcmp(line, "\n", 1) == 0)
{
free(line);
continue;
}
tokens = tokenizer(line);
if (tokens[0] == NULL)
continue;
builtin_status = builtin_execute(tokens);
if (builtin_status == 0 || builtin_status == -1)
{
free(tokens);
free(line);
}
if (builtin_status == 0)
continue;
if (builtin_status == -1)
_exit(EXIT_SUCCESS);
flag = 0; /* 0 if full_path is not malloc'd */
path = _getenv("PATH");
fullpath = _which(tokens[0], fullpath, path);
if (fullpath == NULL)
fullpath = tokens[0];
else
flag = 1; /* if fullpath was malloc'd, flag to free */
child_status = child(fullpath, tokens);
if (child_status == -1)
errors(2);
free_all(tokens, path, line, fullpath, flag);
}
return (0);
}