-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyshell.c
208 lines (183 loc) · 4.05 KB
/
myshell.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <ctype.h>
#include <sys/wait.h>
void komutCalistir(char komut[]){
//burda dizi elemanı gönderdiğimiz için pass by reference oluyor ve komutlar dizisindeki elemanlar bozuluyor bu yüzden fonksiyonda gelen stringi bir başka stringte tutmalıyım.
char temp[strlen(komut)];
strcpy(temp,komut);
int i,sayac;
i=0;
sayac=0;
//Burda bşluk ekliyorum çünkü gelen komutu boşluğa göre ayırıyorum. Ne kadar boşluk o kadar kelime demek eğer sonuna boşluk eklemessem
//hem ls,cat gibi direkt girilen komutları ayıramam, hemde komutlar boşluk sayısı kadar kelimeden oluşuyor.
const char s1[2] = " ";
strncat(temp,s1,1);
while(temp[i] != '\0'){
if(temp[i] == ' '){
sayac++;
}
i++;
}
char *tokenler[sayac];
char *tokencik;
tokencik=strtok(temp,s1);
i=0;
while(tokencik != NULL){
tokenler[i]=tokencik;
tokencik=strtok(NULL,s1);
i++;
}
if(strcmp(tokenler[0],"exit")==0){
printf("Program sonlandiriliyor... \n",tokenler[0]);
exit(0);
}
else if(strcmp(tokenler[0],"bash")==0){
int i,f;
f=fork();
if(f == 0){
i=execve("/bin/bash",NULL,NULL);
perror("Error: \n");
}
else{
wait(&i);
}
}
else if(strcmp(tokenler[0],"tekrar")==0){
int f,i;
if(!isdigit(tokenler[2][0])){
printf("Lutfen tekrar komutunda kelimeden sonra bir sayi giriniz.\n");
}
else if(!isalpha(tokenler[1][0])){
printf("Lutfen tekrar komutundan sonra bir kelime giriniz.\n");
}
else{
char *newargv[3];
newargv[0]=tokenler[1];
newargv[1]=tokenler[2];
newargv[2]=NULL;
f=fork();
if(f == 0){
i=execve("tekrar",newargv,NULL);
perror("Error: ");
}
}
}
else if(strcmp(tokenler[0],"islem")==0){
int i,f,j,status;
int hata=0;
if((strcmp(tokenler[1],"topla")==0) || (strcmp(tokenler[1],"cikar")==0) ){
for(i=0; tokenler[2][i] != '\0'; i++){
if(!isdigit(tokenler[2][i])){
hata=1;
}
}
for(i=0; tokenler[3][i] != '\0'; i++){
if(!isdigit(tokenler[3][i])){
hata=1;
}
}
if(hata){
printf("Lutfen topla veya cikardan sonra 2 adet sayiyi bosluk ile giriniz.\n");
}
else{
char *newargv[4];
newargv[0]=tokenler[1];
newargv[1]=tokenler[2];
newargv[2]=tokenler[3];
newargv[3]=NULL;
f=fork();
if(f == 0){
i=execve("islem",newargv,NULL);
perror("Error:");
}
else{
wait(&status);
if(WIFEXITED(status))
printf("islem sonucu= %d\n",WEXITSTATUS(status));
}
}
}
else {
printf("Islem komutu sadece topla ve cikar ile kullanilabilir.\n");
}
}
else if(strcmp(tokenler[0],"cat")==0){
int f,i;
char *newargv[3];
newargv[0]=tokenler[0];
newargv[1]=tokenler[1];
newargv[2]=NULL;
f=fork();
if(f == 0){
i=execve("/bin/cat",newargv,NULL);
perror("Error: ");
}
else{
wait(&i);
}
}
else if(strcmp(tokenler[0],"clear")==0){
int i,f;
f=fork();
if(f == 0){
system("clear");
}
else{
wait(&i);
}
}
else if(strcmp(tokenler[0],"ls")==0){
int i,f;
char *newargv[2];
newargv[0]="ls";
newargv[1]=NULL;
f=fork();
if(f == 0){
i=execve("/bin/ls",newargv,NULL);
perror("Error: ");
}
else{
wait(&i);
}
}
else{
printf("Komut bulunmadi. \n");
}
}
int main(){
while(1){
int i,j,sayac;
printf("Myshell>>");
char komut[150];
gets(komut);
//Komutları çizgiye göre ayırdığımdan ötürü son komutuda ayırabilmek için çizgi ekledim sonuna
const char s[2] = "|";
strncat(komut,s,1);
i=0;
sayac=0;
//kaç tane çizgi varsa o kadar komutum, kaçtane komutum varsa o kadar dizi için yer ayırıyorum(komutlar dizi)
while(komut[i] != '\0'){
if(komut[i] == '|'){
sayac++;
}
i++;
}
i=0;
char *komutlar[sayac];
char *token;
token=strtok(komut,s);
while(token != NULL){
//Burda dizide tutuyorum çünkü ayırıp direkt fonksiyona yollayınca kalan komutlar gitmiyor.
komutlar[i]=token;
token=strtok(NULL,s);
i++;
}
for(i=0; i<sayac; i++){
komutCalistir(komutlar[i]);
}
}
return 0;
}