-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_prompt.c
116 lines (102 loc) · 1.92 KB
/
shell_prompt.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
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
#include "simple_shell.h"
/**
* main - 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 buf_size = 0;
ssize_t chars_readed = 0;
int cicles = 0;
(void)ac;
while (1)
{
cicles++;
prompt();
signal(SIGINT, handle);
chars_readed = getline(&buffer, &buf_size, stdin);
if (chars_readed == EOF)
_EOF(buffer);
else if (*buffer == '\n')
free(buffer);
else
{
buffer[_strlen(buffer) - 1] = '\0';
command = tokening(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(command, av[0], env, cicles);
}
fflush(stdin);
buffer = NULL, buf_size = 0;
}
if (chars_readed == -1)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
/**
* prompt - prints the prompt
*
* Return: Nothing.
*/
void prompt(void)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "$ ", 3);
}
/**
* handle - handles Ctr + C signal.
* @signals: The signal to handle.
*
* Return: Nothing.
*/
void handle(int signals)
{
(void)signals;
write(STDOUT_FILENO, "$ ", 3);
}
/**
* _EOF - checks if buffer is EOF
* @buffer: The pointer to the input string.
*
* Return: Nothing
*/
void _EOF(char *buffer)
{
if (buffer)
{
free(buffer);
buffer = NULL;
}
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, " ", 1);
free(buffer);
exit(EXIT_SUCCESS);
}
/**
* shell_exit - exits the shell.
* @command: The pointer to tokenized command.
*
* Return: Nothing
*/
void shell_exit(char **command)
{
int sta_tus = 0;
if (command[1] == NULL)
{
free_dp(command);
exit(EXIT_SUCCESS);
}
sta_tus = _atoi(command[1]);
free_dp(command);
exit(sta_tus);
}