forked from adut24/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
148 lines (138 loc) · 2.66 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "main.h"
/**
* main - simple shell
* @ac: number of arguments
* @av: array of arguments
* @env: array of environment variables
* Return: 0, 1, 2 or exit value
*/
int main(int ac, char **av, char **env)
{
size_t bufsize = 0;
char *buffer = NULL;
int nb_cmd = 1, status = 0;
if (signal(SIGINT, sigintHandler) == SIG_ERR)
return (1);
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
{
non_int(av[0], buffer, bufsize, nb_cmd, env, &status);
return (status);
}
while (1)
{
inter(av[0], buffer, bufsize, nb_cmd, env, &status);
nb_cmd++;
}
(void)ac;
return (0);
}
/**
* inter - execute the simple shell in normal mode
* @name: name of the executable
* @buffer: buffer receiving getline
* @bufsize: size of the buffer
* @nb_cmd: number of command executed
* @env: environment variables
* @status: status of function
*/
void inter(char *name, char *buffer, size_t bufsize, int nb_cmd, char **env,
int *status)
{
int nb = 0, i = 0, check = 0;
write(STDOUT_FILENO, "> ", 2);
nb = getline(&buffer, &bufsize, stdin);
if (nb == -1)
{
write(STDOUT_FILENO, "\n", 1);
if (buffer)
free(buffer);
exit(*status);
}
if (nb > 0)
buffer[nb - 1] = '\0';
if (*buffer)
{
while (buffer[i] != '#' && buffer[i])
i++;
if (i > 0)
{
if (buffer[i - 1] == ' ')
buffer[i] = '\0';
}
else
buffer[0] = '\0';
for (i = 0; buffer[i]; i++)
{
if (buffer[i] != ' ')
{
check = 1;
break;
}
}
if (check == 1)
execute_command(buffer, name, nb_cmd, env, status);
}
if (buffer)
{
free(buffer);
buffer = NULL;
}
}
/**
* non_int - execute the simple shell in non interactive mode
* @name: name of the executable
* @buffer: buffer receiving getline
* @bufsize: size of the buffer
* @nb_cmd: number of command executed
* @env: environment variables
* @status: status of function
*/
void non_int(char *name, char *buffer, size_t bufsize, int nb_cmd, char **env,
int *status)
{
int nb = 0, i = 0, check = 0;
while ((nb = getline(&buffer, &bufsize, stdin)) >= 0)
{
if (nb > 0)
buffer[nb - 1] = '\0';
if (*buffer)
{
while (buffer[i] != '#' && buffer[i])
i++;
if (i > 0)
{
if (buffer[i - 1] == ' ')
buffer[i] = '\0';
}
else
buffer[0] = '\0';
for (i = 0; buffer[i]; i++)
{
if (buffer[i] != ' ')
{
check = 1;
break;
}
}
if (check == 1)
execute_command(buffer, name, nb_cmd, env, status);
}
free(buffer);
buffer = NULL;
}
if (buffer)
{
free(buffer);
buffer = NULL;
}
}
/**
* sigintHandler - if Ctrl+C is pressed
* @sig: signal sent to the function
*/
void sigintHandler(int sig)
{
write(STDOUT_FILENO, "\n", 1);
write(STDOUT_FILENO, "> ", 2);
(void)sig;
}