-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
247 lines (215 loc) · 6.31 KB
/
shell.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
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
/*
*Author: Steven Jorgensen
*Date: 2/18/16
*
*
*Program inplements a shell for users to interact with. Included commands are
*
*quit ends program
*date displays current date and time
*curr displays absolute pathname of current dir
*env display all of the environment variables
*cd <dir name> moves current directory to given dir
*hlist display recent commands (up to 10)
*dlist display recent directories (up to 10)
*!N displays and executes command at line N if in hlist
*#N move to directory at given index in dlist
*
* */
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <sstream>
#include <ctime>
#include <map>
#include <list>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
using namespace std;
int main(){
char curdir[1024];
map <long,string> m;
list <string> d;
string input;
long count = 1;
string user = getenv("USER");
struct stat st;
bool repeat = 0;
bool call = 0;
string s2 = "";
getcwd(curdir,sizeof(curdir));
d.push_front(curdir);
while (true){
cout<<"<"<<count<<" "<<user<<"> ";
getline(cin,input);
cout<<endl;
istringstream iss(input);
string s;
iss >> s;
if(s == ""){
continue;
}
// keep hlist at size 10
if(m.size() == 10){
m.erase(m.begin());
}
if(d.size() == 11){
d.pop_back();
}
// add command to hlist
m[count] = input;
// !N implementation
if(s[0] =='!'){
repeat = 1;
string ss = s.substr(1);
int num = atoi(ss.c_str());
if (m.find(num) != m.end()){
cout<<"Command at "<<num<<": "<<m[num]<<endl<<endl;
s = m[num];
m[count] = s;
if(s[0] == '!'){
cout<<"Bad command number"<<endl;
count += 1;
continue;
}
}
else{
cout<<"Command not in hlist"<<endl<<endl;
}
}
// #N implementation
if (s[0] == '#'){
call = 1;
string str = s.substr(1);
int index = atoi(str.c_str());
if (index > d.size() || index < 1){
cout << "Bad index number" <<endl<<endl;
continue;
}
else{
auto j = d.begin();
advance(j, index - 1);
s = "cd";
s2 = *j;
}
}
//quit implementation
if(s == "quit"){
break;
}
// date implementation
else if(s == "date"){
time_t t;
time(&t);
cout<<ctime(&t)<<endl;
}
// curr implementation
else if(s == "curr"){
char dir[1024];
if(getcwd(dir, sizeof(dir)) != NULL){
getcwd(dir,sizeof(dir));
cout<<dir<<endl<<endl;
}
else{
cout<<"Current Directory Error"<<endl<<endl;
}
}
// cd <dir name> implementation
else if (s.substr(0,2) == "cd"){
if (repeat == 0){
iss >> s;
}
else{
repeat = 0;
s.erase(s.begin()); // remove "cd"
s.erase(s.begin());
if(s[0] == ' '){
s.erase(s.begin());
}
else{
s = "";
}
}
if (s[0] == '#'){
call = 1;
string str = s.substr(1);
int index = atoi(str.c_str());
if (index > d.size() || index < 1){
cout << "Bad index number" <<endl<<endl;
continue;
}
else{
auto j = d.begin();
advance(j, index - 1);
s = *j;
}
}
if(s != "" && s != "cd"){
if(s[0] == '~'){
s = "/user/" + s.substr(1);
if (s == "/user/"){
s = s + user;
}
}
if(stat(s.c_str(),&st) != -1){ // check if dir exists
if(S_ISDIR(st.st_mode)){ // check if file
int success = chdir(s.c_str());
if(success == 0){
getcwd(curdir,sizeof(curdir));
d.push_front(curdir);
cout<<"Done"<<endl<<endl;
}
else{
cout<<"Access Denied"<<endl<<endl;
}
}
else{
cout<<"Not a directory"<<endl<<endl;
}
}
else{
cout<<"Directory doesn't exist"<<endl<<endl;
}
}
else{
cout<<"INVALID ARGUMENT... Usage: cd <directory name>"<<endl<<endl;
}
}
// hlist implementation
else if (s == "hlist"){
for(auto i = m.begin(); i != m.end(); i++){
cout<<i->first<<" "<<i->second<<endl;
}
cout<<endl;
}
// dlist implementation
else if ( s == "dlist"){
int ind = 0;
for (auto i : d){
ind += 1;
cout<< ind <<" "<< i <<endl;
}
cout<<endl;
}
// env implementation
else if (s == "env"){
extern char **environ;
char *env = *environ;
int i = 1;
for(;env;i++){
cout<<env<<endl;
env = *(environ + i);
}
cout<<endl;
}
else{
if(s[0] != '!'){
cout<<"INVALID INPUT"<<endl<<endl;
}
}
count += 1;
}
return 0;
}