-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
80 lines (72 loc) · 1.71 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 "main.h"
#include "utilities.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
/**
* main - Entry point of my program
*
* Return: 0 if successful or an Exit error
*/
int main(void) {
char *command, **tokens_array;
int is_terminal = isatty(STDIN_FILENO), flag = 1, status_code = 0, space;
call_signal();
while (1 && flag) {
flag = is_terminal;
if (is_terminal)
print_dollar();
command = read_command();
if (!command) {
free(command);
if (errno != 0)
status_code = 2;
_putchar('\n');
break;
}
space = _stripstr(command);
if (space == 1) {
free(command);
continue;
}
remove_comments(command);
tokens_array = seperate_input(command);
if (command[0] == '\0' || _strcmp(command, "\n") == 0)
continue;
if (execute_builtin(tokens_array, command) == 0) {
free(command);
free(tokens_array);
errno = 0;
continue;
}
status_code = execute_command(command, tokens_array);
if (status_code == 0)
errno = status_code;
}
return (status_code);
}
/**
* execute_command - function to excute commands
* @command: user input
* @tokens_array: tokenized array of inputs
*
* Return: status
*/
int execute_command(char *command, char **tokens_array) {
int status;
if (tokens_array[0][0] == '/' || tokens_array[0][0] == '.') {
status = runpathcmd(command, tokens_array);
} else {
status = runpathlesscmd(command, tokens_array);
}
return (status);
}
/**
* call_signal - functute to call signal commands
*/
void call_signal(void) {
signal(SIGINT, handle_sigint);
signal(SIGQUIT, handle_sigquit);
signal(SIGTSTP, handle_sigstp);
}