-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.cpp
214 lines (189 loc) · 4.71 KB
/
shell.cpp
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
#include <iostream>
#include <string>
#include <string.h>
#include <csignal>
#include <ostream>
#include <unistd.h>
#include <stdlib.h>
#include <cstdio>
#include <errno.h>
#include <dirent.h>
using namespace std;
volatile sig_atomic_t breakCount = 0;
void signal_handler(int sig){
}
void prompt();
int searchInternalCommand(char *cmd);
void runInternalCommand(int index,char *cmd);
void runExternalCommand(char *cmd);
void pwd();
void ls();
void changep(char *switchs);
void rm(char *cmd);
void clearScreen();
char commands[][255] = {"exit","ls","pwd","rm","changep","clscr"};
char promptTextUser[128] = "arian";
char promptTextGroup[128]="ubuntu";
int commandsLength = 6;
int main()
{
signal(SIGINT, signal_handler);
do{
char cmd[256] = {0};
prompt();
cin.clear();
gets(cmd);
int index = searchInternalCommand(cmd);
if(index>-1){
runInternalCommand(index,cmd);
}else{
runExternalCommand(cmd);
}
}while(1);
return 0;
}
void prompt(){
char cwd[1024];
getcwd(cwd,sizeof(cwd));
cout<<promptTextUser<<"@"<<promptTextGroup<<":"<<cwd<<"~$ ";
}
void clearScreen(){
cout<<"\x1b[2J\x1b[1;1H"<<flush;
}
void pwd(){
char cwd[1024];
getcwd(cwd,sizeof(cwd));
cout <<cwd<<endl;
}
void ls(){
DIR *d;
struct dirent *dir;
d = opendir(".");
if(d){
while((dir=readdir(d))!=NULL){
cout<<dir->d_name<<"\n";
}
closedir(d);
}
}
void changep(char *switchs){
char *loc,*loc2;
int locnumber2,locnumber,endOfSwitch,endOfCommand,i,promptTextUserLength,mainParameterLength,promptTextGroupLength;
loc = strchr(switchs,'-');
loc2 = strrchr(switchs,' ');
if(loc){
locnumber = loc-switchs;
}else{
cout<<"You can't Use This Command Without switch"<<endl;
return;
}
if(loc2){
locnumber2 = loc2-switchs;
}
if(locnumber2<locnumber){
cout<<"You can't Use This Command Without Parameter"<<endl;
return;
}
endOfSwitch = loc2-switchs;
endOfCommand = strlen(switchs);
char *mainSwitch = new char[endOfSwitch-locnumber-1];
for(int i=locnumber+1,j=0;i<endOfSwitch;i++,j++){
mainSwitch[j] = switchs[i];
}
char *mainParameter = new char[endOfCommand-endOfSwitch-1];
for(int i=endOfSwitch+1,j=0;i<endOfCommand;i++,j++){
mainParameter[j] = switchs[i];
}
mainParameterLength = strlen(mainParameter);
promptTextUserLength = strlen(promptTextUser);
promptTextGroupLength = strlen(promptTextGroup);
switch(mainSwitch[0]){
//change user
case 'u':
//meghdare ghablie promptTextUser ra pak mikonad
for(i=0;i<promptTextUserLength;i++){
promptTextUser[i] = '\0';
}
//promptTextUser ra meghdar dahi mikonad
for(i=0;i<mainParameterLength;i++){
promptTextUser[i] = mainParameter[i];
}
//promptTextUser = mainParameter;
break;
//change group
case 'g':
for(i=0;i<promptTextGroupLength;i++){
promptTextGroup[i] = '\0';
}
for(i=0;i<mainParameterLength;i++){
promptTextGroup[i] = mainParameter[i];
}
break;
}
}
void rm(char *cmd){
char *loc;
int locnumber,endOfCommand;
loc = strchr(cmd,' ');
if(loc){
locnumber = loc-cmd;
}else{
cout<<"You can't Use This Command Without Parameter"<<endl;
return;
}
endOfCommand = strlen(cmd);
char *fileName = new char[endOfCommand-locnumber-1];
for(int i=locnumber+1,j=0;i<endOfCommand;i++,j++){
fileName[j] = cmd[i];
}
if(remove(fileName)){
cout<<"no such file find to delete"<<endl;
}
}
int searchInternalCommand(char *cmd){
char *loc;
loc = strchr(cmd,' ');
int locnumber;
if(loc){
locnumber = loc-cmd;
}else{
locnumber = strlen(cmd);
}
char *maincmd = new char[locnumber];
for(int i=0;i<locnumber;i++){
maincmd[i] = cmd[i];
}
for(int i=0;i<commandsLength;i++){
if(strcmp(commands[i],maincmd)==0){
return i;
}
}
return -1;
}
void runInternalCommand(int index,char *cmd){
switch(index){
case 0:
exit(0);
break;
case 1:
ls();
break;
case 2:
pwd();
break;
case 3:
rm(cmd);
break;
case 4:
changep(cmd);
break;
case 5:
clearScreen();
break;
default:
break;
}
}
void runExternalCommand(char *cmd){
system(cmd);
}