-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyShell.cpp
More file actions
216 lines (193 loc) · 5.81 KB
/
myShell.cpp
File metadata and controls
216 lines (193 loc) · 5.81 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
#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "myShell.h"
#include <cstring>
#include <sys/wait.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <fstream>
#include <cstdio>
using namespace std;
int main(int argc, char* argv[]){
myShell ms;
ms.execute();
}
void myShell::execute(){
while(true){
cout << greeting;
string command;
getline(cin, command);
add_to_history(command);
parse_and_execute(command);
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* WaitFor(pid) *
* Waits for foreground processes...based on Rochkind's waitfor *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void myShell::WaitFor(int pid){
int gotpid, status;
while( (gotpid = wait(&status)) != pid)
cout << "Process [" << gotpid << "] Finished..." << endl;
}
int myShell::parse_and_execute(string c){
char * cstr = new char [c.length()+1];
std::strcpy (cstr, c.c_str());
// cstr now contains a c-string copy of str
string input = ""; //use this for input for piping or redirection
string output = ""; //output from either ls or pwd
char * p = strtok (cstr," ");
std::streambuf *coutbuf = std::cout.rdbuf();; //save old buf
bool redirect = false;
bool input_redirect = false;
while (p != NULL)
{
if(strcmp(p, "ls")==0){
output = find_dir_entry(input);
char * prev_tolk = p;
p = strtok(NULL," ");
if (p == NULL) {break;}
if (strcmp(p, "<")==0){
p = strtok(NULL," ");
ifstream infile;
infile.open("./"+(string)p);
string line;
getline(infile, line);
input += line + "\n";
cout << input << endl;
}
p = prev_tolk;
}
if(strcmp(p, "pwd")==0){output = pwd();}
if(strcmp(p, "|")==0){input = output;}
if(strcmp(p, "history")==0){output = get_history();}
if(strcmp(p, ">")==0){
redirect = true;
break;
}
//std::cout << p << endl;
p = strtok(NULL," ");
}
//std::cout << c << endl;
//delete[] c;
if(c.compare("exit") == 0){
exit(0);
}
if (redirect){
p = strtok(NULL," ");
std::ofstream out(p);
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
std::cout << output << "\n";
} else {
std::cout << output << "\n";
}
reset_cout(coutbuf);
return 0;
}
string myShell::find_dir_entry (string output){
char *filename;
char *o = new char[output.length() + 1];
strcpy(o, output.c_str());
struct stat dot_sb;
if(strcmp(o, "")==0){
filename = ".";
}else {
filename = o;
}
stat (filename, &dot_sb);
DIR *dirp;
struct stat fileStat;
struct dirent *dp;
dirp = opendir (filename);
//printf("cwd inode is %ld\n", dot_sb.st_ino);
string rv;
//printf("Current directory files\n");
while ((dp = readdir(dirp)) != NULL){
//stat(dp->d_name, &fileStat);
//printf("%ld, %lu: %s\n", fileStat.st_ino, dp->d_ino, dp->d_name);
rv += to_string(dp->d_ino) + ": " + string(dp->d_name) + "\n";
}
closedir(dirp);
return rv;
}
string myShell::pwd(){
char path[256] = {'\0'};
do{
DIR *pdir = opendir(".."); //parent
struct dirent *entp;
DIR *cdir = opendir("."); //current
struct dirent *entc;
__ino_t cid;
while ((entc = readdir (cdir)) != NULL) {
//printf("%lu %s\n", entc->d_ino, entc->d_name);
if(strcmp((const char *)entc->d_name, (const char *)".") == 0){
cid = entc->d_ino;
}
}
char * add_to_path = get_dir_name(entp, pdir, cid);
if(strlen(add_to_path) != 0){
char *tmp = strdup(path);
strcpy(path, add_to_path);
strcat(path, tmp);
free(tmp);
char *tmp2 = strdup(path);
strcpy(path, "/");
strcat(path, tmp2);
//free(tmp2);
chdir("..");
}else{
char *tmp3 = strdup(path);
strcpy(path, "~");
strcat(path, tmp3);
//free(tmp3);
goto home;
}
}while(true);
home:
//printf("%s\n", path);
chdir(path);
return string(path);
}
char * myShell::get_dir_name(struct dirent *entp, DIR *pdir, __ino_t cid){
while ((entp = readdir (pdir)) != NULL) {
//printf("%lu %s\n", entp->d_ino, entp->d_name);
if(entp->d_ino == cid){
char *path = entp->d_name;
return path;
}
}
return "";
}
void myShell::add_to_history(string command){
ofstream outfile;
outfile.open("./history.txt", std::ios_base::app);
ifstream infile;
infile.open("./history.txt");
int number_of_lines = 0;
string line;
while (getline(infile, line))
++number_of_lines;
outfile << to_string(number_of_lines)+": "+command << endl;
outfile.close();
infile.close();
}
string myShell::get_history(){
string rv;
ifstream infile;
infile.open("./history.txt");
string line;
while (getline(infile, line)){
rv += line + "\n";
}
return rv;
}
void myShell::reset_cout(std::streambuf *coutbuf)
{
std::cout.rdbuf(coutbuf); //reset to standard output again
}