Repository navigation
Expand file tree
/
Copy pathshell.c
More file actions
360 lines (300 loc) · 11.2 KB
/
Copy pathshell.c
File metadata and controls
360 lines (300 loc) · 11.2 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <signal.h>
#define COMMANDS_NUMBER 4
#define BUILTIN_COMMANDS_NUMBER 3
#define MAX_BUFF 1024 //for pipeline
char *commands[] = {"ls", "hexdump", "cp", "cat"};
//----Built-in functions----
int cd(char *path[]); //change directory
int clear(char *path[]); //clears the CLI, even if it doesnt need any arg still it accepts *path[] to be listed in builtin_f_ptr[]
int whoami(char *path[]);
char *builtincomm[] = {"cd", "clear", "whoami"};
int (*builtin_f_ptr[])(char *args[]) = {cd, clear, whoami};
//--------------------------
//-----Signals Handling-----
void signal_handler(int sigint);
char n_currentdir[100]; //need this to print path after a signal handling (look in signal_handler function)
char n_currentuser[100]; //same as above
//--------------------------
// ---Shell functions---
int command_exec(char *currentuser, char *args[], char *env[]);
bool command_search(char *args[]);
int builtin_command_search(char *args[]);
int main() {
// ------set env variable------
char *env[]= {"PATH=commands/", NULL};
// -----------------------------
// ---Get the user information---
struct passwd *usr;
unsigned int uid;
uid = geteuid();
usr = getpwuid(uid);
char currentdir[100];
char currentuser[100];
strcpy(currentdir, usr->pw_dir);
strcpy(currentuser, usr->pw_name);
// ---signals handling---
struct sigaction sa;
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
strcpy(n_currentdir, currentdir);
strcpy(n_currentuser, currentuser);
// -----------------------------
// ---CLI---
// -----------------------------
shell: //yeah, i use goto in 2026
while(true) {
char command[1024];
char *args[100];
printf("%s[%s]>", currentuser, currentdir);
if (fgets(command, sizeof(command), stdin) != NULL && command[0] != '\n') {
command[strcspn(command, "\n")] = 0; //cut the final "enter" key
//now the program has to cut the command in a list of token + pipe discovery
char *token = strtok(command, " ");
int i_token = 0;
int pipe_index = -1;
int redirection_index = -1;
while ((token != NULL)) {
args[i_token] = token;
if(!strcmp(token, "|")) pipe_index = i_token;
if(!strcmp(token, ">")) redirection_index = i_token;
i_token++;
token = strtok(NULL, " ");
}
args[i_token] = NULL; //closing the list
//---first checks if the command is not NULL or is exit
if (args[0] == NULL) goto shell;
if (strcmp("exit", args[0]) == 0) {
exit(0);
}
//-------------------------
//------PIPE HANDLING------
//-------------------------
if (pipe_index > 0) {
char **command_1 = args;
char **command_2 = &args[pipe_index + 1];
command_1[pipe_index] = NULL;
int fdpipe[2];
if (pipe(fdpipe) == -1) {
perror("Failed to pipe");
goto shell;
}
pid_t pipe_in = fork();
switch(pipe_in) {
case -1: {
perror("Execution failed, not enough memory");
break;
}
case 0: {
dup2(fdpipe[1], STDOUT_FILENO); //send the output of command_1 to fdpipe[1]
close(fdpipe[1]);
close(fdpipe[0]);
//Signals handling
struct sigaction son_sa;
son_sa.sa_handler = SIG_DFL;
sigemptyset(&son_sa.sa_mask);
son_sa.sa_flags = 0;
sigaction(SIGINT, &son_sa, NULL);
sigaction(SIGQUIT, &son_sa, NULL);
if(builtin_command_search(command_1) != -1) {
builtin_f_ptr[builtin_command_search(command_1)](command_1);
}
else if(command_search(command_1)) {
command_exec(currentuser, command_1, env);
}
exit(1);
break;
}
}
pid_t pipe_out = fork();
switch(pipe_out) {
case -1: {
perror("Execution failed, not enough memory");
break;
}
case 0: {
dup2(fdpipe[1], STDIN_FILENO); //send the output of command_1 to fdpipe[1]
close(fdpipe[1]);
close(fdpipe[0]);
//Signals handling
struct sigaction son_sa;
son_sa.sa_handler = SIG_DFL;
sigemptyset(&son_sa.sa_mask);
son_sa.sa_flags = 0;
sigaction(SIGINT, &son_sa, NULL);
sigaction(SIGQUIT, &son_sa, NULL);
if(builtin_command_search(command_2) != -1) {
builtin_f_ptr[builtin_command_search(command_2)](command_2);
}
else if(command_search(command_2)) {
command_exec(currentuser, command_2, env);
}
exit(1);
break;
}
}
close(fdpipe[0]);
close(fdpipe[1]);
waitpid(pipe_in, NULL, 0);
waitpid(pipe_out, NULL, 0);
goto shell;
}
//----------------------------------
//-------REDIRECTION HANDLING-------
//----------------------------------
if (redirection_index > 0) {
char **comm = args;
comm[redirection_index] = NULL;
char *redirection_file = args[redirection_index+1];
int fd = open(redirection_file, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (fd == -1) {
perror("Error");
goto shell;
}
pid_t pid = fork();
switch (pid) {
case -1: {
perror("Execution failed, not enough memory");
}
case 0: {
dup2(fd, STDOUT_FILENO);
close(fd);
//Signals handling
struct sigaction son_sa;
son_sa.sa_handler = SIG_DFL;
sigemptyset(&son_sa.sa_mask);
son_sa.sa_flags = 0;
sigaction(SIGINT, &son_sa, NULL);
sigaction(SIGQUIT, &son_sa, NULL);
if(builtin_command_search(comm) != -1) {
builtin_f_ptr[builtin_command_search(comm)](comm);
}
else if(command_search(comm)) {
command_exec(currentuser, comm, env);
}
else {
printf("Command not found");
}
exit(1);
break;
}
}
close(fd);
waitpid(pid, NULL, 0);
goto shell;
}
//---------NORMAL EXECUTION---------
//---COMMAND SEARH AND EXECUTION----
//---checks if the command is builtin
int builtin_comm_index = builtin_command_search(args);
if (builtin_comm_index > -1) {
builtin_f_ptr[builtin_comm_index](args);
getcwd(currentdir, sizeof(currentdir)); //refresh dir
strcpy(n_currentdir, currentdir); //refresh global dir and user values for signal handling
strcpy(n_currentuser, currentuser);
goto shell; //no need to enter the following loop
}
//in the end, check for command in args
if (command_search(args)) { //commad_search(args) returns true if found
pid_t pid = fork();
if (pid < 0) {
perror("Execution failed, not enough memory");
}
else if (pid == 0) {
//Signals handling
struct sigaction son_sa;
son_sa.sa_handler = SIG_DFL;
sigemptyset(&son_sa.sa_mask);
son_sa.sa_flags = 0;
sigaction(SIGINT, &son_sa, NULL);
sigaction(SIGQUIT, &son_sa, NULL);
command_exec(currentuser, args, env);
exit(1);
}
else {
int status;
wait(&status);
}
}
}
if (!command_search(args)) {
puts("Command not found");
}
}
return 0;
}
//-----------------------------------
// ------------FUNCTIONS-------------
// ----------------------------------
//----Built-in functions----
int cd(char *path[]) {
struct passwd *usr;
unsigned int uid;
uid = geteuid();
usr = getpwuid(uid);
if (path[1] == NULL) {
chdir(usr->pw_dir);
return 0;
}
if (chdir(path[1]) == -1) {
printf("Can't find directory %s\n", path[1]);
}
return 0;
}
int clear(char *path[]) {
printf("\033[H\033[2J\033[3J"); //ANSI escape sequence
fflush(stdout);
return 0;
}
int whoami(char *path[]){
struct passwd *usr;
unsigned int uid;
uid = geteuid();
usr = getpwuid(uid);
puts(usr->pw_name);
return 0;
}
//----Signal Handler----
void signal_handler(int sigint) {
switch (sigint) {
case SIGINT: printf("\n%s[%s]>", n_currentuser, n_currentdir); break;
case SIGQUIT: printf("\n%s[%s]>", n_currentuser, n_currentdir); break;
default: break;
}
fflush(stdout);
}
// Shell functions
int command_exec(char *currentuser, char *args[], char *env[]) {
char commpath[256];
snprintf(commpath, sizeof(commpath), "/home/%s/Desktop/programmiC/shell/commands/%s", currentuser, args[0]);
execve(commpath, args, env);
perror("Error: ");
return 0;
}
bool command_search(char *args[]) {
for (ssize_t i = 0; i<COMMANDS_NUMBER; i++) {
if (strcmp(commands[i], args[0]) == 0) return true;
}
return false;
}
int builtin_command_search(char *args[]) {
for (int i = 0; i < BUILTIN_COMMANDS_NUMBER; i++) {
if (strcmp(args[0], builtincomm[i]) == 0) {
return i;
}
}
return -1;
}