-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
105 lines (94 loc) · 2.11 KB
/
main.c
File metadata and controls
105 lines (94 loc) · 2.11 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "shell.h"
static char *FIRST_ARG;
int handle_arguments(int ac, char **av, int *exec_file);
void sigintHandler(int sig_num);
char *get_first_av();
/**
* main - Entry point
* @ac: number of arguments
* @av: Array of arguments
*
* Return: 0 on success
*/
int main(int ac, char **av)
{
int read, exec_file = 0;
char *buff = NULL;
size_t buff_len = 0;
int fd;
FIRST_ARG = av[0];
signal(SIGINT, sigintHandler);
fd = handle_arguments(ac, av, &exec_file);
/*update_count_lines();*/
while (1)
{
/* Print console symbol only if it is interactive*/
if (isatty(STDIN_FILENO) == 1 && exec_file == 0)
write(STDOUT_FILENO, "$ ", 2);
/* Read commands from console */
/*read = read_line(fd, &buff);*/
read = getline(&buff, &buff_len, stdin);
if (read == EOF)
{
free(buff);
exit(*process_exit_code());
}
/*handle_history(buff);*/
/* Remove comments & '\n' char from buffer */
buff = handle_comment(buff);
_strtok(buff, "\n");
/* Handling_semicolon, ||, && and executes inside of the function */
handling_semicolon_and_operators(buff, read, av[0]);
}
/* Free buffer memory */
free(buff);
if (exec_file)
close(fd);
return (*process_exit_code());
}
/**
* handle_arguments - Check the number of arguments passed to main
* @ac: Number of arguments
* @av: Array of arguments as strings
* @exec_file: Integer used to check if user wants to exec commands from file
*
* Return: File descriptor to file
*/
int handle_arguments(int ac, char **av, int *exec_file)
{
int fd = STDIN_FILENO;
char *err_msg = "Error: more than one argument\n";
if (ac > 2)
{
write(STDERR_FILENO, err_msg, _strlen(err_msg));
exit(1);
}
if (ac == 2)
{
fd = open(av[1], O_RDONLY);
*exec_file = 1;
}
if (fd == -1)
{
perror(av[0]);
exit(1);
}
return (fd);
}
/**
* sigintHandler - Avoids current process to finish
* @sig_num: Signal number
*/
void sigintHandler(int __attribute__((unused))sig_num)
{
write(STDIN_FILENO, "\n$ ", 3);
}
/**
* get_first_av - Returns the first argument passed to main
*
* Return: Pointer to first arg passed to main
*/
char *get_first_av(void)
{
return (FIRST_ARG);
}