-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
80 lines (79 loc) · 1.35 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
80
#include "shell.h"
/**
* _putchar_cisfun - check the code.
* Return: void.
*/
void _putchar_cisfun(void)
{
_putchar('#');
_putchar('c');
_putchar('i');
_putchar('s');
_putchar('f');
_putchar('u');
_putchar('n');
_putchar('$');
_putchar(' ');
}
/**
* calls - check the code.
* @array: double pointer.
* @line: a pointer.
* @exit_stat:a variable.
* Return: void.
*/
int calls(char **array, char *line, int exit_stat)
{
exit_check(array, line, exit_stat);
env_check(array);
if (access(array[0], X_OK) == 0)
exit_stat = execute_line(array[0], array);
else
path_finder(array);
return (exit_stat);
}
/**
* main - check the code.
* Return: always 0.
*/
int main(void)
{
int exit_stat = -1, is_interactive = isatty(STDIN_FILENO);
char *line = NULL, **array;
size_t len = 0;
while (1)
{
if (is_interactive)
_putchar_cisfun();
if (getline(&line, &len, stdin) == -1)
{
if (is_interactive)
{
perror("getline error");
free(line);
exit(EXIT_SUCCESS);
}
free(line);
break;
}
if (line[0] == '\n' || spaces_tabs_check(line) == 0)
{
free(line);
line = NULL;
continue;
}
array = string_storage(line);
if (clear_check(array) == 0)
{
free(line);
line = NULL;
free_2D(array);
continue;
}
exit_stat = calls(array, line, exit_stat);
free(line);
line = NULL;
free_2D(array);
}
return (0);
}