-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshell.cpp
88 lines (81 loc) · 1.67 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
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
int main()
{
do
{
FILE* pipe=popen("pwd","r");
char buffer[256];
string result="[VIVEK_Terminal:";
while(!feof(pipe))
{
if(fgets(buffer,256,pipe)!=NULL)
{
result+=buffer;
}
}
pclose(pipe);
int length=result.length();
result=result.substr(0,(result.length()-1));
result.append("]");
cout<<result;
char* str=new char;
cin.getline(str,50);
string command="";
command.append(str);
char* arr[10] ;
char* token=new char;
int i=0;
token=strtok(str," ");
arr[0]=new char;
arr[0]=token;
while(token!=NULL)
{
i++;
token=strtok(NULL," ");
arr[i]=new char;
arr[i]=token;
}
/* for(int j=0;j<i;j++)
{
cout<<arr[j]<<endl;
} */
int x;
int d=fork();
if(d==0)
{
char cmd[20]="/bin/";
strcat(cmd,arr[0]);
// cout<<cmd<<" "<<arr[0]<<"\n";
char* arg[10];
arg[0]=new char;
arg[0]=cmd;
for(int j=1;j<i;j++)
{
arg[j]=new char;
arg[j]=arr[j];
}
arg[i]=new char;
arg[i]=(char*)0;
x=execv(cmd,arg);
// cout<<x<<"in child."<<"\n";
if(x==-1){
char cmd1[20]="/usr/bin/";
strcat(cmd1,arr[0]);
arg[0]=cmd1;
x=execv(cmd1,arg);
}
cout<<"\'"<<command<<"\' is an Invalid command.\n";
}
else
{
wait(NULL);
// cout<<"\n"<<x<<"in parent.";
// cout<<"\nCommand Executed.\n";
}
}while(true);
return 0;
}