-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecute.c
More file actions
98 lines (90 loc) · 2.43 KB
/
Execute.c
File metadata and controls
98 lines (90 loc) · 2.43 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
#include "Header.h"
#include "customPwd.h"
#include "customEcho.h"
#include "customLs.h"
#include "customCd.h"
#include "customPinfo.h"
#include "customRepeat.h"
#include "sysCommands.h"
#include "customHistory.h"
#include "customRedirect.h"
#include "jobs.h"
#include "Execute.h"
void Execute(char * token, char * cwd, char * home_dir, char * prev_directory){
// CHECK FOR PIPING
for(int i=0;i<=strlen(token);i++){
if(token[i]=='|'){
customRedirect(token);
return;
}
}
// EXTRACT THE FIRST WORD (COMMAND WIHOUT ARGS)
char curr_command[1024];
curr_command[0] = '\0';
int counter = 0;
for(int i=0; i<=strlen(token); i++){
if((token[i]==' ') || (token[i]=='\t')){
curr_command[counter] = '\0';
break;
}
curr_command[counter++] = token[i];
}
if( strcmp(curr_command,"")==0 ){
int cool = 0;
}
else if( strcmp(curr_command,"cd")==0 ){
int cd_check = customCd(token, cwd, home_dir, prev_directory);
if (cd_check){
strcpy(prev_directory,cwd);
updateCwd(home_dir, cwd);
}
}
else if ( strcmp(curr_command,"pwd")==0 )
customPwd();
else if ( strcmp(curr_command,"echo")==0 )
customEcho(token);
else if ( strcmp(curr_command,"ls")==0 )
customLs(token, home_dir);
else if( strcmp(curr_command,"pinfo")==0 )
customPinfo(token);
else if( strcmp(curr_command,"repeat")==0 )
customRepeat(token, cwd, home_dir, prev_directory);
else if( strcmp(curr_command,"history")==0 )
showHistory(token);
else if( strcmp(curr_command,"jobs")==0 )
jobs(token);
else if ( (strcmp(curr_command,"quit")==0) || (strcmp(curr_command,"exit")==0) || (strcmp(curr_command,"bye")==0)){
printf("Bubyyeeee :)\n");
exit(0);
}
else{
// remove starting and ending's spaces
int start = -1;
int end = strlen(token);
for (int i = 0; i < strlen(token); i++){
if( (token[i]==' ') || (token[i]=='\t'))
start = i;
else
break;
}
for (int i = strlen(token)-1; i >= 0; i--){
if( (token[i]==' ') || (token[i]=='\t'))
end = i;
else
break;
}
// printf("start:%d;end:%d", start, end);
for( int i=start+1; i<end; i++ ){
token[i-start-1] = token[i];
}
token[end-start-1] = '\0';
// check if last element is &
if( token[strlen(token) - 1]=='&' ){
sysCommands(token, 1);
}
else{
sysCommands(token,0);
}
}
return;
}