-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_shell.c
281 lines (242 loc) · 7.3 KB
/
my_shell.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main();
void printPrompt();
char* readLine();
char** parseLine(char*);
void changeDirectory(char*);
void externalCommand(char*,char**);
int backgroundCheck(char**);
int numberPipes(char**);
int executePiped(int, char*);
char** parseLineByPipe(char*);
char** parseLineByAmp(char*);
pid_t shell_pid;
#define MAX_LEN 256
#define MAX_ARGS 128
#define MAX_JOBS 12 //maximum amount of jobs in the background
int main(){
shell_pid=getpid();
pid_t toWaitFor[MAX_JOBS]; //deals with up to 10 jobs in the background
int currentIndexPid=0; //current job in job backgorund list
while (1){
char** args;
char* input;
char inputForPipe[256];
printPrompt();
input=readLine();
strcpy(inputForPipe,input);
args=parseLine(input);
if (args[0]){
if ( strcmp("cd",args[0]) == 0) {
//Tries to change directory to entire arguement, and if arguement is not a directory itself tries to find currentworkingdirectory/arg
char cwd[1024];
getcwd(cwd,sizeof(cwd));
int worked=chdir(args[1]);
if ( worked != 0 ){
int worked2=chdir(strcat(cwd,args[1]));
if (worked2 != 0 ){
printf("Error with file directory %s\n",args[1]);}
return 0;
}
}
else if (strcmp(args[0],"exit") ==0 || strcmp(args[0],"exit\n")==0 ){
//Exit Command
printf("Exiting Shell...\n");
exit(0);
}
else if(strcmp(args[0],"jobs")==0){
int i;
for (i=0; i<MAX_JOBS-1;i++){
//if (toWaitFor[i]){
printf("[%i] %i\n", i,toWaitFor[i]);
//}
}
}
else{ //args is not an internal command
int background; //1 if should run in background, 0 otherwise
int numPipes; //1 if involves piping, 0 otherwise
background=backgroundCheck(args);
numPipes=numberPipes(args);
if ( numPipes==0 ){
//regular implementation- no piping
//may include background (&)
pid_t child;
int status;
child=fork();
if (child<0) {
//error occured
fprintf(stderr,"Fork Failed\n");
}
else if (child!=0){
//Parent Process:
if (background==1){
//run in background
// wait(child,&status,0);
toWaitFor[currentIndexPid]=waitpid(-1,&status,WNOHANG);
if (currentIndexPid>MAX_JOBS-1){
currentIndexPid=0;
}
}
else {
//Run Normal Way: foreground
wait(NULL);
}
}
else{
//Child Process:
if (background==1){
char** noBackgroundLine=parseLineByAmp(inputForPipe);
char** newArgs=parseLine(noBackgroundLine[0]);
execvp(newArgs[0],newArgs);
}
else{
execvp(args[0],args);
}
fprintf(stderr,"Error: %s command not found\n",args[0] );
}
}
else{
//PIPING HERE: (cannot combine & and piping )
executePiped(numPipes, inputForPipe);
}
}
}
free(input);
free(args);
}
}
int executePiped(int numberPipes, char* input ){
char** splitPiped=parseLineByPipe(input);
int i=0;
int previous; //Prevous Output
while (splitPiped[i]!= NULL){
int pipes[2];// create new pipe each time
pipe(pipes);
int pid;
char** command=parseLine(splitPiped[i]);
pid=fork();
if (pid< 0){
fprintf(stderr, "Error with Fork.\n" );
}
else if (pid==0){
//CHILD PROCESS
if (i==0){//First process:
dup2(0,STDIN_FILENO); //reversed from shell-pipe.c for loop
}
else{
dup2(previous,STDIN_FILENO);
}
close(pipes[0]);
if( splitPiped[i+1] != NULL){ //if there is an additional command after this, set up pipes
dup2(pipes[1],STDOUT_FILENO); //set up to recieve
}
close(pipes[0]);
execvp(command[0], command);
printf("Error: command not found or invalid parameters. \n");
exit(EXIT_FAILURE);
}
else{
//PARENT PROCESS
wait(NULL); //wait on child
previous=pipes[0]; //passes the prevous stuff to next command
close(pipes[1]);
}
i++;
free(command);
}
return 0;
}
int backgroundCheck(char** array){
//Tests to see if & contained in the array - 1 if true, 0 if false
int i=0;
int found=0;
while (array[i]!= NULL){
if (strcmp( array[i],"&")==0) found=1;
i++;
}
return found;
}
int numberPipes(char** array){
//Returns number of | in the array
int i=0;
int found;
found=0;
while (array[i]!= NULL){
if (strcmp( array[i],"|")==0)
{
found++;
}
i++;
}
return found;
}
void externalCommand(char * command, char**args){
//take args starting at command up until a & or | - background or pipe
execvp(command,args);
fprintf(stderr,"Error: %s command not found\n", command);
}
void printPrompt(){
char cwd[1024];
if ( getcwd(cwd,sizeof(cwd))!=NULL ) {
printf("%s$ ",cwd);
}
else{
printf("Directory Error$ ");
}
}
char* readLine(){
char *line=NULL;
ssize_t bufsize=0;
getline(&line,&bufsize,stdin);
return line;
}
char** parseLine(char* line){//uses reccomended strtok function to split commands
int bufsize=1024;
char** tokens=malloc(bufsize*sizeof(char*)); //allocate the space for the tokens
int position=0;
char*p;
p=strtok(line," \t\n");
while(p!=NULL){
tokens[position++]=p;
p=strtok(NULL," \t\n");
}
// position++;
tokens[position]=NULL;
return tokens;
}
char** parseLineByPipe(char* line){
int bufsize=1024;
char** tokens=malloc(bufsize*sizeof(char*)); //allocate the space for the tokens
int position=0;
char*savePtr;
char*p;
p=strtok_r(line,"|", &savePtr);
while(p!=NULL){
tokens[position++]=p;
p=strtok_r(NULL,"|", &savePtr );
}
// position++;
tokens[position]=NULL;
return tokens;
}
char** parseLineByAmp(char* line){
int bufsize=1024;
char** tokens=malloc(bufsize*sizeof(char*)); //allocate the space for the tokens
int position=0;
char*savePtr;
char*p;
p=strtok_r(line,"&", &savePtr);
while(p!=NULL){
tokens[position++]=p;
p=strtok_r(NULL,"&", &savePtr );
}
// position++;
tokens[position]=NULL;
return tokens;
}