-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCommandShell.cpp
More file actions
61 lines (53 loc) · 1.55 KB
/
SimpleCommandShell.cpp
File metadata and controls
61 lines (53 loc) · 1.55 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
//This piece of code is a simple Linux-based shell that works like a primitive
//command prompt. Because it uses Linux C++ libraries you will need to run it there.
#include <iostream>
#include <cstring>
#include <"unistd.h">
using namespace std;
int main()
{
char checker;//Checks for the end of line
char store[100][50];//Stores up to 50 arguments
int i=0;
while (1){
i=0;
cout<<"Pengra>";
//Command prompt used our last names as the header
while (checker !='\n'){
cin >> store[i];
if(strcmp(store[i],"exit")==0){
break;
}//Exit command canceled the command program
cin.get(checker);
i++;
}
if(strcmp(store[i],"exit")==0){
break;
}
char fileName[100];
strcpy(fileName, store[0]);//Copies the name of the first argument to be executed
if(i>0){
char fileArgs[100][i];
for(int z=0; z<i; z++){
strcpy(fileArgs[z], store[z+1]);
}
}
pid_t pid;
pid=fork;//Replicates into two forked processes
if(pid<0){cout<<"Error, fork failed.";
return 1;}
else if(pid==0){
if(i==0){
cout<<fileName;
const char * mt=NULL;//Empty char pointer for execv
int execv(const char *fileName, MT);
cout<<"Exec failed."//A process under execv won't read this statement
}
else{int execv(const char *fileName, char *const fileArgs[]);}
}
else{
wait(NULL);
cout<<"Child Complete";//Shows when the process is complete
}
}
return 0;