-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
79 lines (66 loc) · 1.5 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
75
76
77
78
79
#include "main.h"
/**
* check_and_launch_file - Function to check if the file exists
* and is executable and launch it if it is. Otherwise,
* print an error message.
* @args: command cahr pointer
* @argv: arg list container
* @isterm: if it's a tty (isatty)
* @iter: command run count
* Return: errno (last status) on success or not
*/
void check_and_launch_file(char **args, char **argv, int isterm, int iter)
{
char *get_cmd = path(get_envpath(), args[0]);
if (get_cmd == NULL)
{
print_error((iter + '0'), args[0], "not found");
if (isterm != 1)
exit(127);
errno = 127;
return;
}
launch_one(args, argv, get_cmd);
}
/**
* main - entry point
* @argv: array with size of argc
* @argc: the size of argv
* @env: environment path
* Return: errno (last status) on success or not
*/
int main(int argc, char **argv, char **env)
{
char *lineptr = NULL, **args = NULL;
size_t n = 0;
ssize_t value;
int isterm = isatty(0), iter = 0;
(void)argc, (void)env, argv = NULL;
errno = 0;
while (1)
{
iter++;
(isterm == 1) ? print_string("$ ") : 0;
value = getline(&lineptr, &n, stdin);
if (value == -1)
handle_ctrld(value, &lineptr);
handle_htag(lineptr);
args = token_string(lineptr);
if (args[0] == NULL)
{
free(args);
continue;
}
if (access(args[0], X_OK) == -1)
{
if (builtin_handler(args, lineptr) != 0)
continue;
check_and_launch_file(args, argv, isterm, iter);
continue;
}
launch_two(args, argv);
continue;
}
free(lineptr);
return (errno);
}