-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbettershell.c
235 lines (216 loc) · 5.07 KB
/
bettershell.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
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
char pf[100]="/usr/bin:/bin:/sbin";
char *PATH[100];
int cwdd=0;
void tokenizel(char** main,int *size,char *inarr,const char *c){
char *token;
token=strtok(inarr,c);
int count=-1;
while(token){
main[++count]=malloc(sizeof(token)+1);
strcpy(main[count],token);
char *temp=main[count];
if(temp[strlen(temp)-1]==' ' || temp[strlen(temp)-1]=='\n')
temp[strlen(temp)-1]='\0';
if(temp[0]==' ' || temp[0]=='\n') memmove(temp, temp+1, strlen(temp));
token=strtok(NULL,c);
}
main[++count]=NULL;
*size=count;
}
void mainexec(char** argv){
if(fork()==0){
execvp(argv[0],argv);
perror("invalid input\n");
exit(1);
}
else{
wait(NULL);
}
}
void pipeexec(char** inarr,int size){
int fd[15][2];
int count;
int temp;
char *argv[100];
for(temp=0;temp<size;temp++){
tokenizel(argv,&count,inarr[temp]," "); // ls , -l // head -2
if(temp!=size-1){
if(pipe(fd[temp])<0){
perror("ERROR!\n");
return;
}
}
if(fork()==0){
if(temp!=size-1){
dup2(fd[temp][1],1);
close(fd[temp][0]);
close(fd[temp][1]);
}
if(temp!=0){
dup2(fd[temp-1][0],0);
close(fd[temp-1][1]);
close(fd[temp-1][0]);
}
execvp(argv[0],argv);
perror("ERROR!");
exit(1);
}
if(temp!=0){
close(fd[temp-1][0]);
close(fd[temp-1][1]);
}
wait(NULL);
}
}
void inred(char** inarr,int size){
int count,fd;
char *argv[100];
tokenizel(argv,&count,inarr[0]," ");
if(fork()==0){
fd=open(inarr[1],O_RDONLY);
dup2(fd,0);
char str1[100]="/bin/";
strcat(str1,argv[0]);
execvp(argv[0],argv);
perror("invalid input ");
exit(1);
}
wait(NULL);
}
void outred(char** inarr,int size){
int count,fd;
char *argv[100];
tokenizel(argv,&count,inarr[0]," ");
if(fork()==0){
fd=open(inarr[1],O_WRONLY);
dup2(fd,1);
char str1[100]="/bin/";
strcat(str1,argv[0]);
execvp(argv[0],argv);
perror("invalid input ");
exit(1);
}
wait(NULL);
}
void addhis(char *inarr){
FILE *filepointer;
filepointer = fopen("history.txt", "a");
fprintf(filepointer, "%s", inarr);
fclose(filepointer);
}
int main()
{
char inarr[500],*mainarr2[100], *mainarr1[100],currdir[1024];
int size=0; char prompt[100]=">> ";
while(1){
if (cwdd==1)
{
if (getcwd(currdir, sizeof(currdir)) != NULL){
strcpy(prompt,currdir);
strcat(prompt,"$");
}
else perror("getcwd failed\n");
}
printf("%s", prompt);
fgets(inarr, 500, stdin);
addhis(inarr);
if(strstr(inarr,"history")){
FILE *filepointer;
char c;
filepointer = fopen("history.txt", "r");
if (filepointer == NULL)
{
printf("Cannot open file \n");
exit(0);
}
c = fgetc(filepointer);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(filepointer);
}
printf("\n");
fclose(filepointer);
}
else if(strchr(inarr,'|')){
tokenizel(mainarr2,&size,inarr,"|");
pipeexec(mainarr2,size);
}
else if(strchr(inarr,'>')){
tokenizel(mainarr2,&size,inarr,">");
if(size==2){
outred(mainarr2,size);
}
else printf("ERROR!");
}
else if(strchr(inarr,'<')){
tokenizel(mainarr2,&size,inarr,"<");
if(size==2){
inred(mainarr2,size);
}
else printf("ERROR!");
}
else{
tokenizel(mainarr1,&size,inarr," ");
if(strstr(mainarr1[0],"cd")){
chdir(mainarr1[1]);
}
else if(strstr(mainarr1[0],"PS1")){
char temp[100];
char *temp3=" ";
char cw[100]="w$";
strcpy(prompt, "");
strcpy(temp, "");
strcat(temp,&mainarr1[0][5]);
if (size==1)
{
temp[strlen(temp)-1] = '\0';
if (strstr(temp,cw))
{
cwdd=1;
if (getcwd(currdir, sizeof(currdir)) != NULL){
strcpy(prompt,currdir);
strcat(prompt,"$");
}
else perror("getcwd failed\n");
}
else {
cwdd=0;
strcpy(prompt,temp);
strcat(prompt," ");
}
}
else{
cwdd=0;
strcat(temp,temp3);
for (int i = 1; i < size; i++)
{
strcat(temp,mainarr1[i]);
strcat(temp,temp3);
}
temp[strlen(temp)-2] = '\0';
strcat(temp," ");
strcpy(prompt,temp);
}
}
else if(strstr(mainarr1[0],"exit")){
exit(0);
}
else if(strstr(mainarr1[0],"PATH")){
printf("PATH updated!\n");
}
else mainexec(mainarr1);
}
}
return 0;
}