-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
55 lines (54 loc) · 1.18 KB
/
main.c
File metadata and controls
55 lines (54 loc) · 1.18 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
#define _GNU_SOURCE
#include <stdio.h>
#include "holberton.h"
/**
*main - entry point for shell program
*@ac: the number of arguments passed
*@av: an array of strings of the arguments passed
*@env: the current environment
*
*Description: function will read a line, break it into tokens, and execute
*the call, then return to main when end of file is reached
*Return: 0 when EOF is reached (ie user presses Ctrl + D)
*/
int main(int ac, char **av, char **env)
{
char *line, *newline;
size_t len = 0;
ssize_t characters;
char **tokenarray;
int cmdnum = 0;
(void)ac, (void)av;
while (1)
{
line = NULL;
len = 0;
cmdnum++;
if (isatty(STDIN_FILENO) == 1)
printprompt();
signal(SIGINT, ctrlc);
characters = getline(&line, &len, stdin);
if (characters == EOF || characters == -1)
return (ctrld(line));
if (line[0] == '\n')
{
free(line);
continue;
}
newline = _reallocchar(line);
if (newline == NULL)
{
free(line);
return (0);
}
tokenarray = tokensplit(newline);
if (tokenarray == NULL)
{
free(line);
free(newline);
return (0);
}
exec(tokenarray, env, av, line, newline, cmdnum);
free_all(line, newline, tokenarray);
}
}