-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_shell.c
More file actions
120 lines (111 loc) · 1.94 KB
/
Copy pathmain_shell.c
File metadata and controls
120 lines (111 loc) · 1.94 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "shell.h"
/**
* main - A function that runs our shell.
* @ac: The number of inputed arguments.
* @av: The pointer to array of inputed arguments.
* @env: The pointer to array of enviromental variables.
* Return: Always 0.
*/
int main(int ac, char **av, char **env)
{
char *buffer = NULL, **command = NULL;
size_t buff_size = 0;
ssize_t char_read = 0;
int rounds = 0;
(void)ac;
while (1)
{
rounds++;
prompt();
signal(SIGINT, handle);
char_read = getline(&buffer, &buff_size, stdin);
if (char_read == EOF)
{
_EOF(buffer);
}
else if (*buffer == '\n')
{
free(buffer);
}
else
{
buffer[_strlen(buffer) - 1] = '\0';
command = tokenize(buffer, " \0");
free(buffer);
if (_strcmp(command[0], "exit") != 0)
shell_exit(command);
else if (_strcmp(command[0], "cd") != 0)
change_dir(command[1]);
else
create_child_process(command, av[0], env, rounds);
}
fflush(stdin);
buffer = NULL;
buff_size = 0;
}
if (char_read == -1)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
/**
* prompt - prints the prompt
*
* Return: Nothing.
*/
void prompt(void)
{
if (isatty(STDIN_FILENO))
{
write(STDOUT_FILENO, "#cisfun$ ", 9);
}
}
/**
* handle - func to handle the Ctr + C signal.
*
* @signals: signal to be handled.
*
* Return: Nothing.
*/
void handle(int signals)
{
(void)signals;
write(STDOUT_FILENO, "\ncisfun$ ", 10);
}
/**
* _EOF - checks for EOF in a buff
*
* @buffer: input string.
*
* Return: Nothing
*/
void _EOF(char *buffer)
{
if (buffer)
{
free(buffer);
buffer = NULL;
}
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "\n", 1);
free(buffer);
exit(EXIT_SUCCESS);
}
/**
* shell_exit - func to exits the shell.]
*
* @command: tokenized command.
*
* Return: Nothing.
*/
void shell_exit(char **command)
{
int stats = 0;
if (command[1] == NULL)
{
mem_free(command);
exit(EXIT_SUCCESS);
}
stats = _atoi(command[1]);
mem_free(command);
exit(stats);
}