-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
64 lines (58 loc) · 1.07 KB
/
main.c
File metadata and controls
64 lines (58 loc) · 1.07 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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "main.h"
/**
* _field - Read a line from standard input.
*
* Return:
* - A pointer to the read line.
* - NULL if an error occurs or end-of-file is reached.
*/
char *_field(void)
{
char *buffer = NULL;
size_t len = 0;
ssize_t n;
if (isatty(STDIN_FILENO) == 1)
write(STDOUT_FILENO, "$ ", 2);
n = getline(&buffer, &len, stdin);
if (n == EOF)
{
free(buffer);
return (NULL);
}
return (buffer);
}
/**
* main - Entry point of the program.
*
* @argc: Number of command-line arguments.
* @argv: Array of command-line argument strings.
*
* Return:
* - 0 on successful execution.
*/
int main(int argc, char *argv[])
{
char *buffer = NULL;
char **comm = NULL;
int stats = 0, index = 0;
(void)argc;
while (1)
{
buffer = _field();
if (buffer == NULL)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "\n", 1);
return (stats);
}
index++;
comm = arr_str(buffer);
if (comm == NULL)
continue;
if (cmp_builtins(comm[0]))
handle_builtins(comm, argv, &stats, index);
else
stats = _exec(comm, argv, index);
}
return (0);
}